[Slim-Checkins] r10134 - in /trunk/server: Changelog6.html Slim/Utils/IPDetect.pm

dsully at svn.slimdevices.com dsully at svn.slimdevices.com
Mon Oct 2 16:03:31 PDT 2006


Author: dsully
Date: Mon Oct  2 16:03:29 2006
New Revision: 10134

URL: http://svn.slimdevices.com?rev=10134&view=rev
Log:
Bug: 4186
Description: Use a UDP socket without a timeout to try and find the local IP address. Fallback to 127.0.0.1

Modified:
    trunk/server/Changelog6.html
    trunk/server/Slim/Utils/IPDetect.pm

Modified: trunk/server/Changelog6.html
URL: http://svn.slimdevices.com/trunk/server/Changelog6.html?rev=10134&r1=10133&r2=10134&view=diff
==============================================================================
--- trunk/server/Changelog6.html (original)
+++ trunk/server/Changelog6.html Mon Oct  2 16:03:29 2006
@@ -20,6 +20,7 @@
 		<li><a href="http://bugs.slimdevices.com/show_bug.cgi?id=4173">#4173</a> - press right to add to favorites option disappears once you've used it, until you restart server</li>
 		<li><a href="http://bugs.slimdevices.com/show_bug.cgi?id=4173">#4178</a> - Live365 stations don't have a note symbol</li>
  		<li><a href="http://bugs.slimdevices.com/show_bug.cgi?id=4181">#4181</a> - Softsqueeze refuses connection to Squeezenetwork</li>
+		<li><a href="http://bugs.slimdevices.com/show_bug.cgi?id=4186">#4186</a> - webinterface (server settings) not responding because of denied connection to www.google.com</li>
 		<li><a href="http://bugs.slimdevices.com/show_bug.cgi?id=4189">#4189</a> - mp3 file playback gets truncated</li>
 		<li><a href="http://bugs.slimdevices.com/show_bug.cgi?id=4191">#4191</a> - Live365 Search has missing strings</li>
 		<li><a href="http://bugs.slimdevices.com/show_bug.cgi?id=4192">#4192</a> - IE doesn't refresh status</li>

Modified: trunk/server/Slim/Utils/IPDetect.pm
URL: http://svn.slimdevices.com/trunk/server/Slim/Utils/IPDetect.pm?rev=10134&r1=10133&r2=10134&view=diff
==============================================================================
--- trunk/server/Slim/Utils/IPDetect.pm (original)
+++ trunk/server/Slim/Utils/IPDetect.pm Mon Oct  2 16:03:29 2006
@@ -8,10 +8,12 @@
 # version 2.
 
 use strict;
-use IO::Socket;
+use Socket;
+use Symbol;
 use Slim::Utils::Misc;
 
 my $detectedIP = undef;
+my $localhost  = '127.0.0.1';
 
 =head1 NAME
 
@@ -48,24 +50,73 @@
 
 sub _init {
 
-	my $server = 'www.google.com:80';
+	if ($detectedIP) {
+		return;
+	}
 
-	if (!$detectedIP) {
+	# This code used to try and connect to www.google.com:80 in order to
+	# find the local IP address.
+	# 
+	# Thanks to trick from Bill Fenner, trying to use a UDP socket won't
+	# send any packets out over the network, but will cause the routing
+	# table to do a lookup, so we can find our address. Don't use a high
+	# level abstraction like IO::Socket, as it dies when connect() fails.
+	#
+	# time.nist.gov - though it doesn't really matter.
+	my $raddr = '192.43.244.18';
+	my $rport = 123;
 
-		my $socket = IO::Socket::INET->new(
-			'PeerAddr'  => $server,
-			'LocalAddr' => $main::localClientNetAddr,
-		) or do {
+	my $proto = (getprotobyname('udp'))[2];
+	my $pname = (getprotobynumber($proto))[0];
+	my $sock  = Symbol::gensym();
 
-			errorMsg("Failed to detect server IP address. $!\n");
+	my $iaddr = inet_aton($raddr) || do {
+
+		msg("Warning: Couldn't call inet_aton($raddr) - falling back to $localhost\n");
+
+		$detectedIP = $localhost;
+
+		return;
+	};
+
+	my $paddr = sockaddr_in($rport, $iaddr);
+
+	socket($sock, PF_INET, SOCK_DGRAM, $proto) || do {
+
+		msg("Warning: Couldn't call socket(PF_INET, SOCK_DGRAM, \$proto) - falling back to $localhost\n");
+
+		$detectedIP = $localhost;
+
+		return;
+	};
+
+	if ($main::localClientNetAddr && $main::localClientNetAddr =~ /^[\d\.]+$/) {
+
+		my $laddr = inet_aton($main::localClientNetAddr) || INADDR_ANY;
+
+		bind($sock, pack_sockaddr_in(0, $laddr)) or do {
+
+			msg("Warning: Couldn't call bind(pack_sockaddr_in(0, \$laddr) - falling back to $localhost\n");
+
+			$detectedIP = $localhost;
+
 			return;
 		};
+	}
 
-		# Find my half of the connection
-		my ($port, $address) = sockaddr_in( (getsockname($socket))[0] );
+	connect($sock, $paddr) || do {
 
-		$detectedIP = inet_ntoa($address);
-	}
+		msg("Warning: Couldn't call connect() - falling back to $localhost\n");
+
+		$detectedIP = $localhost;
+
+		return;
+	};
+
+	# Find my half of the connection
+	my ($port, $address) = sockaddr_in( (getsockname($sock))[0] );
+
+	$detectedIP = inet_ntoa($address);
 }
 
 1;



More information about the checkins mailing list