A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
raw-sock-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 <iomanip>
12#include <iostream>
13#include <net/ethernet.h>
14#include <net/if.h>
15#include <netinet/in.h>
16#include <netpacket/packet.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
27using namespace ns3;
28
29int
30main(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 == nullptr, "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 raw socket");
72 int sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
73 ABORT_IF(sock == -1, "CreateSocket(): Unable to open raw socket", 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}
#define LOG(x)
Log to std::cout.
#define ABORT_IF(cond, msg, printErrno)
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.
bool gVerbose
Flag to enable / disable verbose log mode.
#define EMU_MAGIC