utils.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * utils.c
  3. * ptunnel is licensed under the BSD license:
  4. *
  5. * Copyright (c) 2004-2011, Daniel Stoedle <daniels@cs.uit.no>,
  6. * Yellow Lemon Software. All rights reserved.
  7. *
  8. * Copyright (c) 2017-2019, Toni Uhlig <matzeton@googlemail.com>
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. *
  16. * - Redistributions in binary form must reproduce the above copyright notice,
  17. * this list of conditions and the following disclaimer in the documentation
  18. * and/or other materials provided with the distribution.
  19. *
  20. * - Neither the name of the Yellow Lemon Software nor the names of its
  21. * contributors may be used to endorse or promote products derived from this
  22. * software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  28. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  32. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  33. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  34. * POSSIBILITY OF SUCH DAMAGE.
  35. *
  36. * Contacting the author:
  37. * You can get in touch with me, Daniel Stødle (that's the Norwegian letter oe,
  38. * in case your text editor didn't realize), here: <daniels@cs.uit.no>
  39. *
  40. * The official ptunnel website is here:
  41. * <http://www.cs.uit.no/~daniels/PingTunnel/>
  42. *
  43. * Note that the source code is best viewed with tabs set to 4 spaces.
  44. */
  45. #include <stdio.h>
  46. #include <stdlib.h>
  47. #include <stdarg.h>
  48. #include <string.h>
  49. #include <time.h>
  50. #include <assert.h>
  51. #ifdef HAVE_ARC4RANDOM
  52. #include <bsd/stdlib.h>
  53. #endif
  54. #ifndef WIN32
  55. #include <syslog.h>
  56. #include <sys/types.h>
  57. #include <sys/socket.h>
  58. #include <netdb.h>
  59. #include <netinet/in.h>
  60. #else
  61. #include <ws2tcpip.h>
  62. #endif
  63. #include <sys/time.h>
  64. #include "utils.h"
  65. #include "options.h"
  66. void pt_log(int level, const char *fmt, ...) {
  67. va_list args;
  68. const char *header[] = { "[err]: ",
  69. "[inf]: ",
  70. "[evt]: ",
  71. "[vbs]: ",
  72. "[dbg]: ",
  73. "[xfr]: " };
  74. #ifndef WIN32
  75. int syslog_levels[] = {LOG_ERR, LOG_NOTICE, LOG_NOTICE, LOG_INFO, LOG_DEBUG, LOG_DEBUG};
  76. #endif /* !WIN32 */
  77. if (level <= opts.log_level) {
  78. va_start(args, fmt);
  79. #ifndef WIN32
  80. if (opts.use_syslog) {
  81. char log[255];
  82. int header_len;
  83. header_len = snprintf(log,sizeof(log),"%s",header[level]);
  84. vsnprintf(log+header_len,sizeof(log)-header_len,fmt,args);
  85. syslog(syslog_levels[level], "%s", log);
  86. }
  87. else
  88. #endif /* !WIN32 */
  89. fprintf(opts.log_file, "%s", header[level]), vfprintf(opts.log_file, fmt, args);
  90. va_end(args);
  91. #ifndef WIN32
  92. if (opts.log_file != stdout && !opts.use_syslog)
  93. #else
  94. if (opts.log_file != stdout)
  95. #endif
  96. fflush(opts.log_file);
  97. }
  98. }
  99. double time_as_double(void) {
  100. double result;
  101. struct timeval tt;
  102. gettimeofday(&tt, 0);
  103. result = (double)tt.tv_sec + ((double)tt.tv_usec / (double)10e5);
  104. return result;
  105. }
  106. int host_to_addr(const char *hostname, uint32_t *result)
  107. {
  108. int ret;
  109. struct addrinfo *addrs = NULL;
  110. struct addrinfo hints;
  111. struct sockaddr_in *addr;
  112. memset(&hints, 0, sizeof(hints));
  113. hints.ai_family = AF_INET;
  114. if ((ret = getaddrinfo(hostname, NULL, &hints, &addrs)) != 0)
  115. return ret;
  116. addr = (struct sockaddr_in *) addrs->ai_addr;
  117. *result = *(uint32_t *) &addr->sin_addr;
  118. freeaddrinfo(addrs);
  119. return 0;
  120. }
  121. #if 0
  122. static const char hextab[] = "0123456789ABCDEF";
  123. void print_hexstr(unsigned char *buf, size_t siz) {
  124. char *out = (char *) calloc(3, siz+1);
  125. unsigned char high, low;
  126. for (size_t i = 0; i < siz; ++i) {
  127. high = (buf[i] & 0xF0) >> 4;
  128. low = buf[i] & 0x0F;
  129. out[i ] = hextab[high];
  130. out[i+1] = hextab[low];
  131. out[i+2] = ' ';
  132. }
  133. printf("%s\n", out);
  134. free(out);
  135. }
  136. #endif
  137. int pt_random(void) {
  138. #ifdef HAVE_ARC4RANDOM
  139. return arc4random();
  140. #else
  141. #ifdef HAVE_RANDOM
  142. #ifndef TIME_UTC
  143. #define TIME_UTC 1
  144. #endif
  145. struct timespec ts;
  146. assert(timespec_get(&ts, TIME_UTC));
  147. srandom(ts.tv_nsec ^ ts.tv_sec);
  148. return random();
  149. #else
  150. srand(time(0));
  151. return rand();
  152. #endif
  153. #endif
  154. }