A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
API
Loading...
Searching...
No Matches
netmap-device-creator.cc
Go to the documentation of this file.
1
/*
2
* Copyright (c) University of Washington
3
*
4
* SPDX-License-Identifier: GPL-2.0-only
5
*/
6
7
#include "
creator-utils.h
"
8
9
#include <arpa/inet.h>
10
#include <errno.h>
11
#include <fcntl.h>
12
#include <iomanip>
13
#include <iostream>
14
#include <net/ethernet.h>
15
#include <net/if.h>
16
#include <netinet/in.h>
17
#include <sstream>
18
#include <stdlib.h>
19
#include <string>
20
#include <sys/ioctl.h>
21
#include <sys/socket.h>
22
#include <sys/un.h>
23
#include <unistd.h>
24
25
#define EMU_MAGIC 65867
26
27
using namespace
ns3
;
28
29
int
30
main(
int
argc,
char
* argv[])
31
{
32
int
c;
33
char
* path =
nullptr
;
34
35
opterr = 0;
36
37
while
((c = getopt(argc, argv,
"vp:"
)) != -1)
38
{
39
switch
(c)
40
{
41
case
'v'
:
42
gVerbose
=
true
;
43
break
;
44
case
'p'
:
45
path = optarg;
46
break
;
47
}
48
}
49
50
//
51
// This program is spawned by an emu net device running in a simulation. It
52
// wants to create a raw socket as described below. We are going to do the
53
// work here since we're running suid root. Once we create the raw socket,
54
// we have to send it back to the emu net device. We do that over a Unix
55
// (local interprocess) socket. The emu net device created a socket to
56
// listen for our response on, and it is expected to have encoded the address
57
// information as a string and to have passed that string as an argument to
58
// us. We see it here as the "path" string. We can't do anything useful
59
// unless we have that string.
60
//
61
ABORT_IF
(!path,
"path is a required argument"
, 0);
62
LOG
(
"Provided path is \""
<< path <<
"\""
);
63
//
64
// The whole reason for all of the hoops we went through to call out to this
65
// program will pay off here. We created this program to run as suid root
66
// in order to keep the main simulation program from having to be run with
67
// root privileges. We need root privileges to be able to open a raw socket
68
// though. So all of these hoops are to allow us to execute the following
69
// single line of code:
70
//
71
LOG
(
"Creating netmap fd"
);
72
int
sock = open(
"/dev/netmap"
, O_RDWR);
73
ABORT_IF
(sock == -1,
"CreateSocket(): Unable to open netmap fd"
, 1);
74
75
//
76
// Send the socket back to the emu net device so it can go about its business
77
//
78
SendSocket
(path, sock,
EMU_MAGIC
);
79
80
return
0;
81
}
LOG
#define LOG(x)
Log to std::cout.
Definition
bench-scheduler.cc:26
creator-utils.h
ABORT_IF
#define ABORT_IF(cond, msg, printErrno)
Definition
creator-utils.h:40
ns3::SendSocket
void SendSocket(const char *path, int fd, const int magic_number)
Send the file descriptor back to the code that invoked the creation.
Definition
creator-utils.cc:43
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::gVerbose
bool gVerbose
Flag to enable / disable verbose log mode.
Definition
creator-utils.cc:31
EMU_MAGIC
#define EMU_MAGIC
Definition
netmap-device-creator.cc:25
src
fd-net-device
helper
netmap-device-creator.cc
Generated on Fri Nov 8 2024 13:59:00 for ns-3 by
1.11.0