netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Glover George" <dime@gulfsales.com>
To: <linux-net@vger.kernel.org>
Subject: Obtaining the ip address of an interface dynamically
Date: Tue, 30 Apr 2002 11:48:05 -0500	[thread overview]
Message-ID: <001401c1f066$cce6c210$0300a8c0@yellow> (raw)

Hi, I'm working on a little module here and was wondering what the BEST
way to get an interface's current ip address by using it's name (i.e.,
eth0) is.  I was trying one way using sockets and it just doesn't seem
to reliably give it to me every time.  I was using these two functions
below, but I it would return NULL some of the time from
IPCon_GetIpAddrByStr sometimes, so I'm not sure.  I need a way to be
able to reliably obtain this ip at any give time (due to dhcp, etc). Any
pointers?  TIA.


struct in_addr * IPCon::IPCon_GetIpAddr(void)
{
        struct ifreq ifr;
        struct sockaddr_in *saddr;
        int fd;
        fd = get_sockfd();
        if (fd >= 0 )
        {
                strcpy(ifr.ifr_name, m_ifname);
                ifr.ifr_addr.sa_family = AF_INET;
                if (ioctl(fd, SIOCGIFADDR, &ifr) == 0)
                {
                        saddr = (sockaddr_in *)&ifr.ifr_addr;
                        return &saddr->sin_addr;
                }
                else
                {
                        close(fd);
                        return NULL;
                }
        }
        return NULL;
}

char * IPCon::IPCon_GetIpAddrStr(void)
{
        /*struct in_addr *adr;
        adr = IPCon_GetIpAddr();
        if (adr == NULL)
                return NULL;
        else
                return inet_ntoa(*adr);*/
        return "208.164.149.42";
}


             reply	other threads:[~2002-04-30 16:48 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-04-30 16:48 Glover George [this message]
2002-04-30 18:36 ` Obtaining the ip address of an interface dynamically Glynn Clements
2002-04-30 19:38 ` Bernd Eckenfels

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='001401c1f066$cce6c210$0300a8c0@yellow' \
    --to=dime@gulfsales.com \
    --cc=linux-net@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).