You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
5.7 KiB

  1. From 6015b3a6f26e04dd5d78cd6c1320886fc9035612 Mon Sep 17 00:00:00 2001
  2. From: Leonid Evdokimov <leon@darkk.net.ru>
  3. Date: Tue, 10 Apr 2012 01:37:34 +0400
  4. Subject: [PATCH 02/12] inet_ntop -> red_inet_ntop
  5. ---
  6. redsocks.c | 13 ++++---------
  7. redudp.c | 19 +++++++++++--------
  8. utils.c | 37 +++++++++++++++++++++++++++++++++----
  9. utils.h | 7 +++++++
  10. 4 files changed, 55 insertions(+), 21 deletions(-)
  11. diff --git a/redsocks.c b/redsocks.c
  12. index d085e10..ba5eab2 100644
  13. --- a/redsocks.c
  14. +++ b/redsocks.c
  15. @@ -207,22 +207,17 @@ void redsocks_log_write_plain(
  16. int saved_errno = errno;
  17. struct evbuffer *fmt = evbuffer_new();
  18. va_list ap;
  19. - char clientaddr_str[INET6_ADDRSTRLEN], destaddr_str[INET6_ADDRSTRLEN];
  20. + char clientaddr_str[RED_INET_ADDRSTRLEN], destaddr_str[RED_INET_ADDRSTRLEN];
  21. if (!fmt) {
  22. log_errno(LOG_ERR, "evbuffer_new()");
  23. // no return, as I have to call va_start/va_end
  24. }
  25. - if (!inet_ntop(clientaddr->sin_family, &clientaddr->sin_addr, clientaddr_str, sizeof(clientaddr_str)))
  26. - strncpy(clientaddr_str, "???", sizeof(clientaddr_str));
  27. - if (!inet_ntop(destaddr->sin_family, &destaddr->sin_addr, destaddr_str, sizeof(destaddr_str)))
  28. - strncpy(destaddr_str, "???", sizeof(destaddr_str));
  29. -
  30. if (fmt) {
  31. - evbuffer_add_printf(fmt, "[%s:%i->%s:%i]: %s",
  32. - clientaddr_str, ntohs(clientaddr->sin_port),
  33. - destaddr_str, ntohs(destaddr->sin_port),
  34. + evbuffer_add_printf(fmt, "[%s->%s]: %s",
  35. + red_inet_ntop(clientaddr, clientaddr_str, sizeof(clientaddr_str)),
  36. + red_inet_ntop(destaddr, destaddr_str, sizeof(destaddr_str)),
  37. orig_fmt);
  38. }
  39. diff --git a/redudp.c b/redudp.c
  40. index 0a97852..9516a50 100644
  41. --- a/redudp.c
  42. +++ b/redudp.c
  43. @@ -436,10 +436,9 @@ static void redudp_pkt_from_socks(int fd, short what, void *_arg)
  44. return;
  45. if (memcmp(&udprelayaddr, &client->udprelayaddr, sizeof(udprelayaddr)) != 0) {
  46. - char buf[INET6_ADDRSTRLEN];
  47. - const char *addr = inet_ntop(udprelayaddr.sin_family, &udprelayaddr.sin_addr, buf, sizeof(buf));
  48. - redudp_log_error(client, LOG_NOTICE, "Got packet from unexpected address %s:%u.",
  49. - addr ? addr : "?", ntohs(udprelayaddr.sin_port));
  50. + char buf[RED_INET_ADDRSTRLEN];
  51. + redudp_log_error(client, LOG_NOTICE, "Got packet from unexpected address %s.",
  52. + red_inet_ntop(&udprelayaddr, buf, sizeof(buf)));
  53. return;
  54. }
  55. @@ -459,10 +458,14 @@ static void redudp_pkt_from_socks(int fd, short what, void *_arg)
  56. if (pkt.header.ip.port != client->instance->config.destaddr.sin_port ||
  57. pkt.header.ip.addr != client->instance->config.destaddr.sin_addr.s_addr)
  58. {
  59. - char buf[INET6_ADDRSTRLEN];
  60. - const char *addr = inet_ntop(AF_INET, &pkt.header.ip.addr, buf, sizeof(buf));
  61. - redudp_log_error(client, LOG_NOTICE, "Socks5 server relayed packet from unexpected address %s:%u.",
  62. - addr ? addr : "?", ntohs(pkt.header.ip.port));
  63. + char buf[RED_INET_ADDRSTRLEN];
  64. + struct sockaddr_in pktaddr = {
  65. + .sin_family = AF_INET,
  66. + .sin_addr = { pkt.header.ip.addr },
  67. + .sin_port = pkt.header.ip.port,
  68. + };
  69. + redudp_log_error(client, LOG_NOTICE, "Socks5 server relayed packet from unexpected address %s.",
  70. + red_inet_ntop(&pktaddr, buf, sizeof(buf)));
  71. return;
  72. }
  73. diff --git a/utils.c b/utils.c
  74. index c6ced51..6e1f3af 100644
  75. --- a/utils.c
  76. +++ b/utils.c
  77. @@ -18,6 +18,7 @@
  78. #include <errno.h>
  79. #include <assert.h>
  80. #include <fcntl.h>
  81. +#include <string.h>
  82. #include <sys/socket.h>
  83. #include <netinet/in.h>
  84. #include <arpa/inet.h>
  85. @@ -42,10 +43,9 @@ int red_recv_udp_pkt(int fd, char *buf, size_t buflen, struct sockaddr_in *inadd
  86. }
  87. if (pktlen >= buflen) {
  88. - char buf[INET6_ADDRSTRLEN];
  89. - const char *addr = inet_ntop(inaddr->sin_family, &inaddr->sin_addr, buf, sizeof(buf));
  90. - log_error(LOG_WARNING, "wow! Truncated udp packet of size %zd from %s:%u! impossible! dropping it...",
  91. - pktlen, addr ? addr : "?", ntohs(inaddr->sin_port));
  92. + char buf[RED_INET_ADDRSTRLEN];
  93. + log_error(LOG_WARNING, "wow! Truncated udp packet of size %zd from %s! impossible! dropping it...",
  94. + pktlen, red_inet_ntop(inaddr, buf, sizeof(buf)));
  95. return -1;
  96. }
  97. @@ -176,4 +176,33 @@ int red_is_socket_connected_ok(struct bufferevent *buffev)
  98. }
  99. }
  100. +char *red_inet_ntop(const struct sockaddr_in* sa, char* buffer, size_t buffer_size)
  101. +{
  102. + const char *retval = 0;
  103. + size_t len = 0;
  104. + uint16_t port;
  105. + const char placeholder[] = "???:???";
  106. +
  107. + assert(buffer_size >= sizeof(placeholder));
  108. +
  109. + memset(buffer, buffer_size, 0);
  110. + if (sa->sin_family == AF_INET) {
  111. + retval = inet_ntop(AF_INET, &sa->sin_addr, buffer, buffer_size);
  112. + port = ((struct sockaddr_in*)sa)->sin_port;
  113. + }
  114. + else if (sa->sin_family == AF_INET6) {
  115. + retval = inet_ntop(AF_INET6, &((const struct sockaddr_in6*)sa)->sin6_addr, buffer, buffer_size);
  116. + port = ((struct sockaddr_in6*)sa)->sin6_port;
  117. + }
  118. + if (retval) {
  119. + assert(retval == buffer);
  120. + len = strlen(retval);
  121. + snprintf(buffer + len, buffer_size - len, ":%d", ntohs(port));
  122. + }
  123. + else {
  124. + strcpy(buffer, placeholder);
  125. + }
  126. + return buffer;
  127. +}
  128. +
  129. /* vim:set tabstop=4 softtabstop=4 shiftwidth=4: */
  130. diff --git a/utils.h b/utils.h
  131. index f691b77..d3af00f 100644
  132. --- a/utils.h
  133. +++ b/utils.h
  134. @@ -57,6 +57,13 @@ int fcntl_nonblock(int fd);
  135. (what) & EVBUFFER_TIMEOUT ? "EVBUFFER_TIMEOUT" : "0", \
  136. (what) & ~(EVBUFFER_READ|EVBUFFER_WRITE|EVBUFFER_EOF|EVBUFFER_ERROR|EVBUFFER_TIMEOUT)
  137. +#if INET6_ADDRSTRLEN < INET_ADDRSTRLEN
  138. +# error Impossible happens: INET6_ADDRSTRLEN < INET_ADDRSTRLEN
  139. +#else
  140. +# define RED_INET_ADDRSTRLEN (INET6_ADDRSTRLEN + 1 + 5 + 1) // addr + : + port + \0
  141. +#endif
  142. +char *red_inet_ntop(const struct sockaddr_in* sa, char* buffer, size_t buffer_size);
  143. +
  144. /* vim:set tabstop=4 softtabstop=4 shiftwidth=4: */
  145. /* vim:set foldmethod=marker foldlevel=32 foldmarker={,}: */
  146. #endif /* UTILS_H_SAT_FEB__2_02_24_05_2008 */
  147. --
  148. 1.9.1