| gabriel rosenkoetter on Tue, 24 Dec 2002 15:10:34 -0500 |
|
On Tue, Dec 24, 2002 at 02:14:20PM -0500, Michael C. Toren wrote:
> # mct, Tue Dec 24 13:06:10 EST 2002
> use Socket;
> my $host = shift || "207.106.1.2";
> my $udp = getprotobyname("udp") or die "getprotobyname: $!\n";
> socket(S, PF_INET, SOCK_DGRAM, $udp) or die "socket: $!\n";
> my $sin = sockaddr_in 42, inet_aton $host;
> connect S, $sin or die "connect: $!\n";
> print inet_ntoa((sockaddr_in getsockname S)[1]), "\n";
As should be predictable, this falls flat on its face on multi-homed
hosts:
grappa:~/bin% ifconfig -a | grep -A4 ^sf
sf0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 1500
address: 00:00:d1:ec:9c:35
media: Ethernet autoselect (100baseTX full-duplex)
status: active
inet 66.92.234.100 netmask 0xffffff00 broadcast 66.92.234.255
--
sf1: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 1500
address: 00:00:d1:ec:9c:36
media: Ethernet autoselect (100baseTX full-duplex)
status: active
inet 10.0.0.1 netmask 0xff000000 broadcast 10.0.0.255
--
sf2: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 1500
address: 00:00:d1:ec:9c:37
media: Ethernet autoselect (100baseTX full-duplex)
status: active
inet 192.168.0.4 netmask 0xffffff00 broadcast 192.168.0.255
--
sf3: flags=8802<BROADCAST,SIMPLEX,MULTICAST> mtu 1500
address: 00:00:d1:ec:9c:38
media: Ethernet autoselect (none)
status: no carrier
lo0: flags=8009<UP,LOOPBACK,MULTICAST> mtu 33220
grappa:~% ip.pl
66.92.234.100
grappa:~% ip.pl 66.92.234.98
66.92.234.98
grappa:~% ip.pl 10.0.0.234
10.0.0.1
grappa:~% ip.pl 192.168.0.3
192.168.0.4
(Well, flat on its face in the "telling you the local IP address"
department... it is doing exactly what it's asked, but not the best
it could. Istr a way to iterate through the interfaces in C... can't
you get at that through Socket.pm? I'm in the middle of something
else right now, but I'll go check when I'm done.)
Btw, mct, cute choice on the default remote host. I guess it can
probably take the load.
> [1] Successfully tested on:
> - Linux quint 2.4.19 #1 Sun Aug 4 01:14:19 EDT 2002 sparc64 unknown
> - Linux antarctica 2.4.19 #1 Tue Oct 29 21:33:52 EST 2002 i686 unknown
> - FreeBSD x-wing 4.6.2-RELEASE-p3 FreeBSD 4.6.2-RELEASE-p3 #1
> - SunOS access 4.1.4 1 sun4m
> - SunOS postal 5.8 Generic_108528-06 sun4u sparc SUNW,Ultra-1
Works fine on:
NetBSD uriel 1.5.3_ALPHA NetBSD 1.5.3_ALPHA (URIEL) #2: Wed Jan 16 07:12:29 EST 2002 gr@uriel.eclipsed.net:/usr/src/sys/arch/macppc/compile/URIEL macppc
NetBSD grappa 1.6F NetBSD 1.6F (GRAPPA) #8: Thu Aug 15 00:54:54 EDT 2002 gr@grappa:/mp3/tmp/netbsd/src/sys/arch/i386/compile/GRAPPA i386
Can't so much check NeXTStep (don't have a C compiler, and
bootstrapping gcc would only take about a week ;^>) or Irix (don't
have a hard drive... whoops). But if Perl behaves and the system's
POSIX, that code should be fine.
--
gabriel rosenkoetter
gr@eclipsed.net
Attachment:
pgpH0PBc4aIGL.pgp
|
|