]> git.ipfire.org Git - thirdparty/ulogd2.git/commitdiff
gprint, oprint: use inet_ntop to format ip addresses
authorJeremy Sowden <jeremy@azazel.net>
Mon, 21 Aug 2023 19:42:33 +0000 (20:42 +0100)
committerFlorian Westphal <fw@strlen.de>
Thu, 14 Sep 2023 12:22:49 +0000 (14:22 +0200)
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 <jeremy@azazel.net>
Signed-off-by: Florian Westphal <fw@strlen.de>
output/ulogd_output_GPRINT.c
output/ulogd_output_OPRINT.c

index eeeec6ac3eb066e98a59e6d0d6e1dc045554e39b..093d3ea2b254f6781383d9899faf1dc0b446466d 100644 (file)
@@ -27,6 +27,8 @@
 #include <time.h>
 #include <errno.h>
 #include <inttypes.h>
+#include <arpa/inet.h>
+#include <netinet/in.h>
 #include <ulogd/ulogd.h>
 #include <ulogd/conffile.h>
 
@@ -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;
index 0409133e8227fad1c29320ca0a4c2a8b6e9f613d..b5586e850aa4d5f9dcc640627139c868d4de2cb4 100644 (file)
@@ -24,6 +24,8 @@
 #include <string.h>
 #include <errno.h>
 #include <inttypes.h>
+#include <arpa/inet.h>
+#include <netinet/in.h>
 #include <ulogd/ulogd.h>
 #include <ulogd/conffile.h>
 
 #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, "<none>\n");
                        break;