bind.c 4.1 KB

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