Finding your own IP address? - TCP-IP

This is a discussion on Finding your own IP address? - TCP-IP ; I'm looking for a way to discover from inside a C program what your own IP address is. Specifically, I'm trying to implement the RFC 2571 algorithm for determining SnmpEngineID. This requires knowing your "lowest non-special IP address". Finding out ...

+ Reply to Thread
Results 1 to 6 of 6

Thread: Finding your own IP address?

  1. Finding your own IP address?

    I'm looking for a way to discover from inside a C program what your
    own IP address is. Specifically, I'm trying to implement the RFC 2571
    algorithm for determining SnmpEngineID. This requires knowing your
    "lowest non-special IP address". Finding out all the IP addresses
    assigned to interfaces would be ideal; finding out at least one would
    would be a start.

    This needs to work on random Unix boxes as well as on Windows. It
    doesn't have to be the same method on all systems; I can switch out to
    system-dependent code, but I do need something that works on them all.

    One idea we've got is to do a gethostname(), then do a name lookup on
    that. It'll probably work, but getting DNS involved seems sub-optimal
    if it can be avoided.

    Another idea was to create a UDP socket, bind it to INADDR_ANY, do a
    getsockname(), and see what it got bound to.

    Any other ideas?


  2. Re: Finding your own IP address?

    In article , Roy Smith wrote:

    >I'm looking for a way to discover from inside a C program what your
    >own IP address is.


    >This needs to work on random Unix boxes as well as on Windows.


    >One idea we've got is to do a gethostname(), then do a name lookup on
    >that. It'll probably work, but getting DNS involved seems sub-optimal
    >if it can be avoided.


    Using DNS sounds like an excellent way to get plenty of wrong answers.

    I try to use a local flavor of getifaddrs(3) for daemons that want to
    bind separate sockets to all interfaces so that they can answer UDP
    requests with the right source IP address. Such code is not very
    portable without plenty of #ifdef's and stubbs. For example, some
    systems do not have freeifaddrs(). See
    http://www.google.com/search?q=getifaddrs

    Where getifaddrs() is absent, I try to fall back on own kludge
    implementation of getifaddrs() that uses SIOCGIFCONF. See
    http://www.google.com/search?q=SIOCGIFCONF

    See als the source for the ifconfig command on your target platforms.


    I've suppressed all recollection what if anything can be done on Windows.


    Vernon Schryver vjs@rhyolite.com

  3. Re: Finding your own IP address?

    vjs@calcite.rhyolite.com (Vernon Schryver) wrote:
    > I try to use a local flavor of getifaddrs(3) for daemons that want to
    > bind separate sockets to all interfaces so that they can answer UDP
    > requests with the right source IP address. Such code is not very
    > portable without plenty of #ifdef's and stubbs. For example, some
    > systems do not have freeifaddrs(). See
    > http://www.google.com/search?q=getifaddrs


    I just looked on three of our systems (solaris, hpux, and linux), and none
    of them appear to have getifaddrs(). But, they all have SIOCGIFCONF
    (spelled differently on Windows), so it seems like building my own
    getifaddrs() should be possible. Thanks for the tip.

  4. Re: Finding your own IP address?


    Roy Smith wrote:

    > Another idea was to create a UDP socket, bind it to INADDR_ANY, do a
    > getsockname(), and see what it got bound to.


    That will just return INADDR_ANY, since that's what you bound it to.

    DS


  5. Re: Finding your own IP address?

    On Tue, 15 Aug 2006 08:20:57 -0400, Roy Smith wrote:

    > vjs@calcite.rhyolite.com (Vernon Schryver) wrote:
    >> I try to use a local flavor of getifaddrs(3) for daemons that want to
    >> bind separate sockets to all interfaces so that they can answer UDP
    >> requests with the right source IP address. Such code is not very
    >> portable without plenty of #ifdef's and stubbs. For example, some
    >> systems do not have freeifaddrs(). See
    >> http://www.google.com/search?q=getifaddrs

    >
    > I just looked on three of our systems (solaris, hpux, and linux), and none
    > of them appear to have getifaddrs(). But, they all have SIOCGIFCONF
    > (spelled differently on Windows), so it seems like building my own
    > getifaddrs() should be possible. Thanks for the tip.



    I came across one solution but I don't think it is the most efficient way.

    1. call gethostname() to get hostname
    2. call gethostbyname() with your hostname as input parameter to get the
    local IP.

    I've used this before to send IP address as data.

    Hope this helps

  6. Re: Finding your own IP address?

    On Wed, 23 Aug 2006 19:58:48 +0100, Ural Mutlu wrote:

    > On Tue, 15 Aug 2006 08:20:57 -0400, Roy Smith wrote:
    >
    >> vjs@calcite.rhyolite.com (Vernon Schryver) wrote:
    >>> I try to use a local flavor of getifaddrs(3) for daemons that want to
    >>> bind separate sockets to all interfaces so that they can answer UDP
    >>> requests with the right source IP address. Such code is not very
    >>> portable without plenty of #ifdef's and stubbs. For example, some
    >>> systems do not have freeifaddrs(). See
    >>> http://www.google.com/search?q=getifaddrs

    >>
    >> I just looked on three of our systems (solaris, hpux, and linux), and none
    >> of them appear to have getifaddrs(). But, they all have SIOCGIFCONF
    >> (spelled differently on Windows), so it seems like building my own
    >> getifaddrs() should be possible. Thanks for the tip.

    >
    >
    > I came across one solution but I don't think it is the most efficient way.
    >
    > 1. call gethostname() to get hostname
    > 2. call gethostbyname() with your hostname as input parameter to get the
    > local IP.
    >
    > I've used this before to send IP address as data.
    >
    > Hope this helps


    I have to add that those functions are IPv4 functions and ideally you
    should be using protocol independent functions instead. check getaddrinfo
    and getnameinfo..


+ Reply to Thread