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