cURL / Mailing Lists / curl-library / Single Mail

curl-library

[PATCH] Do not try to resolve interfaces names via DNS

From: Jason Glasgow <jglasgow_at_chromium.org>
Date: Fri, 4 Nov 2011 16:48:05 -0400

Do not try to resolve interfaces names via DNS by recognizing
interface names in a few ways. If the interface option argument has a
prefix of "if!" then treat the argument as only an interface.
Similarly, if the interface argument is the name of an interface (even
if it does not have an IP address assigned), treat it as an interface
name. Finally, if the interface argument is prefixed by "host!" treat
it as a hostname that must be resolved by /etc/hosts or DNS.

These changes allow a client using the multi interfaces to avoid
blocking on name resolution if the interface loses its IP address or
disappears.

---
 lib/connect.c |   17 ++++++++++++++++-
 lib/if2ip.c   |   32 ++++++++++++++++++++++++++++++++
 lib/if2ip.h   |    1 +
 3 files changed, 49 insertions(+), 1 deletions(-)
diff --git a/lib/connect.c b/lib/connect.c
index 2a1876e..006e9f9 100644
--- a/lib/connect.c
+++ b/lib/connect.c
@@ -252,6 +252,10 @@ static CURLcode bindlocal(struct connectdata *conn,
   int error;
   char myhost[256] = "";
   int done = 0; /* -1 for error, 1 for address found */
+  int is_interface = FALSE;
+  int is_host = FALSE;
+  static const char *if_prefix = "if!";
+  static const char *host_prefix = "host!";
   /*************************************************************
    * Select device to bind socket to
@@ -263,9 +267,20 @@ static CURLcode bindlocal(struct connectdata *conn,
   memset(&sa, 0, sizeof(struct Curl_sockaddr_storage));
   if(dev && (strlen(dev)<255) ) {
+    if(strncmp(if_prefix, dev, strlen(if_prefix)) == 0) {
+      dev += strlen(if_prefix);
+      is_interface = TRUE;
+    }
+    else if(strncmp(host_prefix, dev, strlen(host_prefix)) == 0) {
+      dev += strlen(host_prefix);
+      is_host = TRUE;
+    }
     /* interface */
-    if(Curl_if2ip(af, dev, myhost, sizeof(myhost))) {
+    if(!is_host && (is_interface || Curl_if_is_interface_name(dev))) {
+      if(Curl_if2ip(af, dev, myhost, sizeof(myhost)) == NULL)
+        return CURLE_INTERFACE_FAILED;
+
       /*
        * We now have the numerical IP address in the 'myhost' buffer
        */
diff --git a/lib/if2ip.c b/lib/if2ip.c
index 4924f73..2e41fb5 100644
--- a/lib/if2ip.c
+++ b/lib/if2ip.c
@@ -71,6 +71,25 @@
 #if defined(HAVE_GETIFADDRS)
+bool Curl_if_is_interface_name(const char *interface)
+{
+  bool result = FALSE;
+
+  struct ifaddrs *iface, *head;
+  char *ip=NULL;
+
+  if(getifaddrs(&head) >= 0) {
+    for(iface=head; iface != NULL; iface=iface->ifa_next) {
+      if(curl_strequal(iface->ifa_name, interface)) {
+        result = TRUE;
+        break;
+      }
+    }
+    freeifaddrs(head);
+  }
+  return result;
+}
+
 char *Curl_if2ip(int af, const char *interface, char *buf, int buf_size)
 {
   struct ifaddrs *iface, *head;
@@ -109,6 +128,14 @@ char *Curl_if2ip(int af, const char *interface, char
*buf, int buf_size)
 #elif defined(HAVE_IOCTL_SIOCGIFADDR)
+bool Curl_if_is_interface_name(const char *interface)
+{
+  /* This is here just to support the old interfaces */
+  char buf[256];
+
+  return (Curl_if2ip(AF_INET, interface, buf, sizeof(buf)) != NULL);
+}
+
 char *Curl_if2ip(int af, const char *interface, char *buf, int buf_size)
 {
   struct ifreq req;
@@ -148,6 +175,11 @@ char *Curl_if2ip(int af, const char *interface, char
*buf, int buf_size)
 #else
+bool Curl_if_is_interface_name(const char *interface)
+{
+  return FALSE;
+}
+
 char *Curl_if2ip(int af, const char *interf, char *buf, int buf_size)
 {
     (void) af;
diff --git a/lib/if2ip.h b/lib/if2ip.h
index cdf2638..678e3a5 100644
--- a/lib/if2ip.h
+++ b/lib/if2ip.h
@@ -23,6 +23,7 @@
***************************************************************************/
 #include "setup.h"
+extern bool Curl_if_is_interface_name(const char *interface);
 extern char *Curl_if2ip(int af, const char *interf, char *buf, int
buf_size);
 #ifdef __INTERIX
-- 
1.7.3.1
--0016363b85f44a28d704b1084b2b
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">On Fri, Nov 4, 2011 at 6:45 PM, Daniel Stenberg <span dir=
=3D"ltr">&lt;<a href=3D"mailto:daniel_at_haxx.se" target=3D"_blank">daniel_at_hax=
x.se</a>&gt;</span> wrote:<br><div class=3D"gmail_quote"><blockquote class=
=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padd=
ing-left:1ex">
<div>On Fri, 4 Nov 2011, Jason Glasgow wrote:<br>
<br>
<blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p=
x #ccc solid;padding-left:1ex">
I think it would be clearer to just have two different options.<br>
</blockquote>
<br></div>
Clearer? Yes. Breaking old programs? Yes. Acceptable? No.</blockquote><div>=
Okay. =A0How about the patch below?</div><blockquote class=3D"gmail_quote" =
style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div>
<br>
<blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p=
x #ccc solid;padding-left:1ex">
- CURLOPT_INTERFACE which would only refer to an interface name<br>
</blockquote>
<br></div>
Right, but it has been documented to accept a host name as well for about 2=
00 years. Changing this now will break programs. We can&#39;t do this.<br>
<br>
We would have to add two new options.<div><br>
<br>
<blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p=
x #ccc solid;padding-left:1ex">
I do admit that this would require rework of some clients -- but I think th=
is is reasonable because the two are distinct arguments, and clients may ha=
ve to be rewritten anyway if they want to add the if! or host! prefix.<br>
</blockquote>
<br></div>
Adding prefixes will fix programs using the future libcurl version but all =
existing program will remain working the same, and I find that a much stron=
ger argument...</blockquote><div>I have added if! and host! prefix processi=
ng and below is a refined patch.</div>
<div><br></div><div>Comments welcome.</div><div><br></div><div><br></div><d=
iv><div>From 65f2614b4599c4847b4fbfddc38dfd9c71f43cfd Mon Sep 17 00:00:00 2=
001</div><div>From: Jason Glasgow &lt;<a href=3D"mailto:jglasgow_at_chromium.o=
rg">jglasgow_at_chromium.org</a>&gt;</div>
<div>Date: Fri, 4 Nov 2011 16:48:05 -0400</div><div>Subject: [PATCH] Do not=
 try to resolve interfaces names via DNS</div><div><br></div><div>Do not tr=
y to resolve interfaces names via DNS by recognizing</div><div>interface na=
mes in a few ways. =A0If the interface option argument has a</div>
<div>prefix of &quot;if!&quot; then treat the argument as only an interface=
.</div><div>Similarly, if the interface argument is the name of an interfac=
e (even</div><div>if it does not have an IP address assigned), treat it as =
an interface</div>
<div>name. =A0Finally, if the interface argument is prefixed by &quot;host!=
&quot; treat</div><div>it as a hostname that must be resolved by /etc/hosts=
 or DNS.</div><div><br></div><div>These changes allow a client using the mu=
lti interfaces to avoid</div>
<div>blocking on name resolution if the interface loses its IP address or</=
div><div>disappears.</div><div>---</div><div>=A0lib/connect.c | =A0 17 ++++=
++++++++++++-</div><div>=A0lib/if2ip.c =A0 | =A0 32 +++++++++++++++++++++++=
+++++++++</div>
<div>=A0lib/if2ip.h =A0 | =A0 =A01 +</div><div>=A03 files changed, 49 inser=
tions(+), 1 deletions(-)</div><div><br></div><div>diff --git a/lib/connect.=
c b/lib/connect.c</div><div>index 2a1876e..006e9f9 100644</div><div>--- a/l=
ib/connect.c</div>
<div>+++ b/lib/connect.c</div><div>@@ -252,6 +252,10 @@ static CURLcode bin=
dlocal(struct connectdata *conn,</div><div>=A0 =A0int error;</div><div>=A0 =
=A0char myhost[256] =3D &quot;&quot;;</div><div>=A0 =A0int done =3D 0; /* -=
1 for error, 1 for address found */</div>
<div>+ =A0int is_interface =3D FALSE;</div><div>+ =A0int is_host =3D FALSE;=
</div><div>+ =A0static const char *if_prefix =3D &quot;if!&quot;;</div><div=
>+ =A0static const char *host_prefix =3D &quot;host!&quot;;</div><div>=A0</=
div><div>=A0 =A0/**********************************************************=
***</div>
<div>=A0 =A0 * Select device to bind socket to</div><div>@@ -263,9 +267,20 =
@@ static CURLcode bindlocal(struct connectdata *conn,</div><div>=A0 =A0mem=
set(&amp;sa, 0, sizeof(struct Curl_sockaddr_storage));</div><div>=A0</div><=
div>=A0 =A0if(dev &amp;&amp; (strlen(dev)&lt;255) ) {</div>
<div>+ =A0 =A0if(strncmp(if_prefix, dev, strlen(if_prefix)) =3D=3D 0) {</di=
v><div>+ =A0 =A0 =A0dev +=3D strlen(if_prefix);</div><div>+ =A0 =A0 =A0is_i=
nterface =3D TRUE;</div><div>+ =A0 =A0}</div><div>+ =A0 =A0else if(strncmp(=
host_prefix, dev, strlen(host_prefix)) =3D=3D 0) {</div>
<div>+ =A0 =A0 =A0dev +=3D strlen(host_prefix);</div><div>+ =A0 =A0 =A0is_h=
ost =3D TRUE;</div><div>+ =A0 =A0}</div><div>=A0</div><div>=A0 =A0 =A0/* in=
terface */</div><div>- =A0 =A0if(Curl_if2ip(af, dev, myhost, sizeof(myhost)=
)) {</div><div>+ =A0 =A0if(!is_host &amp;&amp; (is_interface || Curl_if_is_=
interface_name(dev))) {</div>
<div>+ =A0 =A0 =A0if(Curl_if2ip(af, dev, myhost, sizeof(myhost)) =3D=3D NUL=
L)</div><div>+ =A0 =A0 =A0 =A0return CURLE_INTERFACE_FAILED;</div><div>+</d=
iv><div>=A0 =A0 =A0 =A0/*</div><div>=A0 =A0 =A0 =A0 * We now have the numer=
ical IP address in the &#39;myhost&#39; buffer</div>
<div>=A0 =A0 =A0 =A0 */</div><div>diff --git a/lib/if2ip.c b/lib/if2ip.c</d=
iv><div>index 4924f73..2e41fb5 100644</div><div>--- a/lib/if2ip.c</div><div=
>+++ b/lib/if2ip.c</div><div>@@ -71,6 +71,25 @@</div><div>=A0</div><div>=A0=
#if defined(HAVE_GETIFADDRS)</div>
<div>=A0</div><div>+bool Curl_if_is_interface_name(const char *interface)</=
div><div>+{</div><div>+ =A0bool result =3D FALSE;</div><div>+</div><div>+ =
=A0struct ifaddrs *iface, *head;</div><div>+ =A0char *ip=3DNULL;</div><div>=
+</div><div>
+ =A0if(getifaddrs(&amp;head) &gt;=3D 0) {</div><div>+ =A0 =A0for(iface=3Dh=
ead; iface !=3D NULL; iface=3Diface-&gt;ifa_next) {</div><div>+ =A0 =A0 =A0=
if(curl_strequal(iface-&gt;ifa_name, interface)) {</div><div>+ =A0 =A0 =A0 =
=A0result =3D TRUE;</div><div>
+ =A0 =A0 =A0 =A0break;</div><div>+ =A0 =A0 =A0}</div><div>+ =A0 =A0}</div>=
<div>+ =A0 =A0freeifaddrs(head);</div><div>+ =A0}</div><div>+ =A0return res=
ult;</div><div>+}</div><div>+</div><div>=A0char *Curl_if2ip(int af, const c=
har *interface, char *buf, int buf_size)</div>
<div>=A0{</div><div>=A0 =A0struct ifaddrs *iface, *head;</div><div>@@ -109,=
6 +128,14 @@ char *Curl_if2ip(int af, const char *interface, char *buf, int=
 buf_size)</div><div>=A0</div><div>=A0#elif defined(HAVE_IOCTL_SIOCGIFADDR)=
</div>
<div>=A0</div><div>+bool Curl_if_is_interface_name(const char *interface)</=
div><div>+{</div><div>+ =A0/* This is here just to support the old interfac=
es */</div><div>+ =A0char buf[256];</div><div>+</div><div>+ =A0return (Curl=
_if2ip(AF_INET, interface, buf, sizeof(buf)) !=3D NULL);</div>
<div>+}</div><div>+</div><div>=A0char *Curl_if2ip(int af, const char *inter=
face, char *buf, int buf_size)</div><div>=A0{</div><div>=A0 =A0struct ifreq=
 req;</div><div>@@ -148,6 +175,11 @@ char *Curl_if2ip(int af, const char *i=
nterface, char *buf, int buf_size)</div>
<div>=A0</div><div>=A0#else</div><div>=A0</div><div>+bool Curl_if_is_interf=
ace_name(const char *interface)</div><div>+{</div><div>+ =A0return FALSE;</=
div><div>+}</div><div>+</div><div>=A0char *Curl_if2ip(int af, const char *i=
nterf, char *buf, int buf_size)</div>
<div>=A0{</div><div>=A0 =A0 =A0(void) af;</div><div>diff --git a/lib/if2ip.=
h b/lib/if2ip.h</div><div>index cdf2638..678e3a5 100644</div><div>--- a/lib=
/if2ip.h</div><div>+++ b/lib/if2ip.h</div><div>@@ -23,6 +23,7 @@</div><div>=
=A0 ***********************************************************************=
****/</div>
<div>=A0#include &quot;setup.h&quot;</div><div>=A0</div><div>+extern bool C=
url_if_is_interface_name(const char *interface);</div><div>=A0extern char *=
Curl_if2ip(int af, const char *interf, char *buf, int buf_size);</div><div>=
=A0</div>
<div>=A0#ifdef __INTERIX</div><div>--=A0</div><div>1.7.3.1</div></div><div>=
<br></div></div></div>
--0016363b85f44a28d704b1084b2b--
--===============1560777914==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette:  http://curl.haxx.se/mail/etiquette.html
--===============1560777914==--
Received on 2001-09-17