[PATCH 1 of 2] HTTP: Add client source port to any error that is logged

Quanah Gibson-Mount quanah at zimbra.com
Thu Apr 24 17:19:46 UTC 2014


# HG changeset patch
# User Quanah Gibson-Mount <quanah at zimbra.com>
# Date 1398357557 18000
# Node ID 4b7d2e503c06758330aabcc21ffbbab77f09568e
# Parent  1b0c55d38d0b7ba69dcad79760a3fadc30696a9d
HTTP: Add client source port to any error that is logged
For TRAC ticket 531

diff -r 1b0c55d38d0b -r 4b7d2e503c06 src/http/ngx_http_request.c
--- a/src/http/ngx_http_request.c	Thu Apr 24 16:54:23 2014 +0400
+++ b/src/http/ngx_http_request.c	Thu Apr 24 11:39:17 2014 -0500
@@ -3548,6 +3548,11 @@
     u_char              *p;
     ngx_http_request_t  *r;
     ngx_http_log_ctx_t  *ctx;
+    ngx_uint_t          remote_port=0;
+    struct sockaddr_in         *sin;
+#if (NGX_HAVE_INET6)
+    struct sockaddr_in6        *sin6;
+#endif
 
     if (log->action) {
         p = ngx_snprintf(buf, len, " while %s", log->action);
@@ -3557,15 +3562,32 @@
 
     ctx = log->data;
 
-    p = ngx_snprintf(buf, len, ", client: %V", &ctx->connection->addr_text);
-    len -= p - buf;
-
     r = ctx->request;
-
     if (r) {
+        switch (r->connection->sockaddr->sa_family) {
+    #if (NGX_HAVE_INET6)
+        case AF_INET6:
+            sin6 = (struct sockaddr_in6 *) r->connection->sockaddr;
+            remote_port = ntohs(sin6->sin6_port);
+            break;
+    #endif
+    
+        default: /* AF_INET */
+            sin = (struct sockaddr_in *) r->connection->sockaddr;
+            remote_port = ntohs(sin->sin_port);
+            break;
+        }
+    
+        if (remote_port && remote_port < 65536) {
+          p = ngx_snprintf(buf, len, ", client: %V:%ui", &ctx->connection->addr_text,remote_port);
+        } else {
+          p = ngx_snprintf(buf, len, ", client: %V", &ctx->connection->addr_text);
+        }
+        len -= p - buf;
+    
         return r->log_handler(r, ctx->current_request, p, len);
-
     } else {
+        p = ngx_snprintf(buf, len, ", client: %V", &ctx->connection->addr_text);
         p = ngx_snprintf(p, len, ", server: %V",
                          &ctx->connection->listening->addr_text);
     }



More information about the nginx-devel mailing list