NetconUtilities.cpp 4.4 KB

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