]> git.ipfire.org Git - thirdparty/squid.git/blobdiff - lib/rfc3596.c
SourceFormat Enforcement
[thirdparty/squid.git] / lib / rfc3596.c
index 24ca56360325275ebf2285237e2c95255148e05e..d04afb0362a84e5901b5641033f1226353a23512 100644 (file)
@@ -1,79 +1,20 @@
-
-/*
- * $Id$
- *
- * Low level DNS protocol routines
- * AUTHOR: Amos Jeffries, Rafael Martinez Torres
- *
- * SQUID Web Proxy Cache          http://www.squid-cache.org/
- * ----------------------------------------------------------
- *
- *  Squid is the result of efforts by numerous individuals from
- *  the Internet community; see the CONTRIBUTORS file for full
- *  details.   Many organizations have provided support for Squid's
- *  development; see the SPONSORS file for full details.  Squid is
- *  Copyrighted (C) 2001 by the Regents of the University of
- *  California; see the COPYRIGHT file for full details.  Squid
- *  incorporates software developed and/or copyrighted by other
- *  sources; see the CREDITS file for full details.
- *
- *  This code is copyright (C) 2007 by Treehouse Networks Ltd of
- *  New Zealand. It is published and Lisenced as an extension of
- *  squid under the same conditions as the main squid application.
- *
- *  This program is free software; you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
- *  (at your option) any later version.
- *
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
- *
- *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
- *
- */
-
 /*
- * KNOWN BUGS:
- *
- * UDP replies with TC set should be retried via TCP
- */
-
-/**
- * April 2007
- *
- * Provides RFC3596 functions to handle purely IPv6 DNS.
- * Adds AAAA and IPv6 PTR records.
- * Other IPv6 records are not mentioned by this RFC.
- *
- * IPv4 equivalents are taken care of by the RFC1035 library.
- * Where one protocol lookup must be followed by another, the caller
- * is resposible for the order and handling of the lookups.
+ * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
  *
+ * Squid software is distributed under GPLv2+ license and includes
+ * contributions from numerous individuals and organizations.
+ * Please see the COPYING and CONTRIBUTORS files for details.
  */
 
-#include "config.h"
+#include "squid.h"
 #include "util.h"
 
-#if HAVE_STDIO_H
-#include <stdio.h>
-#endif
 #if HAVE_UNISTD_H
 #include <unistd.h>
 #endif
-#if HAVE_STDLIB_H
-#include <stdlib.h>
-#endif
 #if HAVE_MEMORY_H
 #include <memory.h>
 #endif
-#if HAVE_SYS_TYPES_H
-#include <sys/types.h>
-#endif
 #if HAVE_ASSERT_H
 #include <assert.h>
 #endif
 #include <strings.h>
 #endif
 
+#include "rfc2671.h"
 #include "rfc3596.h"
 
 #ifndef SQUID_RFC1035_H
 #error RFC3596 Library depends on RFC1035
 #endif
 
+/*
+ * Low level DNS protocol routines
+ *
+ * Provides RFC3596 functions to handle purely IPv6 DNS.
+ * Adds AAAA and IPv6 PTR records.
+ * Other IPv6 records are not mentioned by this RFC.
+ *
+ * IPv4 equivalents are taken care of by the RFC1035 library.
+ * Where one protocol lookup must be followed by another, the caller
+ * is resposible for the order and handling of the lookups.
+ *
+ * KNOWN BUGS:
+ *
+ * UDP replies with TC set should be retried via TCP
+ */
+
 /**
  * Builds a message buffer with a QUESTION to lookup records
  * for a hostname.  Caller must allocate 'buf' which should
@@ -99,7 +57,7 @@
  * Returns the size of the query
  */
 ssize_t
