Determining local IP address in P2P application - winapi

I am developing an P2P application where a peers talk to the server to inform its Private and Public IP. The application uses UDP for communication.
To get the private IP the client uses gethostbyname and bind it to that IP. The problem is when the system has more than one NIC. The problem is when one of the NIC is not connected to internet. So to avoid it i am using INADDR_ANY and bind it.
Now i need to get my local IP address to inform to the server. Is there any API which will tell me which IP address of the NIC is active?

You need to bind to an explicit IP address rather than INADDR_ANY as binding to INADDR_ANY will mean that calling getsockname() on the socket to get the local address will simply return INADDR_ANY.
So, what you need to do is iterate the available endpoints (using getaddrinfo()) and create a socket on each. These will then give you the correct address if you call getsockname() on them later.

Related

How to identify proxy protocol from IP and Port?

Say I have a list of proxies - I pull out of one of these proxies. It's nothing but ip and port. From a programming level, you need to know the protocol to use such as socks5, socks5h, http, https... etc etc. Is there a way to retrieve what kind of protocol a proxy uses from the information given?
If you are using Node.js you can try check-proxy library, though it does much more than just checking protocol.
Your proxy server identify the port number for example 6080,9180,etc so you can easily identify the proxy server.
Your id address also private or public you can use 'proxy server ip address' that automatically create a virtual proxy network.
Example: Your private ip address is 172.16.10.158 you can use proxy server, your ip address will be 136.56.89.210. You can use public ip ex 125.124.85.69 change in to 179.68.36.49.

Force gateway for IP connection

I have two NICs in my Windows PC, one for Internet and the other for outbound UDP streams. Both NICs have gateways and I tweak the metrics so that Internet bound traffic goes to the first. I would rather disable the gateway on the second NIC and specify the gateway when I create the UDP socket. Is this possible? Can I force the destination MAC address on a socket?
You have to bind() the socket to the local IP address of the NIC you want to use. If you don't know the IP, use GetAdaptersInfo() or GetAdaptersAddresses() to enumerate the NICs until you find the one you want, and then you will know its current IP to bind to.

Bind Ruby TCP Client to a specific IP address

I have a server with 4 ip addresses and four clients that each register their ip address with a service. Each client runs the same ruby script that connects via a TCP socket to the service.
The problem I have is all four scripts are connecting via the same IP address so three are receiving authorisation errors because they have the wrong ip address.
Is it possible to tell the TCP client to use a specific ip address?

Local IP address of an incoming connection in Socket IO - (behind a router)

I'm interested in differentiating connections coming from the same PC.
socket.request.connection.remoteAddress; works perfectly for me on 1.0.4 to give me the public IP, but it will run into trouble when people are behind routers
For public ip - Get the client's IP address in socket.io
You have to use a combination of remote IP and remote Port to differentiate connections. If multiple connections come from behind the same router, your server would see them having the same public IP from the router, but they would be using different ports.

Knowing the internal IP of an http request coming through NAT on a machine with multiple NICs?

Imagine a Windows box, which:
hosts a WCF service
has multiple NICs
sits behind NAT
When a user issues a request to the service (on top of the WCF infrastructure), he uses the external address assigned to the target machine by the NAT.
I have to write some piece of code inside the WCF service, which must know which of the several NICs that the machine has was used to actually handle the network traffic. How does this code identify the NIC is less important - it could be its MAC address (the best) or it could be the (internal) IP address of the NIC.
How can I do it?
EDIT1
I will try to supply the question context. There are two agents. Both expose the same WCF service. In addition, one of the agents can be instructed to start probing the network towards the second agent in the following fashion:
Agent A is asked to probe the network to agent B
Agent A negotiates with agent B the UDP port to utilize for the sake of probing using the WCF service exposed by the agent B.
Once negotiation is over, the agent A starts some custom protocol over UDP, where the agent B acts as the server - i.e. it binds to the UDP port negotiated in the previous item.
Binding to a UDP port requires two pieces - the IP address and UDP port, where the IP address can either be a specific IP address or * (to bind to all the IP addresses associated with the machine). The latter option is not good for us - I will omit the reasons. This leaves us the former option - binding to the specific IP address. However, when the agent B is behind NAT, the IP address used to talk to the WCF service is the external IP address assigned to the agent by the NAT. Binding, on the other hand, requires the respective internal IP address - how to get it?
Can you check the OperationContext.Current.Channel.LocalAddress (it's an EndpointAddress) inside a WCF operation?
As a side note, getting the remote address can be done with:
OperationContext context = OperationContext.Current;
MessageProperties prop = context.IncomingMessageProperties;
RemoteEndpointMessageProperty endpoint =
prop[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
string ip = endpoint.Address;
--larsw
To get the MAC use
System.Net.NetworkInformation.NetworkInterface.GetPhysicalAddress();
All Nics:
System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces();
to find out what is the real listening ip address you can write a code that listen to your port on each address and ping it from an agent emulator to see that the address is valid.
Cheers,
Gilad

Resources