common.inc.c 5.1 KB

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