bind.c 3.9 KB

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