diff --git a/src/ip-util.c b/src/ip-util.c index 41d2d95a..3f2c0d37 100644 --- a/src/ip-util.c +++ b/src/ip-util.c @@ -192,3 +192,64 @@ char *ipv4_route_to_cidr(void *pool, const char *route) return talloc_asprintf(pool, "%.*s/%d", len, route, prefix); } + +char *human_addr2(const struct sockaddr *sa, socklen_t salen, + void *_buf, size_t buflen, unsigned full) +{ + char *save_buf = _buf; + char *buf = _buf; + size_t l; + const char *ret; + unsigned port; + + if (!buf || !buflen) + return NULL; + + if (sa->sa_family != AF_INET && sa->sa_family != AF_INET6) { + return NULL; + } + + if (salen == sizeof(struct sockaddr_in6)) { + port = (unsigned)ntohs(((struct sockaddr_in6*)sa)->sin6_port); + + if (full != 0 && port != 0 && buflen > 0) { + *buf = '['; + buf++; + buflen--; + } + + ret = inet_ntop(AF_INET6, &((struct sockaddr_in6*)sa)->sin6_addr, buf, buflen); + } else { + port = (unsigned)ntohs(((struct sockaddr_in*)sa)->sin_port); + + ret = inet_ntop(AF_INET, &((struct sockaddr_in*)sa)->sin_addr, buf, buflen); + } + + if (ret == NULL) { + return NULL; + } + + if (full == 0) + goto finish; + + l = strlen(buf); + buf += l; + buflen -= l; + + if (salen == sizeof(struct sockaddr_in6) && port != 0 && buflen > 0) { + *buf = ']'; + buf++; + buflen--; + } + + if (port != 0 && buflen > 0) { + *buf = ':'; + buf++; + buflen--; + + snprintf(buf, buflen, "%u", port); + } + +finish: + return save_buf; +} diff --git a/src/ip-util.h b/src/ip-util.h index 2f6f5a41..3d8bd4fb 100644 --- a/src/ip-util.h +++ b/src/ip-util.h @@ -55,4 +55,9 @@ char *ipv4_route_to_cidr(void *pool, const char *route); #define SA_IN_P_TYPE(addr, type) ((type==AF_INET)?SA_IN_U8_P(addr):SA_IN6_U8_P(addr)) #define SA_IN_SIZE(size) ((size==sizeof(struct sockaddr_in))?sizeof(struct in_addr):sizeof(struct in6_addr)) +char *human_addr2(const struct sockaddr *sa, socklen_t salen, + void *buf, size_t buflen, unsigned full); + +#define human_addr(x, y, z, w) human_addr2(x, y, z, w, 1) + #endif diff --git a/src/log.c b/src/log.c index ade05574..4cdbb533 100644 --- a/src/log.c +++ b/src/log.c @@ -26,71 +26,10 @@ #include #include -#include #include #include #include -char *human_addr2(const struct sockaddr *sa, socklen_t salen, - void *_buf, size_t buflen, unsigned full) -{ - char *save_buf = _buf; - char *buf = _buf; - size_t l; - const char *ret; - unsigned port; - - if (!buf || !buflen) - return NULL; - - if (sa->sa_family != AF_INET && sa->sa_family != AF_INET6) { - return NULL; - } - - if (salen == sizeof(struct sockaddr_in6)) { - port = (unsigned)ntohs(((struct sockaddr_in6*)sa)->sin6_port); - - if (full != 0 && port != 0 && buflen > 0) { - *buf = '['; - buf++; - buflen--; - } - - ret = inet_ntop(AF_INET6, &((struct sockaddr_in6*)sa)->sin6_addr, buf, buflen); - } else { - port = (unsigned)ntohs(((struct sockaddr_in*)sa)->sin_port); - - ret = inet_ntop(AF_INET, &((struct sockaddr_in*)sa)->sin_addr, buf, buflen); - } - - if (ret == NULL) { - return NULL; - } - - if (full == 0) - goto finish; - - l = strlen(buf); - buf += l; - buflen -= l; - - if (salen == sizeof(struct sockaddr_in6) && port != 0 && buflen > 0) { - *buf = ']'; - buf++; - buflen--; - } - - if (port != 0 && buflen > 0) { - *buf = ':'; - buf++; - buflen--; - - snprintf(buf, buflen, "%u", port); - } - -finish: - return save_buf; -} void __attribute__ ((format(printf, 3, 4))) _oclog(const worker_st * ws, int priority, const char *fmt, ...) diff --git a/src/vpn.h b/src/vpn.h index 1758ff84..649e91d4 100644 --- a/src/vpn.h +++ b/src/vpn.h @@ -467,10 +467,6 @@ struct main_server_st; #include unsigned extract_prefix(char *network); -char *human_addr2(const struct sockaddr *sa, socklen_t salen, - void *buf, size_t buflen, unsigned full); - -#define human_addr(x, y, z, w) human_addr2(x, y, z, w, 1) /* macros */ #define TOS_PACK(x) (x<<4)