NetconUtilities.cpp 4.4 KB

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