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
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
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”);
#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;
}