ia64/xen-unstable

view tools/python/xen/util/ip.py @ 1736:eeaf210226a7

bitkeeper revision 1.1062.1.3 (40f07134wkFr7Sj5k07HZt68SJBVCg)

fix simple error path in 'xm'
author iap10@labyrinth.cl.cam.ac.uk
date Sat Jul 10 22:44:04 2004 +0000 (2004-07-10)
parents e04e40b4615f
children cda0735bdc4c
line source
1 import os
2 import re
3 import socket
4 import struct
5 import errno
7 def _readlines(fd):
8 """Version of readlines safe against EINTR.
9 """
10 import errno
12 lines = []
13 while 1:
14 try:
15 line = fd.readline()
16 except IOError, ex:
17 if ex.errno == errno.EINTR:
18 continue
19 else:
20 raise
21 if line == '': break
22 lines.append(line)
23 return lines
25 def _readline(fd):
26 """Version of readline safe against EINTR.
27 """
28 while 1:
29 try:
30 return fd.readline()
31 except IOError, ex:
32 if ex.errno == errno.EINTR:
33 continue
34 else:
35 raise
37 ##### Networking-related functions
39 """Bridge for network backend.
40 When bridging is used, eth0 may not have an IP address,
41 as it may have been moved onto the bridge.
42 """
43 NBE_BRIDGE = 'xen-br0'
45 def get_current_ipaddr(dev='eth0'):
46 """Get the primary IP address for the given network interface.
48 dev network interface (default eth0)
50 returns interface address as a string
51 """
52 fd = os.popen( '/sbin/ifconfig ' + dev + ' 2>/dev/null' )
53 lines = _readlines(fd)
54 for line in lines:
55 m = re.search( '^\s+inet addr:([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+).*',
56 line )
57 if m:
58 return m.group(1)
59 if dev == 'eth0':
60 return get_current_ipaddr(NBE_BRIDGE)
61 return None
63 def get_current_ipmask(dev='eth0'):
64 """Get the primary IP netmask for a network interface.
66 dev network interface (default eth0)
68 returns interface netmask as a string
69 """
70 fd = os.popen( '/sbin/ifconfig ' + dev + ' 2>/dev/null' )
71 lines = _readlines(fd)
72 for line in lines:
73 m = re.search( '^.+Mask:([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+).*',
74 line )
75 if m:
76 return m.group(1)
77 if dev == 'eth0':
78 return get_current_ipmask(NBE_BRIDGE)
79 return None
81 def get_current_ipgw(dev='eth0'):
82 """Get the IP gateway for a network interface.
84 dev network interface (default eth0)
86 returns gateway address as a string
87 """
88 fd = os.popen( '/sbin/route -n' )
89 lines = _readlines(fd)
90 for line in lines:
91 m = re.search( '^\S+\s+([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)' +
92 '\s+\S+\s+\S*G.*' + dev + '.*', line )
93 if m:
94 return m.group(1)
95 if dev == 'eth0':
96 return get_current_ipgw(NBE_BRIDGE)
97 return None
99 def inet_aton(addr):
100 """Convert an IP addr in IPv4 dot notation into an int.
102 addr IP address as a string
104 returns integer
105 """
106 b = socket.inet_aton(addr)
107 return struct.unpack('!I', b)[0]
109 def inet_ntoa(n):
110 """Convert an int into an IP addr in IPv4 dot notation.
112 n IP address
114 returns string
115 """
116 b = struct.pack('!I', n)
117 return socket.inet_ntoa(b)
119 def add_offset_to_ip(addr, offset):
120 """Add a numerical offset to an IP addr in IPv4 dot notation.
122 addr IP address
123 offset offset to add
125 returns new address
126 """
127 n = inet_aton(addr)
128 n += offset
129 return inet_ntoa(n)
131 def check_subnet( ip, network, netmask ):
132 """Check if an IP address is in the subnet defined by
133 a network address and mask'.
135 ip IP adress
136 network network address
137 netmask network mask
139 returns 1 if it is in the subnet, 0 if not
140 """
141 n_ip = inet_aton(ip)
142 n_net = inet_aton(network)
143 n_mask = inet_aton(netmask)
144 return (n_ip & n_mask) == (n_net & n_mask)