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