PATCH: Add $fqdn stream and http variable.

David Coles coles.david at gmail.com
Fri Nov 23 22:31:17 UTC 2018


# HG changeset patch
# User David Coles <coles.david at gmail.com>
# Date 1543000128 0
#      Fri Nov 23 19:08:48 2018 +0000
# Node ID 8e014e3bdfbda3c7a22c7980c163bcea1c4474b3
# Parent  be5cb9c67c05ccaf22dab7abba78aa4c1545a8ee
Add $fqdn stream and http variable.

This exposes the system's fully-qualified domain name as reported by calling
getaddrbyname on the hostname. Typically $hostname will only contain the first
segment of the host's domainname.

diff -r be5cb9c67c05 -r 8e014e3bdfbd src/core/ngx_config.h
--- a/src/core/ngx_config.h Wed Nov 21 20:23:16 2018 +0300
+++ b/src/core/ngx_config.h Fri Nov 23 19:08:48 2018 +0000
@@ -124,6 +124,11 @@
 #define NGX_MAXHOSTNAMELEN  256
 #endif

+#ifdef MAXFQDNLEN
+#define NGX_MAXFQDNLEN  MAXFQDNLEN
+#else
+#define NGX_MAXFQDNLEN  256
+#endif

 #define NGX_MAX_UINT32_VALUE  (uint32_t) 0xffffffff
 #define NGX_MAX_INT32_VALUE   (uint32_t) 0x7fffffff
diff -r be5cb9c67c05 -r 8e014e3bdfbd src/core/ngx_cycle.c
--- a/src/core/ngx_cycle.c Wed Nov 21 20:23:16 2018 +0300
+++ b/src/core/ngx_cycle.c Fri Nov 23 19:08:48 2018 +0000
@@ -9,6 +9,8 @@
 #include <ngx_core.h>
 #include <ngx_event.h>

+#include <netdb.h>
+

 static void ngx_destroy_cycle_pools(ngx_conf_t *conf);
 static ngx_int_t ngx_init_zone_pool(ngx_cycle_t *cycle,
@@ -53,6 +55,7 @@
     ngx_core_conf_t     *ccf, *old_ccf;
     ngx_core_module_t   *module;
     char                 hostname[NGX_MAXHOSTNAMELEN];
+    struct hostent      *hostent;

     ngx_timezone_update();

@@ -213,6 +216,23 @@
     ngx_strlow(cycle->hostname.data, (u_char *) hostname, cycle->hostname.len);


+    hostent = gethostbyname(hostname);
+    if (hostent == NULL) {
+        ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "gethostbyname() failed");
+        ngx_destroy_pool(pool);
+        return NULL;
+    }
+
+    cycle->fqdn.len = ngx_strlen(hostent->h_name);
+
+    cycle->fqdn.data = ngx_pnalloc(pool, cycle->fqdn.len);
+    if (cycle->fqdn.data == NULL) {
+        ngx_destroy_pool(pool);
+        return NULL;
+    }
+    ngx_strlow(cycle->fqdn.data, (u_char *) hostent->h_name, cycle->fqdn.len);
+
+
     if (ngx_cycle_modules(cycle) != NGX_OK) {
         ngx_destroy_pool(pool);
         return NULL;
diff -r be5cb9c67c05 -r 8e014e3bdfbd src/core/ngx_cycle.h
--- a/src/core/ngx_cycle.h Wed Nov 21 20:23:16 2018 +0300
+++ b/src/core/ngx_cycle.h Fri Nov 23 19:08:48 2018 +0000
@@ -81,6 +81,7 @@
     ngx_str_t                 prefix;
     ngx_str_t                 lock_file;
     ngx_str_t                 hostname;
+    ngx_str_t                 fqdn;
 };


