bind.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. printf("Hello from the new port binder\n");
  74. if (type == 1) { // TCP
  75. if (listen(fd, 5) == -1) {
  76. perror("Unable to listen net socket");
  77. return -1;
  78. }
  79. }
  80. return fd;
  81. }
  82. // send a file descriptor via inter process communication
  83. int send_fd(int fd, int fd_to_send) {
  84. struct iovec iov[1];
  85. struct cmsghdr *cmptr;
  86. struct msghdr msg;
  87. char buf[] = "FD";
  88. iov[0].iov_base = buf;
  89. iov[0].iov_len = 2;
  90. cmptr = (struct cmsghdr *)malloc(CONTROLLEN);
  91. cmptr->cmsg_level = SOL_SOCKET;
  92. cmptr->cmsg_type = SCM_RIGHTS;
  93. cmptr->cmsg_len = CONTROLLEN;
  94. msg.msg_iov = iov;
  95. msg.msg_iovlen = 1;
  96. msg.msg_name = NULL;
  97. msg.msg_namelen = 0;
  98. msg.msg_control = cmptr;
  99. msg.msg_controllen = CONTROLLEN;
  100. *(int *) CMSG_DATA(cmptr) = fd_to_send;
  101. if (sendmsg(fd, &msg, 0) == -1) {
  102. perror("sendmsg failed");
  103. }
  104. return 0;
  105. }
  106. int main(int argc, char *argv[]) {
  107. int type;
  108. int port;
  109. int ipc_fd, net_fd;
  110. if (argc < 3) {
  111. LOGI("usage: %s <protocol> <port>\n", argv[0]);
  112. LOGI("where protocol is either TCP or UDP and port is between 1 and 65535\n");
  113. exit(EXIT_FAILURE);
  114. }
  115. LOGI("port binder reporting %s %s\n", argv[1], argv[2]);
  116. if (strncmp(argv[1], TCP, 3) == 0) {
  117. type = 1;
  118. } else if (strncmp(argv[1], UDP, 3) == 0) {
  119. type = 0;
  120. } else {
  121. exit(EXIT_FAILURE);
  122. }
  123. port = atoi(argv[2]);
  124. if (!(port >= 0 && port <= 65535)) {
  125. exit(EXIT_FAILURE);
  126. }
  127. if ((net_fd = net_sock(type, port)) == -1) {
  128. close(net_fd);
  129. exit(EXIT_FAILURE);
  130. }
  131. LOGI("net_fd: %d\n", net_fd);
  132. if ((ipc_fd = ipc_sock()) == -1) {
  133. close(net_fd);
  134. close(ipc_fd);
  135. exit(EXIT_FAILURE);
  136. }
  137. LOGI("ipc_fd: %d\n", ipc_fd);
  138. int status;
  139. status = send_fd(ipc_fd, net_fd);
  140. LOGI("send_fd: %d\n", status);
  141. close(ipc_fd);
  142. //close(net_fd);
  143. if (status == -1) {
  144. return (EXIT_FAILURE);
  145. }
  146. return EXIT_SUCCESS;
  147. }