Date: Tue, 28 Dec 1999 22:09:30 -0500 (EST) From: Matt Gostick <matt@crazylogic.net> To: freebsd-questions@freebsd.org Subject: ioctl programming probs Message-ID: <Pine.BSF.4.10.9912282005040.40430-100000@thunk.crazylogic.net>
next in thread | raw e-mail | index | archive | help
I'm trying to make a simple little program that gets the ip address of a given interface. I'm using ioctl to do this and am having a wierd problem that I can't figure out. Any help is appretiated. From what I understand about ioctl ... the below program should display the interface name, ip address, destination address and the broadcast address. Unfortuanely it's not. It's not crashing ... it just always prints: name : lo0 addr : 16.2.0.0 dstaddr : 16.2.0.0 broadaddr : 16.2.0.0 I've tried this on two different FreeBSD-3.2 machines with the same result, even the same IP's. I've even tried switching the interface names to something different, and am still getting the same IP's. I have searched newsgroups and mailing lists only to come up with no solution. I was hoping that those familiar with ioctl under FreeBSD will spot my mistake in a couple of seconds... :) I'm new to it so any help is very much appretiated. /* --<get_if_ip.c>------------------------------------- */ #include <stdio.h> #include <sys/types.h> #include <sys/ioctl.h> #include <sys/socket.h> #include <net/if.h> int main (void) { int fd; struct ifreq ifr; /* open a socket to use for ioctl */ if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { perror("socket"); return -1; } strcpy(ifr.ifr_name, "lo0"); /* get if address */ if (ioctl(fd, SIOCGIFADDR, &ifr, sizeof(ifr)) == -1) { close(fd); perror("ioctl"); return -1; } printf("name : %s\n", ifr.ifr_name); printf("addr : %s\n", inet_ntoa(ifr.ifr_addr)); printf("dstaddr : %s\n", inet_ntoa(ifr.ifr_dstaddr)); printf("broadaddr : %s\n", inet_ntoa(ifr.ifr_broadaddr)); close(fd); return 0; } /* --<end get_if_ip.c>---------------------------------- */ Thanks, Matt To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-questions" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?Pine.BSF.4.10.9912282005040.40430-100000>