netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Obtaining the ip address of an interface dynamically
@ 2002-04-30 16:48 Glover George
  2002-04-30 18:36 ` Glynn Clements
  2002-04-30 19:38 ` Bernd Eckenfels
  0 siblings, 2 replies; 3+ messages in thread
From: Glover George @ 2002-04-30 16:48 UTC (permalink / raw)
  To: linux-net

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";
}


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Obtaining the ip address of an interface dynamically
  2002-04-30 16:48 Obtaining the ip address of an interface dynamically Glover George
@ 2002-04-30 18:36 ` Glynn Clements
  2002-04-30 19:38 ` Bernd Eckenfels
  1 sibling, 0 replies; 3+ messages in thread
From: Glynn Clements @ 2002-04-30 18:36 UTC (permalink / raw)
  To: Glover George; +Cc: linux-net


Glover George wrote:

> 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;

You are returning a pointer to data which is on the stack, and which
ceases to be valid once you've returned from the function.

You should either have the caller supply the buffer, e.g.

	bool IPCon::IPCon_GetIpAddr(struct in_addr *result)

or allocate the memory dynamically, with malloc() or new.

-- 
Glynn Clements <glynn.clements@virgin.net>

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Obtaining the ip address of an interface dynamically
  2002-04-30 16:48 Obtaining the ip address of an interface dynamically Glover George
  2002-04-30 18:36 ` Glynn Clements
@ 2002-04-30 19:38 ` Bernd Eckenfels
  1 sibling, 0 replies; 3+ messages in thread
From: Bernd Eckenfels @ 2002-04-30 19:38 UTC (permalink / raw)
  To: linux-net

In article <001401c1f066$cce6c210$0300a8c0@yellow> you wrote:
> IPCon_GetIpAddrByStr sometimes, so I'm not sure.

You should look for errno/return value of ictl to be able to provide more
info where the problem is. 

BTW: you version leaks file descriptors.


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2002-04-30 19:38 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2002-04-30 16:48 Obtaining the ip address of an interface dynamically Glover George
2002-04-30 18:36 ` Glynn Clements
2002-04-30 19:38 ` Bernd Eckenfels

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).