utils.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. #ifdef HAVE_CONFIG_H
  46. #include "config.h"
  47. #endif
  48. #include <stdio.h>
  49. #include <stdlib.h>
  50. #include <unistd.h>
  51. #include <fcntl.h>
  52. #include <stdarg.h>
  53. #include <string.h>
  54. #include <time.h>
  55. #ifdef HAVE_BSD_STDLIB_H
  56. #include <bsd/stdlib.h>
  57. #endif
  58. #ifndef WIN32
  59. #include <errno.h>
  60. #include <syslog.h>
  61. #include <sys/types.h>
  62. #include <sys/stat.h>
  63. #include <sys/socket.h>
  64. #include <netdb.h>
  65. #include <netinet/in.h>
  66. #else
  67. #include <ws2tcpip.h>
  68. #endif
  69. #include <sys/time.h>
  70. #include "utils.h"
  71. #include "options.h"
  72. void pt_log(enum log_level level, const char * fmt, ...)
  73. {
  74. va_list args;
  75. const char * header[] = {"[err]: ", "[inf]: ", "[evt]: ", "[vbs]: ", "[dbg]: ", "[xfr]: "};
  76. #ifndef WIN32
  77. int syslog_levels[] = {LOG_ERR, LOG_NOTICE, LOG_NOTICE, LOG_INFO, LOG_DEBUG, LOG_DEBUG};
  78. #endif /* !WIN32 */
  79. if (level <= opts.log_level) {
  80. va_start(args, fmt);
  81. #ifndef WIN32
  82. if (opts.use_syslog) {
  83. char log[255];
  84. int header_len;
  85. header_len = snprintf(log, sizeof(log), "%s", header[level]);
  86. vsnprintf(log + header_len, sizeof(log) - header_len, fmt, args);
  87. syslog(syslog_levels[level], "%s", log);
  88. } else
  89. #endif /* !WIN32 */
  90. fprintf(opts.log_file, "%s", header[level]), vfprintf(opts.log_file, fmt, args);
  91. va_end(args);
  92. #ifndef WIN32
  93. if (opts.log_file != stdout && !opts.use_syslog)
  94. #else
  95. if (opts.log_file != stdout)
  96. #endif
  97. fflush(opts.log_file);
  98. }
  99. }
  100. double time_as_double(void)
  101. {
  102. double result;
  103. struct timeval tt;
  104. gettimeofday(&tt, 0);
  105. result = (double)tt.tv_sec + ((double)tt.tv_usec / (double)10e5);
  106. return result;
  107. }
  108. int host_to_addr(const char * hostname, uint32_t * result)
  109. {
  110. int ret;
  111. struct addrinfo * addrs = NULL;
  112. struct addrinfo hints;
  113. struct sockaddr_in * addr;
  114. memset(&hints, 0, sizeof(hints));
  115. hints.ai_family = AF_INET;
  116. if ((ret = getaddrinfo(hostname, NULL, &hints, &addrs)) != 0)
  117. return ret;
  118. addr = (struct sockaddr_in *)addrs->ai_addr;
  119. *result = *(uint32_t *)&addr->sin_addr;
  120. freeaddrinfo(addrs);
  121. return 0;
  122. }
  123. static const char hextab[] = "0123456789ABCDEF";
  124. void log_sendrecv_hexstr(const char *prefix, void *buf, size_t siz) {
  125. if (opts.log_level != kLog_sendrecv) {
  126. return;
  127. }
  128. const size_t outsiz = siz * 3;
  129. if (outsiz + 1 > BUFSIZ) {
  130. pt_log(kLog_error, "Can not print hex string with size %zu: too big\n", siz);
  131. return;
  132. }
  133. char out[outsiz + 1];
  134. unsigned char high, low;
  135. size_t i, j;
  136. for (i = 0, j = 0; j < siz && i < outsiz; i += 3, ++j) {
  137. high = (((unsigned char *)buf)[j] & 0xF0) >> 4;
  138. low = ((unsigned char *)buf)[j] & 0x0F;
  139. out[i ] = hextab[high];
  140. out[i+1] = hextab[low];
  141. out[i+2] = ' ';
  142. }
  143. out[i] = '\0';
  144. pt_log(kLog_sendrecv, "%s[HEX]: %s\n", prefix, out);
  145. }
  146. int pt_random(void)
  147. {
  148. #if defined(HAVE_ARC4RANDOM) || defined(__COVERITY__)
  149. return arc4random();
  150. #else
  151. #if defined(RNGDEV) && !defined(_WIN32)
  152. static int rng_fd = -1;
  153. ssize_t bytes_read;
  154. int rnd_val;
  155. if (rng_fd < 0) {
  156. rng_fd = open(RNGDEV, O_RDONLY);
  157. if (rng_fd < 0) {
  158. pt_log(kLog_error, "FATAL: Could not open random device '%s': %s\n", RNGDEV, strerror(errno));
  159. exit(EXIT_FAILURE);
  160. }
  161. }
  162. bytes_read = read(rng_fd, &rnd_val, sizeof rnd_val);
  163. if (bytes_read != sizeof rnd_val) {
  164. if (bytes_read < 0)
  165. pt_log(kLog_error, "FATAL: Read from random device failed: %s\n", strerror(errno));
  166. else
  167. pt_log(kLog_error, "FATAL: Read only %zd bytes (wanted %zd bytes)\n", bytes_read, sizeof rnd_val);
  168. exit(EXIT_FAILURE);
  169. }
  170. return rnd_val;
  171. #else
  172. srand(time(0));
  173. return rand();
  174. #endif
  175. #endif
  176. }