Determine IP address programmatically

Hope this is the right place to ask this…

What is the best way of finding out, in code, the current ip address
assigned to a particular device e.g. en0?

Thanks in advance,
James.

Look at the SIOCGIFADDR ioctl.

-seanb

Zak Spede <zak.spede@raceme.com> wrote:

Hope this is the right place to ask this…

What is the best way of finding out, in code, the current ip address
assigned to a particular device e.g. en0?

Thanks in advance,
James.

Thanks for the response Sean - I’d figured out this was probably the way to
go.

I tried the following code, but got error 48 on the ioctl() call - ‘Not
supported’

ifreq ifnet_flags;
memset(&ifnet_flags, 0, sizeof(ifreq));

int dev_fd = open("/dev/io-net/en0", O_RDWR);
int status = ioctl(dev_fd, SIOCGIFFLAGS, &ifnet_flags);

Am I getting a file des in the right way? Or do I need to open a socket and
use that?
Do I need to do some initialisation on the ifreq struct? If so where can I
look to found out?


Thanks again,
James.

“Sean Boudreau” <seanb@node25.ott.qnx.com> wrote in message
news:b3dlmd$req$1@nntp.qnx.com

Look at the SIOCGIFADDR ioctl.

-seanb

Zak Spede <> zak.spede@raceme.com> > wrote:
Hope this is the right place to ask this…

What is the best way of finding out, in code, the current ip address
assigned to a particular device e.g. en0?

Thanks in advance,
James.

Yes, you need to open a socket:

fd = socket(AF_INET, SOCK_DGRAM, 0);
strcpy(ifnet.flags.ifr_name, “en0”);

Murf

Zak Spede wrote:

Thanks for the response Sean - I’d figured out this was probably the way to
go.

I tried the following code, but got error 48 on the ioctl() call - ‘Not
supported’

ifreq ifnet_flags;
memset(&ifnet_flags, 0, sizeof(ifreq));

int dev_fd = open("/dev/io-net/en0", O_RDWR);
int status = ioctl(dev_fd, SIOCGIFFLAGS, &ifnet_flags);

Am I getting a file des in the right way? Or do I need to open a socket and
use that?
Do I need to do some initialisation on the ifreq struct? If so where can I
look to found out?

Thanks again,
James.

“Sean Boudreau” <> seanb@node25.ott.qnx.com> > wrote in message
news:b3dlmd$req$> 1@nntp.qnx.com> …

Look at the SIOCGIFADDR ioctl.

-seanb

Zak Spede <> zak.spede@raceme.com> > wrote:
Hope this is the right place to ask this…

What is the best way of finding out, in code, the current ip address
assigned to a particular device e.g. en0?

Thanks in advance,
James.

Your’e asking for the flags (SIOCGIFFLAGS) instead of the address; try
SIOCGIFADDR.

Murf

Zak Spede wrote:

OK, this is the code i’m now using (on QNX6.1):

struct ifreq ifnet;
char* ifname = “en0”;
strncpy(ifnet.ifr_name, ifname, IFNAMSIZ);
int sock = socket(AF_INET, SOCK_DGRAM, 0);
int status = ioctl(sock, SIOCGIFFLAGS, &ifnet);

sockaddr_in* addr = (sockaddr_in*)(&(ifnet.ifr_ifru.ifru_addr));
char* ipString = inet_ntoa(addr->sin_adr);

the IP is always 255.255.255.255. What am I doing wrong??

thanks,
James
John A. Murphy wrote:
Yes, you need to open a socket:

fd = socket(AF_INET, SOCK_DGRAM, 0);
strcpy(ifnet.flags.ifr_name, “en0”);

Murf

Zak Spede wrote:


Thanks for the response Sean - I’d figured out this was probably the way to
go.

I tried the following code, but got error 48 on the ioctl() call - ‘Not
supported’

ifreq ifnet_flags;
memset(&ifnet_flags, 0, sizeof(ifreq));

int dev_fd = open("/dev/io-net/en0", O_RDWR);
int status = ioctl(dev_fd, SIOCGIFFLAGS, &ifnet_flags);

Am I getting a file des in the right way? Or do I need to open a socket and
use that?
Do I need to do some initialisation on the ifreq struct? If so where can I
look to found out?

Thanks again,
James.

