22 #include <arpa/inet.h>
27 #include <net/ethernet.h>
29 #include <netinet/in.h>
33 #include <sys/ioctl.h>
34 #include <sys/socket.h>
54 SendSocket(
const char* path,
int fd,
const int magic_number)
60 LOG(
"Create Unix socket");
61 int sock = socket(PF_UNIX, SOCK_DGRAM, 0);
62 ABORT_IF(sock == -1,
"Unable to open socket", 1);
71 socklen_t clientAddrLen;
72 struct sockaddr_un clientAddr;
74 LOG(
"Decode address " << path);
76 ABORT_IF(
rc ==
false,
"Unable to decode path", 0);
79 int status = connect(sock, (
struct sockaddr*)&clientAddr, clientAddrLen);
80 ABORT_IF(status == -1,
"Unable to connect to emu device", 1);
101 uint32_t magic = magic_number;
102 iov.iov_base = &magic;
103 iov.iov_len =
sizeof(magic);
116 size_t msg_size =
sizeof(int);
117 char control[CMSG_SPACE(msg_size)];
131 msg.msg_name =
nullptr;
135 msg.msg_control = control;
136 msg.msg_controllen =
sizeof(control);
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);
159 msg.msg_controllen = cmsg->cmsg_len;
165 int* fdptr = (
int*)(CMSG_DATA(cmsg));
171 ssize_t len = sendmsg(sock, &msg, 0);
172 ABORT_IF(len == -1,
"Could not send socket back to emu net device", 1);
174 LOG(
"sendmsg complete");
#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.