pp.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #include <errno.h>
  2. #include <netinet/in.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <sys/socket.h>
  7. #include <sys/types.h>
  8. #include <sys/uio.h>
  9. #include <sys/un.h>
  10. #include <unistd.h>
  11. #define CONTROLLEN CMSG_LEN(sizeof(int))
  12. #define UNIX_PATH "hostage"
  13. #define TCP "TCP"
  14. #define UDP "UDP"
  15. int ipc_sock() {
  16. int fd;
  17. struct sockaddr_un addr;
  18. if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
  19. perror("Unable to create local socket");
  20. return -1;
  21. }
  22. memset(&addr, 0, sizeof(addr));
  23. addr.sun_family = AF_UNIX;
  24. strncpy(&addr.sun_path[1], UNIX_PATH, strlen(UNIX_PATH));
  25. if (connect(fd, (struct sockaddr*) &addr,
  26. sizeof(sa_family_t) + strlen(UNIX_PATH) + 1) == -1) {
  27. perror("Unable to connect local socket");
  28. return -1;
  29. }
  30. return fd;
  31. }
  32. int net_sock(int type, int port) {
  33. int fd;
  34. int reuseaddr = 1;
  35. struct sockaddr_in addr;
  36. if ((fd = socket(AF_INET, (type == 1 ? SOCK_STREAM : SOCK_DGRAM), 0)) == -1) {
  37. perror("Unable to create net socket");
  38. return -1;
  39. }
  40. memset(&addr, 0, sizeof(addr));
  41. addr.sin_family = AF_INET;
  42. addr.sin_addr.s_addr = INADDR_ANY;
  43. addr.sin_port = htons(port);
  44. if (type == 1) {
  45. if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, sizeof(reuseaddr))
  46. == -1) {
  47. perror("Unable to set socket options");
  48. return -1;
  49. }
  50. }
  51. if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
  52. perror("Unable to bind net socket");
  53. return -1;
  54. }
  55. if (type == 1) {
  56. if (listen(fd, 5) == -1) {
  57. perror("Unable to listen net socket");
  58. return -1;
  59. }
  60. }
  61. return fd;
  62. }
  63. int send_fd(int fd, int fd_to_send) {
  64. struct iovec iov[1];
  65. struct cmsghdr *cmptr;
  66. struct msghdr msg;
  67. char buf[2] = "FD";
  68. iov[0].iov_base = buf;
  69. iov[0].iov_len = 2;
  70. cmptr = malloc(CONTROLLEN);
  71. cmptr->cmsg_level = SOL_SOCKET;
  72. cmptr->cmsg_type = SCM_RIGHTS;
  73. cmptr->cmsg_len = CONTROLLEN;
  74. msg.msg_iov = iov;
  75. msg.msg_iovlen = 1;
  76. msg.msg_name = NULL;
  77. msg.msg_namelen = 0;
  78. msg.msg_control = cmptr;
  79. msg.msg_controllen = CONTROLLEN;
  80. *(int *) CMSG_DATA(cmptr) = fd_to_send;
  81. if (sendmsg(fd, &msg, 0) == -1) {
  82. perror("sendmsg failed");
  83. }
  84. return 0;
  85. }
  86. int main(int argc, char *argv[]) {
  87. int type;
  88. int port;
  89. int ipc_fd, net_fd;
  90. if (argc < 3) {
  91. exit(EXIT_FAILURE);
  92. }
  93. if (strncmp(argv[1], TCP) == 0) {
  94. type = 1;
  95. } else if (strncmp(argv[1], UDP) == 0) {
  96. type = 0;
  97. } else {
  98. exit(EXIT_FAILURE);
  99. }
  100. if ((port = atoi(argv[2])) < 1 || (port = atoi(argv[2])) > 65535) {
  101. exit(EXIT_FAILURE);
  102. }
  103. if ((ipc_fd = ipc_sock()) == -1) {
  104. close(ipc_fd);
  105. exit(EXIT_FAILURE);
  106. }
  107. printf("ipc_fd: %d\n", ipc_fd);
  108. if ((net_fd = net_sock(type, port)) == -1) {
  109. close(ipc_fd);
  110. close(net_fd);
  111. exit(EXIT_FAILURE);
  112. }
  113. printf("net_fd: %d\n", net_fd);
  114. int status;
  115. status = send_fd(ipc_fd, net_fd);
  116. printf("send_fd: %d\n", status);
  117. close(ipc_fd);
  118. close(net_fd);
  119. if (status == -1) {
  120. return (EXIT_FAILURE);
  121. }
  122. return EXIT_SUCCESS;
  123. }