From: Jeremy Sowden Date: Mon, 21 Aug 2023 19:42:33 +0000 (+0100) Subject: gprint, oprint: use inet_ntop to format ip addresses X-Git-Tag: ulogd-2.0.9~26 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f04bf6794d1153abd2c3b0bfededd9403d79acf6;p=thirdparty%2Fulogd2.git gprint, oprint: use inet_ntop to format ip addresses Replace hand-rolled ipv4-only formatting code in order to be able to support ipv6 addresses. This also changes the byte-order expected by oprint from HBO to NBO. Signed-off-by: Jeremy Sowden Signed-off-by: Florian Westphal --- diff --git a/output/ulogd_output_GPRINT.c b/output/ulogd_output_GPRINT.c index eeeec6a..093d3ea 100644 --- a/output/ulogd_output_GPRINT.c +++ b/output/ulogd_output_GPRINT.c @@ -27,6 +27,8 @@ #include #include #include +#include +#include #include #include @@ -69,12 +71,6 @@ static struct config_keyset gprint_kset = { }, }; -#define NIPQUAD(addr) \ - ((unsigned char *)&addr)[0], \ - ((unsigned char *)&addr)[1], \ - ((unsigned char *)&addr)[2], \ - ((unsigned char *)&addr)[3] - static int gprint_interp(struct ulogd_pluginstance *upi) { struct gprint_priv *opi = (struct gprint_priv *) &upi->private; @@ -158,20 +154,24 @@ static int gprint_interp(struct ulogd_pluginstance *upi) rem -= ret; size += ret; break; - case ULOGD_RET_IPADDR: + case ULOGD_RET_IPADDR: { + struct in_addr ipv4addr; + ret = snprintf(buf+size, rem, "%s=", key->name); if (ret < 0) break; rem -= ret; size += ret; - ret = snprintf(buf+size, rem, "%u.%u.%u.%u,", - NIPQUAD(key->u.value.ui32)); - if (ret < 0) + ipv4addr.s_addr = key->u.value.ui32; + if (!inet_ntop(AF_INET, &ipv4addr, buf + size, rem)) break; + ret = strlen(buf + size); + rem -= ret; size += ret; break; + } default: /* don't know how to interpret this key. */ break; diff --git a/output/ulogd_output_OPRINT.c b/output/ulogd_output_OPRINT.c index 0409133..b5586e8 100644 --- a/output/ulogd_output_OPRINT.c +++ b/output/ulogd_output_OPRINT.c @@ -24,6 +24,8 @@ #include #include #include +#include +#include #include #include @@ -31,18 +33,6 @@ #define ULOGD_OPRINT_DEFAULT "/var/log/ulogd_oprint.log" #endif -#define NIPQUAD(addr) \ - ((unsigned char *)&addr)[0], \ - ((unsigned char *)&addr)[1], \ - ((unsigned char *)&addr)[2], \ - ((unsigned char *)&addr)[3] - -#define HIPQUAD(addr) \ - ((unsigned char *)&addr)[3], \ - ((unsigned char *)&addr)[2], \ - ((unsigned char *)&addr)[1], \ - ((unsigned char *)&addr)[0] - struct oprint_priv { FILE *of; }; @@ -59,7 +49,7 @@ static int oprint_interp(struct ulogd_pluginstance *upi) if (!ret) ulogd_log(ULOGD_NOTICE, "no result for %s ?!?\n", upi->input.keys[i].name); - + if (!IS_VALID(*ret)) continue; @@ -85,10 +75,18 @@ static int oprint_interp(struct ulogd_pluginstance *upi) case ULOGD_RET_UINT64: fprintf(opi->of, "%" PRIu64 "\n", ret->u.value.ui64); break; - case ULOGD_RET_IPADDR: - fprintf(opi->of, "%u.%u.%u.%u\n", - HIPQUAD(ret->u.value.ui32)); + case ULOGD_RET_IPADDR: { + char addrbuf[INET_ADDRSTRLEN + 1] = ""; + struct in_addr ipv4addr; + + ipv4addr.s_addr = ret->u.value.ui32; + if (!inet_ntop(AF_INET, &ipv4addr, addrbuf, + sizeof(addrbuf))) + break; + + fprintf(opi->of, "%s\n", addrbuf); break; + } case ULOGD_RET_NONE: fprintf(opi->of, "\n"); break;