Re: [gnet-dev] [PATCH] bind outgoing connections to local interface



This is a common request and I still don't understand why it's needed. 
The OS should be able to correctly choose which interface to bind to. 
Is there some application for this that I'm missing?

David


On Fri, 2003-12-05 at 05:25, Michael Alger wrote:
> Attached is a patch (against the CVS version) that adds the ability to
> bind outgoing connections from a GTcpSocket to a specific interface.
> 
> I've just implemented what I needed for my own application, so the API
> is not yet quite complete.  My program uses gnet_conn_new(), so that's
> where I started from.  I'll add the missing calls when/if this part of
> the modification is accepted (I'm not convinced I picked the best name
> for the new functions, for one thing).
> 
> I've implemented it by adding new functions with "_bind_addr" appended
> to the old function name and an extra GInetAddr pointer which provides
> the local interface name to bind to.  The old functions are stubs that
> call the new _bind_addr functions with a NULL local address.  I used a
> GInetAddr for the local interface name so I didn't have to do an async
> lookup on that in case the user provides a hostname; it would probably
> be easier to use in many cases with a gchar* for the local address.
> 
> Functions added:
> 
>   gnet_conn_new_bind_addr()
>   gnet_tcp_socket_connect_async_bind_addr()
>   gnet_tcp_socket_new_async_bind_addr()
>   gnet_tcp_socket_new_async_direct_bind_addr()
> 
> It also adds a GInetAddr ptr, called 'localaddr' to:
> 
>   GTcpSocketAsyncState
>   GTcpSocketConnectState
> 
> This is cloned by some of the _bind_addr() functions and deleted when
> the state is freed.
> 
> The primary change is in _new_async_direct; instead of using socket()
> directly the socket is created by gnet_private_create_listen_socket()
> and then just before connecting calling bind() on the socket.  I took
> the code for this from _server_new_full().
> 
> Despite what the comments say, the port part of the GInetAddr used to
> set the local interface address is ignored.  I don't actually need to
> connect from a particular port and I couldn't figure out how to do it
> so I gave up on this part.
> 
> Example of the usage, taken from my program:
> 
>   {
>     GInetAddr *addr = gnet_inetaddr_new ("192.168.7.8", 0);
> 
>     connection = gnet_conn_new_bind_addr (host, port, addr,
>                                           IRCNetwork::gconn_callback,
>                                           this);
> 
>     gnet_inetaddr_unref	(addr);
>   }
> 
> There's two unrelated tweaks made by the patch, too:
> 
>   inetaddr.c: gnet_gethostbyname()
>   Call of inet_pton() was checked for success with != 0; I've changed
>   it to > 0, since the manpage says it can return a negative value if
>   an unsupported protocol family is specified.  While it's very, very
>   unlikely that that would actually happen, I was feeling pedantic.
> 
>   gnet-private.h
>   Two minor typos of "async" as "asymc" in the comments.
> 
> Mike.
> 
> ______________________________________________________________________
> _______________________________________________
> gnet-dev mailing list
> gnet-dev lists gnetlibrary org
> http://lists.gizmolabs.org/mailman/listinfo/gnet-dev
-- 
David Helder - dhelder gizmolabs org - http://www.gizmolabs.org/~dhelder




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