p.c 2.8 KB

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