cURL / Mailing Lists / curl-library / Single Mail

curl-library

[Fwd: Patch for SO_BINDTODEVICE]

From: Ben Greear <greearb_at_candelatech.com>
Date: Wed, 11 Feb 2004 15:01:33 -0800

-------- Original Message --------
Subject: Patch for SO_BINDTODEVICE
Date: Wed, 11 Feb 2004 14:47:14 -0800
From: Ben Greear <greearb_at_candelatech.com>
Organization: Candela Technologies
To: CURL Library-List <curl-library_at_lists.sourceforge.net>

Hello!

I have a patch here that should allow CURL to bind more
tightly to an interface. It's use may be somewhat limited
in scope, but it does fix a problem for me :)

This patch is against 7.10.4

Thanks,
Ben

RCS file: /home/cvs_user/btbits/btbits/l4libs/curl-7.10.4/lib/connect.c,v
retrieving revision 1.1
diff -u -r1.1 connect.c
--- lib/connect.c 5 Apr 2003 03:59:17 -0000 1.1
+++ lib/connect.c 11 Feb 2004 22:43:38 -0000
@@ -213,12 +213,14 @@
      size_t size;
      char myhost[256] = "";
      in_addr_t in;
-
+ int was_iface = 0;
+
      if(Curl_if2ip(data->set.device, myhost, sizeof(myhost))) {
        /*
         * We now have the numerical IPv4-style x.y.z.w in the 'myhost' buffer
         */
        h = Curl_resolv(data, myhost, 0);
+ was_iface = 1;
      }
      else {
        if(strlen(data->set.device)>1) {
@@ -246,6 +248,36 @@

      infof(data, "We bind local end to %s\n", myhost);

+
+#ifdef __linux__
+/* #warning "Compiling for SO_BINDTODEVICE.." */
+ /* I am not sure any other OSs provide this feature, and at the least
+ * I cannot test. --Ben
+ *
+ * This feature allows one to tightly bind the local socket to a particular
+ * interface. This will force even requests to other local interfaces to go
+ * out the external interface.
+ *
+ */
+ if (was_iface) {
+ /* Only bind to the interface when specified as interface, not just as a
+ * hostname or ip address.
+ */
+ if (setsockopt(sockfd, SOL_SOCKET, SO_BINDTODEVICE,
+ data->set.device, strlen(data->set.device + 1)) != 0) {
+ /* printf("Failed to BINDTODEVICE, socket: %d device: %s error: %s\n",
+ sockfd, data->set.device, strerror(errno)); */
+ failf(data, "SO_BINDTODEVICE failed");
+ }
+ else {
+ /* printf("Successfully bound to device: %s\n", data->set.device); */
+ }
+ }
+ else {
+ /* printf("We were not requested to bind to an interface: %s\n", data->set.device); */
+ }
+#endif
+
      in=inet_addr(myhost);
      if (INADDR_NONE != in) {

@@ -254,7 +286,7 @@

          Curl_resolv_unlock(data, h);
          /* we don't need it anymore after this function has returned */
-
+
  #ifdef ENABLE_IPV6
          (void)sa; /* prevent compiler warning */
          if( bind(sockfd, addr->ai_addr, addr->ai_addrlen) >= 0) {
@@ -287,6 +319,7 @@
            }
          }
  #endif
+
          else {
            switch(errno) {
            case EBADF:

-- 
Ben Greear <greearb_at_candelatech.com>
Candela Technologies Inc  http://www.candelatech.com
-- 
Ben Greear <greearb_at_candelatech.com>
Candela Technologies Inc  http://www.candelatech.com
Received on 2004-02-12