Browse Source

Fixes wrong use of 'inet_aton(..)' with IPv6 addresses.

From the Python docs:

  "inet_aton() does not support IPv6, and inet_pton() should
   be used instead for IPv4/v6 dual stack support."

Hence, if one specified an IPv6 address to bind to as command line para-
meter, the daemon always fall back to the default address '::', as the
exception thrown by 'inet_aton()' was interpreted as format error of the
specified address by the program.

To fix this, we now use 'inet_pton(..)' as suggested by the API docs and
terminate, if the specified bind-to string is not in the format if an IPv6
address. We only bind the service to '::' if an --bind-to parameter is not
present.

Signed-off-by: Stefan Laudemann <thisco@zitmail.uni-paderborn.de>
Stefan Laudemann 9 years ago
parent
commit
1af2158d28
1 changed files with 9 additions and 3 deletions
  1. 9 3
      server.py

+ 9 - 3
server.py

@@ -5,6 +5,7 @@ import socket
 import random, string
 import daemon, getopt, sys
 import daemon.pidlockfile
+import traceback
 import os
 
 def myrandom(length):
@@ -67,7 +68,7 @@ if __name__ == '__main__':
 
 	daemonize = True
 	port = 1337
-	bindTo = '::'
+	bindTo = None
 
 	for opt, arg in opts:
 		if opt in ("-d", "--do-not-daemonize"):
@@ -76,13 +77,18 @@ if __name__ == '__main__':
 			port = int(arg)
 		elif opt in ("-b", "--bind-to"):
 			try: 
-				socket.inet_aton(arg)
+				socket.inet_pton(socket.AF_INET6, arg)
 				bindTo = str(arg)
 			except:
-				print('Listening IP address is either invalid or unset. Using default ::')
+				traceback.print_exc()
+				sys.exit(1)
 		else:
 			assert False
 	
+	if bindTo == None:
+		print("Listening IP address is unset. Using default ::")
+		bindTo = "::"
+
 	if daemonize == False:
 		serve(port, bindTo)
 	else: