[syslog-ng] [PATCH] Fix logging of IPv6 addresses in template format M_SOURCE_IP

Michael Granzow mgranzow at zeus.com
Wed Apr 15 15:09:25 CEST 2009


Signed-off-by: Michael Granzow <mgranzow at zeus.com>
---
 src/gsockaddr.c |   12 ++++++++++++
 src/gsockaddr.h |    1 +
 src/templates.c |   22 +++++++++++-----------
 3 files changed, 24 insertions(+), 11 deletions(-)

diff --git a/src/gsockaddr.c b/src/gsockaddr.c
index 8216426..5d1de84 100644
--- a/src/gsockaddr.c
+++ b/src/gsockaddr.c
@@ -627,3 +627,15 @@ g_sockaddr_unix_format(GSockAddr *addr, gchar *text, gulong n, gint format)
   return text;
 }
 
+gboolean g_sockaddr_ip46_check(GSockAddr *a)
+{
+   if( !a )
+      return 0;
+
+   return a->sa_funcs == &inet_sockaddr_funcs
+#ifdef ENABLE_IPV6
+      || a->sa_funcs == &inet6_sockaddr_funcs
+#endif
+      ;
+
+}
diff --git a/src/gsockaddr.h b/src/gsockaddr.h
index 9678409..ff74d7b 100644
--- a/src/gsockaddr.h
+++ b/src/gsockaddr.h
@@ -220,5 +220,6 @@ g_sockaddr_inet6_set_port(GSockAddr *s, guint16 port)
 GSockAddr *g_sockaddr_unix_new(const gchar *name);
 GSockAddr *g_sockaddr_unix_new2(struct sockaddr_un *s_un, int sunlen);
 
+gboolean g_sockaddr_ip46_check(GSockAddr *a);
 
 #endif
diff --git a/src/templates.c b/src/templates.c
index 723db95..15793bc 100644
--- a/src/templates.c
+++ b/src/templates.c
@@ -390,17 +390,17 @@ log_macro_expand(GString *result, gint id, guint32 flags, gint ts_format, TimeZo
       break;
     case M_SOURCE_IP:
       {
-        gchar *ip;
-        
-        if (msg->saddr && g_sockaddr_inet_check(msg->saddr)) 
-          {
-            gchar buf[16];
-            
-            g_inet_ntoa(buf, sizeof(buf), ((struct sockaddr_in *) &msg->saddr->sa)->sin_addr);
-            ip = buf;
-          }
-        else 
-          {
+        gchar ipaddr[256];
+        char const* ip;
+        if( msg && g_sockaddr_ip46_check( msg->saddr ) ) {
+           msg->saddr->sa_funcs->sa_format(
+              msg->saddr,
+              ipaddr,
+              sizeof(ipaddr),
+              GSA_ADDRESS_ONLY
+           );
+           ip = ipaddr;
+        } else { // comes from local host
             ip = "127.0.0.1";
           }
         result_append(result, ip, strlen(ip), !!(flags & LT_ESCAPE));
-- 
1.5.6.3



More information about the syslog-ng mailing list