winsock... binding to WRONG IP byte order WORKS!
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: winsock... binding to WRONG IP byte order WORKS!

  1. #1
    Join Date
    Apr 1999
    Location
    Tampa, FL
    Posts
    114

    winsock... binding to WRONG IP byte order WORKS!

    I was updating a wrapper 32 bit windows DLL on an old winsock utilities library, to allow for the case where I need to specifically bind() my sockets to one or another IP addresses, on a multi homed system. I'm working with Visual studio 2008, on XP pro if it matters.

    Anyway, previously I just would not bother doing a bind, just allowing winsock to choose an appropriate IP. But now I was doing something like the below. (Assume dwMyIP is a ULONG containing a known good IP, in host order.)


    Code:
    struct sockaddr_in sock_bind_addr;
     
    sock_bind_addr.sin_family = AF_INET;  
    sock_bind_addr.sin_addr.S_un.S_addr = htonl(dwMyIp); 
    sock_bind_addr.sin_port = 0;   // 0 allows winsock to pick a port.
       
    if (bind(*pSockToStartWith, (struct sockaddr *)(&sock_bind_addr), sizeof(struct sockaddr_in)) == SOCKET_ERROR) {
    
    ... (Houston, we have a problem)'
     return -1;
    }
    So anyway, the bind would fail, and on a whim I decided to
    remove the htonl() call, and store the 32 bit address in host order in the sockaddr_in structure. It WORKED!

    Now I know everyone will jump to tell me that obviously I was missing something, and that my address was already in network order. Well consider this... the network IP in question was "192.168.0.190" Single step debugging though the code reveals that at this point in the program, dwMyIp = 0xBE00A8C0, which as an IP address is nothing like mine (makes sense... since I'm on an intel/windows machine). If, however I run my dwMyIP through htonl(), I get 0xC0A800BE, which indeed equates to 192.168.0.190. But that won't work, and I get an error (error 10049).

    Further, after completing the bind the "wrong" way (with my host order IP), I can connect to a test application on another machine, which does indeed report the incoming connection from 192.168.0.190. Now I repeated this test with the ULONG equivalent of every available IP IP I'm set up on my system and the result is the same. I can bind to any valid address as long as I store it in host order, not network order.

    OK...getting something to work the WRONG way and ignoring the problem is a prescription for a disaster sooner or later. So I'm about to make a hidden configuration variable for this to take care of both cases. But can anyone tell me why this is happening? Is there a known problem with the winsock (winsock 2 if it matters) bind() function? I can tell you from using the TCP/IP server variation, where I DO have to specify a port to connect to, that the expected behavior there is correct (meaning, if I want a client to connect to me at port 1234, I *DO* have to bind my listening socket to htons(1234).

    As far as my server goes, its currently bound to IP_ANY, which in windows equates to 0. I have not yet tested to to see how it responds to a host format (little endian) IP, and I'll do that next. But I thought it was a good stopping point and a good time to ask.

    Thanks for any help!

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,360

    Re: winsock... binding to WRONG IP byte order WORKS!

    Are you sure your dwMyIp does contain the address in correct host format?
    Victor Nijegorodov

  3. #3
    Join Date
    Apr 1999
    Location
    Tampa, FL
    Posts
    114

    Re: winsock... binding to WRONG IP byte order WORKS!

    Victor...

    positive. when the variable is equal to dwMyIp = 0xBE00A8C0, it works. Lo to hi (host order) of that value is 192.168.0.190, which is my adapter address. Run it through htonl() and you get C0AB00BE, which is correct network order, and that doesn't work.

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,360

    Re: winsock... binding to WRONG IP byte order WORKS!

    Just use inet_aton or directly assign in_addr.S_un_b members (s_b1 to s_b4) and you will never have any problem with IPv4 addresses!
    Victor Nijegorodov

  5. #5
    Join Date
    Apr 1999
    Location
    Tampa, FL
    Posts
    114

    Re: winsock... binding to WRONG IP byte order WORKS!

    Quote Originally Posted by VictorN View Post
    Just use inet_aton or directly assign in_addr.S_un_b members (s_b1 to s_b4) and you will never have any problem with IPv4 addresses!

    Thanks. Well inet_aton() is a unix function which I don't have it in winsock. What I do have is inet_addr(), which indeed works. When passed "192.168.0.190" it returns x = 3187714240, which in hex is 0xBE00A8C0. But from left to right (network order) this equates to 190.0.168.192, which again is the exact opposite of what its supposed to be according to everything I've read. Further, it is also the exact opposite of what works for a port number.

    The problem I suppose was that for better or worse, I feel much better understanding than just accepting. I already knew that the wrong byte order is what worked. But I was really hoping to discover from someone more experienced WHY this is happening. If this is just one of those things where Microsoft just took it upon itself to do things their own way, that's fine. But it ought to be documented somewhere, or at least someone else should know about it, right? Well I DO feel better now because I have found this anomaly documented at this link on the msdn, in case anyone else is curious. Here it actually says that when viewed on an intel architecture, the return value of inet_addr() will order the bytes of an a.b.c.d address as d.c.b.a. No sensible explanation why mind you! but at least it is documented!

    thanks again!


    http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,360

    Re: winsock... binding to WRONG IP byte order WORKS!

    Quote Originally Posted by Randy C View Post
    Thanks. Well inet_aton() is a unix function which I don't have it in winsock. What I do have is inet_addr(),
    I'm awfully sorry!
    It was my incorrect copy-paste operation! Of course, I meant inet_addr()!
    Victor Nijegorodov

  7. #7
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,109

    Re: winsock... binding to WRONG IP byte order WORKS!

    I think you are confusing two things:
    1. when setting up a connection, you always use the host byte order, because that is what is used internally on the system you are working on. It is the OS's responsibility to resolve any TCP-specific value to a general understandable format. IOW, you don't care about translating values other than using the socket functions such as VictorN suggested.
    2. When transmitting data in a non-proprietary format, you DO need to care about the format, that means you always use ntoh.. and hton.. whenever youd send or receive a number. You never know what kind of OS the other side of your connection resides on.

    HTH,
    Richard

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center