How to set local IP when originating TCP/IP connections? - c

Using Linux and glibc, when originating a TCP/IP connection (calling connect() on a socket), how can I choose explicitly from which of my IP local adresses I want to originate that connection, providing I have more than one interface (with different IPs) from where I can reach the remote host?
Is it possible to do so, or it is always up to the operating system IP stack to chose the route automatically?

Bind to a specific address instead of 0.0.0.0 or ::.

Related

what is all interfaces 0.0.0.0 in anypoint studio http connector?

[enter image description here][1]Can someone explain what does the Mulesoft Http Connector option All-interfaces (0.0.0.0) means . I know it's common and we do it almost every time but whats the significance and why do we use it ?
The ip address 0.0.0.0/0 is a special IP address which has different meanings in different contexts. More often it is used as a way to specify "any IPv4 address at all". It is used in this way when configuring servers (i.e. when binding listening sockets). This means the service is listening on all the configured network interfaces and can accept connection from "all ip addresses on your local machine."

How to know the various IP addresses of the server using C, Linux, Socket?

I know one IP address of the remote node via. traceroute. However, I want to know all the IP address in its interfaces. How can I do so? The node doesn't have hostname so I can't use the API to get various ifaddress using hostname API. I searched various places but couldn't find on how to get the various IP address of the remote node. Is it possible to get it? Basically, I am implementing traceroute and my aim here is to display the loopback address of the intermediate node and not the actual interface address. In case loopback address is not available, I would like to display the actual interface address. I am struggling here and have no clue on it. This is all implemented using C - linux, UDP socket.
I want to know all the IP address in its interfaces. How can I do so
Lookup DNS or login in and use the OS dependant tools/APIs to lookup the local interfaces' IP addresses, else you are lost.
Technically if you are on the same network you could use the macid, and sniffing network traffic, to find other IPs on that interface, but once you leave your local network, its near impossible to find out other ips that machine is listening to, this would be a security issue if they made this information available.
The way traceroute works is by sending packets with increasing IP TTL values towards a remote address and the routers along the way will generate back ICMP TTL time exceeded thus revealing their IP source address back to the sender and so you get to know the hops that your data might be traversing when attempting to communicate to a specific remote address. The icmp error message will contain the ip address of the hop's incoming interface and in your case you want to discover also all other ip addresses assigned to further outgoing interfaces (outgoing as seen from your sender perspective) but this is not possible by using the above mentioned technique or any other.

Clarity on bind() socket function

While I was reading how to make a TCP client/server connection in C, I had a doubt about the bind() function.
I read that you need this function to "bind" a socket you've created to a local IPEndPoint, because the client/server connection takes a socket pair, made by LocalIP:LocalPort, RemoteIP:RemotePort. Thus my questions are:
What happens and what does the kernel do when a client doesn't call bind(), but calls connect() immediately after creating a socket (this is a common thing; I do it too in a client program, but I don't understand why I needn't to bind)?
... and above all ...
Why does a server program call bind(), specifying INADDR_ANY as the LocalIP address? I read that this constant is useful to specify a generic IP address, that is the server accepts data from anywhere on the Internet. But... is this a specification of a local IP address, or does it indicates where the clients can reach the server? I'm getting very confused...
1) You usually only need to call Bind if you want to create a server socket. There are some cases where it is required to establish a client socket, but more often than not, it is not necessary for for a client sockets. If you want to wait for incoming connections on a certain port, you have to bind to it. If you want to connect out to some IP and Port, there's no need to bind. The server socket's bind takes exclusive access to the TCP port. Nothing else can come online and bind to that port until your application closes or the socket is closed by you.
2) You are specifying which IP on the local computer to bind to. A single computer can have many IP addresses. Your computer may have a wired and wireless connection. Each has its own IP on the local network. You can specifically bind to one of those IPs and not the other. You could even have one application bound to port 473 (for example) on one IP and an entirely different application bound to port 473 on the other IP. If you specify INADDR_ANY, you are binding to all valid IPs the machine has. So it doesn't matter what IP the client used to get to you, it will work.
What happens and what does the kernel do when a client doesn't call bind(), but calls connect() immediately after creating a socket (this is a common thing; I do it too in a client program, but I don't understand why I needn't to bind)?
When you make an outbound connection without first binding the socket to an IP/port, the kernel will pick a source IP and port automatically, based on routing tables and what ports are available.
Why does a server program call bind(), specifying INADDR_ANY as the LocalIP address? I read that this constant is useful to specify a generic IP address, that is the server accepts data from anywhere on the Internet. But... is this a specification of a local IP address, or does it indicates where the clients can reach the server? I'm getting very confused...
What you've read is inaccurate -- the IP address in the sockaddr passed to bind() doesn't indicate where the server will accept connections from. It indicates what local IP addresses the socket should be attached to. INADDR_ANY indicates that you want to listen for connections on the specified port on any and all IP addresses attached to the machine. On servers with multiple IP addresses, it's often useful to specify one IP address to bind() to, so that other sockets can be bound to the same port on other IPs. It's also often useful to bind to a port on localhost only.

