dns_hostnames with port specification

From: David G. Andersen <dga@dont-contact.us>
Date: Wed, 30 Jun 2004 00:03:49 -0400

It turns out I wanted to run squid with an alternative nameserver,
both as a normal user. To support this use scenario, Squid
needed to be able to communicate with a nameserver not running
on port 53, by specifying a port in the dns_nameservers config
line:

  dns_nameservers 127.0.0.1:5353 127.0.0.1:5354

The changes are simple, and add a nice bit of
functionality when doing things like testing an upgrade of
BIND, or trying to migrate to a different nameserver.

If useful, they're attached. Diffs are against 2.5-stable4,
but very simple. Line numbers in the src/dns_internal.c
may be slightly different due to local modifications.

  -Dave

Index: lib/safe_inet_addr.c
===================================================================
diff -u -r1.1.1.1 -r1.2
--- lib/safe_inet_addr.c 6 Nov 2003 21:32:48 -0000 1.1.1.1
+++ lib/safe_inet_addr.c 29 Jun 2004 19:04:41 -0000 1.2
@@ -62,5 +62,47 @@
     A.s_addr = inet_addr(addrbuf);
     if (addr)
        addr->s_addr = A.s_addr;
+ return 1;
+}
+
+
+int
+safe_inet_addr_with_port(const char *buf, struct in_addr *addr, short *port)
+{
+ static char addrbuf[32];
+ int a1 = 0, a2 = 0, a3 = 0, a4 = 0, p = 0;
+ struct in_addr A;
+ char x;
+#if defined(_SQUID_HPUX_)
+ /*
+ * MIYOSHI Tsutomu <mijosxi@ike.tottori-u.ac.jp> says scanning 'buf'
+ * causes a bus error on hppa1.1-hp-hpux9.07, so we
+ * have a broad hack for all HP systems.
+ */
+ static char buftmp[32];
+ snprintf(buftmp, 32, "%s", buf);
+ if (sscanf(buftmp, "%d.%d.%d.%d%c", &a1, &a2, &a3, &a4, &x) != 4 &&
+ sscanf(buftmp, "%d.%d.%d.%d:%d%c", &a1, &a2, &a3, &a4, &p, &x) != 5)
+#else
+ if (sscanf(buf, "%d.%d.%d.%d%c", &a1, &a2, &a3, &a4, &x) != 4 &&
+ sscanf(buf, "%d.%d.%d.%d:%d%c", &a1, &a2, &a3, &a4, &p, &x) != 5)
+#endif
+ return 0;
+ if (a1 < 0 || a1 > 255)
+ return 0;
+ if (a2 < 0 || a2 > 255)
+ return 0;
+ if (a3 < 0 || a3 > 255)
+ return 0;
+ if (a4 < 0 || a4 > 255)
+ return 0;
+ if (p < 0 || p > 65535)
+ return 0;
+ snprintf(addrbuf, 32, "%d.%d.%d.%d", a1, a2, a3, a4);
+ A.s_addr = inet_addr(addrbuf);
+ if (addr)
+ addr->s_addr = A.s_addr;
+ if (port)
+ *port = p;
     return 1;
 }

Index: include/util.h
===================================================================
diff -u -r1.1.1.1 -r1.2
--- include/util.h 6 Nov 2003 21:32:48 -0000 1.1.1.1
+++ include/util.h 29 Jun 2004 19:04:42 -0000 1.2
@@ -112,6 +112,7 @@
 
 typedef struct in_addr SIA;
 extern int safe_inet_addr(const char *, SIA *);
+extern int safe_inet_addr_with_port(const char *, SIA *, short *);
 extern time_t parse_iso3307_time(const char *buf);
 extern char *base64_decode(const char *coded);
 extern const char *base64_encode(const char *decoded);

Index: src/dns_internal.c
===================================================================
diff -u -r1.5 -r1.6
--- src/dns_internal.c 17 Nov 2003 23:34:35 -0000 1.5
+++ src/dns_internal.c 29 Jun 2004 19:04:42 -0000 1.6
@@ -102,7 +102,8 @@
 idnsAddNameserver(const char *buf)
 {
     struct in_addr A;
- if (!safe_inet_addr(buf, &A)) {
+ short port;
+ if (!safe_inet_addr_with_port(buf, &A, &port)) {
        debug(78, 0) ("WARNING: rejecting '%s' as a name server, because it is not a numeric IP address\n", buf);
        return;
     }
@@ -126,7 +127,10 @@
     }
     assert(nns < nns_alloc);
     nameservers[nns].S.sin_family = AF_INET;
- nameservers[nns].S.sin_port = htons(DOMAIN_PORT);
+ if (0 == port)
+ nameservers[nns].S.sin_port = htons(DOMAIN_PORT);
+ else
+ nameservers[nns].S.sin_port = htons(port);
     nameservers[nns].S.sin_addr.s_addr = A.s_addr;
     debug(78, 3) ("idnsAddNameserver: Added nameserver #%d: %s\n",
        nns, inet_ntoa(nameservers[nns].S.sin_addr));
Received on Wed Jun 30 2004 - 05:07:31 MDT

This archive was generated by hypermail pre-2.1.9 : Wed Jun 30 2004 - 12:00:03 MDT