Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get IP address from socket descriptor?

I've opened a TCP socket server (I've omitted a few stuff, it is taken from here

sockfd = socket(p->ai_family, p->ai_socktype,
            p->ai_protocol))

Is it possible to get the IP address of the server from sockfd? If not where should I look?

EDIT: I want to know the address of the server (this is before any client connects).

like image 414
michelemarcon Avatar asked Feb 05 '13 15:02

michelemarcon


People also ask

How do I find client IP address in socket programming?

OK assuming you are using IPV4 then do the following: struct sockaddr_in* pV4Addr = (struct sockaddr_in*)&client_addr; struct in_addr ipAddr = pV4Addr->sin_addr; If you then want the ip address as a string then do the following: char str[INET_ADDRSTRLEN]; inet_ntop( AF_INET, &ipAddr, str, INET_ADDRSTRLEN );

How do I find the IP address of a process in Linux?

Use lsof -p pid . You need to grep on the output to get the port values for eg. something like this - lsof -p pid| grep TCP. This will list all the ports opened or connected to by the process.

Is sockets available through file descriptors?

There is no difference between a socket (descriptor) and a file descriptor(s). A socket is just a special form of a file. For example, you can use the syscalls used on file descriptors, read() and write(), on socket descriptors.

How do I get the IP address of a socket?

First, import the socket module and then get the h_name using the socket.gethostname (). Now, find the IP address by passing the h_name as an argument to the socket.gethostbyname () and store it in a variable. Print the IP address.

Why can't I get the server's IP address from a socket?

You can't use the socket to get the server's address before a client connects, because it isn't known. In principle, a host may have multiple IPs. The IP used for a connection to a server is the one belonging to the interface, through which the connection arrived. Until a connection arrives, it isn't known.

What is the IP address of the listening socket?

The IP used for a connection to a server is the one belonging to the interface, through which the connection arrived. Until a connection arrives, it isn't known. Even if you have only one IP, connections may arrive from within the machine, in which case the address would be 127.0.0.1. So the listening socket has no information about the IP.

What is the IP address used for a server connection?

The IP used for a connection to a server is the one belonging to the interface, through which the connection arrived. Until a connection arrives, it isn't known. Even if you have only one IP, connections may arrive from within the machine, in which case the address would be 127.0.0.1.


2 Answers

If you want to know who's at the other end of your socket you can use getpeername in Linux. getsockname will tell you who you are. You decide what address you want your server to sit on initially though, at bind time.

You may also find this SO question useful: bind socket to network interface

And the book "Unix Network Programming, vol 1", by W. Richard Stevens.

like image 118
Joe Avatar answered Oct 26 '22 21:10

Joe


You can't use the socket to get the server's address before a client connects, because it isn't known.

In principle, a host may have multiple IPs. The IP used for a connection to a server is the one belonging to the interface, through which the connection arrived. Until a connection arrives, it isn't known.
Even if you have only one IP, connections may arrive from within the machine, in which case the address would be 127.0.0.1.

So the listening socket has no information about the IP.
You'll need to find what interfaces the machine has, and what's their IPs.

like image 41
ugoren Avatar answered Oct 26 '22 21:10

ugoren