Guest User

Untitled

a guest
Feb 23rd, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. #include <errno.h>
  2. #include <string.h>
  3. #include <iostream>
  4. #include <sys/types.h>
  5. #include <sys/socket.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <fcntl.h>
  9. #include <sys/syscall.h>
  10. #include <stdio.h>
  11. #include <unistd.h>
  12.  
  13. #define LOGD(...) do { printf(__VA_ARGS__); printf("\n"); } while(0)
  14. #define LOGE(...) do { printf(__VA_ARGS__); printf("\n"); } while(0)
  15. #define LOGW(...) do { printf(__VA_ARGS__); printf("\n"); } while(0)
  16.  
  17. using namespace std;
  18.  
  19. /**
  20. * Sends given file descriptior via given socket
  21. *
  22. * @param socket to be used for fd sending
  23. * @param fd to be sent
  24. * @return sendmsg result
  25. *
  26. * @note socket should be (PF_UNIX, SOCK_DGRAM)
  27. */
  28. int sendfd(int socket, int fd) {
  29. char dummy = '$';
  30. struct msghdr msg;
  31. struct iovec iov;
  32.  
  33. char cmsgbuf[CMSG_SPACE(sizeof(int))];
  34.  
  35. iov.iov_base = &dummy;
  36. iov.iov_len = sizeof(dummy);
  37.  
  38. msg.msg_name = NULL;
  39. msg.msg_namelen = 0;
  40. msg.msg_iov = &iov;
  41. msg.msg_iovlen = 1;
  42. msg.msg_flags = 0;
  43. msg.msg_control = cmsgbuf;
  44. msg.msg_controllen = CMSG_LEN(sizeof(int));
  45.  
  46. struct cmsghdr* cmsg = CMSG_FIRSTHDR(&msg);
  47. cmsg->cmsg_level = SOL_SOCKET;
  48. cmsg->cmsg_type = SCM_RIGHTS;
  49. cmsg->cmsg_len = CMSG_LEN(sizeof(int));
  50.  
  51. *(int*) CMSG_DATA(cmsg) = fd;
  52.  
  53. int ret = sendmsg(socket, &msg, 0);
  54.  
  55. if (ret == -1) {
  56. LOGE("sendmsg failed with %s", strerror(errno));
  57. }
  58.  
  59. return ret;
  60. }
  61.  
  62. /**
  63. * Receives file descriptor using given socket
  64. *
  65. * @param socket to be used for fd recepion
  66. * @return received file descriptor; -1 if failed
  67. *
  68. * @note socket should be (PF_UNIX, SOCK_DGRAM)
  69. */
  70. int recvfd(int socket) {
  71. int len;
  72. int fd;
  73. char buf[1];
  74. struct iovec iov;
  75. struct msghdr msg;
  76. struct cmsghdr *cmsg;
  77. char cms[CMSG_SPACE(sizeof(int))];
  78.  
  79. iov.iov_base = buf;
  80. iov.iov_len = sizeof(buf);
  81.  
  82. msg.msg_name = 0;
  83. msg.msg_namelen = 0;
  84. msg.msg_iov = &iov;
  85. msg.msg_iovlen = 1;
  86. msg.msg_flags = 0;
  87. msg.msg_control = (caddr_t) cms;
  88. msg.msg_controllen = sizeof cms;
  89.  
  90. len = recvmsg(socket, &msg, 0);
  91.  
  92. if (len < 0) {
  93. LOGE("recvmsg failed with %s", strerror(errno));
  94. return -1;
  95. }
  96.  
  97. if (len == 0) {
  98. LOGE("recvmsg failed no data");
  99. return -1;
  100. }
  101.  
  102. cmsg = CMSG_FIRSTHDR(&msg);
  103. memmove(&fd, CMSG_DATA(cmsg), sizeof(int));
  104. return fd;
  105. }
  106.  
  107. #define TXTSRV "server\n"
  108. #define TXTCLI "client\n"
  109.  
  110. void main_server(int socket) {
  111. int fd;
  112. mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
  113. char filename[] = "/tmp/file";
  114. fd = open(filename, O_WRONLY | O_CREAT | O_APPEND, mode);
  115.  
  116. write(fd, TXTSRV, strlen(TXTSRV));
  117. sendfd(socket, fd);
  118. close(fd);
  119. }
  120.  
  121. void main_client(int socket) {
  122. int fd = recvfd(socket);
  123. write(fd, TXTCLI, strlen(TXTCLI));
  124. close(fd);
  125. }
  126.  
  127. int main() {
  128. int pid;
  129. int pair[2];
  130.  
  131. cout << "started..." << endl;
  132.  
  133. if (socketpair(PF_UNIX, SOCK_DGRAM, 0, pair) < 0) {
  134. cout << "socketpair failed" << endl;
  135. return 1;
  136. }
  137.  
  138. if ((pid = fork()) < 0) {
  139. cout << "fork failed" << endl;
  140. return 1;
  141. }
  142.  
  143. if (pid != 0) {
  144. cout << "i am a parent" << endl;
  145. close(pair[0]);
  146. main_server(pair[1]);
  147. } else {
  148. cout << "i am a child" << endl;
  149. close(pair[1]);
  150. main_client(pair[0]);
  151. }
  152.  
  153. return 0;
  154. }
Add Comment
Please, Sign In to add comment