“Sean Boudreau” <> seanb@node25.ott.qnx.com> > wrote in message
news:b3dlmd$req$> 1@nntp.qnx.com> …

Look at the SIOCGIFADDR ioctl.

-seanb

Zak Spede <> zak.spede@raceme.com> > wrote:

Hope this is the right place to ask this…

What is the best way of finding out, in code, the current ip address
assigned to a particular device e.g. en0?

Thanks in advance,
James.

OK, this is the code i’m now using (on QNX6.1):

struct ifreq ifnet;
char* ifname = “en0”;
strncpy(ifnet.ifr_name, ifname, IFNAMSIZ);
int sock = socket(AF_INET, SOCK_DGRAM, 0);
int status = ioctl(sock, SIOCGIFFLAGS, &ifnet);

sockaddr_in* addr = (sockaddr_in*)(&(ifnet.ifr_ifru.ifru_addr));
char* ipString = inet_ntoa(addr->sin_adr);

the IP is always 255.255.255.255. What am I doing wrong??

thanks,
James
John A. Murphy wrote:

Yes, you need to open a socket:

fd = socket(AF_INET, SOCK_DGRAM, 0);
strcpy(ifnet.flags.ifr_name, “en0”);

Murf

Zak Spede wrote:


Thanks for the response Sean - I’d figured out this was probably the way to
go.

I tried the following code, but got error 48 on the ioctl() call - ‘Not
supported’

ifreq ifnet_flags;
memset(&ifnet_flags, 0, sizeof(ifreq));

int dev_fd = open("/dev/io-net/en0", O_RDWR);
int status = ioctl(dev_fd, SIOCGIFFLAGS, &ifnet_flags);

Am I getting a file des in the right way? Or do I need to open a socket and
use that?
Do I need to do some initialisation on the ifreq struct? If so where can I
look to found out?

Thanks again,
James.

“Sean Boudreau” <> seanb@node25.ott.qnx.com> > wrote in message
news:b3dlmd$req$> 1@nntp.qnx.com> …

Look at the SIOCGIFADDR ioctl.

-seanb

Zak Spede <> zak.spede@raceme.com> > wrote:

Hope this is the right place to ask this…

What is the best way of finding out, in code, the current ip address
assigned to a particular device e.g. en0?

Thanks in advance,
James.

Cheers John,
that did the trick!!

James.

John A. Murphy wrote:

Your’e asking for the flags (SIOCGIFFLAGS) instead of the address; try
SIOCGIFADDR.

Murf

Zak Spede wrote:


OK, this is the code i’m now using (on QNX6.1):

struct ifreq ifnet;
char* ifname = “en0”;
strncpy(ifnet.ifr_name, ifname, IFNAMSIZ);
int sock = socket(AF_INET, SOCK_DGRAM, 0);
int status = ioctl(sock, SIOCGIFFLAGS, &ifnet);

sockaddr_in* addr = (sockaddr_in*)(&(ifnet.ifr_ifru.ifru_addr));
char* ipString = inet_ntoa(addr->sin_adr);

the IP is always 255.255.255.255. What am I doing wrong??

thanks,
James
John A. Murphy wrote:

Yes, you need to open a socket:

fd = socket(AF_INET, SOCK_DGRAM, 0);
strcpy(ifnet.flags.ifr_name, “en0”);

Murf

Zak Spede wrote:



Thanks for the response Sean - I’d figured out this was probably the way to
go.

I tried the following code, but got error 48 on the ioctl() call - ‘Not
supported’

ifreq ifnet_flags;
memset(&ifnet_flags, 0, sizeof(ifreq));

int dev_fd = open("/dev/io-net/en0", O_RDWR);
int status = ioctl(dev_fd, SIOCGIFFLAGS, &ifnet_flags);

Am I getting a file des in the right way? Or do I need to open a socket and
use that?
Do I need to do some initialisation on the ifreq struct? If so where can I
look to found out?

Thanks again,
James.

“Sean Boudreau” <> seanb@node25.ott.qnx.com> > wrote in message
news:b3dlmd$req$> 1@nntp.qnx.com> …


Look at the SIOCGIFADDR ioctl.

-seanb

Zak Spede <> zak.spede@raceme.com> > wrote:


Hope this is the right place to ask this…

What is the best way of finding out, in code, the current ip address
assigned to a particular device e.g. en0?

Thanks in advance,
James.