socket bind

Posted on 2013-10-01
Medium Priority
Last Modified: 2013-10-03
       int fd;
       int sock_type =  SOCK_STREAM;

        if ((fd = socket(AF_INET, sock_type, IPPROTO_TCP)) < 0) {
              // ERROR 

        struct sockaddr_in sin;
        memset((char *)&sin, 0, sizeof (sin));
        sin.sin_family = AF_INET;
        sin.sin_addr.s_addr = 0;  // Why does this work even if it is 0. 
                 // Is there any significance of '0' compared to real ip adress of my host ??
        sin.sin_port = htons(port);

        while (bind(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0)
                // sleep for fews and retry and succeed

Open in new window

// Why does this code works and also bind correctly even though I am assigning "0" to sin.sin_addr.s_addr
Question by:perlperl
  • 3
  • 3

Author Comment

ID: 39538661
What is the difference in behavior when I set s_adrr

1) sin.sin_addr.s_addr = 0  
2) sin.sin_addr.s_addr  = // uint32 value of localhost
3) sin.sin_addr.s_addr = // uint32 value of actual IP address. example

Does Case 1 blocks any incoming request from external to my host. (I mean any external client other than the request from same host)
LVL 36

Accepted Solution

mccarl earned 2000 total points
ID: 39538723
For case 1), 0 is actually the value of a constant called INADDR_ANY, and so passing 0 (either literally or via that constant) is a "special" value which tells bind() to bind to ALL available interfaces. This means that the socket will accept incoming connections regardless of whether the come in externally or internally via localhost.

The other cases bind to specific interfaces and so only allow incoming connections via the particular interface, ie. binds to localhost and so only local connections are allowed, etc.

Author Comment

ID: 39539580
Thanks mccarl.

case 1 -> open to entrie world (even outside subnet). If I have three NICS, it will listen on all the three interfaces.
case 2 -> only local connection allowed when client connects using
case 3 -> What does this mean then ? Is this similar to case 1 except it will only listen to one of the specific three NICS only.
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.

LVL 36

Assisted Solution

mccarl earned 2000 total points
ID: 39544932
Sorry about the delay, I missed this one somehow...

case 1 -> open to entrie world (even outside subnet).
Whether it is open to the entire would outside a subnet is not determined here. That would be determined by whatever routers/firewalls/etc that are a part of the network. All this is saying, (as you correctly mention) is that incoming connections will be accepted on any interface available to the tcp/ip stack, ie. if you have three NIC's it will listen to all 3 AND the localhost interface.

The comments made for the other 2 cases are correct.

Author Closing Comment

ID: 39545160
Thanks a lot!!!!
LVL 36

Expert Comment

ID: 39545209
You're welcome! :)

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

588 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