bind.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. // LocalSocket uses the Linux abstract namespace instead of the filesystem.
  13. // In C these addresses are specified by prepending '\0' to the path.
  14. // http://stackoverflow.com/questions/14643571/localsocket-communication-with-unix-domain-in-android-ndk
  15. #define UNIX_PATH "\0hostage"
  16. #define TCP "TCP"
  17. #define UDP "UDP"
  18. // create unix domain local socket for inter process communication
  19. int ipc_sock() {
  20. int fd;
  21. struct sockaddr_un addr;
  22. if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
  23. perror("Unable to create local socket");
  24. return -1;
  25. }
  26. // Also note that you should not pass sizeof(sockaddr_un) to bind or connect because all bytes
  27. // following the '\0' character are interpreted as the abstract socket name. Calculate and pass
  28. // the real size instead
  29. addr.sun_family = AF_UNIX;
  30. int addrsize = strlen(&(UNIX_PATH)[1]) + 1;
  31. memcpy(addr.sun_path, UNIX_PATH, addrsize);
  32. addrsize += sizeof(addr.sun_family); // total size
  33. if (connect(fd, (struct sockaddr*)&addr, addrsize) == -1) {
  34. perror("Unable to connect local socket");
  35. return -1;
  36. }
  37. return fd;
  38. }
  39. int net_sock(int type, int port) {
  40. int fd;
  41. int reuseaddr = 1;
  42. struct sockaddr_in addr;
  43. if ((fd = socket(AF_INET, (type == 1 ? SOCK_STREAM : SOCK_DGRAM), (type == 1 ? IPPROTO_IP : IPPROTO_UDP))) == -1) {
  44. perror("Unable to create net socket");
  45. return -1;
  46. }
  47. memset(&addr, 0, sizeof(addr));
  48. addr.sin_family = AF_INET;
  49. addr.sin_addr.s_addr = INADDR_ANY;
  50. addr.sin_port = htons(port);
  51. if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, sizeof(reuseaddr)) == -1) {
  52. perror("Unable to set socket options");
  53. return -1;
  54. }
  55. if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
  56. perror("Unable to bind net socket");
  57. return -1;
  58. }
  59. if (type == 1) {
  60. if (listen(fd, 5) == -1) {
  61. perror("Unable to listen net socket");
  62. return -1;
  63. }
  64. }
  65. return fd;
  66. }
  67. // send a file descriptor via inter process communication
  68. int send_fd(int fd, int fd_to_send) {
  69. struct iovec iov[1];
  70. struct cmsghdr *cmptr;
  71. struct msghdr msg;
  72. char buf[2] = "FD";
  73. iov[0].iov_base = buf;
  74. iov[0].iov_len = 2;
  75. cmptr = malloc(CONTROLLEN);
  76. cmptr->cmsg_level = SOL_SOCKET;
  77. cmptr->cmsg_type = SCM_RIGHTS;
  78. cmptr->cmsg_len = CONTROLLEN;
  79. msg.msg_iov = iov;
  80. msg.msg_iovlen = 1;
  81. msg.msg_name = NULL;
  82. msg.msg_namelen = 0;
  83. msg.msg_control = cmptr;
  84. msg.msg_controllen = CONTROLLEN;
  85. *(int *) CMSG_DATA(cmptr) = fd_to_send;
  86. if (sendmsg(fd, &msg, 0) == -1) {
  87. perror("sendmsg failed");
  88. }
  89. return 0;
  90. }
  91. int main(int argc, char *argv[]) {
  92. int type;
  93. int port;
  94. int ipc_fd, net_fd;
  95. if (argc < 3) {
  96. printf("usage: %s <protocol> <port>\n", argv[0]);
  97. printf("where protocol is either TCP or UDP and port is between 1 and 65535\n");
  98. exit(EXIT_FAILURE);
  99. }
  100. if (strncmp(argv[1], TCP, 3) == 0) {
  101. type = 1;
  102. } else if (strncmp(argv[1], UDP, 3) == 0) {
  103. type = 0;
  104. } else {
  105. exit(EXIT_FAILURE);
  106. }
  107. port = atoi(argv[2]);
  108. if (!(port >= 0 && port <= 65535)) {
  109. exit(EXIT_FAILURE);
  110. }
  111. if ((ipc_fd = ipc_sock()) == -1) {
  112. close(ipc_fd);
  113. exit(EXIT_FAILURE);
  114. }
  115. printf("ipc_fd: %d\n", ipc_fd);
  116. if ((net_fd = net_sock(type, port)) == -1) {
  117. close(ipc_fd);
  118. close(net_fd);
  119. exit(EXIT_FAILURE);
  120. }
  121. printf("net_fd: %d\n", net_fd);
  122. int status;
  123. status = send_fd(ipc_fd, net_fd);
  124. printf("send_fd: %d\n", status);
  125. close(ipc_fd);
  126. close(net_fd);
  127. if (status == -1) {
  128. return (EXIT_FAILURE);
  129. }
  130. return EXIT_SUCCESS;
  131. }