common.inc.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. #include <sys/syscall.h>
  40. #ifndef _COMMON_H
  41. #define _COMMON_H 1
  42. #define DEBUG_LEVEL 0
  43. #define MSG_TRANSFER 1 // RX/TX specific statements
  44. #define MSG_ERROR 2 // Errors
  45. #define MSG_INFO 3 // Information which is generally useful to any user
  46. #define MSG_DEBUG 4 // Information which is only useful to someone debugging
  47. #define MSG_DEBUG_EXTRA 5 // If nothing in your world makes sense
  48. #ifdef NETCON_INTERCEPT
  49. void print_addr(struct sockaddr *addr)
  50. {
  51. char *s = NULL;
  52. switch(addr->sa_family) {
  53. case AF_INET: {
  54. struct sockaddr_in *addr_in = (struct sockaddr_in *)addr;
  55. s = malloc(INET_ADDRSTRLEN);
  56. inet_ntop(AF_INET, &(addr_in->sin_addr), s, INET_ADDRSTRLEN);
  57. break;
  58. }
  59. case AF_INET6: {
  60. struct sockaddr_in6 *addr_in6 = (struct sockaddr_in6 *)addr;
  61. s = malloc(INET6_ADDRSTRLEN);
  62. inet_ntop(AF_INET6, &(addr_in6->sin6_addr), s, INET6_ADDRSTRLEN);
  63. break;
  64. }
  65. default:
  66. break;
  67. }
  68. fprintf(stderr, "IP address: %s\n", s);
  69. free(s);
  70. }
  71. #endif
  72. #ifdef NETCON_SERVICE
  73. namespace ZeroTier {
  74. #endif
  75. void dwr(int level, const char *fmt, ... )
  76. {
  77. if(level > DEBUG_LEVEL)
  78. return;
  79. int saveerr;
  80. saveerr = errno;
  81. va_list ap;
  82. va_start(ap, fmt);
  83. #ifdef VERBOSE // So we can cut out some clutter in the strace output while debugging
  84. char timestring[20];
  85. time_t timestamp;
  86. timestamp = time(NULL);
  87. strftime(timestring, sizeof(timestring), "%H:%M:%S", localtime(&timestamp));
  88. pid_t tid = syscall(SYS_gettid);
  89. fprintf(stderr, "%s [tid=%7d] ", timestring, tid);
  90. #endif
  91. vfprintf(stderr, fmt, ap);
  92. fflush(stderr);
  93. errno = saveerr;
  94. va_end(ap);
  95. }
  96. #ifdef NETCON_SERVICE
  97. }
  98. #endif
  99. #endif