[nginx] Resolver: improved code readability.

Ruslan Ermilov ru at nginx.com
Fri Dec 13 16:54:36 UTC 2013


details:   http://hg.nginx.org/nginx/rev/5c410d6ca7dd
branches:  
changeset: 5468:5c410d6ca7dd
user:      Ruslan Ermilov <ru at nginx.com>
date:      Fri Dec 06 14:30:27 2013 +0400
description:
Resolver: improved code readability.

Renamed ngx_resolver_query_t to ngx_resolver_hdr_t as it describes
the header that is common to DNS queries and answers.

Replaced the magic number 12 by the size of the header structure.

The other changes are self-explanatory.

diffstat:

 src/core/ngx_resolver.c |  158 ++++++++++++++++++++++++++---------------------
 1 files changed, 86 insertions(+), 72 deletions(-)

diffs (truncated from 401 to 300 lines):

diff -r f6258a7092e4 -r 5c410d6ca7dd src/core/ngx_resolver.c
--- a/src/core/ngx_resolver.c	Thu Dec 12 20:28:48 2013 +0400
+++ b/src/core/ngx_resolver.c	Fri Dec 06 14:30:27 2013 +0400
@@ -26,7 +26,7 @@ typedef struct {
     u_char  nns_lo;
     u_char  nar_hi;
     u_char  nar_lo;
-} ngx_resolver_query_t;
+} ngx_resolver_hdr_t;
 
 
 typedef struct {
@@ -1021,31 +1021,31 @@ ngx_resolver_read_response(ngx_event_t *
 static void
 ngx_resolver_process_response(ngx_resolver_t *r, u_char *buf, size_t n)
 {
-    char                  *err;
-    size_t                 len;
-    ngx_uint_t             i, times, ident, qident, flags, code, nqs, nan,
-                           qtype, qclass;
-    ngx_queue_t           *q;
-    ngx_resolver_qs_t     *qs;
-    ngx_resolver_node_t   *rn;
-    ngx_resolver_query_t  *query;
-
-    if ((size_t) n < sizeof(ngx_resolver_query_t)) {
+    char                 *err;
+    size_t                len;
+    ngx_uint_t            i, times, ident, qident, flags, code, nqs, nan,
+                          qtype, qclass;
+    ngx_queue_t          *q;
+    ngx_resolver_qs_t    *qs;
+    ngx_resolver_hdr_t   *response;
+    ngx_resolver_node_t  *rn;
+
+    if (n < sizeof(ngx_resolver_hdr_t)) {
         goto short_response;
     }
 
-    query = (ngx_resolver_query_t *) buf;
-
-    ident = (query->ident_hi << 8) + query->ident_lo;
-    flags = (query->flags_hi << 8) + query->flags_lo;
-    nqs = (query->nqs_hi << 8) + query->nqs_lo;
-    nan = (query->nan_hi << 8) + query->nan_lo;
+    response = (ngx_resolver_hdr_t *) buf;
+
+    ident = (response->ident_hi << 8) + response->ident_lo;
+    flags = (response->flags_hi << 8) + response->flags_lo;
+    nqs = (response->nqs_hi << 8) + response->nqs_lo;
+    nan = (response->nan_hi << 8) + response->nan_lo;
 
     ngx_log_debug6(NGX_LOG_DEBUG_CORE, r->log, 0,
                    "resolver DNS response %ui fl:%04Xui %ui/%ui/%ud/%ud",
                    ident, flags, nqs, nan,
-                   (query->nns_hi << 8) + query->nns_lo,
-                   (query->nar_hi << 8) + query->nar_lo);
+                   (response->nns_hi << 8) + response->nns_lo,
+                   (response->nar_hi << 8) + response->nar_lo);
 
     if (!(flags & 0x8000)) {
         ngx_log_error(r->log_level, r->log, 0,
@@ -1087,7 +1087,7 @@ ngx_resolver_process_response(ngx_resolv
         goto done;
     }
 
-    i = sizeof(ngx_resolver_query_t);
+    i = sizeof(ngx_resolver_hdr_t);
 
     while (i < (ngx_uint_t) n) {
         if (buf[i] == '\0') {
@@ -1152,7 +1152,7 @@ found:
 
 short_response:
 
-    err = "short dns response";
+    err = "short DNS response";
 
 done:
 
@@ -1180,12 +1180,15 @@ ngx_resolver_process_a(ngx_resolver_t *r
     uint32_t              hash;
     in_addr_t             addr, *addrs;
     ngx_str_t             name;
-    ngx_uint_t            qtype, qident, naddrs, a, i, n, start;
+    ngx_uint_t            type, qident, naddrs, a, i, n, start;
     ngx_resolver_an_t    *an;
     ngx_resolver_ctx_t   *ctx, *next;
     ngx_resolver_node_t  *rn;
 
-    if (ngx_resolver_copy(r, &name, buf, &buf[12], &buf[last]) != NGX_OK) {
+    if (ngx_resolver_copy(r, &name, buf,
+                          buf + sizeof(ngx_resolver_hdr_t), buf + last)
+        != NGX_OK)
+    {
         return;
     }
 
@@ -1215,7 +1218,7 @@ ngx_resolver_process_a(ngx_resolver_t *r
     ngx_resolver_free(r, name.data);
 
     if (code == 0 && nan == 0) {
-        code = 3; /* NXDOMAIN */
+        code = NGX_RESOLVE_NXDOMAIN;
     }
 
     if (code) {
@@ -1246,7 +1249,6 @@ ngx_resolver_process_a(ngx_resolver_t *r
     addr = 0;
     addrs = NULL;
     cname = NULL;
-    qtype = 0;
     ttl = 0;
 
     for (a = 0; a < nan; a++) {
@@ -1273,7 +1275,7 @@ ngx_resolver_process_a(ngx_resolver_t *r
     test_length:
 
         if (i - start < 2) {
-            err = "invalid name in dns response";
+            err = "invalid name in DNS response";
             goto invalid;
         }
 
@@ -1285,7 +1287,7 @@ ngx_resolver_process_a(ngx_resolver_t *r
 
         an = (ngx_resolver_an_t *) &buf[i];
 
-        qtype = (an->type_hi << 8) + an->type_lo;
+        type = (an->type_hi << 8) + an->type_lo;
         len = (an->len_hi << 8) + an->len_lo;
         ttl = (an->ttl[0] << 24) + (an->ttl[1] << 16)
             + (an->ttl[2] << 8) + (an->ttl[3]);
@@ -1294,7 +1296,9 @@ ngx_resolver_process_a(ngx_resolver_t *r
             ttl = 0;
         }
 
-        if (qtype == NGX_RESOLVE_A) {
+        switch (type) {
+
+        case NGX_RESOLVE_A:
 
             i += sizeof(ngx_resolver_an_t);
 
@@ -1309,16 +1313,25 @@ ngx_resolver_process_a(ngx_resolver_t *r
 
             i += len;
 
-        } else if (qtype == NGX_RESOLVE_CNAME) {
+            break;
+
+        case NGX_RESOLVE_CNAME:
+
             cname = &buf[i] + sizeof(ngx_resolver_an_t);
             i += sizeof(ngx_resolver_an_t) + len;
 
-        } else if (qtype == NGX_RESOLVE_DNAME) {
+            break;
+
+        case NGX_RESOLVE_DNAME:
+
             i += sizeof(ngx_resolver_an_t) + len;
 
-        } else {
+            break;
+
+        default:
+
             ngx_log_error(r->log_level, r->log, 0,
-                          "unexpected qtype %ui", qtype);
+                          "unexpected RR type %ui", type);
         }
     }
 
@@ -1347,27 +1360,25 @@ ngx_resolver_process_a(ngx_resolver_t *r
 
                     if (buf[i] & 0xc0) {
                         i += 2;
-                        goto ok;
+                        break;
                     }
 
                     if (buf[i] == 0) {
                         i++;
-                        goto ok;
+                        break;
                     }
 
                     i += 1 + buf[i];
                 }
 
-            ok:
-
                 an = (ngx_resolver_an_t *) &buf[i];
 
-                qtype = (an->type_hi << 8) + an->type_lo;
+                type = (an->type_hi << 8) + an->type_lo;
                 len = (an->len_hi << 8) + an->len_lo;
 
                 i += sizeof(ngx_resolver_an_t);
 
-                if (qtype == NGX_RESOLVE_A) {
+                if (type == NGX_RESOLVE_A) {
 
                     addrs[n++] = htonl((buf[i] << 24) + (buf[i + 1] << 16)
                                        + (buf[i + 2] << 8) + (buf[i + 3]));
@@ -1422,12 +1433,13 @@ ngx_resolver_process_a(ngx_resolver_t *r
         rn->query = NULL;
 
         return;
-
-    } else if (cname) {
+    }
+
+    if (cname) {
 
         /* CNAME only */
 
-        if (ngx_resolver_copy(r, &name, buf, cname, &buf[last]) != NGX_OK) {
+        if (ngx_resolver_copy(r, &name, buf, cname, buf + last) != NGX_OK) {
             return;
         }
 
@@ -1460,13 +1472,12 @@ ngx_resolver_process_a(ngx_resolver_t *r
     }
 
     ngx_log_error(r->log_level, r->log, 0,
-               "no A or CNAME types in DNS responses, unknown query type: %ui",
-               qtype);
+                  "no A or CNAME types in DNS response");
     return;
 
 short_response:
 
-    err = "short dns response";
+    err = "short DNS response";
 
 invalid:
 
@@ -1494,29 +1505,32 @@ ngx_resolver_process_ptr(ngx_resolver_t 
     size_t                len;
     in_addr_t             addr;
     int32_t               ttl;
-    ngx_int_t             digit;
+    ngx_int_t             octet;
     ngx_str_t             name;
     ngx_uint_t            i, mask, qident;
     ngx_resolver_an_t    *an;
     ngx_resolver_ctx_t   *ctx, *next;
     ngx_resolver_node_t  *rn;
 
-    if (ngx_resolver_copy(r, NULL, buf, &buf[12], &buf[n]) != NGX_OK) {
+    if (ngx_resolver_copy(r, NULL, buf,
+                          buf + sizeof(ngx_resolver_hdr_t), buf + n)
+        != NGX_OK)
+    {
         goto invalid_in_addr_arpa;
     }
 
     addr = 0;
-    i = 12;
+    i = sizeof(ngx_resolver_hdr_t);
 
     for (mask = 0; mask < 32; mask += 8) {
         len = buf[i++];
 
-        digit = ngx_atoi(&buf[i], len);
-        if (digit == NGX_ERROR || digit > 255) {
+        octet = ngx_atoi(&buf[i], len);
+        if (octet == NGX_ERROR || octet > 255) {
             goto invalid_in_addr_arpa;
         }
 
-        addr += digit << mask;
+        addr += octet << mask;
         i += len;
     }
 
@@ -1547,7 +1561,7 @@ ngx_resolver_process_ptr(ngx_resolver_t 
     }
 
     if (code == 0 && nan == 0) {
-        code = 3; /* NXDOMAIN */
+        code = NGX_RESOLVE_NXDOMAIN;
     }
 
     if (code) {
@@ -1581,7 +1595,7 @@ ngx_resolver_process_ptr(ngx_resolver_t 
 
     /* compression pointer to "XX.XX.XX.XX.in-addr.arpa */
 
-    if (buf[i] != 0xc0 || buf[i + 1] != 0x0c) {
+    if (buf[i] != 0xc0 || buf[i + 1] != sizeof(ngx_resolver_hdr_t)) {
         err = "invalid in-addr.arpa name in DNS response";
         goto invalid;
     }
@@ -1607,7 +1621,7 @@ ngx_resolver_process_ptr(ngx_resolver_t 
         goto short_response;
     }
 



More information about the nginx-devel mailing list