p.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #include <stdlib.h>
  2. #include <unistd.h>
  3. #include <string.h>
  4. #include <sys/socket.h>
  5. #include <sys/types.h>
  6. #include <sys/uio.h>
  7. #include <sys/un.h>
  8. #include <netinet/in.h>
  9. #include <errno.h>
  10. #include <android/log.h>
  11. #define LOG_TAG "hostage: bindport"
  12. #define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
  13. #define LOGW(...) __android_log_print(ANDROID_LOG_WARN,LOG_TAG,__VA_ARGS__)
  14. #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
  15. #define CONTROLLEN CMSG_LEN(sizeof(int))
  16. char *socket_path = "hostage";
  17. int ipc_sock() {
  18. int fd;
  19. struct sockaddr_un addr;
  20. if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
  21. LOGE("Unable to create local socket: %d", errno);
  22. return -1;
  23. }
  24. memset(&addr, 0, sizeof(addr));
  25. addr.sun_family = AF_UNIX;
  26. strncpy(&addr.sun_path[1], socket_path, strlen(socket_path));
  27. if (connect(fd, (struct sockaddr*) &addr,
  28. sizeof(sa_family_t) + strlen(socket_path) + 1) == -1) {
  29. LOGE("Unable to connect local socket: %d", errno);
  30. return -1;
  31. }
  32. return fd;
  33. }
  34. int net_sock(int port) {
  35. int fd;
  36. struct sockaddr_in addr;
  37. if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
  38. LOGE("Unable to create net socket: %d", errno);
  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 (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
  46. LOGE("Unable to bind net socket: %d", errno);
  47. return -1;
  48. }
  49. if (listen(fd, 5) == -1) {
  50. LOGE("Unable to listen net socket: %d", errno);
  51. return -1;
  52. }
  53. return fd;
  54. }
  55. int send_fd(int fd, int fd_to_send) {
  56. struct iovec iov[1];
  57. struct cmsghdr *cmptr;
  58. struct msghdr msg;
  59. char buf[2] = "FD";
  60. iov[0].iov_base = buf;
  61. iov[0].iov_len = 2;
  62. cmptr = malloc(CONTROLLEN);
  63. cmptr->cmsg_level = SOL_SOCKET;
  64. cmptr->cmsg_type = SCM_RIGHTS;
  65. cmptr->cmsg_len = CONTROLLEN;
  66. msg.msg_iov = iov;
  67. msg.msg_iovlen = 1;
  68. msg.msg_name = NULL;
  69. msg.msg_namelen = 0;
  70. msg.msg_control = cmptr;
  71. msg.msg_controllen = CONTROLLEN;
  72. *(int *) CMSG_DATA(cmptr) = fd_to_send;
  73. if (sendmsg(fd, &msg, 0) == -1) {
  74. LOGE("sendmsg failed: %d", errno);
  75. }
  76. return 0;
  77. }
  78. int main(int argc, char *argv[]) {
  79. int port;
  80. int ipc_fd, net_fd;
  81. if (argc < 2) {
  82. exit(EXIT_FAILURE);
  83. }
  84. if ((port = atoi(argv[1])) < 1 || (port = atoi(argv[1])) > 65535) {
  85. exit(EXIT_FAILURE);
  86. }
  87. if ((ipc_fd = ipc_sock()) == -1) {
  88. close(ipc_fd);
  89. exit(EXIT_FAILURE);
  90. }
  91. LOGI("ipc_fd: %d", ipc_fd);
  92. if ((net_fd = net_sock(port)) == -1) {
  93. close(ipc_fd);
  94. close(net_fd);
  95. exit(EXIT_FAILURE);
  96. }
  97. LOGI("net_fd: %d", net_fd);
  98. int status;
  99. status = send_fd(ipc_fd, net_fd);
  100. LOGI("send_fd: %d", status);
  101. close(ipc_fd);
  102. close(net_fd);
  103. if (status == -1) {
  104. return (EXIT_FAILURE);
  105. }
  106. return EXIT_SUCCESS;
  107. }