Subject: Re: SIOCAIFADDR vs ifconfig
To: Tomas Berndtsson <>
From: Andrew Brown <>
List: tech-net
Date: 09/24/2001 09:54:45
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

>> >ifconfig lo0 netmask alias
>> And if you do: 
>> ifconfig lo0 netmask 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.

this program works for me.  see if you rip out the bits you need.

|-----< "CODE WARRIOR" >-----|             * "ah!  i see you have the internet (Andrew Brown)                that goes *ping*!"       * "information is power -- share the wealth."

Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="siocaifaddr.c"

#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/in.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>

main(int argc, char *argv[])
	int s;
	struct ifaliasreq ifaliasreq;
	struct sockaddr_in *in;

	s = socket(PF_INET, SOCK_STREAM, 0);

	memset(&ifaliasreq, 0, sizeof(ifaliasreq));

	sprintf(ifaliasreq.ifra_name, "lo0");

	in = (struct sockaddr_in *) &ifaliasreq.ifra_addr;
	in->sin_family = AF_INET;
	in->sin_len = sizeof(ifaliasreq.ifra_addr);
	in->sin_addr.s_addr = inet_addr("");

	in = (struct sockaddr_in *) &ifaliasreq.ifra_mask;
	in->sin_family = AF_INET;
	in->sin_len = sizeof(ifaliasreq.ifra_mask);
	in->sin_addr.s_addr = inet_addr("");

	if (ioctl(s, SIOCAIFADDR, &ifaliasreq) == -1)

	return (0);