getting IP address in use

bridged with qdn.public.ddk.network
Post Reply
Paul Newman

getting IP address in use

Post by Paul Newman » Tue May 20, 2003 8:58 pm

Hi, I need to show my user the current IP address on en0 as well as whether
the link on ppp0 is up. I don't want to capture the output of ifconfig and
show it to the user. How would I get the values programmatically?

Thanks.

Paul

Sean Boudreau

Re: getting IP address in use

Post by Sean Boudreau » Wed May 21, 2003 1:13 pm

Paul Newman <pnewman@nfini.com> wrote:
Hi, I need to show my user the current IP address on en0 as well as whether
the link on ppp0 is up. I don't want to capture the output of ifconfig and
show it to the user. How would I get the values programmatically?

Thanks.

Paul
I'm not sure what you mean by the link being up, but the following gets
an interface's flags and its first addr.

-seanb

---------------------------------------------

#include <sys/socket.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <stdio.h>
#include <net/if.h>


int
main(void)
{
int s;
struct ifreq ifreq;

if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("socket");
return EXIT_FAILURE;
}

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

strcpy(ifreq.ifr_name, "en0");

if (ioctl(s, SIOCGIFFLAGS, &ifreq) == -1) {
perror("ioctl");
return EXIT_FAILURE;
}

if (ifreq.ifr_flags & IFF_UP)
printf("en0 is up\n");
else
printf("en0 is down\n");

return EXIT_SUCCESS;
}



---------------------------------------------



#include <sys/socket.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <stdio.h>
#include <net/if.h>
#include <netinet/in.h>


int
main(void)
{
int s;
struct ifreq ifreq;
char dst[INET_ADDRSTRLEN];
struct sockaddr_in *sa_in;

if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("socket");
return EXIT_FAILURE;
}

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

strcpy(ifreq.ifr_name, "en0");

if (ioctl(s, SIOCGIFADDR, &ifreq) == -1) {
perror("ioctl");
return EXIT_FAILURE;
}

sa_in = (struct sockaddr_in *)&ifreq.ifr_addr;
inet_ntop(AF_INET, &sa_in->sin_addr.s_addr, dst, sizeof(dst));
printf("en0: %s\n", dst);

return EXIT_SUCCESS;
}

Post Reply

Return to “qdn.public.ddk.network”