Added
Link Here
|
1 |
--- google_compute_engine/networking/ip_forwarding/ip_forwarding_utils.py.orig 2018-06-11 23:51:09 UTC |
2 |
+++ google_compute_engine/networking/ip_forwarding/ip_forwarding_utils.py |
3 |
@@ -15,6 +15,8 @@ |
4 |
|
5 |
"""Utilities for configuring IP address forwarding.""" |
6 |
|
7 |
+import netaddr |
8 |
+import netifaces |
9 |
import re |
10 |
import subprocess |
11 |
|
12 |
@@ -32,27 +34,11 @@ class IpForwardingUtils(object): |
13 |
logger: logger object, used to write to SysLog and serial port. |
14 |
proto_id: string, the routing protocol identifier for Google IP changes. |
15 |
""" |
16 |
- self.logger = logger |
17 |
- self.proto_id = proto_id or '66' |
18 |
|
19 |
- def _CreateRouteOptions(self, **kwargs): |
20 |
- """Create a dictionary of parameters to append to the ip route command. |
21 |
- |
22 |
- Args: |
23 |
- **kwargs: dict, the string parameters to update in the ip route command. |
24 |
- |
25 |
- Returns: |
26 |
- dict, the string parameters to append to the ip route command. |
27 |
- """ |
28 |
- options = { |
29 |
- 'proto': self.proto_id, |
30 |
- 'scope': 'host', |
31 |
- } |
32 |
- options.update(kwargs) |
33 |
- return options |
34 |
+ self.logger = logger |
35 |
|
36 |
- def _RunIpRoute(self, args=None, options=None): |
37 |
- """Run a command with ip route and return the response. |
38 |
+ def _RunIfconfig(self, args=None, options=None): |
39 |
+ """Run a command with ifconfig and return the response. |
40 |
|
41 |
Args: |
42 |
args: list, the string ip route command args to execute. |
43 |
@@ -63,7 +49,7 @@ class IpForwardingUtils(object): |
44 |
""" |
45 |
args = args or [] |
46 |
options = options or {} |
47 |
- command = ['ip', 'route'] |
48 |
+ command = ['ifconfig'] |
49 |
command.extend(args) |
50 |
for item in options.items(): |
51 |
command.extend(item) |
52 |
@@ -94,25 +80,33 @@ class IpForwardingUtils(object): |
53 |
forwarded_ips = forwarded_ips or [] |
54 |
for ip in forwarded_ips: |
55 |
if ip and (IP_REGEX.match(ip) or IP_ALIAS_REGEX.match(ip)): |
56 |
- addresses.append(ip[:-3] if ip.endswith('/32') else ip) |
57 |
+ addresses.extend([str(addr) for addr in list(netaddr.IPNetwork(ip))]) |
58 |
else: |
59 |
self.logger.warning('Could not parse IP address: "%s".', ip) |
60 |
return addresses |
61 |
|
62 |
- def GetForwardedIps(self, interface): |
63 |
+ def GetForwardedIps(self, interface, interface_ip): |
64 |
"""Retrieve the list of configured forwarded IP addresses. |
65 |
|
66 |
Args: |
67 |
interface: string, the output device to query. |
68 |
+ interface_ip: string, current interface ip address. |
69 |
|
70 |
Returns: |
71 |
list, the IP address strings. |
72 |
""" |
73 |
- args = ['ls', 'table', 'local', 'type', 'local'] |
74 |
- options = self._CreateRouteOptions(dev=interface) |
75 |
- result = self._RunIpRoute(args=args, options=options) |
76 |
- result = re.sub(r'local\s', r'', result) |
77 |
- return self.ParseForwardedIps(result.split()) |
78 |
+ try: |
79 |
+ ips = netifaces.ifaddresses(interface) |
80 |
+ ips = ips[netifaces.AF_INET] |
81 |
+ except (ValueError, IndexError): |
82 |
+ return [] |
83 |
+ forwarded_ips = [] |
84 |
+ for ip in ips: |
85 |
+ if ip['addr'] != interface_ip: |
86 |
+ forwarded_ips.append( |
87 |
+ '%s/%d' % (ip['addr'], |
88 |
+ netaddr.IPAddress(ip['netmask']).netmask_bits())) |
89 |
+ return self.ParseForwardedIps(forwarded_ips) |
90 |
|
91 |
def AddForwardedIp(self, address, interface): |
92 |
"""Configure a new IP address on the network interface. |
93 |
@@ -121,10 +115,8 @@ class IpForwardingUtils(object): |
94 |
address: string, the IP address to configure. |
95 |
interface: string, the output device to use. |
96 |
""" |
97 |
- address = address if IP_ALIAS_REGEX.match(address) else '%s/32' % address |
98 |
- args = ['add', 'to', 'local', address] |
99 |
- options = self._CreateRouteOptions(dev=interface) |
100 |
- self._RunIpRoute(args=args, options=options) |
101 |
+ for ip in list(netaddr.IPNetwork(address)): |
102 |
+ self._RunIfconfig(args=[interface, 'alias', '%s/32' % str(ip)]) |
103 |
|
104 |
def RemoveForwardedIp(self, address, interface): |
105 |
"""Delete an IP address on the network interface. |
106 |
@@ -133,7 +125,5 @@ class IpForwardingUtils(object): |
107 |
address: string, the IP address to configure. |
108 |
interface: string, the output device to use. |
109 |
""" |
110 |
- address = address if IP_ALIAS_REGEX.match(address) else '%s/32' % address |
111 |
- args = ['delete', 'to', 'local', address] |
112 |
- options = self._CreateRouteOptions(dev=interface) |
113 |
- self._RunIpRoute(args=args, options=options) |
114 |
+ ip = netaddr.IPNetwork(address) |
115 |
+ self._RunIfconfig(args=[interface, '-alias', str(ip.ip)]) |