I figured out the problem. In case of eth0
, I am using a socket descriptor as the argument to ioctl()
, in case of tun device, I am using tun device descriptor. I created a socket in case of tun device too and the ioctl()
worked. Following is the code:
280 void setip (int fd) {
281
282 struct ifreq ifr;
283 struct sockaddr_in addr;
284 int stat, s;
285
286 memset(&ifr, 0, sizeof(ifr));
287 memset(&addr, 0, sizeof(addr));
288 strncpy(ifr.ifr_name, in.dev.device, IFNAMSIZ);
289
290 addr.sin_family = AF_INET;
291 s = socket(addr.sin_family, SOCK_DGRAM, 0);
292 stat = inet_pton(addr.sin_family, in.dev.ip_addr, &addr.sin_addr);
293 if (stat == 0)
294 raise_error("inet_pton() - invalid ip");
295 if (stat == -1)
296 raise_error("inet_pton() - invalid family");
297
298 if (stat == 1);
299 else
300 raise_error("inet_pton()");
301
302 ifr.ifr_addr = *(struct sockaddr *) &addr;
303 /* This is just to test if address conversion happened properly */
304 char buff[BUFF_SIZE];
305 char * foo;
306 foo = inet_ntop(AF_INET, &addr.sin_addr, buff, BUFF_SIZE);
307 if (foo == NULL)
308 raise_error("inet_ntop()");
309 else
310 printf("main = %s, addr = %s\n",in.dev.ip_addr, buff);
311
312 //if (ioctl(fd, SIOCSIFADDR, (caddr_t) &ifr) == -1)
313 if (ioctl(s, SIOCSIFADDR, (caddr_t) &ifr) == -1)
314 raise_error("ioctl() - SIOCSIFADDR");
315 }
Thanks Giuseppe Pes for your reply :)