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> 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" 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);
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 parameter to Vsys control socket", 0);
140 SetTunUp (
const char *ip,
const char *prefix,
const char *if_name)
146 memset(errbuff, 0, 4096);
163 fprintf (in,
"%s\n%s\n%s\nsnat=1\n", if_name, ip, prefix);
168 fread((
void*)errbuff, 4096, 1, out);
171 ABORT_IF (strcmp(errbuff,
"") != 0, errbuff, 0);
177 main (
int argc,
char *argv[])
183 int iftype = IFF_TUN;
186 memset(if_name, 0, 4096);
189 while ((c = getopt (argc, argv,
"vi:n:tp:")) != -1)
211 ABORT_IF (ip == NULL,
"IP Address is a required argument", 0);
212 LOG (
"Provided IP Address is \"" << ip <<
"\"");
214 ABORT_IF (prefix == NULL,
"Prefix is a required argument", 0);
215 LOG (
"Provided prefix \"" << prefix <<
"\"");
217 ABORT_IF (path == NULL,
"path is a required argument", 0);
218 LOG (
"Provided path is \"" << path <<
"\"");
220 LOG (
"Creating Tap");
222 int fd =
TunAlloc (iftype, if_name);
223 ABORT_IF (fd == -1,
"main(): Unable to create tap device", 1);
226 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.
ns3::StringValue attribute value declarations.
#define ABORT_IF(cond, msg, printErrno)
int gVerbose
Flag to enable / disable verbose log mode.
void SetTunUp(const char *ip, const char *prefix, const char *if_name)
Sets the TAP/TUN interface up.
int TunAlloc(int iftype, char *if_name)
Creates a TUN/TAP device on a PlanetLab virtual machine (sliver).
Every class exported by the ns3 library is enclosed in the ns3 namespace.
int ReceiveVifFd(int fd, char *vif_name)
Reads vif FD from "fd", writes interface name to vif_name, and returns vif FD.