bind.c 2.9 KB

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