C Programming Using a specific Host IP to connect to server

I am looking at a machine with multiple IP address (ex ethernet and wireless) and need my client to connect to a server using a specific Client IP address. I can perform this easy in Java (done doing the Socket() with 4 args), but the only reference to use a specific host IP address that I can see is with using bind() but as far as I know that only is used for servers.
Synopsis:
Client has 2 IP address (ethernet and wireless) trying to connect to a server.. but using a specific IP (don't need to worry about "finding" the IP addresses as they will be in a config file or DEFINE
Not a windows only answer but a C programing that is portable (I use mac osx/linux usually but also windows)
I've never done this before but I think you can bind a socket to an ip address and then use connect with that socket. The pertinent section from here http://pubs.opengroup.org/onlinepubs/009695399/functions/connect.html states:
If the socket has not already been bound to a local address, connect()
shall bind it to an address which, unless the socket's address family
is AF_UNIX, is an unused local address.
Which implies that bind can be used before connect.
Your reference is correct. If you use bind(), then that address will be used to bind the client socket to the interface you want to use. bind() is not only used for server sockets.
https://beej.us/guide/bgnet/html/multi/syscalls.html#bind
By using the AI_PASSIVE flag, I'm telling the program to bind to the
IP of the host it's running on. If you want to bind to a specific
local IP address, drop the AI_PASSIVE and put an IP address in for the
first argument to getaddrinfo().

What client-side situations need bind()?

I'm learning C socket programming. When would you use bind() on the client-side? What types of program will need it and why? Where can I find an example?
On the client side, you would only use bind if you want to use a specific client-side port, which is rare. Usually on the client, you specify the IP address and port of the server machine, and the OS will pick which port you will use. Generally you don't care, but in some cases, there may be a firewall on the client that only allows outgoing connections on certain port. In that case, you will need to bind to a specific port before the connection attempt will work.
An example would be the data connection of an active FTP connection. In this case, the server connects from its port 20 to the IP and port specified by a PORT or EPRT command.
A classic example of a client program using bind() is the (obsolete) rlogin / rsh family of network clients. These clients were intended to be used within networks with strong trust relationships - in some cases the server machine trusts the client machine to tell it the username of the user that is connecting. This required that the client program connect from a low port (a port less than 1024), because such ports are restricted to the root user and thus (in theory) prove that the client being used is authorised by the system administrator.
The NFS protocol has similar trust relationships, and similarly the client makes connections from a low port number, using bind().
Another example is IRC clients that allow the user to specify a particular source IP address to connect from. This is to accomodate users with many IP addresses assigned to their machine, each with a different "vanity" domain name assigned to it. Choosing which IP to connect from (with bind()) allows the user to choose which domain name to appear as on IRC.
A good situation would be in a p2p case, you’re communicating with a STUN Server with a bound socket, and the STUN Server tells you the port on which he is receiving messages from your socket (that can be different from the one you specified when you bound your socket depending on your network and more specifically on your NAT type). This will allow you to be aware of the real port translation that your NAT is doing, and you’ll be able to give this information to potential peers that want to connect to you.
Binding the socket is useful as some NATs are dynamically giving you ports (binding on port x twice might not give you the same “real” port). So you’ll be able to directly use the socket you bound to listen on the port.
I suppose you should bind() in the case of UDP sockets.
bind function is one of "key" functions. It associates your socket (server or client) with address (ip + port). As for Windows you must use bind for WinSockets. There is good book about it "Network Programming for Microsoft Windows" by Anthony Jones and Jim Ohlund.
Bind can be used to attach names to a sockets. Thus, say you create a software that uses a particular TCP port, you need to bind it and then, you will know the TCP port it is using.

Resources