A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
creator-utils.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 "encode-decode.h"
10
11#include <arpa/inet.h>
12#include <cstring>
13#include <errno.h>
14#include <iomanip>
15#include <iostream>
16#include <net/ethernet.h>
17#include <net/if.h>
18#include <netinet/in.h>
19#include <sstream>
20#include <stdlib.h>
21#include <string>
22#include <sys/ioctl.h>
23#include <sys/socket.h>
24#include <sys/un.h>
25#include <unistd.h>
26
27namespace ns3
28{
29
30/// Flag to enable / disable verbose log mode
31bool gVerbose = false;
32
33/**
34 * \brief Send the file descriptor back to the code that invoked the creation.
35 *
36 * \param path The socket address information from the Unix socket we use
37 * to send the created socket back to.
38 * \param fd The file descriptor we're going to send.
39 * \param magic_number A verification number to verify the caller is talking to the
40 * right process.
41 */
42void
43SendSocket(const char* path, int fd, const int magic_number)
44{
45 //
46 // Open a Unix (local interprocess) socket to call back to the emu net
47 // device.
48 //
49 LOG("Create Unix socket");
50 int sock = socket(PF_UNIX, SOCK_DGRAM, 0);
51 ABORT_IF(sock == -1, "Unable to open socket", 1);
52
53 //
54 // We have this string called path, which is really a hex representation
55 // of the endpoint that the net device created. It used a forward encoding
56 // method (BufferToString) to take the sockaddr_un it made and passed
57 // the resulting string to us. So we need to take the inverse method
58 // (StringToBuffer) and build the same sockaddr_un over here.
59 //
60 socklen_t clientAddrLen;
61 struct sockaddr_un clientAddr;
62
63 LOG("Decode address " << path);
64 bool rc = ns3::StringToBuffer(path, (uint8_t*)&clientAddr, &clientAddrLen);
65 ABORT_IF(rc == false, "Unable to decode path", 0);
66
67 LOG("Connect");
68 int status = connect(sock, (struct sockaddr*)&clientAddr, clientAddrLen);
69 ABORT_IF(status == -1, "Unable to connect to emu device", 1);
70
71 LOG("Connected");
72
73 //
74 // This is arcane enough that a few words are worthwhile to explain what's
75 // going on here.
76 //
77 // The interesting information (the socket FD) is going to go back to the
78 // fd net device as an integer of ancillary data. Ancillary data is bits
79 // that are not a part a socket payload (out-of-band data). We're also
80 // going to send one integer back. It's just initialized to a magic number
81 // we use to make sure that the fd device is talking to the emu socket
82 // creator and not some other creator process.
83 //
84 // The struct iovec below is part of a scatter-gather list. It describes a
85 // buffer. In this case, it describes a buffer (an integer) containing the
86 // data that we're going to send back to the emu net device (that magic
87 // number).
88 //
89 struct iovec iov;
90 uint32_t magic = magic_number;
91 iov.iov_base = &magic;
92 iov.iov_len = sizeof(magic);
93
94 //
95 // The CMSG macros you'll see below are used to create and access control
96 // messages (which is another name for ancillary data). The ancillary
97 // data is made up of pairs of struct cmsghdr structures and associated
98 // data arrays.
99 //
100 // First, we're going to allocate a buffer on the stack to contain our
101 // data array (that contains the socket). Sometimes you'll see this called
102 // an "ancillary element" but the msghdr uses the control message termimology
103 // so we call it "control."
104 //
105 constexpr size_t msg_size = sizeof(int);
106 char control[CMSG_SPACE(msg_size)];
107
108 //
109 // There is a msghdr that is used to minimize the number of parameters
110 // passed to sendmsg (which we will use to send our ancillary data). This
111 // structure uses terminology corresponding to control messages, so you'll
112 // see msg_control, which is the pointer to the ancillary data and controllen
113 // which is the size of the ancillary data array.
114 //
115 // So, initialize the message header that describes our ancillary/control data
116 // and point it to the control message/ancillary data we just allocated space
117 // for.
118 //
119 struct msghdr msg;
120 msg.msg_name = nullptr;
121 msg.msg_namelen = 0;
122 msg.msg_iov = &iov;
123 msg.msg_iovlen = 1;
124 msg.msg_control = control;
125 msg.msg_controllen = sizeof(control);
126 msg.msg_flags = 0;
127
128 //
129 // A cmsghdr contains a length field that is the length of the header and
130 // the data. It has a cmsg_level field corresponding to the originating
131 // protocol. This takes values which are legal levels for getsockopt and
132 // setsockopt (here SOL_SOCKET). We're going to use the SCM_RIGHTS type of
133 // cmsg, that indicates that the ancillary data array contains access rights
134 // that we are sending back to the emu net device.
135 //
136 // We have to put together the first (and only) cmsghdr that will describe
137 // the whole package we're sending.
138 //
139 struct cmsghdr* cmsg;
140 cmsg = CMSG_FIRSTHDR(&msg);
141 cmsg->cmsg_level = SOL_SOCKET;
142 cmsg->cmsg_type = SCM_RIGHTS;
143 cmsg->cmsg_len = CMSG_LEN(msg_size);
144 //
145 // We also have to update the controllen in case other stuff is actually
146 // in there we may not be aware of (due to macros).
147 //
148 msg.msg_controllen = cmsg->cmsg_len;
149
150 //
151 // Finally, we get a pointer to the start of the ancillary data array and
152 // put our file descriptor in.
153 //
154 int* fdptr = (int*)(CMSG_DATA(cmsg));
155 *fdptr = fd; //
156
157 //
158 // Actually send the file descriptor back to the emulated net device.
159 //
160 ssize_t len = sendmsg(sock, &msg, 0);
161 ABORT_IF(len == -1, "Could not send socket back to emu net device", 1);
162
163 LOG("sendmsg complete");
164}
165
166} // namespace ns3
#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.
bool StringToBuffer(std::string s, uint8_t *buffer, uint32_t *len)
Convert string encoded by the inverse function (TapBufferToString) back into a byte buffer.