UGN Security
Posted By: SilentRage Winsock Control - Advanced uses - 09/04/02 03:47 PM
This is a fairly simple notice for people already familiar with the uses of the Winsock control.

Trick #1 - The BIND Method

For some reason, it wasn't until recently that I discovered exactly what the Bind method can do. Previously I thought it was only good for binding UDP sockets to a port.

I was wrong. This is the answer to my prayers. Various programs give you the option of listening on any local IP. 0.0.0.0 is for ANY port of course. Or you may select 127.0.0.1 if you only want to accept local connections. And of course, if you have more than one IP, you can listen on any of them as well.

While this is fairly obvious in the Winsock API, until now, I thought it was outside the Winsock Control's capabilities.

Want to create a TCP listening session on 127.0.0.1? Do the following:

Winsock.Bind 10000, "127.0.0.1"
Winsock.Listen

Trick #2 - UDP Broadcast

This one isn't as obvious as the other one, but just as easy. First, let me paint an example of its use.

Problem: You have a multiplayer network-enabled game, and you want to have the convenience of the game giving you a listing of all available hosted games on the network.

Solution: UDP Broadcasting! Set each host to bind a UDP socket to a single port - let's say 4500.

Host.Bind 4500, "0.0.0.0"

Ok, now the Client has to somehow get that hosts address. Do this:

Client.RemoteHost = "255.255.255.255"
Client.RemotePort = 4500
Client.SendData "PING"

That well send "PING" to every computer on the network targeted at port 4500! Now, the host is expecting a request like this, so it sends a response:

Host.RemoteHost = Host.RemoteHostIP
Host.SendData "PONG"

Now the client will recieve a PONG from every host on the network - and it may list each host for the user to connect to via TCP after whatever means of negotiation you want.

-

If you don't know how to use the Winsock Control, and would like to know, ask me to send you an example of it's use.
Posted By: ninjaneo Re: Winsock Control - Advanced uses - 09/05/02 03:37 AM
now thats just [censored] jeet
Posted By: Mornse Re: Winsock Control - Advanced uses - 09/05/02 11:01 PM
I don't really understand the Bind. Like what would you use that for?
Posted By: SilentRage Re: Winsock Control - Advanced uses - 09/06/02 01:12 AM
Well, I was pretty clear on what it can be used for. So, I'll explain a bit about what it means to have more than one IP and the 'special' IP's.

Let's say your computer has a network card on a local network. This network card may have the IP of 192.168.1.3 for example. However, your LAN doesn't have access to the internet, so you have to use a 56k dial-up connection. When you get on the internet, you now also have an internet IP given to you by your ISP. Let's say it is 12.345.6.78. These IP addresses give your applications special capabilities when they listen on a port. You see, when somebody tries to connect to your computer, not only is there a PORT it's connecting to, it's also connecting to an IP. Here, I'll explain.

You have a FTP server listening on 192.168.1.3:21. In order for a client to connect to it, the destination IP address must be 192.168.1.3. That is an IP only accessible from your network. That means ONLY people on your LAN can connect to the FTP server. If somebody tried to connect to 12.345.6.78:21 - it will be a connection refused cause there's nothing listening on that IP.

The opposite is true. Servers listening on 12.345.6.78 cannot be connected to from other computers on the LAN (unless you have some static routing going on - but that's special). However, it can easily be connected to by people on the internet.

Now, there is also a special IP 127.0.0.1. If a server is listening on that IP, only programs on the same computer can connect to it. If some other computer tried connecting to 127.0.0.1... well, it won't be your computer it's trying to connect to. Understand? That means you can have servers on certain IP's to restrict who can connect to it.

Another special IP is 0.0.0.0. This is where a server is listening on ALL IP addresses. It doesn't matter if you're connecting to 127.0.0.1, 12.345.6.78, or 192.168.1.3 - they'll all successfully connect.

The BIND method is how you can tell the winsock control on WHICH IP you want to listen on. If you don't tell it what IP you want it to listen on, it will always listen on 0.0.0.0 - and you may not want that.

Now go ahead and read what I said about the BIND method above and you will better understand what I'm talking about.
Posted By: Mornse Re: Winsock Control - Advanced uses - 09/06/02 03:17 AM
Ahh, gotcha. Thank you very much for the explanation. That's cool [censored], I'm gonna try that one time.
Posted By: psychogen Re: Winsock Control - Advanced uses - 09/26/02 02:49 PM
/me smiles

Nice article SilentRage

I have been using bind for the OfficeChat app I am writing part time. You may remember when I asked about DHCP detection through VB, well I after I did some reading online I found out that I was going totally the complex way of getting around to finding the OfficeChat server on a LAN, so I used bind to broadcast available servers from a central broadcasting server (with static ip) then the client can choose which server to connect to from a listview.

:p

Bind rox0rs!!!
© UGN Security Forum