bug-commoncpp
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Binding to source address for client connections


From: Anand Narwani
Subject: Re: Binding to source address for client connections
Date: Fri, 8 Feb 2002 14:04:55 -0800 (PST)

David,
I have attached the patches to this email.  Please let
me know if I need to resend as plain text in an email.

Anand
--- David Sugar <address@hidden> wrote:
> 
> If you would like to submit a patch for this, I
> would be happy to look at
> it and possibly include it...
> 
> David
> 
> On Fri, 8 Feb 2002, Anand Narwani wrote:
> 
> > Hello,
> > I have a requirement to be able to bind a
> URLStream to
> > a source interface when making connections to
> remote
> > servers.  This allows me to control the source
> address
> > used for the TCP session.
> > I have since modified the URLStream class to allow
> me
> > to do this.  Essentially, I modified the
> > sendHTTPHeaders method to bind to a source
> interface
> > if it has been set.
> > Are there any plans to include a feature of this
> kind
> > into the main releases?
> >
> > Thanks in advance,
> > Anand Narwani
> >
> > __________________________________________________
> > Do You Yahoo!?
> > Send FREE Valentine eCards with Yahoo! Greetings!
> > http://greetings.yahoo.com
> >
> > _______________________________________________
> > Bug-commoncpp mailing list
> > address@hidden
> > http://mail.gnu.org/mailman/listinfo/bug-commoncpp
> >
> 


__________________________________________________
Do You Yahoo!?
Send FREE Valentine eCards with Yahoo! Greetings!
http://greetings.yahoo.com
--- posix/url.cpp       Mon Oct 29 07:31:18 2001
+++ posix/url.cpp       Thu Feb  7 15:16:34 2002
@@ -72,6 +72,7 @@
        encoding = URL_BINARY_ENCODING;
        auth = URL_ANONYMOUS_AUTH;
        cookie = agent = pragma = referer = user = password = NULL;
+       localif = NULL;
        setError(false);
 }
 
@@ -253,6 +254,7 @@
        if(Socket::state != SOCKET_AVAILABLE)
                Close();
 
+       
        if(!strnicmp(path, "file:", 5))
        {
                urlmethod = FILE_GET_METHOD;
@@ -479,6 +481,33 @@
 
         if(Socket::state != SOCKET_AVAILABLE)
                         Close();
+       if (localif != NULL) {
+               struct ifreq ifr;
+               int alen = sizeof(source);
+
+               memset(&ifr, 0, sizeof(ifr));
+               strncpy(ifr.ifr_name, localif, 15);
+               if (ioctl(so, SIOCGIFINDEX, &ifr) < 0) {
+                       cerr << "Warning Unknown interface!" << endl;
+               } else {
+                       if (setsockopt(so, 
+                               SOL_SOCKET, 
+                               SO_BINDTODEVICE, 
+                               &ifr,
+                               sizeof(ifr)) == -1) {
+                                       cerr << "Ignoring Interface!" <<endl;
+                       } else if (getsockname(so, (struct 
sockaddr*)&source,(socklen_t *) &alen) == -1) {
+                               cerr << "getsockname error!" << endl;
+                       } else if (bind(so, (struct sockaddr*)&source, 
sizeof(source)) == -1) {
+                                       cerr << "Bind Error!" << endl;
+                       } else {
+                                       source.sin_port = 0;
+                       }
+                       
+
+               }
+
+       }
 
        if(proxyPort)
                Connect(proxyHost, proxyPort, bufsize);
--- posix/url.h Thu Oct  4 15:28:22 2001
+++ posix/url.h Fri Feb  1 08:00:43 2002
@@ -44,6 +44,8 @@
 #ifndef __CCXX_SOCKET_H__
 #include <cc++/socket.h>
 #endif
+#include <net/if.h>
+#include <sys/ioctl.h>
 
 typedef enum
 {
@@ -100,6 +102,8 @@
 {
 private:
        const char *agent, *referer, *cookie, *pragma, *user, *password;
+       const char *localif;
+       InetHostAddress localAddr;
        InetHostAddress proxyHost, prevHost;
        tpport_t proxyPort, prevPort;
        InetHostAddress refhost;
@@ -112,6 +116,7 @@
        bool follow;
        unsigned chunk;
        char authbuf[128];
+       struct sockaddr_in source;
 
        urlerror_t getHTTPHeaders(char *buffer, unsigned bufsize);
 
@@ -282,6 +287,13 @@
         */
        inline void setProtocol(urlprotocol_t pro)
                {protocol = pro;};
+       /**
+        * Specify local interface to use
+        *
+        * @param Local interface name
+        */
+       inline void setLocalInterface(const char *intf) 
+       {localif=intf;} 
 };
 
 extern __EXPORT char *urldecode(char *source, char *dest = NULL);

reply via email to

[Prev in Thread] Current Thread [Next in Thread]