Common.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. #define DEBUG_LEVEL 5
  39. #define MSG_ERROR 0 // Errors
  40. #define MSG_INFO 1 // Information which is generally useful to any user
  41. #define MSG_DEBUG 2 // Information which is only useful to someone debugging
  42. #include "Common.h"
  43. #ifdef NETCON_INTERCEPT
  44. extern pthread_mutex_t loglock;
  45. void print_addr(struct sockaddr *addr)
  46. {
  47. char *s = NULL;
  48. switch(addr->sa_family) {
  49. case AF_INET: {
  50. struct sockaddr_in *addr_in = (struct sockaddr_in *)addr;
  51. s = malloc(INET_ADDRSTRLEN);
  52. inet_ntop(AF_INET, &(addr_in->sin_addr), s, INET_ADDRSTRLEN);
  53. break;
  54. }
  55. case AF_INET6: {
  56. struct sockaddr_in6 *addr_in6 = (struct sockaddr_in6 *)addr;
  57. s = malloc(INET6_ADDRSTRLEN);
  58. inet_ntop(AF_INET6, &(addr_in6->sin6_addr), s, INET6_ADDRSTRLEN);
  59. break;
  60. }
  61. default:
  62. break;
  63. }
  64. fprintf(stderr, "IP address: %s\n", s);
  65. free(s);
  66. }
  67. #endif
  68. #ifdef NETCON_SERVICE
  69. namespace ZeroTier {
  70. #endif
  71. void dwr(int level, char *fmt, ... )
  72. {
  73. #ifdef NETCON_SERVICE
  74. if(level > DEBUG_LEVEL)
  75. return;
  76. #endif
  77. int saveerr;
  78. saveerr = errno;
  79. va_list ap;
  80. va_start(ap, fmt);
  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. #ifdef VERBOSE
  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