common.inc.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 <stdio.h>
  28. #include <netdb.h>
  29. #include <stdarg.h>
  30. #include <errno.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <time.h>
  34. #include <unistd.h>
  35. #include <arpa/inet.h>
  36. #include <netinet/in.h>
  37. #include <pthread.h>
  38. #include <fcntl.h>
  39. #define DEBUG_LEVEL 3
  40. #define MSG_ERROR 0 // Errors
  41. #define MSG_INFO 1 // Information which is generally useful to any user
  42. #define MSG_DEBUG 2 // Information which is only useful to someone debugging
  43. #define MSG_DEBUG_EXTRA 3 // If nothing in your world makes sense
  44. #ifdef NETCON_INTERCEPT
  45. static pthread_mutex_t loglock;
  46. void print_addr(struct sockaddr *addr)
  47. {
  48. char *s = NULL;
  49. switch(addr->sa_family) {
  50. case AF_INET: {
  51. struct sockaddr_in *addr_in = (struct sockaddr_in *)addr;
  52. s = malloc(INET_ADDRSTRLEN);
  53. inet_ntop(AF_INET, &(addr_in->sin_addr), s, INET_ADDRSTRLEN);
  54. break;
  55. }
  56. case AF_INET6: {
  57. struct sockaddr_in6 *addr_in6 = (struct sockaddr_in6 *)addr;
  58. s = malloc(INET6_ADDRSTRLEN);
  59. inet_ntop(AF_INET6, &(addr_in6->sin6_addr), s, INET6_ADDRSTRLEN);
  60. break;
  61. }
  62. default:
  63. break;
  64. }
  65. fprintf(stderr, "IP address: %s\n", s);
  66. free(s);
  67. }
  68. #endif
  69. #ifdef NETCON_SERVICE
  70. namespace ZeroTier {
  71. #endif
  72. void dwr(int level, const char *fmt, ... )
  73. {
  74. if(level > DEBUG_LEVEL)
  75. return;
  76. int saveerr;
  77. saveerr = errno;
  78. va_list ap;
  79. va_start(ap, fmt);
  80. #ifdef VERBOSE // So we can cut out some clutter in the strace output while debugging
  81. char timestring[20];
  82. time_t timestamp;
  83. timestamp = time(NULL);
  84. strftime(timestring, sizeof(timestring), "%H:%M:%S", localtime(&timestamp));
  85. pid_t pid = getpid();
  86. fprintf(stderr, "%s [pid=%7d] ", timestring, pid);
  87. #endif
  88. vfprintf(stderr, fmt, ap);
  89. fflush(stderr);
  90. errno = saveerr;
  91. va_end(ap);
  92. }
  93. #ifdef NETCON_SERVICE
  94. }
  95. #endif
  96. ssize_t sock_fd_write(int sock, int fd);
  97. ssize_t sock_fd_read(int sock, void *buf, ssize_t bufsize, int *fd);
  98. ssize_t sock_fd_write(int sock, int fd)
  99. {
  100. ssize_t size;
  101. struct msghdr msg;
  102. struct iovec iov;
  103. char buf = '\0';
  104. int buflen = 1;
  105. union {
  106. struct cmsghdr cmsghdr;
  107. char control[CMSG_SPACE(sizeof (int))];
  108. } cmsgu;
  109. struct cmsghdr *cmsg;
  110. iov.iov_base = &buf;
  111. iov.iov_len = buflen;
  112. msg.msg_name = NULL;
  113. msg.msg_namelen = 0;
  114. msg.msg_iov = &iov;
  115. msg.msg_iovlen = 1;
  116. if (fd != -1) {
  117. msg.msg_control = cmsgu.control;
  118. msg.msg_controllen = sizeof(cmsgu.control);
  119. cmsg = CMSG_FIRSTHDR(&msg);
  120. cmsg->cmsg_len = CMSG_LEN(sizeof (int));
  121. cmsg->cmsg_level = SOL_SOCKET;
  122. cmsg->cmsg_type = SCM_RIGHTS;
  123. *((int *) CMSG_DATA(cmsg)) = fd;
  124. } else {
  125. msg.msg_control = NULL;
  126. msg.msg_controllen = 0;
  127. }
  128. size = sendmsg(sock, &msg, 0);
  129. if (size < 0)
  130. perror ("sendmsg");
  131. return size;
  132. }
  133. ssize_t sock_fd_read(int sock, void *buf, ssize_t bufsize, int *fd)
  134. {
  135. ssize_t size;
  136. if (fd) {
  137. struct msghdr msg;
  138. struct iovec iov;
  139. union {
  140. struct cmsghdr cmsghdr;
  141. char control[CMSG_SPACE(sizeof (int))];
  142. } cmsgu;
  143. struct cmsghdr *cmsg;
  144. iov.iov_base = buf;
  145. iov.iov_len = bufsize;
  146. msg.msg_name = NULL;
  147. msg.msg_namelen = 0;
  148. msg.msg_iov = &iov;
  149. msg.msg_iovlen = 1;
  150. msg.msg_control = cmsgu.control;
  151. msg.msg_controllen = sizeof(cmsgu.control);
  152. size = recvmsg (sock, &msg, 0);
  153. if (size < 0) {
  154. perror ("recvmsg");
  155. exit(1);
  156. }
  157. cmsg = CMSG_FIRSTHDR(&msg);
  158. if (cmsg && cmsg->cmsg_len == CMSG_LEN(sizeof(int))) {
  159. if (cmsg->cmsg_level != SOL_SOCKET) {
  160. fprintf (stderr, "invalid cmsg_level %d\n",cmsg->cmsg_level);
  161. exit(1);
  162. }
  163. if (cmsg->cmsg_type != SCM_RIGHTS) {
  164. fprintf (stderr, "invalid cmsg_type %d\n",cmsg->cmsg_type);
  165. exit(1);
  166. }
  167. *fd = *((int *) CMSG_DATA(cmsg));
  168. } else *fd = -1;
  169. } else {
  170. size = read (sock, buf, bufsize);
  171. if (size < 0) {
  172. perror("read");
  173. exit(1);
  174. }
  175. }
  176. return size;
  177. }