-rfc3596BuildHostQuery(const char *hostname, char *buf, size_t sz, unsigned short qid, rfc1035_query * query, int qtype)
+rfc3596BuildHostQuery(const char *hostname, char *buf, size_t sz, unsigned short qid, rfc1035_query * query, int qtype, ssize_t edns_sz)
 {
     static rfc1035_message h;
     size_t offset = 0;
@@ -109,12 +67,15 @@ rfc3596BuildHostQuery(const char *hostname, char *buf, size_t sz, unsigned short
     h.rd = 1;
     h.opcode = 0;               /* QUERY */
     h.qdcount = (unsigned int) 1;
+    h.arcount = (edns_sz > 0 ? 1 : 0);
     offset += rfc1035HeaderPack(buf + offset, sz - offset, &h);
     offset += rfc1035QuestionPack(buf + offset,
                                   sz - offset,
                                   hostname,
                                   qtype,
                                   RFC1035_CLASS_IN);
+    if (edns_sz > 0)
+        offset += rfc2671RROptPack(buf + offset, sz - offset, edns_sz);
 
     if (query) {
         query->qtype = qtype;
@@ -135,9 +96,9 @@ rfc3596BuildHostQuery(const char *hostname, char *buf, size_t sz, unsigned short
  * \return the size of the query
  */
 ssize_t
-rfc3596BuildAQuery(const char *hostname, char *buf, size_t sz, unsigned short qid, rfc1035_query * query)
+rfc3596BuildAQuery(const char *hostname, char *buf, size_t sz, unsigned short qid, rfc1035_query * query, ssize_t edns_sz)
 {
-    return rfc3596BuildHostQuery(hostname, buf, sz, qid, query, RFC1035_TYPE_A);
+    return rfc3596BuildHostQuery(hostname, buf, sz, qid, query, RFC1035_TYPE_A, edns_sz);
 }
 
 /**
@@ -149,12 +110,11 @@ rfc3596BuildAQuery(const char *hostname, char *buf, size_t sz, unsigned short qi
  * \return the size of the query
  */
 ssize_t
-rfc3596BuildAAAAQuery(const char *hostname, char *buf, size_t sz, unsigned short qid, rfc1035_query * query)
+rfc3596BuildAAAAQuery(const char *hostname, char *buf, size_t sz, unsigned short qid, rfc1035_query * query, ssize_t edns_sz)
 {
-    return rfc3596BuildHostQuery(hostname, buf, sz, qid, query, RFC1035_TYPE_AAAA);
+    return rfc3596BuildHostQuery(hostname, buf, sz, qid, query, RFC1035_TYPE_AAAA, edns_sz);
 }
 
-
 /**
  * Builds a message buffer with a QUESTION to lookup PTR records
  * for an address.  Caller must allocate 'buf' which should
@@ -164,8 +124,7 @@ rfc3596BuildAAAAQuery(const char *hostname, char *buf, size_t sz, unsigned short
  * \return the size of the query
  */
 ssize_t
-
-rfc3596BuildPTRQuery4(const struct in_addr addr, char *buf, size_t sz, unsigned short qid, rfc1035_query * query)
+rfc3596BuildPTRQuery4(const struct in_addr addr, char *buf, size_t sz, unsigned short qid, rfc1035_query * query, ssize_t edns_sz)
 {
     static char rev[RFC1035_MAXHOSTNAMESZ];
     unsigned int i;
@@ -177,30 +136,28 @@ rfc3596BuildPTRQuery4(const struct in_addr addr, char *buf, size_t sz, unsigned
              (i >> 16) & 255,
              (i >> 24) & 255);
 
-    return rfc3596BuildHostQuery(rev, buf, sz, qid, query, RFC1035_TYPE_PTR);
+    return rfc3596BuildHostQuery(rev, buf, sz, qid, query, RFC1035_TYPE_PTR, edns_sz);
 }
 
 ssize_t
-
-rfc3596BuildPTRQuery6(const struct in6_addr addr, char *buf, size_t sz, unsigned short qid, rfc1035_query * query)
+rfc3596BuildPTRQuery6(const struct in6_addr addr, char *buf, size_t sz, unsigned short qid, rfc1035_query * query, ssize_t edns_sz)
 {
     static char rev[RFC1035_MAXHOSTNAMESZ];
     const uint8_t* r = addr.s6_addr;
     char* p = rev;
-    int i; // NP: MUST allow signed for loop termination.
+    int i; /* NP: MUST allow signed for loop termination. */
 
     /* work from the raw addr field. anything else may have representation changes. */
     /* The sin6_port and sin6_addr members shall be in network byte order. */
     for (i = 15; i >= 0; i--, p+=4) {
-        snprintf(p, 5, "%1x.%1x.", ((r[i]>>4)&0xf), (r[i])&0xf );
+        snprintf(p, 5, "%1x.%1x.", ((r[i])&0xf), (r[i]>>4)&0xf );
     }
 
     snprintf(p,10,"ip6.arpa.");
 
-    return rfc3596BuildHostQuery(rev, buf, sz, qid, query, RFC1035_TYPE_PTR);
+    return rfc3596BuildHostQuery(rev, buf, sz, qid, query, RFC1035_TYPE_PTR, edns_sz);
 }
 
-
 #if DRIVER
 
 /* driver needs the rfc1035 code _without_ the main() */
@@ -209,18 +166,19 @@ rfc3596BuildPTRQuery6(const struct in6_addr addr, char *buf, size_t sz, unsigned
 #  undef main(a,b)
 
 #include <sys/socket.h>
-#include <sys/time.h>
 
 int
 main(int argc, char *argv[])
 {
-    char input[512];
-    char buf[512];
-    char rbuf[512];
-    size_t sz = 512;
+#define PACKET_BUFSZ        1024
+    char input[PACKET_BUFSZ];
+    char buf[PACKET_BUFSZ];
+    char rbuf[PACKET_BUFSZ];
+    size_t sz = PACKET_BUFSZ;
     unsigned short sid, sidb;
     int s;
     int rl;
+    ssize_t edns_max = -1;
 
     struct sockaddr* S;
     int var = 1;
@@ -238,8 +196,11 @@ main(int argc, char *argv[])
             prefer = AF_INET;
         else if (argv[var][1] == '6')
             prefer = AF_INET6;
+        else if (argv[var][1] == 'E')
+            edns_max = atoi(argv[var++]);
         else {
-            fprintf(stderr, "usage: %s [-6|-4] ip port\n", argv[0]);
+            fprintf(stderr, "usage: %s [-6|-4] [-E packet-size] ip port\n", argv[0]);
+            fprintf(stderr, "  EDNS packets my be up to %d\n", PACKET_BUFSZ);
             return 1;
         }
 
@@ -253,7 +214,6 @@ main(int argc, char *argv[])
         return 1;
     }
 
-
     memset(&S, '\0', sizeof(S));
 
     if (prefer == 6) {
@@ -263,46 +223,44 @@ main(int argc, char *argv[])
         ((struct sockaddr_in6 *)S)->sin6_family = AF_INET6;
         ((struct sockaddr_in6 *)S)->sin6_port = htons(atoi(argv[var+1]));
 
-        if ( ! xinet_pton(AF_INET6, argv[var], &((struct sockaddr_in6 *)S)->sin6_addr.s_addr) )
+        if ( ! inet_pton(AF_INET6, argv[var], &((struct sockaddr_in6 *)S)->sin6_addr.s_addr) ) {
             perror("listen address");
-        return 1;
-    }
+            return 1;
+        }
 
-    s = socket(PF_INET6, SOCK_DGRAM, 0);
-}
-else
-{
-    S = (struct sockaddr *) new sockaddr_in;
-    memset(S,0,sizeof(struct sockaddr_in));
+        s = socket(PF_INET6, SOCK_DGRAM, 0);
+    } else {
+        S = (struct sockaddr *) new sockaddr_in;
+        memset(S,0,sizeof(struct sockaddr_in));
 
-    ((struct sockaddr_in *)S)->sin_family = AF_INET;
-    ((struct sockaddr_in *)S)->sin_port = htons(atoi(argv[var+1]));
+        ((struct sockaddr_in *)S)->sin_family = AF_INET;
+        ((struct sockaddr_in *)S)->sin_port = htons(atoi(argv[var+1]));
 
-    if ( ! xinet_pton(AF_INET, argv[var], &((struct sockaddr_in *)S)->sin_addr.s_addr) )
-        perror("listen address");
-    return 1;
-}
+        if ( ! inet_pton(AF_INET, argv[var], &((struct sockaddr_in *)S)->sin_addr.s_addr) )
+            perror("listen address");
+        return 1;
+    }
 }
 
-while (fgets(input, 512, stdin))
+while (fgets(input, PACKET_BUFSZ, stdin))
 {
 
     struct in6_addr junk6;
 
     struct in_addr junk4;
     strtok(input, "\r\n");
-    memset(buf, '\0', 512);
-    sz = 512;
+    memset(buf, '\0', PACKET_BUFSZ);
+    sz = PACKET_BUFSZ;
 
-    if (xinet_pton(AF_INET6, input, &junk6)) {
-        sid = rfc1035BuildPTRQuery6(junk6, buf, &sz);
+    if (inet_pton(AF_INET6, input, &junk6)) {
+        sid = rfc1035BuildPTRQuery6(junk6, buf, &sz, edns_max);
         sidb=0;
-    } else if (xinet_pton(AF_INET, input, &junk4)) {
-        sid = rfc1035BuildPTRQuery4(junk4, buf, &sz);
+    } else if (inet_pton(AF_INET, input, &junk4)) {
+        sid = rfc1035BuildPTRQuery4(junk4, buf, &sz, edns_max);
         sidb=0;
     } else {
-        sid = rfc1035BuildAAAAQuery(input, buf, &sz);
-        sidb = rfc1035BuildAQuery(input, buf, &sz);
+        sid = rfc1035BuildAAAAQuery(input, buf, &sz, edns_max);
+        sidb = rfc1035BuildAQuery(input, buf, &sz, edns_max);
     }
 
     sendto(s, buf, sz, 0, S, sizeof(*S));
@@ -323,8 +281,8 @@ while (fgets(input, 512, stdin))
         continue;
     }
 
-    memset(rbuf, '\0', 512);
-    rl = recv(s, rbuf, 512, 0);
+    memset(rbuf, '\0', PACKET_BUFSZ);
+    rl = recv(s, rbuf, PACKET_BUFSZ, 0);
     {
         unsigned short rid = 0;
         int i;
@@ -336,7 +294,7 @@ while (fgets(input, 512, stdin))
                                  &rid);
 
         if (n < 0) {
-            printf("ERROR %d\n", rfc1035_errno);
+            printf("ERROR %d\n", -n);
         } else if (rid != sid && rid != sidb) {
             printf("ERROR, ID mismatch (%#hx, %#hx)\n", sid, rid);
             printf("ERROR, ID mismatch (%#hx, %#hx)\n", sidb, rid);
@@ -374,6 +332,7 @@ while (fgets(input, 512, stdin))
 }
 
 return 0;
-       }
+}
 
 #endif
+