• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 662
  • Last Modified:

getaddrinfo and bind

struct addrinfo hints, *start, *results;
char* str_port = // some port that I receive from different function
	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, str_port, &hints, &results));

start = results;
	do {
		if (results->ai_family == AF_INET || results->ai_family == AF_INET6 ) {
                      int  fd = socket(results->ai_family, results->ai_socktype, results->ai_protocol)

                      while (bind(fd, results->ai_addr, results->ai_addrlen)  < 0) {
                          // sleep for fews and retry and succeed
         } while ( (results=results->ai_next) != NULL);

Open in new window

The above code creates and bind a socket to port "str_port". Also it binds to all interfaces since I have provided flag AF_PASSIVE.

The above is called multiple times with various port number that I need to bind to all interfaces.
If for a specific port I need to bind to localhost only (both ipv4 and ipv6 socket), how can I achieve that.

Is there a different flag I need to use other than AF_PASSIVE or do I need to change the address results->ai_addr just before calling the bind?
  • 2
1 Solution
mccarlIT Business Systems Analyst / Software DeveloperCommented:
A quote from the man page for getaddrinfo...
If the AI_PASSIVE flag is not set in hints.ai_flags, then the
returned socket addresses will be suitable for use with connect(2),
sendto(2), or sendmsg(2).  If node is NULL, then the network address
will be set to the loopback interface address (INADDR_LOOPBACK for
IPv4 addresses, IN6ADDR_LOOPBACK_INIT for IPv6 address); this is used
by applications that intend to communicate with peers running on the
same host.
Therefore, to bind to localhost only, simply DON'T set the AI_PASSIVE flag, and you are already passing NULL as the "node" argument so that should return to you the correct strcuts for binding locally only.
perlperlAuthor Commented:
Oh i see, i just have to remove the line and this should bind to loopback (localhost) asdress only which will prevent external connection.

hints.ai_flag = AI_PASSIVE

mccarlIT Business Systems Analyst / Software DeveloperCommented:

Or if you want to make it explicit you can do
hints.ai_flag = 0;

Open in new window

(But yeah, removing that line totally has the same effect because you "bzero" your hints struct on the line before it anyway)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now