Common.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. extern 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