develooper Front page | perl.perl5.porters | Postings from October 1999

Re: Make IO::Socket::INET pick up LocalAddr from the ENVIRONMENT

Thread Previous | Thread Next
Gisle Aas
October 28, 1999 03:56
Re: Make IO::Socket::INET pick up LocalAddr from the ENVIRONMENT
Message ID:
Gurusamy Sarathy <> writes:

> On 26 Oct 1999 09:08:21 -0000, Gisle Aas wrote:
> >I have had several requests for a way to control the 'LocalAddr' used
> >for connections made by LWP.  I think the best way to deal with this
> >is to make is configurable at the IO::Socket::INET level.  In this
> >patch I have chosen to make it default from an environment variable.
> >Another approach could be to provide a class method or simply a global
> >variable like $IO::Socket::INET::DEFAULT_LOCAL_ADDR.  Any other
> >thoughts or suggestiong for a better environment variable name?
> I don't much like using %ENV for holding defaults for modules.

OK.  This is another version of my patch.  This time using a global.

> A global in the IO:: namespace is better, but it seems to me the default
> for LocalAddr should automatically be the most typical/reasonable one
> ("localhost"?), without people having to poke around globals.

If you don't specify a LocalAddr, then the socket will not be bound,
and the OS will automatically choose some interface based on the route
taken to reach the PeerAddr.  For hosts with multiple interfaces
LocalAddr can be used to choose which route you want.  The default
should still be not to bind the socket.


--- lib/IO/Socket/	Tue Oct 26 10:49:28 1999
+++ lib/IO/Socket/	Thu Oct 28 12:27:24 1999
@@ -14,7 +14,7 @@
 use Exporter;
 @ISA = qw(IO::Socket);
-$VERSION = "1.24";
+$VERSION = "1.25";
 IO::Socket::INET->register_domain( AF_INET );
@@ -23,6 +23,8 @@
 		    icmp => SOCK_RAW
+use vars qw($DEFAULT_LOCAL_ADDR);
 sub new {
     my $class = shift;
     unshift(@_, "PeerAddr") if @_ == 1;
@@ -93,6 +95,8 @@
     $arg->{LocalAddr} = $arg->{LocalHost}
 	if exists $arg->{LocalHost} && !exists $arg->{LocalAddr};
+    $arg->{LocalAddr} = $DEFAULT_LOCAL_ADDR
+        if defined($DEFAULT_LOCAL_ADDR) && !exists $arg->{LocalAddr};
     ($laddr,$lport,$proto) = _sock_info($arg->{LocalAddr},
@@ -267,7 +271,7 @@
 newly created symbol (see the C<Symbol> package). C<new>
 optionally takes arguments, these arguments are in key-value pairs.
-In addition to the key-value pairs accepted by L<IO::Socket>,
+In addition to the key-value pairs accepted by C<IO::Socket>,
 C<IO::Socket::INET> provides.
@@ -301,6 +305,13 @@
 The C<PeerPort> specification can also be embedded in the C<PeerAddr>
 by preceding it with a ":".
+The C<LocalAddr> specify which local interface to use and is specified
+using the same form as for C<PeerAddr>.  The address must belong to
+one of the interfaces of the host.  If the global variable
+$IO::Socket::INET::DEFAULT_LOCAL_ADDR is set, then this is used as the
+default value.  If neither the C<LocalAddr> parameter nor the global
+is set, then the socket will not be bound to any address.
 If C<Proto> is not given and you specify a symbolic C<PeerPort> port,
 then the constructor will try to derive C<Proto> from the service
 name.  As a last resort C<Proto> "tcp" is assumed.  The C<Type>
@@ -326,7 +337,7 @@
 As of VERSION 1.18 all IO::Socket objects have autoflush turned on
 by default. This was not the case with earlier releases.

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About