pp.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. int ipc_sock() {
  14. int fd;
  15. struct sockaddr_un addr;
  16. if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
  17. perror("Unable to create local socket");
  18. return -1;
  19. }
  20. memset(&addr, 0, sizeof(addr));
  21. addr.sun_family = AF_UNIX;
  22. strncpy(&addr.sun_path[1], UNIX_PATH, strlen(UNIX_PATH));
  23. if (connect(fd, (struct sockaddr*) &addr,
  24. sizeof(sa_family_t) + strlen(UNIX_PATH) + 1) == -1) {
  25. perror("Unable to connect local socket");
  26. return -1;
  27. }
  28. return fd;
  29. }
  30. int net_sock(int port) {
  31. int fd;
  32. int reuse = 1;
  33. struct sockaddr_in addr;
  34. if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
  35. perror("Unable to create net socket");
  36. return -1;
  37. }
  38. memset(&addr, 0, sizeof(addr));
  39. addr.sin_family = AF_INET;
  40. addr.sin_addr.s_addr = INADDR_ANY;
  41. addr.sin_port = htons(port);
  42. if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) == -1) {
  43. perror("Unable to set socket options");
  44. return -1;
  45. }
  46. if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
  47. perror("Unable to bind net socket");
  48. return -1;
  49. }
  50. if (listen(fd, 5) == -1) {
  51. perror("Unable to listen net socket");
  52. return -1;
  53. }
  54. return fd;
  55. }
  56. int send_fd(int fd, int fd_to_send) {
  57. struct iovec iov[1];
  58. struct cmsghdr *cmptr;
  59. struct msghdr msg;
  60. char buf[2] = "FD";
  61. iov[0].iov_base = buf;
  62. iov[0].iov_len = 2;
  63. cmptr = malloc(CONTROLLEN);
  64. cmptr->cmsg_level = SOL_SOCKET;
  65. cmptr->cmsg_type = SCM_RIGHTS;
  66. cmptr->cmsg_len = CONTROLLEN;
  67. msg.msg_iov = iov;
  68. msg.msg_iovlen = 1;
  69. msg.msg_name = NULL;
  70. msg.msg_namelen = 0;
  71. msg.msg_control = cmptr;
  72. msg.msg_controllen = CONTROLLEN;
  73. *(int *) CMSG_DATA(cmptr) = fd_to_send;
  74. if (sendmsg(fd, &msg, 0) == -1) {
  75. perror("sendmsg failed");
  76. }
  77. return 0;
  78. }
  79. int main(int argc, char *argv[]) {
  80. int port;
  81. int ipc_fd, net_fd;
  82. if (argc < 2) {
  83. exit(EXIT_FAILURE);
  84. }
  85. if ((port = atoi(argv[1])) < 1 || (port = atoi(argv[1])) > 65535) {
  86. exit(EXIT_FAILURE);
  87. }
  88. if ((ipc_fd = ipc_sock()) == -1) {
  89. close(ipc_fd);
  90. exit(EXIT_FAILURE);
  91. }
  92. printf("ipc_fd: %d\n", ipc_fd);
  93. if ((net_fd = net_sock(port)) == -1) {
  94. close(ipc_fd);
  95. close(net_fd);
  96. exit(EXIT_FAILURE);
  97. }
  98. printf("net_fd: %d\n", net_fd);
  99. int status;
  100. status = send_fd(ipc_fd, net_fd);
  101. printf("send_fd: %d\n", status);
  102. close(ipc_fd);
  103. close(net_fd);
  104. if (status == -1) {
  105. return (EXIT_FAILURE);
  106. }
  107. return EXIT_SUCCESS;
  108. }