A Discrete-Event Network Simulator
API
netmap-device-creator.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) University of Washington
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */
18 
19 #include <unistd.h>
20 #include <string>
21 #include <iostream>
22 #include <iomanip>
23 #include <sstream>
24 #include <stdlib.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 
28 #include <sys/socket.h>
29 #include <sys/un.h>
30 #include <sys/ioctl.h>
31 #include <net/ethernet.h>
32 #include <net/if.h>
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
35 
36 #include "creator-utils.h"
37 
38 #define EMU_MAGIC 65867
39 
40 using namespace ns3;
41 
42 int
43 main (int argc, char *argv[])
44 {
45  int c;
46  char *path = NULL;
47 
48  opterr = 0;
49 
50  while ((c = getopt (argc, argv, "vp:")) != -1)
51  {
52  switch (c)
53  {
54  case 'v':
55  gVerbose = true;
56  break;
57  case 'p':
58  path = optarg;
59  break;
60  }
61  }
62 
63  //
64  // This program is spawned by an emu net device running in a simulation. It
65  // wants to create a raw socket as described below. We are going to do the
66  // work here since we're running suid root. Once we create the raw socket,
67  // we have to send it back to the emu net device. We do that over a Unix
68  // (local interprocess) socket. The emu net device created a socket to
69  // listen for our response on, and it is expected to have encoded the address
70  // information as a string and to have passed that string as an argument to
71  // us. We see it here as the "path" string. We can't do anything useful
72  // unless we have that string.
73  //
74  ABORT_IF (path == NULL, "path is a required argument", 0);
75  LOG ("Provided path is \"" << path << "\"");
76  //
77  // The whole reason for all of the hoops we went through to call out to this
78  // program will pay off here. We created this program to run as suid root
79  // in order to keep the main simulation program from having to be run with
80  // root privileges. We need root privileges to be able to open a raw socket
81  // though. So all of these hoops are to allow us to execute the following
82  // single line of code:
83  //
84  LOG ("Creating netmap fd");
85  int sock = open ("/dev/netmap", O_RDWR);
86  ABORT_IF (sock == -1, "CreateSocket(): Unable to open netmap fd", 1);
87 
88  //
89  // Send the socket back to the emu net device so it can go about its business
90  //
91  SendSocket (path, sock, EMU_MAGIC);
92 
93  return 0;
94 }
95 
#define LOG(x)
#define ABORT_IF(cond, msg, printErrno)
Definition: creator-utils.h:51
void SendSocket(const char *path, int fd, const int magic_number)
Send the file descriptor back to the code that invoked the creation.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
int gVerbose
Flag to enable / disable verbose log mode.
#define EMU_MAGIC