From 629943cd7b855971df47cbcbabc7bc9535d0aa78 Mon Sep 17 00:00:00 2001
From: Jason Glasgow <jglasgow@chromium.org>
Date: Fri, 4 Nov 2011 16:48:05 -0400
Subject: [PATCH] Do not try to resolve interfaces names via DNS

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.
---
 docs/libcurl/curl_easy_setopt.3 |    9 ++++++++-
 lib/connect.c                   |   17 ++++++++++++++++-
 lib/if2ip.c                     |   31 +++++++++++++++++++++++++++++++
 lib/if2ip.h                     |    1 +
 4 files changed, 56 insertions(+), 2 deletions(-)

diff --git a/docs/libcurl/curl_easy_setopt.3 b/docs/libcurl/curl_easy_setopt.3
index 7cf9a04..1b0c9b6 100644
--- a/docs/libcurl/curl_easy_setopt.3
+++ b/docs/libcurl/curl_easy_setopt.3
@@ -842,7 +842,14 @@ negotiation. (Added in 7.19.4).
 .IP CURLOPT_INTERFACE
 Pass a char * as parameter. This sets the interface name to use as outgoing
 network interface. The name can be an interface name, an IP address, or a host
-name.
+name.  If the parameter starts with "if!" then it is treated as only as
+interface name and no attempt will ever be named to do treat it as an
+IP address or to do name resolution on it.  If the parameter starts
+with "host!" it is treated as either an IP address or a hostname.
+Hostnames are resolved synchronously.  Using the if! format is highly
+recommended when using the multi interfaces to avoid allowing the code
+to block.  If "if!" is specified but the parameter does not match an
+existing interface, CURLE_INTERFACE_FAILED is returned.
 .IP CURLOPT_LOCALPORT
 Pass a long. This sets the local port number of the socket used for
 connection. This can be used in combination with \fICURLOPT_INTERFACE\fP and
diff --git a/lib/connect.c b/lib/connect.c
index af225ab..906fd9d 100644
--- a/lib/connect.c
+++ b/lib/connect.c
@@ -240,6 +240,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
@@ -251,9 +255,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..0ae375b 100644
--- a/lib/if2ip.c
+++ b/lib/if2ip.c
@@ -71,6 +71,24 @@
 
 #if defined(HAVE_GETIFADDRS)
 
+bool Curl_if_is_interface_name(const char *interface)
+{
+  bool result = FALSE;
+
+  struct ifaddrs *iface, *head;
+
+  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 +127,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 +174,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

