IBR-DTNSuite  0.8
tools/src/DTNTun/tun.c
Go to the documentation of this file.
00001 #include "tun.h"
00002 
00003 
00004  int tun_create(char *dev)
00005   {
00006       struct ifreq ifr;
00007       int fd, err;
00008 
00009       if( (fd = open("/dev/net/tun", O_RDWR)) < 0 )
00010          return -1;
00011 
00012       memset(&ifr, 0, sizeof(ifr));
00013 
00014       /* Flags: IFF_TUN   - TUN device (no Ethernet headers)
00015        *        IFF_TAP   - TAP device
00016        *
00017        *        IFF_NO_PI - Do not provide packet information
00018        */
00019       ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
00020       if( *dev )
00021          strncpy(ifr.ifr_name, dev, IFNAMSIZ);
00022 
00023       if( (err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0 ){
00024           perror("Can't create TUN device: ");
00025          close(fd);
00026          return err;
00027       }
00028       strcpy(dev, ifr.ifr_name);
00029       return fd;
00030   }
00031 
00032 
00034   int tun_conf_ipv4(char *ifname,  char *ip_from, char *ip_to) {
00035     char ifcfg_cmd[256];
00036 
00037     if (strlen(ip_from) > 15) {
00038         printf("tun_set_ipv4: Invalid from address %s\n",ip_from);
00039         return -1;
00040     }
00041     if (strlen(ip_to) > 15) {
00042         printf("tun_set_ipv4: Invalid to address %s\n",ip_to);
00043         return -1;
00044     }
00045     snprintf(ifcfg_cmd, 256, "ifconfig %s -pointopoint %s dstaddr %s",ifname, ip_from, ip_to);
00046     if ( system(ifcfg_cmd) != 0) {
00047         puts("Error in ifconfig");
00048         return -1;
00049     }
00050 
00051 
00052     return 0;
00053 
00054 }