Subject: Re: ifconfig and inet
To: Patrick Welche <prlw1@newn.cam.ac.uk>
From: Andrew Brown <atatat@atatdot.net>
List: netbsd-users
Date: 07/30/2002 10:14:27
>I may have missed something, but what? I ifconfig fxp0 inet 0.0.0.0, then
>run the program below. ifconfig -a now no longer shows an inet line for
>fxp0, yet the interface works (can ssh in), so the program must have worked
>and set the interface. ifconfig -a not showing it makes me wonder if I
>missed something anyway. Thoughts?
don't forget to (a) zero out the struct ifreq first, and (b) set
sin->sin_len to something sensible. then ifconfig -a will work.
>#include <sys/ioctl.h>
>#include <sys/socket.h>
>#include <netinet/in.h>
>#include <arpa/inet.h>
>#include <net/if.h>
>
>#include <err.h>
>#include <stdio.h>
>#include <string.h>
>#include <unistd.h>
>
>int main()
>{
> int s;
> struct ifreq ifr;
> struct sockaddr_in *sin;
>
> s=socket(PF_INET,SOCK_DGRAM,0);
> strncpy(ifr.ifr_name,"fxp0",sizeof(ifr.ifr_name));
> sin=(struct sockaddr_in*)&ifr.ifr_addr;
> sin->sin_family=AF_INET;
> if(inet_pton(AF_INET,"192.168.4.21",&sin->sin_addr)<0)err(1,NULL);
> printf("%s: %s\n",ifr.ifr_name,inet_ntoa(sin->sin_addr));
> if(ioctl(s,SIOCSIFADDR,&ifr)<0)err(1,"SIOCSIFADDR");
> if(ioctl(s,SIOCGIFADDR,&ifr)<0)err(1,"SIOCGIFADDR");
> printf("%s\n",inet_ntoa(sin->sin_addr));
> close(s);
>
> return 0;
>}
--
|-----< "CODE WARRIOR" >-----|
codewarrior@daemon.org * "ah! i see you have the internet
twofsonet@graffiti.com (Andrew Brown) that goes *ping*!"
andrew@crossbar.com * "information is power -- share the wealth."