diff src/core/ngx_proxy_protocol.c @ 6184:fa663739e115

Stream: client-side PROXY protocol. The new directive "proxy_protocol" toggles sending out PROXY protocol header to upstream once connection is established.
author Roman Arutyunyan <arut@nginx.com>
date Tue, 16 Jun 2015 13:45:16 +0300
parents 3a72b1805c52
children a420cb1c170b
line wrap: on
line diff
--- a/src/core/ngx_proxy_protocol.c
+++ b/src/core/ngx_proxy_protocol.c
@@ -89,3 +89,52 @@ invalid:
 
     return NULL;
 }
+
+
+u_char *
+ngx_proxy_protocol_write(ngx_connection_t *c, u_char *buf, u_char *last)
+{
+    ngx_uint_t  port, lport;
+
+    if (last - buf < NGX_PROXY_PROTOCOL_MAX_HEADER) {
+        return NULL;
+    }
+
+    if (ngx_connection_local_sockaddr(c, NULL, 0) != NGX_OK) {
+        return NULL;
+    }
+
+    switch (c->sockaddr->sa_family) {
+
+    case AF_INET:
+        buf = ngx_cpymem(buf, "PROXY TCP4 ", sizeof("PROXY TCP4 ") - 1);
+
+        port = ntohs(((struct sockaddr_in *) c->sockaddr)->sin_port);
+        lport = ntohs(((struct sockaddr_in *) c->local_sockaddr)->sin_port);
+
+        break;
+
+#if (NGX_HAVE_INET6)
+    case AF_INET6:
+        buf = ngx_cpymem(buf, "PROXY TCP6 ", sizeof("PROXY TCP6 ") - 1);
+
+        port = ntohs(((struct sockaddr_in6 *) c->sockaddr)->sin6_port);
+        lport = ntohs(((struct sockaddr_in6 *) c->local_sockaddr)->sin6_port);
+
+        break;
+#endif
+
+    default:
+        return ngx_cpymem(buf, "PROXY UNKNOWN" CRLF,
+                          sizeof("PROXY UNKNOWN" CRLF) - 1);
+    }
+
+    buf += ngx_sock_ntop(c->sockaddr, c->socklen, buf, last - buf, 0);
+
+    *buf++ = ' ';
+
+    buf += ngx_sock_ntop(c->local_sockaddr, c->local_socklen, buf, last - buf,
+                         0);
+
+    return ngx_slprintf(buf, last, " %ui %ui" CRLF, port, lport);
+}