Project Home
Project Home
Wiki
Wiki
Discussion Forums
Discussions
Project Information
Project Info
Forum Topic - how to programmingly set ifconfig alias: (1 Item)
   
how to programmingly set ifconfig alias  
Hi,

The following is the code piece used to set the IP address of one physcial interface, say e0. It works well.

struct ifreq niIfreq;
long socketId; 
struct sockaddr_in addr_in;

memset (&addr_in, 0, sizeof(struct sockaddr_in));
memset (&niIfreq, 0, sizeof(struct ifreq));

addr_in.sin_family      = AF_INET;
addr_in.sin_addr.s_addr = htonl (INADDR_ANY);
addr_in.sin_port        = htons (5000);

socketId = socket(AF_INET, SOCK_DGRAM, 0);
bind(socketId, (struct sockaddr *)&addr_in, sizeof(struct sockaddr_in));

strncpy(niIfreq.ifr_name, "en0", sizeof("en0"));
niIfreq.ifr_ifru.ifru_addr.sa_len = 6;
niIfreq.ifr_ifru.ifru_addr.sa_family = 0;

niIfreq.ifr_ifru.ifru_addr.sa_data[0] = (uint8_t)0x00;
niIfreq.ifr_ifru.ifru_addr.sa_data[1] = (uint8_t)0x00;
niIfreq.ifr_ifru.ifru_addr.sa_data[2] = (uint8_t)0x0B;
niIfreq.ifr_ifru.ifru_addr.sa_data[3] = (uint8_t)0x05;
niIfreq.ifr_ifru.ifru_addr.sa_data[4] = (uint8_t)0x00;
niIfreq.ifr_ifru.ifru_addr.sa_data[5] = (uint8_t)0x06;

ioctl(socketId, SIOCSIFNETMASK, &niIfreq);
###############

Question,
is it possible to write to simple data struct of the same niIfreq to achive the 
result of the following commad?
e.g
the code to realize "ifconfig en0 alias xx.xx.xx.xx".

if not, what should be the code?

Thanks in advance.