xen-devel.lists.xenproject.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] Add interface name definition support for xend-relocation-address
@ 2010-05-24 14:59 Michal Novotny
  0 siblings, 0 replies; only message in thread
From: Michal Novotny @ 2010-05-24 14:59 UTC (permalink / raw)
  To: 'xen-devel@lists.xensource.com'

[-- Attachment #1: Type: text/plain, Size: 996 bytes --]

Hi,
this is patch to add a new feature for xend-relocation-address option to 
support definition by interface name which can be useful for people 
having e.g. a cluster environment with multiple network interfaces on 
all of the machines with only one reserved to be registered to a private 
cluster network. This way they won't need to specify the relocation 
address manually on all the machines but just simple providing the 
interface name to get the IP address from would do the job (all the 
machines have to have this interface named the same to make it working, 
of course).

Technically it reads the interface name and gets it's IP address using 
ioctl call of SIOCGIFADDR and if the interface doesn't have the address, 
i.e. if non-existing interface or hostname was provided the original 
ifname is returned to preserve the old behaviour.

Signed-off-by: Michal Novotny <minovotn@redhat.com>

-- 
Michal Novotny<minovotn@redhat.com>, RHCE
Virtualization Team (xen userspace), Red Hat


[-- Attachment #2: xen-xend-config-relocation-address-ifname-support.patch --]
[-- Type: text/x-patch, Size: 4666 bytes --]

diff -r 93410e5e4ad8 tools/examples/xend-config.sxp
--- a/tools/examples/xend-config.sxp	Sat May 22 06:36:41 2010 +0100
+++ b/tools/examples/xend-config.sxp	Mon May 24 16:49:10 2010 +0200
@@ -110,6 +110,8 @@
 # Address xend should listen on for relocation-socket connections, if
 # xend-relocation-server is set.
 # Meaning and default as for xend-address above.
+# Also, interface name is allowed (e.g. eth0) there to get the
+# relocation address to be bound on.
 #(xend-relocation-address '')
 
 # The hosts allowed to talk to the relocation port.  If this is empty (the
diff -r 93410e5e4ad8 tools/python/xen/web/tcp.py
--- a/tools/python/xen/web/tcp.py	Sat May 22 06:36:41 2010 +0100
+++ b/tools/python/xen/web/tcp.py	Mon May 24 16:49:10 2010 +0200
@@ -21,6 +21,8 @@ import re
 import re
 import socket
 import time
+import fcntl     # For get_interface_addr
+import struct    # For get_interface_addr
 
 import connection
 
@@ -35,6 +37,49 @@ class TCPListener(connection.SocketListe
         self.hosts_allow = hosts_allow
         connection.SocketListener.__init__(self, protocol_class)
 
+    def isValidHex(self, word):
+        # If we have empty word we treat it as valid
+        if len(word) == 0:
+            return True
+        try:
+            int(word, 16)
+            return True
+        except ValueError:
+            return False
+
+    def isValidIP(self, ipaddr):
+        # Check for IPv4 address
+        numValid = 0
+        tmp = ipaddr.split('.')
+        for byte in tmp:
+            if byte.isdigit():
+                numValid += 1
+
+        if numValid == len(tmp):
+            return True
+
+        # Check for IPv6 address
+        numValid = 0
+        tmp = ipaddr.split(':')
+        for word in tmp:
+            if self.isValidHex(word):
+                numValid += 1
+
+        return numValid == len(tmp)
+
+    def getIfAddr(self, ifname):
+        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
+        try:
+            x = socket.inet_ntoa(fcntl.ioctl(
+                s.fileno(),
+                0x8915,  # SIOCGIFADDR
+                struct.pack('256s', ifname[:15])
+            )[20:24])
+            s.close()
+        except Exception, e:
+            x = ifname
+
+        return x
 
     def createSocket(self):
         sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
@@ -46,6 +91,9 @@ class TCPListener(connection.SocketListe
         timeout = time.time() + 30
         while True:
             try:
+                if not self.isValidIP(self.interface):
+                    self.interface = self.getIfAddr(self.interface)
+                log.debug("Listening on %s:%s" % (self.interface, self.port))
                 sock.bind((self.interface, self.port))
                 return sock
             except socket.error, (_errno, strerrno):
@@ -78,6 +126,49 @@ class SSLTCPListener(TCPListener):
 
         TCPListener.__init__(self, protocol_class, port, interface, hosts_allow)
 
+    def isValidHex(self, word):
+        # If we have empty word we treat it as valid
+        if len(word) == 0:
+            return True
+        try:
+            int(word, 16)
+            return True
+        except ValueError:
+            return False
+
+    def isValidIP(self, ipaddr):
+        # Check for IPv4 address
+        numValid = 0
+        tmp = ipaddr.split('.')
+        for byte in tmp:
+            if byte.isdigit():
+                numValid += 1
+
+        if numValid == len(tmp):
+            return True
+
+        # Check for IPv6 address
+        numValid = 0
+        tmp = ipaddr.split(':')
+        for word in tmp:
+            if self.isValidHex(word):
+                numValid += 1
+
+        return numValid == len(tmp)
+
+    def getIfAddr(self, ifname):
+        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
+        try:
+            x = socket.inet_ntoa(fcntl.ioctl(
+                s.fileno(),
+                0x8915,  # SIOCGIFADDR
+                struct.pack('256s', ifname[:15])
+            )[20:24])
+            s.close()
+        except Exception, e:
+            x = ifname
+
+        return x
 
     def createSocket(self):
         from OpenSSL import SSL
@@ -97,6 +188,9 @@ class SSLTCPListener(TCPListener):
         timeout = time.time() + 30
         while True:
             try:
+                if not self.isValidIP(self.interface):
+                    self.interface = self.getIfAddr(self.interface)
+                log.debug("Listening on %s:%s" % (self.interface, self.port))
                 sock.bind((self.interface, self.port))
                 return sock
             except socket.error, (_errno, strerrno):

[-- Attachment #3: Type: text/plain, Size: 138 bytes --]

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xensource.com
http://lists.xensource.com/xen-devel

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2010-05-24 14:59 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-05-24 14:59 [PATCH] Add interface name definition support for xend-relocation-address Michal Novotny

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).