NetconUtilities.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. #ifndef _NETCON_UTILITIES_CPP
  36. #define _NETCON_UTILITIES_CPP
  37. #define DEBUG_LEVEL 3
  38. namespace ZeroTier
  39. {
  40. void dwr(int level, char *fmt, ... )
  41. {
  42. if(level > DEBUG_LEVEL)
  43. return;
  44. va_list ap;
  45. va_start(ap, fmt);
  46. vfprintf(stderr, fmt, ap);
  47. fflush(stderr);
  48. va_end(ap);
  49. }
  50. void dwr(char *fmt, ... )
  51. {
  52. va_list ap;
  53. va_start(ap, fmt);
  54. vfprintf(stderr, fmt, ap);
  55. fflush(stderr);
  56. va_end(ap);
  57. }
  58. void clearscreen()
  59. {
  60. fprintf(stderr, "\033[2J");
  61. }
  62. //void reset_cursor()
  63. void gotoxy(int x,int y) {
  64. fprintf(stderr, "%c[%d;%df",0x1B,y,x);
  65. }
  66. // Gets the process/path name associated with a pid
  67. void get_path_from_pid(char* dest, int pid)
  68. {
  69. char ppath[80];
  70. sprintf(ppath, "/proc/%d/exe", pid);
  71. if (readlink (ppath, dest, 80) != -1){
  72. }
  73. }
  74. // Gets the process/path name associated with a fd
  75. void get_path_from_fd(char* dest, int pid, int fd)
  76. {
  77. char ppfd[80];
  78. sprintf(ppfd, "/proc/%d/fd/%d", pid, fd);
  79. if (readlink (ppfd, dest, 80) != -1){
  80. }
  81. }
  82. // Functions used to pass file descriptors between processes
  83. ssize_t sock_fd_write(int sock, int fd)
  84. {
  85. ssize_t size;
  86. struct msghdr msg;
  87. struct iovec iov;
  88. char buf = '\0';
  89. int buflen = 1;
  90. union
  91. {
  92. struct cmsghdr cmsghdr;
  93. char control[CMSG_SPACE(sizeof (int))];
  94. } cmsgu;
  95. struct cmsghdr *cmsg;
  96. iov.iov_base = &buf;
  97. iov.iov_len = buflen;
  98. msg.msg_name = NULL;
  99. msg.msg_namelen = 0;
  100. msg.msg_iov = &iov;
  101. msg.msg_iovlen = 1;
  102. if (fd != -1) {
  103. msg.msg_control = cmsgu.control;
  104. msg.msg_controllen = sizeof(cmsgu.control);
  105. cmsg = CMSG_FIRSTHDR(&msg);
  106. cmsg->cmsg_len = CMSG_LEN(sizeof (int));
  107. cmsg->cmsg_level = SOL_SOCKET;
  108. cmsg->cmsg_type = SCM_RIGHTS;
  109. *((int *) CMSG_DATA(cmsg)) = fd;
  110. } else {
  111. msg.msg_control = NULL;
  112. msg.msg_controllen = 0;
  113. }
  114. size = sendmsg(sock, &msg, 0);
  115. if (size < 0)
  116. perror ("sendmsg");
  117. return size;
  118. }
  119. ssize_t sock_fd_read(int sock, void *buf, ssize_t bufsize, int *fd)
  120. {
  121. ssize_t size;
  122. if (fd) {
  123. struct msghdr msg;
  124. struct iovec iov;
  125. union
  126. {
  127. struct cmsghdr cmsghdr;
  128. char control[CMSG_SPACE(sizeof (int))];
  129. } cmsgu;
  130. struct cmsghdr *cmsg;
  131. iov.iov_base = buf;
  132. iov.iov_len = bufsize;
  133. msg.msg_name = NULL;
  134. msg.msg_namelen = 0;
  135. msg.msg_iov = &iov;
  136. msg.msg_iovlen = 1;
  137. msg.msg_control = cmsgu.control;
  138. msg.msg_controllen = sizeof(cmsgu.control);
  139. size = recvmsg (sock, &msg, 0);
  140. if (size < 0) {
  141. perror ("recvmsg");
  142. exit(1);
  143. }
  144. cmsg = CMSG_FIRSTHDR(&msg);
  145. if (cmsg && cmsg->cmsg_len == CMSG_LEN(sizeof(int))) {
  146. if (cmsg->cmsg_level != SOL_SOCKET) {
  147. fprintf (stderr, "invalid cmsg_level %d\n",
  148. cmsg->cmsg_level);
  149. exit(1);
  150. }
  151. if (cmsg->cmsg_type != SCM_RIGHTS) {
  152. fprintf (stderr, "invalid cmsg_type %d\n",
  153. cmsg->cmsg_type);
  154. exit(1);
  155. }
  156. *fd = *((int *) CMSG_DATA(cmsg));
  157. } else
  158. *fd = -1;
  159. } else {
  160. size = read (sock, buf, bufsize);
  161. if (size < 0) {
  162. perror("read");
  163. exit(1);
  164. }
  165. }
  166. return size;
  167. }
  168. }
  169. #endif