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
                     }
                     break;
               }
         } 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?
perlperlAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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

Thanks!!!!!!!
0
mccarlIT Business Systems Analyst / Software DeveloperCommented:
Correct.

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)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.