Subject: Re: SIOCAIFADDR vs ifconfig
To: None <itojun@iijlab.net>
From: Tomas Berndtsson <tomber@packetfront.com>
List: tech-net
Date: 09/24/2001 16:06:54
itojun@iijlab.net writes:
> >> And if you do:
> >> ifconfig lo0 192.168.127.26 netmask 255.255.255.255 alias
> >Same thing. When I set with ifconfig, it works. When I set with my
> >program, using SIOCAIFADDR, it doesn't work, even if the output from
> >netstat -rn and ifconfig -A lo0 is exactly the same.
>
> if you could post your program somewhere someone could debug it.
Ok. Here's my small test program, which doesn't work.
Running:
./iftest lo0 192.168.127.26 255.255.255.0
should, AFAIK, be the same as:
ifconfig lo0 192.168.127.26 netmask 255.255.255.0
--- iftest.c ---
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/if.h>
int main(int argc, char *argv[])
{
struct ifaliasreq ifra;
struct sockaddr_in ip;
int s;
if(argc < 4) {
printf("Usage: %s <if> <ip> <netmask>\n", argv[0]);
exit(1);
}
s = socket(PF_INET, SOCK_DGRAM, 0);
if(s < 0) {
perror("socket");
exit(1);
}
memset(&ifra, 0, sizeof(struct ifaliasreq));
strncpy(ifra.ifra_name, argv[1], IFNAMSIZ);
ip.sin_len = sizeof(struct sockaddr_in);
ip.sin_family = AF_INET;
inet_aton(argv[2], &ip.sin_addr);
memcpy(&ifra.ifra_addr, &ip, sizeof(struct sockaddr_in));
inet_aton(argv[3], &ip.sin_addr);
memcpy(&ifra.ifra_mask, &ip, sizeof(struct sockaddr_in));
if(ioctl(s, SIOCAIFADDR, &ifra) < 0)
perror("ioctl");
close(s);
return 0;
}
--- EOF ---
Greetings,
Tomas