p.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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: p"
  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. int reuse = 1;
  37. struct sockaddr_in addr;
  38. if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
  39. LOGE("Unable to create net socket: %d", errno);
  40. return -1;
  41. }
  42. memset(&addr, 0, sizeof(addr));
  43. addr.sin_family = AF_INET;
  44. addr.sin_addr.s_addr = INADDR_ANY;
  45. addr.sin_port = htons(port);
  46. if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) == -1){
  47. LOGE("Unable to set socketopt: %d", errno);
  48. return -1;
  49. }
  50. if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
  51. LOGE("Unable to bind net socket: %d", errno);
  52. return -1;
  53. }
  54. if (listen(fd, 5) == -1) {
  55. LOGE("Unable to listen net socket: %d", errno);
  56. return -1;
  57. }
  58. return fd;
  59. }
  60. int send_fd(int fd, int fd_to_send) {
  61. struct iovec iov[1];
  62. struct cmsghdr *cmptr;
  63. struct msghdr msg;
  64. char buf[2] = "FD";
  65. iov[0].iov_base = buf;
  66. iov[0].iov_len = 2;
  67. cmptr = malloc(CONTROLLEN);
  68. cmptr->cmsg_level = SOL_SOCKET;
  69. cmptr->cmsg_type = SCM_RIGHTS;
  70. cmptr->cmsg_len = CONTROLLEN;
  71. msg.msg_iov = iov;
  72. msg.msg_iovlen = 1;
  73. msg.msg_name = NULL;
  74. msg.msg_namelen = 0;
  75. msg.msg_control = cmptr;
  76. msg.msg_controllen = CONTROLLEN;
  77. *(int *) CMSG_DATA(cmptr) = fd_to_send;
  78. if (sendmsg(fd, &msg, 0) == -1) {
  79. LOGE("sendmsg failed: %d", errno);
  80. }
  81. return 0;
  82. }
  83. int main(int argc, char *argv[]) {
  84. int port;
  85. int ipc_fd, net_fd;
  86. if (argc < 2) {
  87. exit(EXIT_FAILURE);
  88. }
  89. if ((port = atoi(argv[1])) < 1 || (port = atoi(argv[1])) > 65535) {
  90. exit(EXIT_FAILURE);
  91. }
  92. if ((ipc_fd = ipc_sock()) == -1) {
  93. close(ipc_fd);
  94. exit(EXIT_FAILURE);
  95. }
  96. LOGI("ipc_fd: %d", ipc_fd);
  97. if ((net_fd = net_sock(port)) == -1) {
  98. close(ipc_fd);
  99. close(net_fd);
  100. exit(EXIT_FAILURE);
  101. }
  102. LOGI("net_fd: %d", net_fd);
  103. int status;
  104. status = send_fd(ipc_fd, net_fd);
  105. LOGI("send_fd: %d", status);
  106. close(ipc_fd);
  107. close(net_fd);
  108. if (status == -1) {
  109. return (EXIT_FAILURE);
  110. }
  111. return EXIT_SUCCESS;
  112. }