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