A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
creator-utils.cc
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) University of Washington
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */
18 
19 #include <unistd.h>
20 #include <string>
21 #include <iostream>
22 #include <iomanip>
23 #include <sstream>
24 #include <stdlib.h>
25 #include <errno.h>
26 
27 #include <sys/socket.h>
28 #include <sys/un.h>
29 #include <sys/ioctl.h>
30 #include <net/ethernet.h>
31 #include <net/if.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
34 
35 #include "creator-utils.h"
36 #include "encode-decode.h"
37 
38 namespace ns3 {
39 
40 int gVerbose = 0;
41 
51 void
52 SendSocket (const char *path, int fd, const int magic_number)
53 {
54  //
55  // Open a Unix (local interprocess) socket to call back to the emu net
56  // device.
57  //
58  LOG ("Create Unix socket");
59  int sock = socket (PF_UNIX, SOCK_DGRAM, 0);
60  ABORT_IF (sock == -1, "Unable to open socket", 1);
61 
62  //
63  // We have this string called path, which is really a hex representation
64  // of the endpoint that the net device created. It used a forward encoding
65  // method (BufferToString) to take the sockaddr_un it made and passed
66  // the resulting string to us. So we need to take the inverse method
67  // (StringToBuffer) and build the same sockaddr_un over here.
68  //
69  socklen_t clientAddrLen;
70  struct sockaddr_un clientAddr;
71 
72  LOG ("Decode address " << path);
73  bool rc = ns3::StringToBuffer (path, (uint8_t *)&clientAddr, &clientAddrLen);
74  ABORT_IF (rc == false, "Unable to decode path", 0);
75 
76  LOG ("Connect");
77  int status = connect (sock, (struct sockaddr*)&clientAddr, clientAddrLen);
78  ABORT_IF (status == -1, "Unable to connect to emu device", 1);
79 
80  LOG ("Connected");
81 
82  //
83  // This is arcane enough that a few words are worthwhile to explain what's
84  // going on here.
85  //
86  // The interesting information (the socket FD) is going to go back to the
87  // fd net device as an integer of ancillary data. Ancillary data is bits
88  // that are not a part a socket payload (out-of-band data). We're also
89  // going to send one integer back. It's just initialized to a magic number
90  // we use to make sure that the fd device is talking to the emu socket
91  // creator and not some other creator process.
92  //
93  // The struct iovec below is part of a scatter-gather list. It describes a
94  // buffer. In this case, it describes a buffer (an integer) containing the
95  // data that we're going to send back to the emu net device (that magic
96  // number).
97  //
98  struct iovec iov;
99  uint32_t magic = magic_number;
100  iov.iov_base = &magic;
101  iov.iov_len = sizeof(magic);
102 
103  //
104  // The CMSG macros you'll see below are used to create and access control
105  // messages (which is another name for ancillary data). The ancillary
106  // data is made up of pairs of struct cmsghdr structures and associated
107  // data arrays.
108  //
109  // First, we're going to allocate a buffer on the stack to contain our
110  // data array (that contains the socket). Sometimes you'll see this called
111  // an "ancillary element" but the msghdr uses the control message termimology
112  // so we call it "control."
113  //
114  size_t msg_size = sizeof(int);
115  char control[CMSG_SPACE (msg_size)];
116 
117  //
118  // There is a msghdr that is used to minimize the number of parameters
119  // passed to sendmsg (which we will use to send our ancillary data). This
120  // structure uses terminology corresponding to control messages, so you'll
121  // see msg_control, which is the pointer to the ancillary data and controllen
122  // which is the size of the ancillary data array.
123  //
124  // So, initialize the message header that describes our ancillary/control data
125  // and point it to the control message/ancillary data we just allocated space
126  // for.
127  //
128  struct msghdr msg;
129  msg.msg_name = 0;
130  msg.msg_namelen = 0;
131  msg.msg_iov = &iov;
132  msg.msg_iovlen = 1;
133  msg.msg_control = control;
134  msg.msg_controllen = sizeof (control);
135  msg.msg_flags = 0;
136 
137  //
138  // A cmsghdr contains a length field that is the length of the header and
139  // the data. It has a cmsg_level field corresponding to the originating
140  // protocol. This takes values which are legal levels for getsockopt and
141  // setsockopt (here SOL_SOCKET). We're going to use the SCM_RIGHTS type of
142  // cmsg, that indicates that the ancillary data array contains access rights
143  // that we are sending back to the emu net device.
144  //
145  // We have to put together the first (and only) cmsghdr that will describe
146  // the whole package we're sending.
147  //
148  struct cmsghdr *cmsg;
149  cmsg = CMSG_FIRSTHDR (&msg);
150  cmsg->cmsg_level = SOL_SOCKET;
151  cmsg->cmsg_type = SCM_RIGHTS;
152  cmsg->cmsg_len = CMSG_LEN (msg_size);
153  //
154  // We also have to update the controllen in case other stuff is actually
155  // in there we may not be aware of (due to macros).
156  //
157  msg.msg_controllen = cmsg->cmsg_len;
158 
159  //
160  // Finally, we get a pointer to the start of the ancillary data array and
161  // put our file descriptor in.
162  //
163  int *fdptr = (int*)(CMSG_DATA (cmsg));
164  *fdptr = fd; //
165 
166  //
167  // Actually send the file descriptor back to the emulated net device.
168  //
169  ssize_t len = sendmsg (sock, &msg, 0);
170  ABORT_IF (len == -1, "Could not send socket back to emu net device", 1);
171 
172  LOG ("sendmsg complete");
173 }
174 
175 } // namespace ns3
void SendSocket(const char *path, int fd, const int magic_number)
Send the file descriptor back to the code that invoked the creation.
bool StringToBuffer(std::string s, uint8_t *buffer, uint32_t *len)
Convert string encoded by the inverse function (TapBufferToString) back into a byte buffer...