diff -r be5cb9c67c05 -r 8e014e3bdfbd src/http/ngx_http_variables.c
--- a/src/http/ngx_http_variables.c Wed Nov 21 20:23:16 2018 +0300
+++ b/src/http/ngx_http_variables.c Fri Nov 23 19:08:48 2018 +0000
@@ -134,6 +134,8 @@
     ngx_http_variable_value_t *v, uintptr_t data);
 static ngx_int_t ngx_http_variable_hostname(ngx_http_request_t *r,
     ngx_http_variable_value_t *v, uintptr_t data);
+static ngx_int_t ngx_http_variable_fqdn(ngx_http_request_t *r,
+    ngx_http_variable_value_t *v, uintptr_t data);
 static ngx_int_t ngx_http_variable_pid(ngx_http_request_t *r,
     ngx_http_variable_value_t *v, uintptr_t data);
 static ngx_int_t ngx_http_variable_msec(ngx_http_request_t *r,
@@ -338,6 +340,9 @@
     { ngx_string("hostname"), NULL, ngx_http_variable_hostname,
       0, 0, 0 },

+    { ngx_string("fqdn"), NULL, ngx_http_variable_fqdn,
+      0, 0, 0 },
+
     { ngx_string("pid"), NULL, ngx_http_variable_pid,
       0, 0, 0 },

@@ -2256,6 +2261,20 @@


 static ngx_int_t
+ngx_http_variable_fqdn(ngx_http_request_t *r,
+    ngx_http_variable_value_t *v, uintptr_t data)
+{
+    v->len = ngx_cycle->fqdn.len;
+    v->valid = 1;
+    v->no_cacheable = 0;
+    v->not_found = 0;
+    v->data = ngx_cycle->fqdn.data;
+
+    return NGX_OK;
+}
+
+
+static ngx_int_t
 ngx_http_variable_pid(ngx_http_request_t *r,
     ngx_http_variable_value_t *v, uintptr_t data)
 {
diff -r be5cb9c67c05 -r 8e014e3bdfbd src/stream/ngx_stream_variables.c
--- a/src/stream/ngx_stream_variables.c Wed Nov 21 20:23:16 2018 +0300
+++ b/src/stream/ngx_stream_variables.c Fri Nov 23 19:08:48 2018 +0000
@@ -40,6 +40,8 @@
     ngx_stream_variable_value_t *v, uintptr_t data);
 static ngx_int_t ngx_stream_variable_hostname(ngx_stream_session_t *s,
     ngx_stream_variable_value_t *v, uintptr_t data);
+static ngx_int_t ngx_stream_variable_fqdn(ngx_stream_session_t *s,
+    ngx_stream_variable_value_t *v, uintptr_t data);
 static ngx_int_t ngx_stream_variable_pid(ngx_stream_session_t *s,
     ngx_stream_variable_value_t *v, uintptr_t data);
 static ngx_int_t ngx_stream_variable_msec(ngx_stream_session_t *s,
@@ -96,6 +98,9 @@
     { ngx_string("hostname"), NULL, ngx_stream_variable_hostname,
       0, 0, 0 },

+    { ngx_string("fqdn"), NULL, ngx_stream_variable_fqdn,
+      0, 0, 0 },
+
     { ngx_string("pid"), NULL, ngx_stream_variable_pid,
       0, 0, 0 },

@@ -778,6 +783,20 @@


 static ngx_int_t
+ngx_stream_variable_fqdn(ngx_stream_session_t *s,
+    ngx_stream_variable_value_t *v, uintptr_t data)
+{
+    v->len = ngx_cycle->fqdn.len;
+    v->valid = 1;
+    v->no_cacheable = 0;
+    v->not_found = 0;
+    v->data = ngx_cycle->fqdn.data;
+
+    return NGX_OK;
+}
+
+
+static ngx_int_t
 ngx_stream_variable_pid(ngx_stream_session_t *s,
     ngx_stream_variable_value_t *v, uintptr_t data)
 {


More information about the nginx-devel mailing list