bind.c 2.9 KB

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