NetconUtilities.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2015 ZeroTier, Inc.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #include <stdlib.h>
  28. #include <unistd.h>
  29. #include <stdio.h>
  30. #include <stdarg.h>
  31. #include <sys/socket.h>
  32. #include "lwip/ip.h"
  33. #include "lwip/ip_addr.h"
  34. #include "lwip/ip_frag.h"
  35. #include "Intercept.h"
  36. #ifndef _NETCON_UTILITIES_CPP
  37. #define _NETCON_UTILITIES_CPP
  38. #define DEBUG_LEVEL 5
  39. namespace ZeroTier
  40. {
  41. void dwr(int level, char *fmt, ... )
  42. {
  43. if(level > DEBUG_LEVEL)
  44. return;
  45. va_list ap;
  46. va_start(ap, fmt);
  47. vfprintf(stderr, fmt, ap);
  48. fflush(stderr);
  49. va_end(ap);
  50. }
  51. void dwr(char *fmt, ... )
  52. {
  53. va_list ap;
  54. va_start(ap, fmt);
  55. vfprintf(stderr, fmt, ap);
  56. fflush(stderr);
  57. va_end(ap);
  58. }
  59. void unload_rpc(void *data, pid_t &pid, pid_t &tid, int &rpc_count, char (timestamp[20]), char &cmd, void* &payload)
  60. {
  61. unsigned char *buf = (unsigned char*)data;
  62. /*
  63. dwr(" - IDX_PID = %d\n", IDX_PID);
  64. dwr(" - IDX_TID = %d\n", IDX_TID);
  65. dwr(" - IDX_COUNT = %d\n", IDX_COUNT);
  66. dwr(" - IDX_TIME = %d\n", IDX_TIME);
  67. dwr(" - IDX_PAYLOAD = %d\n", IDX_PAYLOAD);
  68. */
  69. memcpy(&pid, &buf[IDX_PID], sizeof(pid_t));
  70. memcpy(&tid, &buf[IDX_TID], sizeof(pid_t));
  71. memcpy(&rpc_count, &buf[IDX_COUNT], sizeof(int));
  72. memcpy(timestamp, &buf[IDX_TIME], 20);
  73. memcpy(&cmd, &buf[IDX_PAYLOAD], sizeof(char));
  74. payload = buf+IDX_PAYLOAD+1;
  75. //dwr("RX: (pid=%d, tid=%d, rpc_count=%d, timestamp=%s, cmd=%d\n", pid, tid, rpc_count, timestamp, cmd);
  76. }
  77. void clearscreen(){
  78. fprintf(stderr, "\033[2J");
  79. }
  80. //void reset_cursor()
  81. void gotoxy(int x,int y) {
  82. fprintf(stderr, "%c[%d;%df",0x1B,y,x);
  83. }
  84. // Gets the process/path name associated with a pid
  85. void get_path_from_pid(char* dest, int pid)
  86. {
  87. char ppath[80];
  88. sprintf(ppath, "/proc/%d/exe", pid);
  89. if (readlink (ppath, dest, 80) != -1){
  90. }
  91. }
  92. // Gets the process/path name associated with a fd
  93. void get_path_from_fd(char* dest, int pid, int fd)
  94. {
  95. char ppfd[80];
  96. sprintf(ppfd, "/proc/%d/fd/%d", pid, fd);
  97. if (readlink (ppfd, dest, 80) != -1){
  98. }
  99. }
  100. // Functions used to pass file descriptors between processes
  101. ssize_t sock_fd_write(int sock, int fd)
  102. {
  103. ssize_t size;
  104. struct msghdr msg;
  105. struct iovec iov;
  106. char buf = '\0';
  107. int buflen = 1;
  108. union
  109. {
  110. struct cmsghdr cmsghdr;
  111. char control[CMSG_SPACE(sizeof (int))];
  112. } cmsgu;
  113. struct cmsghdr *cmsg;
  114. iov.iov_base = &buf;
  115. iov.iov_len = buflen;
  116. msg.msg_name = NULL;
  117. msg.msg_namelen = 0;
  118. msg.msg_iov = &iov;
  119. msg.msg_iovlen = 1;
  120. if (fd != -1) {
  121. msg.msg_control = cmsgu.control;
  122. msg.msg_controllen = sizeof(cmsgu.control);
  123. cmsg = CMSG_FIRSTHDR(&msg);
  124. cmsg->cmsg_len = CMSG_LEN(sizeof (int));
  125. cmsg->cmsg_level = SOL_SOCKET;
  126. cmsg->cmsg_type = SCM_RIGHTS;
  127. *((int *) CMSG_DATA(cmsg)) = fd;
  128. } else {
  129. msg.msg_control = NULL;
  130. msg.msg_controllen = 0;
  131. }
  132. size = sendmsg(sock, &msg, 0);
  133. if (size < 0)
  134. perror ("sendmsg");
  135. return size;
  136. }
  137. ssize_t sock_fd_read(int sock, void *buf, ssize_t bufsize, int *fd)
  138. {
  139. ssize_t size;
  140. if (fd) {
  141. struct msghdr msg;
  142. struct iovec iov;
  143. union
  144. {
  145. struct cmsghdr cmsghdr;
  146. char control[CMSG_SPACE(sizeof (int))];
  147. } cmsgu;
  148. struct cmsghdr *cmsg;
  149. iov.iov_base = buf;
  150. iov.iov_len = bufsize;
  151. msg.msg_name = NULL;
  152. msg.msg_namelen = 0;
  153. msg.msg_iov = &iov;
  154. msg.msg_iovlen = 1;
  155. msg.msg_control = cmsgu.control;
  156. msg.msg_controllen = sizeof(cmsgu.control);
  157. size = recvmsg (sock, &msg, 0);
  158. if (size < 0) {
  159. perror ("recvmsg");
  160. exit(1);
  161. }
  162. cmsg = CMSG_FIRSTHDR(&msg);
  163. if (cmsg && cmsg->cmsg_len == CMSG_LEN(sizeof(int))) {
  164. if (cmsg->cmsg_level != SOL_SOCKET) {
  165. fprintf (stderr, "invalid cmsg_level %d\n",
  166. cmsg->cmsg_level);
  167. exit(1);
  168. }
  169. if (cmsg->cmsg_type != SCM_RIGHTS) {
  170. fprintf (stderr, "invalid cmsg_type %d\n",
  171. cmsg->cmsg_type);
  172. exit(1);
  173. }
  174. *fd = *((int *) CMSG_DATA(cmsg));
  175. } else
  176. *fd = -1;
  177. } else {
  178. size = read (sock, buf, bufsize);
  179. if (size < 0) {
  180. perror("read");
  181. exit(1);
  182. }
  183. }
  184. return size;
  185. }
  186. }
  187. #endif