common.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 "common.h"
  39. void dwr(const char *fmt, ...);
  40. /* defined in intercept and service */
  41. //extern FILE* logfile;
  42. //extern char* logfilename;
  43. //extern flog = -1;
  44. extern pthread_mutex_t loglock;
  45. #ifdef NETCON_SERVICE
  46. void dwr(int, const char *fmt, ...);
  47. void dwr(const char *fmt, ...)
  48. {
  49. int saveerr;
  50. va_list ap;
  51. va_start(ap, fmt);
  52. saveerr = errno;
  53. dwr(-1, fmt, ap);
  54. errno = saveerr;
  55. va_end(ap);
  56. }
  57. void dwr(int pid, const char *fmt, ...)
  58. #endif
  59. #ifdef NETCON_INTERCEPT
  60. void dwr(const char *fmt, ...)
  61. #endif
  62. {
  63. va_list ap;
  64. int saveerr;
  65. char timestring[20];
  66. time_t timestamp;
  67. timestamp = time(NULL);
  68. strftime(timestring, sizeof(timestring), "%H:%M:%S", localtime(&timestamp));
  69. //if(logfile)
  70. // fprintf(logfile, "%s ", timestring);
  71. fprintf(stderr, "%s ", timestring);
  72. #ifdef NETCON_SERVICE
  73. if(ns != NULL)
  74. {
  75. size_t num_intercepts = ns->intercepts.size();
  76. size_t num_connections = ns->connections.size();
  77. //if(logfile)
  78. // fprintf(logfile, "[i/c/tid=%3lu|%3lu|%7d]", num_intercepts, num_connections, pid);
  79. fprintf(stderr, "[i/c/tid=%3lu|%3lu|%7d]", num_intercepts, num_connections, pid);
  80. }
  81. else {
  82. //if(logfile)
  83. // fprintf(logfile, "[i/c/tid=%3d|%3d|%7d]", 0, 0, -1);
  84. fprintf(stderr, "[i/c/tid=%3d|%3d|%7d]", 0, 0, -1);
  85. }
  86. #endif
  87. #ifdef NETCON_INTERCEPT
  88. //pthread_mutex_lock(&loglock);
  89. int pid = getpid();
  90. //if(logfile)
  91. // fprintf(logfile, "[tid=%7d]", pid);
  92. fprintf(stderr, "[tid=%7d]", pid);
  93. //pthread_mutex_unlock(&loglock);
  94. #endif
  95. //if(logfile)
  96. // fputs(" ", logfile);
  97. fputs(" ", stderr);
  98. /* logfile */
  99. va_start(ap, fmt);
  100. saveerr = errno;
  101. //if(logfile){
  102. // vfprintf(logfile, fmt, ap);
  103. // fflush(logfile);
  104. //}
  105. va_end(ap);
  106. /* console */
  107. va_start(ap, fmt);
  108. vfprintf(stderr, fmt, ap);
  109. fflush(stderr);
  110. errno = saveerr;
  111. va_end(ap);
  112. }