in reply to LWP and LocalIP/LocalPort
So I've done some tests.
Set something listening on a port:
Try to connect to it:perl -mHTTP::Daemon -e '$d=new HTTP::Daemon(LocalPort=>12345);while ($ +c=$d->accept){sleep 10;$c->send_file_response("index.html")}'
Check netstat -aperl -e 'use LWP::Simple;@LWP::Protocol::http::EXTRA_SOCK_OPTS = (Loca +lAddr=>"localhost:54321");get("http://localhost:12345")'
So we can use a string in the form 'x.x.x.x:port' with LocalAddrtcp 71 0 mondas:12345 mondas:51575 ES +TABLISHED
http://jonallen.info/cgi/delay.pl will wait 10 seconds, then print "Hello, World!" (to give me time to run netstat and set up processes on different machines).
We know now that specifying an local port number works. What would happen if, under high load, your server tried to send several XML 'pings' at once?
At the same time, try to access a different remote server using the same local port[jj@mondas jj]$ perl -le 'use LWP::Simple;@LWP::Protocol::http::EXTRA_ +SOCK_OPTS = (LocalAddr=>"192.168.1.1:54321");getprint("http://jonalle +n.info/cgi/delay.pl");' Hello, World!
So if you are going to restrict the outgoing port numbers, be aware that in some conditions the local port you need will be in use. If you *really* need to do this, set up a range of ports and have your program try to use each one in turn and only fail if they are all in use.[jj@mondas jj]$ perl -e 'use LWP::Simple;@LWP::Protocol::http::EXTRA_S +OCK_OPTS = (LocalAddr=>"192.168.1.1:54321");getprint("http://www.goog +le.com");' 500 Can't connect to www.google.com:80 (Cannot assign requested addres +s) <URL:http://www.google.com>
Machine has 192.168.1.10 aliased to 192.168.1.1
Start Apache as an unprivileged user (will bind to port 8080).[root@mondas log]# ifconfig eth0 Link encap:Ethernet HWaddr 52:54:05:C5:13:7A inet addr:192.168.1.1 Bcast:192.168.1.255 Mask:255.255.255 +.0 EtherTalk Phase 2 addr:65280/97 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:27996973 errors:0 dropped:2 overruns:0 frame:6 TX packets:32099772 errors:484 dropped:0 overruns:0 carrier: +968 collisions:146707 RX bytes:3038904039 (2898.1 Mb) TX bytes:4085277291 (3896.0 + Mb) eth0:0 Link encap:Ethernet HWaddr 52:54:05:C5:13:7A inet addr:192.168.1.10 Bcast:192.168.1.255 Mask:255.255.25 +5.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 EtherTalk Phase 2 addr:0/0 UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:17172 errors:0 dropped:0 overruns:0 frame:0 TX packets:17172 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 RX bytes:4381193 (4.1 Mb) TX bytes:4381193 (4.1 Mb)
You can of course use the Listen and BindAddress directives in httpd.conf to force Apache to use only a certain IP alias. However the default configuration will bind to every IP address, so it is worth checking with netstat that nothing else is using the local port that you want to use.[jj@mondas jj]$ perl -e 'use LWP::Simple;@LWP::Protocol::http::EXTRA_S +OCK_OPTS = (LocalAddr=>"192.168.1.1:8080");getprint("http://jonallen. +info/cgi/delay.pl");' 500 Can't connect to jonallen.info:80 (Address already in use) <URL:ht +tp://jonallen.info/cgi/delay.pl> [jj@mondas jj]$ perl -e 'use LWP::Simple;@LWP::Protocol::http::EXTRA_S +OCK_OPTS = (LocalAddr=>"192.168.1.10:8080");getprint("http://jonallen +.info/cgi/delay.pl");' 500 Can't connect to jonallen.info:80 (Address already in use) <URL:ht +tp://jonallen.info/cgi/delay.pl>
Start the HTTP::Daemon one-liner (on 192.168.1.1). From another computer, connect to this daemon with a local port of 11111
Then (at the same time) from the machine running HTTP::Daemon, try to use the local port 11111:perl -e 'use LWP::Simple;@LWP::Protocol::http::EXTRA_SOCK_OPTS = (Loca +lAddr=>"192.168.1.8:11111");getprint("http://192.168.1.1:12345");'
Check netstat on 192.168.1.1perl -e 'use LWP::Simple;@LWP::Protocol::http::EXTRA_SOCK_OPTS = (Loca +lAddr=>"192.168.1.1:11111");getprint("http://jonallen.info/cgi/delay. +pl");'
So incoming connections do not restrict what ports you can use for outgoing connections.tcp 0 0 mondas.coldsoluti:11111 212.69.202.117:http ES +TABLISHED tcp 73 0 mondas.coldsoluti:12345 lclus1.coldsoluti:11111 ES +TABLISHED
This leads me to the conclusion that your code is fine, and as long as nothing else is listening on port 8080, it should work. Note however that if you try these sort of examples over the Internet, caches and proxies run by ISPs will change the local port numbers. So if machine 'A' opens a connection with a destination address of machine 'B' and a local port '22222', you cannot guarantee that when machine 'B' accepts the connection the remote port will be '22222'.
I would suggest that you disable your firewall (at least for outgoing connections), try your code again, and use netstat to find out exactly what it is doing.
JJ
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Re: LWP and LocalIP/LocalPort
by jk2addict (Chaplain) on Oct 16, 2002 at 20:40 UTC | |
by jj808 (Hermit) on Oct 16, 2002 at 21:32 UTC | |
by jk2addict (Chaplain) on Oct 17, 2002 at 01:05 UTC |