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