Added human_addr2() which will display port number only when requested.

This commit is contained in:
Nikos Mavrogiannopoulos
2014-01-11 22:12:28 +01:00
parent e5466c0688
commit 043355799d
3 changed files with 21 additions and 15 deletions

View File

@@ -28,8 +28,8 @@
#include <worker.h>
#include <main.h>
const char *human_addr(const struct sockaddr *sa, socklen_t salen,
void *_buf, size_t buflen)
const char *human_addr2(const struct sockaddr *sa, socklen_t salen,
void *_buf, size_t buflen, unsigned full)
{
const char *save_buf = _buf;
char *buf = _buf;
@@ -38,7 +38,7 @@ const char *human_addr(const struct sockaddr *sa, socklen_t salen,
if (!buf || !buflen)
return NULL;
if (salen == sizeof(struct sockaddr_in6) &&
if (full != 0 && salen == sizeof(struct sockaddr_in6) &&
((struct sockaddr_in6*)sa)->sin6_port != 0) {
*buf = '[';
buf++;
@@ -48,6 +48,9 @@ const char *human_addr(const struct sockaddr *sa, socklen_t salen,
if (getnameinfo(sa, salen, buf, buflen, NULL, 0, NI_NUMERICHOST) != 0)
return NULL;
if (full == 0)
goto finish;
l = strlen(buf);
buf += l;
buflen -= l;
@@ -71,6 +74,7 @@ const char *human_addr(const struct sockaddr *sa, socklen_t salen,
buf[0] = 0;
}
finish:
return save_buf;
}

View File

@@ -424,8 +424,8 @@ static int append_user_info(DBusMessageIter * subs, struct proc_st *ctmp)
}
strtmp =
human_addr((struct sockaddr *)&ctmp->remote_addr,
ctmp->remote_addr_len, ipbuf, sizeof(ipbuf));
human_addr2((struct sockaddr *)&ctmp->remote_addr,
ctmp->remote_addr_len, ipbuf, sizeof(ipbuf), 0);
if (strtmp == NULL)
strtmp = "";
if (dbus_message_iter_append_basic
@@ -441,8 +441,8 @@ static int append_user_info(DBusMessageIter * subs, struct proc_st *ctmp)
strtmp = NULL;
if (ctmp->ipv4 != NULL)
strtmp =
human_addr((struct sockaddr *)&ctmp->ipv4->rip,
ctmp->ipv4->rip_len, ipbuf, sizeof(ipbuf));
human_addr2((struct sockaddr *)&ctmp->ipv4->rip,
ctmp->ipv4->rip_len, ipbuf, sizeof(ipbuf), 0);
if (strtmp == NULL)
strtmp = "";
if (dbus_message_iter_append_basic
@@ -453,8 +453,8 @@ static int append_user_info(DBusMessageIter * subs, struct proc_st *ctmp)
strtmp = NULL;
if (ctmp->ipv4 != NULL)
strtmp =
human_addr((struct sockaddr *)&ctmp->ipv4->lip,
ctmp->ipv4->lip_len, ipbuf, sizeof(ipbuf));
human_addr2((struct sockaddr *)&ctmp->ipv4->lip,
ctmp->ipv4->lip_len, ipbuf, sizeof(ipbuf), 0);
if (strtmp == NULL)
strtmp = "";
if (dbus_message_iter_append_basic
@@ -465,8 +465,8 @@ static int append_user_info(DBusMessageIter * subs, struct proc_st *ctmp)
strtmp = NULL;
if (ctmp->ipv6 != NULL)
strtmp =
human_addr((struct sockaddr *)&ctmp->ipv6->rip,
ctmp->ipv6->rip_len, ipbuf, sizeof(ipbuf));
human_addr2((struct sockaddr *)&ctmp->ipv6->rip,
ctmp->ipv6->rip_len, ipbuf, sizeof(ipbuf), 0);
if (strtmp == NULL)
strtmp = "";
if (dbus_message_iter_append_basic
@@ -477,8 +477,8 @@ static int append_user_info(DBusMessageIter * subs, struct proc_st *ctmp)
strtmp = NULL;
if (ctmp->ipv6 != NULL)
strtmp =
human_addr((struct sockaddr *)&ctmp->ipv6->lip,
ctmp->ipv6->lip_len, ipbuf, sizeof(ipbuf));
human_addr2((struct sockaddr *)&ctmp->ipv6->lip,
ctmp->ipv6->lip_len, ipbuf, sizeof(ipbuf), 0);
if (strtmp == NULL)
strtmp = "";
if (dbus_message_iter_append_basic

View File

@@ -239,8 +239,10 @@ struct main_server_st;
#include <tun.h>
const char *human_addr(const struct sockaddr *sa, socklen_t salen,
void *buf, size_t buflen);
const 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)
/* Helper casts */
#define SA_IN_P(p) (&((struct sockaddr_in *)(p))->sin_addr)