[Winsock] Get the address of the internet connected NIC

Started by
6 comments, last by Antheus 16 years, 7 months ago
Hello. When im binding my socket, i want to find out what local address that is connected to the internet, if there are any, and bind() it to that address. What is the best way to get this info? getaddrinfo() dosn't know if an address is connected to a lan that has no internet access, or if it is connected to a lan with an internet forwarding gateway, or if its directly connected to internet via PPPoE or something like that. Maybe this info be found in the windows register? (just using INADDR_ANY dosn't cut it in this case, i need a specific address to bind to) To clearify what i want to use this for im writing an utility that moitors my internet traffic. Thanks in advance for any input.
Shields up! Rrrrred alert!
Advertisement
I doubt there's a convenient function in WinSock2 to determine if a network address is connected to the internet, simply because there are too many ways the internet connection can be established. My guess is that the only way would be to manually test each avaliable NIC for a working internet connection (by pinging google, for example).

Maybe someone else can help you better, I'm not 100% sure on this.

[Edited by - shurcool on September 18, 2007 11:37:08 AM]
I believe the only reliable way is indeed to send a packet around the net and extract the IP from it. Another method I've used is to traverse the list of addresses and find one that doesn't fall inside the common LAN ranges such as 10.x.x.x, 192.x.x.x etc. This of course is less reliable but will work most of the time.
There is no way, unless you know the exact hardware and routing information of your subnet.

Your WAN address (usual term) isn't something tangible. It may be the address of your machine, or one hidden behind 2 NATs and one static route.

Simply put: such thing doesn't exist.

Reason for this lies in the way IP works. Each node (thing that has unique address) knows nothing of either left or right of it. Sending a packet outside and waiting for response isn't reliable either, since in a NAT setting, you'll probably never see your external IP.

This is why all servers require you to manually configure your WAN IP or hostname.
What you want to bind to is the interface that's on the same subnet as your default gateway. You can use Windows-specific configuration calls to find the address of the default gateway, and the subnet of each adapter, and compare.

Of course, in the case of NAT, this won't actually bind to an address that's visible on the Internet, but you probably already knew that.
enum Bool { True, False, FileNotFound };
Thanks for your replies!

Maybe im going about this the wrong way, what i want to do is to write an application that i can use to log my games network traffic. I am doing this by setting the SIO_RCVALL option with ioctl(), so that recvfrom() returns both sent and received packets. But using this option with ioctl() only works if i have bound my socket to a specific address, INADDR_ANY will cause it to fail.
I suppose i could just enumerate all addresses and listen to them all, but that feels like a waste, and could complicate things if this application is run on a gateway machine and not infact the machine that is running the game.
Since my game only supports multiplayer over internet via a matchmaking service, i need to know that im listening to the internet connected address.

Does anyone have any better suggestion on how to solve this?

hplus: ah, but can't a computer be connected to more than one gateway? Im not very good at networking theory, can a computer have internet access from more than one address? If so, how does it choose which one to use when sending packets to an internet address?
Could you give me some hints to what these functions are on finding out what the default gateway is?

Thanks.
Shields up! Rrrrred alert!
Quote:can't a computer be connected to more than one gateway?


Most computers have only one default gateway, and make an interface determination based only on netmask. You can only configure a node with more than one gateway if it acts as a router, and then it will typically make packet interface determinations based on route tables, updated using RIP, BGP, or some other such route information protocol.

If you have the source to the game, how about storing the data at the application level? Just store a separate copy when you call send() or recv().

If you don't have the source to the game, then perhaps you can use WireShark for recording the traffic? Or if that's not the UI you want, try using the underlying library, pcap (or WinPcap).
enum Bool { True, False, FileNotFound };
Quote:an application that i can use to log my games network traffic


libpcap has trivial API that you can use to capture everything sent over the wire. It can bind to any NIC, interface, IP, it has versatile filtering system, and it's usable out-of-box, with just a handful of calls.

It also supports creation and sending of arbitrary packets, although that does involve a bit of juggling with bits and bytes, but nothing too horrible.

This topic is closed to new replies.

Advertisement