moved human_addr2() to ip-util.c

This commit is contained in:
Nikos Mavrogiannopoulos
2016-02-19 09:56:21 +01:00
parent 007e390d63
commit 029e42d07d
4 changed files with 66 additions and 65 deletions

View File

@@ -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;
}

View File

@@ -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

View File

@@ -26,71 +26,10 @@
#include <arpa/inet.h>
#include <base64-helper.h>
#include <vpn.h>
#include <worker.h>
#include <main.h>
#include <sec-mod.h>
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, ...)

View File

@@ -467,10 +467,6 @@ struct main_server_st;
#include <tun.h>
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)