[PATCH] Fix ptr resolving with cname

Ruslan Ermilov ru at nginx.com
Thu Dec 17 14:43:54 UTC 2015


On Wed, Dec 16, 2015 at 11:59:29AM +0800, DannyAAM wrote:
> # HG changeset patch
> # User DannyAAM <danny at saru.moe>
> # Date 1449696194 -28800
> #      Thu Dec 10 05:23:14 2015 +0800
> # Branch fix-ptr-cname
> # Node ID 9d8c7332b7300908414e3bec78a90d9d14b30af8
> # Parent  dfe68c41f34f865bc7b45cbe6b7d0f639de283fc
> Fix ptr resolving with cname
> 
> Make ptr process aware of cname & follow it.
> (This depends on resolver's recursive answer.)

Thanks for trying.  Unfortunately, your patch has many issues,
mainly caused by not checking for malformed responses.

I have a better patch in the works that doesn't have a complete
CNAME support in PTR responses, but otherwise works in the
described situation plus doesn't require a RR to be compressed
like is found in responses generated by some SOHO routers.

Please let me know if you'd like to try my patch.

> diff -r dfe68c41f34f -r 9d8c7332b730 src/core/ngx_resolver.c
> --- a/src/core/ngx_resolver.c	Wed Dec 09 17:47:21 2015 +0300
> +++ b/src/core/ngx_resolver.c	Thu Dec 10 05:23:14 2015 +0800
> @@ -2032,7 +2032,7 @@
>      int32_t               ttl;
>      ngx_int_t             octet;
>      ngx_str_t             name;
> -    ngx_uint_t            i, mask, qident, class;
> +    ngx_uint_t            i, mask, qident, type, class;
>      ngx_queue_t          *expire_queue;
>      ngx_rbtree_t         *tree;
>      ngx_resolver_an_t    *an;
> @@ -2196,9 +2196,14 @@
>          goto invalid;
>      }
>  
> +
> +
>      an = (ngx_resolver_an_t *) &buf[i + 2];
>  
> +cname_continue:
> +
>      class = (an->class_hi << 8) + an->class_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]);
> @@ -2213,6 +2218,34 @@
>          ttl = 0;
>      }
>  
> +    /* CNAME processing */
> +    if (type == NGX_RESOLVE_CNAME) {
> +        do {
> +            if (buf[i] == 0xc0) {
> +                i += 2;
> +                break;
> +            } else {
> +                i += 1 + buf[i];
> +            }
> +        } while (buf[i] != 0);
> +        an = (ngx_resolver_an_t *) &buf[i];
> +        len = (an->len_hi << 8) + an->len_lo;
> +        i += sizeof(ngx_resolver_an_t) + len;
> +
> +        ngx_uint_t nameidx = i;
> +        do {
> +            if (buf[nameidx] == 0xc0) {
> +                nameidx += 2;
> +                break;
> +            } else {
> +                nameidx += 1 + buf[nameidx];
> +            }
> +        } while (buf[nameidx] != 0);
> +        an = (ngx_resolver_an_t *) &buf[nameidx];
> +
> +        goto cname_continue;
> +    }
> +
>      ngx_log_debug3(NGX_LOG_DEBUG_CORE, r->log, 0,
>                    "resolver qt:%ui cl:%ui len:%uz",
>                    (an->type_hi << 8) + an->type_lo,



More information about the nginx-devel mailing list