Solved

socket and server ip address

Posted on 2013-11-11
3
340 Views
Last Modified: 2013-11-11
I am creating and binding a socket on my server with the following optons on a clustered environment

	struct addrinfo hints, *start, *results;
	bzero(&hints, sizeof(struct addrinfo));
	hints.ai_flags = AI_PASSIVE; // Use address return by getaddrinfo in bind() return addrinfo will have INADDR_ANY set to IPv4 adress and INADDR6_ANY_INIT for IPv6 address
	hints.ai_family = AF_UNSPEC;  // return both the family AF_INET and AF_INET6 address structures
	hints.ai_socktype = SOCK_STREAM;
	hints.ai_protocol = IPPROTO_TCP;

getaddrinfo(NULL, "4455", &hints, &results);
int fd = socket(results->ai_family, results->ai_socktype, results->ai_protocol);
bind(fd, results->ai_addr, results->ai_addrlen);

Open in new window


The above server is running in background and continously listening for incoming packets.

Once I accept a new request I want to make sure that the client's request came on specific server's IP address only.

I tried two options to get server's IP:

METHOD 1  ---> This prints  Addres = 0.0.0.0
// I believe this is because I have AI_PASSIVE set. 
	struct sockaddr_storage addr;
	len = sizeof (struct sockaddr_storage);
	 getsockname(fd, (struct sockaddr*)&addr, &len );
	struct sockaddr_in *sin = (struct sockaddr_in *)&addr;
	char str[INET_ADDRSTRLEN];
	inet_ntop(AF_INET, &sin->sin_addr, str, INET_ADDRSTRLEN);
	DBG0(" Addres = " << str);    

METHOD 2:  --> This prints  Adress =127.0.0.1
In this method, instead of using "fd", I am using the socket descriptor from "accept" call

Open in new window


0.0.0.0 and 127.0.01 does not help me as client may have initiated a request againt server's ip address say 10.10.20.20
How do I get the specific IP address from either "fd" or "file descriptor from returned accept call"  so that I can make a check that client sent request on this IP only.
0
Comment
Question by:perlperl
3 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 39639760
'getsockname()' returns your local connection information. If you want the remote info, use 'getpeername()' (https://www.linux.com/learn/docs/man/3178-getpeername2) instead:

	struct sockaddr_storage addr;
	len = sizeof (struct sockaddr_storage);
	 getpeername(fd, (struct sockaddr*)&addr, &len ); // <---- !
	struct sockaddr_in *sin = (struct sockaddr_in *)&addr;
	char str[INET_ADDRSTRLEN];
	inet_ntop(AF_INET, &sin->sin_addr, str, INET_ADDRSTRLEN);
	DBG0(" Addres = " << str);    

Open in new window

0
 

Accepted Solution

by:
perlperl earned 0 total points
ID: 39639807
I wanted local connection only not the peer name.

Can I close this Q please, there was a bug in my code ;) and I fixed it. I did not specify the address properly
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

The article explains the protocols and technology which is involved when two computers on different TCP/IP networks communicate with each other. In the diagram, a router is used to segregate two networks. The networks are 192.168.1.0/24 and 192…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
After creating this article (http://www.experts-exchange.com/articles/23699/Setup-Mikrotik-routers-with-OSPF.html), I decided to make a video (no audio) to show you how to configure the routers and run some trace routes and pings between the 7 sites…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now