28 #include <sys/socket.h>
30 #include <sys/ioctl.h>
31 #include <net/ethernet.h>
33 #include <netinet/in.h>
34 #include <netpacket/packet.h>
35 #include <arpa/inet.h>
37 #include "emu-encode-decode.h"
39 #define EMU_MAGIC 65867
41 static int gVerbose = 0;
46 std::cout << __FUNCTION__ << "(): " << msg << std::endl; \
49 #define ABORT(msg, printErrno) \
50 std::cout << __FILE__ << ": fatal error at line " << __LINE__ << ": " << __FUNCTION__ << "(): " << msg << std::endl; \
53 std::cout << " errno = " << errno << " (" << std::strerror (errno) << ")" << std::endl; \
57 #define ABORT_IF(cond, msg, printErrno) \
60 ABORT (msg, printErrno); \
72 SendSocket (
const char *path,
int fd)
78 LOG (
"Create Unix socket");
79 int sock = socket (PF_UNIX, SOCK_DGRAM, 0);
80 ABORT_IF (sock == -1,
"Unable to open socket", 1);
89 socklen_t clientAddrLen;
90 struct sockaddr_un clientAddr;
92 LOG (
"Decode address " << path);
94 ABORT_IF (rc ==
false,
"Unable to decode path", 0);
97 int status = connect (sock, (
struct sockaddr*)&clientAddr, clientAddrLen);
98 ABORT_IF (status == -1,
"Unable to connect to emu device", 1);
119 uint32_t magic = EMU_MAGIC;
120 iov.iov_base = &magic;
121 iov.iov_len =
sizeof(magic);
134 size_t msg_size =
sizeof(int);
135 char control[CMSG_SPACE (msg_size)];
153 msg.msg_control = control;
154 msg.msg_controllen =
sizeof (control);
168 struct cmsghdr *cmsg;
169 cmsg = CMSG_FIRSTHDR (&msg);
170 cmsg->cmsg_level = SOL_SOCKET;
171 cmsg->cmsg_type = SCM_RIGHTS;
172 cmsg->cmsg_len = CMSG_LEN (msg_size);
177 msg.msg_controllen = cmsg->cmsg_len;
183 int *fdptr = (
int*)(CMSG_DATA (cmsg));
189 ssize_t len = sendmsg (sock, &msg, 0);
190 ABORT_IF (len == -1,
"Could not send socket back to emu net device", 1);
192 LOG (
"sendmsg complete");
196 main (
int argc,
char *argv[])
203 while ((c = getopt (argc, argv,
"vp:")) != -1)
227 ABORT_IF (path == NULL,
"path is a required argument", 0);
228 LOG (
"Provided path is \"" << path <<
"\"");
237 LOG (
"Creating raw socket");
238 int sock = socket (PF_PACKET, SOCK_RAW, htons (ETH_P_ALL));
239 ABORT_IF (sock == -1,
"CreateSocket(): Unable to open raw socket", 1);
244 SendSocket (path, sock);
bool EmuStringToBuffer(std::string s, uint8_t *buffer, uint32_t *len)
Convert string encoded by the inverse function (EmuBufferToString) back into a byte buffer...