Thread: Get local IP address

  1. #1
    return 0;
    Join Date
    Jan 2005
    Location
    Netherlands
    Posts
    89

    Get local IP address

    How can I find the IP that's binded to a socket? When doing this:

    Code:
    struct sockaddr_in local;
    local.sin_family      = AF_INET;
    local.sin_port        = htons(0);
    local.sin_addr.s_addr = INADDR_ANY;
    memset(&local.sin_zero,0,8);
    
    // Bind stuff
    
    int saSize            = sizeof(struct sockaddr);
    getsockname(sock,(struct sockaddr *)&local,&saSize);
    
    printf("%s:%d\n",inet_ntoa(local.sin_addr),ntohs(local.sin_port));
    It gives me "0.0.0.0:<port>", where <port> is the binded port. So I can get the port number, but now I also need the IP address...

  2. #2
    return 0;
    Join Date
    Jan 2005
    Location
    Netherlands
    Posts
    89
    Ok nevermind, I found the answer. For those of you who're interested, it's (obviously) quite logical:

    INADDR_ANY means it will listen to all addresses that the current host owns, so it's impossible to do getsockname() on the listening socket (because it would have to store more than one sockaddr, for example one for 127.0.0.1 and one for your real IP). But when calling getsockname() on a new socket created by accept(), you will get the right IP address. Here's a little code to test it:

    Code:
    #include <stdio.h>
    #include <winsock2.h>
    
    int main() {
      WSADATA wsaData;
      if(WSAStartup(MAKEWORD(2,0),&wsaData) == -1)
        return 0;
    
      int sock;
      if((sock = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP)) == -1)
        return 0;
    
      struct sockaddr_in local;
      local.sin_family      = AF_INET;
      local.sin_port        = htons(1032);
      local.sin_addr.s_addr = INADDR_ANY;
      memset(&local.sin_zero,0,8);
    
      if(bind(sock,(struct sockaddr *)&local,sizeof(struct sockaddr)) == -1)
        return 0;
    
      if(listen(sock,0) == -1)
        return 0;
    
      int saSize            = sizeof(struct sockaddr);
      getsockname(sock,(struct sockaddr *)&local,&saSize);
    
      struct sockaddr_in remote;
    
      int client;
      while(client = accept(sock,(struct sockaddr *)&remote,&saSize)) {
        getsockname(client,(struct sockaddr *)&local,&saSize);
        printf("%s:%d\n",inet_ntoa(local.sin_addr),ntohs(local.sin_port));
        close(client);
      }
    
      return 0;
    }
    When telnetting to localhost port 1032, it will give "127.0.0.1:1032". When telnetting to <my ip> port 1032, it will give "<my ip>:1032".

  3. #3
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    Just something that poped in my head:
    from a routing table point of view 0.0.0.0 is all addresses... in other words listen on all addresses
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  4. #4
    Registered User
    Join Date
    Aug 2004
    Posts
    14
    Does it show up as 0.0.0.0 because it isnt binded yet?

    In routing, we use 0.0.0.0 to define a gateway of last resort.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    To you the local machines ip address, call gethostname(), then gethostbyname().

    In routing, we use 0.0.0.0 to define a gateway of last resort.
    No, you dont. A gateway of last resort is just an ip address to send packets to that are destined to a subnet that the router doesn't know about. Setting the gateway to 0.0.0.0 wouldn't be very helpful at all.

  6. #6
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    by default, every pc has a "route all to default gateway" line in it's routing table, as a last resolve.

    example:

    Network Dest: 0.0.0.0
    Default Gateway: 192.168.100.254
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    And on a listening socket, it means listen on all interfaces. Basicly its a catch all. some one can connect on any ip, and it will catch it.
    If any part of my post is incorrect, please correct me.

    This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.

  8. #8
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Actually the only way to be sure to bind to all interfaces is to bind to INADDR_ANY. Binding to 0.0.0.0 may work depending upon the underlying implementation.

    by default, every pc has a "route all to default gateway" line in it's routing table, as a last resolve.
    This is only true for windows machines. On my linux machine, I have to set the gateway of last resort manually.

  9. #9
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    if i'm not misstaken it can be set by dhcp, i think...
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  10. #10
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Yes, it can if you have a DHCP client installed, and your router is running a DHCP server.

  11. #11
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    which it is, if you're working "directly" with the ISP, without any local router with nat.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  12. #12
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    I'm not sure what point you're trying to make here...

    Oh, and my ISP certanly is not running a DHCP server. Neither is my router. In fact, my computer doesn't even have a DHCP client installed. What brought up DCHP anyways? It really has nothing at all to do with gateways which is what I thought this conversation was about. All DHCP does is dynamically set a computer/routers network settings.

  13. #13
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    nm, i think this thread kinda went off topping after the second post



    but still, an ISP without DHCP server? how are you connected?
    Last edited by Devil Panther; 02-28-2005 at 02:56 PM.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  14. #14
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    DSL. It uses PPPoE instead of DHCP. In fact, the only ISP I can think of that uses DHCP is cable providers.

  15. #15
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    i see your point, thanks
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linux raw socket programming
    By cnb in forum Networking/Device Communication
    Replies: 17
    Last Post: 11-08-2010, 08:56 AM
  2. IP address network order to string?
    By pobri19 in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-19-2009, 05:15 AM
  3. IP address configuration Files?
    By Matus in forum Tech Board
    Replies: 3
    Last Post: 01-29-2009, 04:35 PM
  4. MSN Vital Information
    By iain in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 09-22-2001, 08:55 PM