35 #include <sys/ioctl.h> 
   36 #include <sys/types.h> 
   37 #include <sys/socket.h> 
   40 #include <linux/if_tun.h> 
   41 #include <net/route.h> 
   42 #include <netinet/in.h> 
   44 #include "creator-utils.h" 
   46 #define PLANETLAB_MAGIC 75867 
   48 #define VSYS_TUNTAP "/vsys/fd_tuntap.control" 
   49 #define VSYS_VIFUP_IN "/vsys/vif_up.in" 
   50 #define VSYS_VIFUP_OUT "/vsys/vif_up.out" 
   61 ReceiveVifFd (
int fd, 
char *vif_name)
 
   66   size_t ccmsg[CMSG_SPACE (
sizeof(
int)) / 
sizeof(size_t)];
 
   70   iov.iov_base = vif_name;
 
   71   iov.iov_len = IFNAMSIZ;
 
   79   msg.msg_control = ccmsg;
 
   80   msg.msg_controllen = 
sizeof(ccmsg);
 
   82   while (((rv = recvmsg (fd, &msg, 0)) == -1) && errno == EINTR)
 
   85   ABORT_IF (rv == -1, 
"Could not receive fd from Vsys", 0);
 
   86   ABORT_IF (!rv, 
"Could not receive fd from Vsys (EOF)", 0);
 
   88   cmsg = CMSG_FIRSTHDR (&msg);
 
   89   ABORT_IF (!cmsg->cmsg_type == SCM_RIGHTS, 
"got control message of unknown type" << cmsg->cmsg_type, 0);
 
   91   int* retfd  = (
int*)CMSG_DATA (cmsg);
 
  105 TunAlloc (
int iftype, 
char *if_name)
 
  108   struct sockaddr_un addr;
 
  111   control_fd = socket (AF_UNIX, SOCK_STREAM, 0);
 
  112   ABORT_IF (control_fd == -1, 
"Could not create UNIX socket", 0);
 
  114   memset (&addr, 0, 
sizeof(
struct sockaddr_un));
 
  117   addr.sun_family = AF_UNIX;
 
  118   strncpy (addr.sun_path, VSYS_TUNTAP, 
sizeof(addr.sun_path) - 1);
 
  120   ret = connect (control_fd, (
struct sockaddr *) &addr,
 
  121                  sizeof(
struct sockaddr_un));
 
  122   ABORT_IF (ret == -1, 
"Could not connect to Vsys control socket", 0);
 
  125   ret = send (control_fd, &iftype, 
sizeof(iftype), 0);
 
  126   ABORT_IF (ret != 
sizeof(iftype), 
"Could not send paramater to Vsys control socket", 0);
 
  128   return ReceiveVifFd (control_fd, if_name);
 
  140 SetTunUp (
const char *ip, 
const char *prefix, 
const char *if_name)
 
  147   memset(errbuff, 0, 4096);
 
  149   in = fopen (VSYS_VIFUP_IN, 
"a");
 
  153       ABORT_IF (in == NULL, 
"Failed to open " << VSYS_VIFUP_IN, 0);
 
  156   out = fopen (VSYS_VIFUP_OUT, 
"r");
 
  160       ABORT_IF (out == NULL, 
"Failed to open " << VSYS_VIFUP_OUT, 0);
 
  164   fprintf (in, 
"%s\n%s\n%s\nsnat=1\n", if_name, ip, prefix);
 
  169   nbytes = fread((
void*)errbuff, 4096, 1, out);
 
  172   ABORT_IF (strcmp(errbuff, 
"") != 0, errbuff, 0);
 
  178 main (
int argc, 
char *argv[])
 
  184   int iftype = IFF_TUN;
 
  187   memset(if_name, 0, 4096);
 
  190   while ((c = getopt (argc, argv, 
"vi:n:tp:")) != -1)
 
  212   ABORT_IF (ip == NULL, 
"IP Address is a required argument", 0);
 
  213   LOG (
"Provided IP Address is \"" << ip << 
"\"");
 
  215   ABORT_IF (prefix == NULL, 
"Prefix is a required argument", 0);
 
  216   LOG (
"Provided prefix \"" << prefix << 
"\"");
 
  218   ABORT_IF (path == NULL, 
"path is a required argument", 0);
 
  219   LOG (
"Provided path is \"" << path << 
"\"");
 
  221   LOG (
"Creating Tap");
 
  223   int fd = TunAlloc (iftype, if_name);
 
  224   ABORT_IF (fd == -1, 
"main(): Unable to create tap device", 1);
 
  227   SetTunUp (ip, prefix, (
const char*)if_name);
 
void SendSocket(const char *path, int fd, const int magic_number)
Send the file descriptor back to the code that invoked the creation.