Zsolt Haraszti | 86be6f1 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 1 | # |
Zsolt Haraszti | 3eb27a5 | 2017-01-03 21:56:48 -0800 | [diff] [blame] | 2 | # Copyright 2017 the original author or authors. |
Zsolt Haraszti | 86be6f1 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | # |
| 16 | |
| 17 | """ |
| 18 | Some network related convenience functions |
| 19 | """ |
| 20 | |
| 21 | from netifaces import AF_INET |
| 22 | |
| 23 | import netifaces as ni |
khenaidoo | 26a8c01 | 2017-07-07 18:25:47 -0400 | [diff] [blame] | 24 | import netaddr |
Zsolt Haraszti | 86be6f1 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 25 | |
khenaidoo | 26a8c01 | 2017-07-07 18:25:47 -0400 | [diff] [blame] | 26 | |
| 27 | def _get_all_interfaces(): |
| 28 | m_interfaces = [] |
| 29 | for iface in ni.interfaces(): |
| 30 | m_interfaces.append((iface, ni.ifaddresses(iface))) |
| 31 | return m_interfaces |
| 32 | |
| 33 | |
| 34 | def _get_my_primary_interface(): |
Zsolt Haraszti | 86be6f1 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 35 | gateways = ni.gateways() |
| 36 | assert 'default' in gateways, \ |
| 37 | ("No default gateway on host/container, " |
| 38 | "cannot determine primary interface") |
| 39 | default_gw_index = gateways['default'].keys()[0] |
| 40 | # gateways[default_gw_index] has the format (example): |
| 41 | # [('10.15.32.1', 'en0', True)] |
| 42 | interface_name = gateways[default_gw_index][0][1] |
| 43 | return interface_name |
| 44 | |
| 45 | |
khenaidoo | 26a8c01 | 2017-07-07 18:25:47 -0400 | [diff] [blame] | 46 | def get_my_primary_local_ipv4(inter_core_subnet=None, ifname=None): |
| 47 | if not inter_core_subnet: |
| 48 | return _get_my_primary_local_ipv4(ifname) |
| 49 | # My IP should belong to the specified subnet |
| 50 | for iface in ni.interfaces(): |
| 51 | m_ip = ni.ifaddresses(iface)[AF_INET][0]['addr'] |
| 52 | _ip = netaddr.IPAddress(m_ip).value |
| 53 | m_network = netaddr.IPNetwork(inter_core_subnet) |
| 54 | if _ip >= m_network.first and _ip <= m_network.last: |
| 55 | return m_ip |
| 56 | return None |
| 57 | |
| 58 | |
| 59 | def get_my_primary_interface(pon_subnet=None): |
| 60 | if not pon_subnet: |
| 61 | return _get_my_primary_interface() |
| 62 | # My interface should have an IP that belongs to the specified subnet |
| 63 | for iface in ni.interfaces(): |
| 64 | m_ip = ni.ifaddresses(iface)[AF_INET][0]['addr'] |
| 65 | m_ip = netaddr.IPAddress(m_ip).value |
| 66 | m_network = netaddr.IPNetwork(pon_subnet) |
| 67 | if m_ip >= m_network.first and m_ip <= m_network.last: |
| 68 | return iface |
| 69 | return None |
| 70 | |
| 71 | |
| 72 | def _get_my_primary_local_ipv4(ifname=None): |
khenaidoo | ccc4225 | 2017-07-06 23:00:49 -0400 | [diff] [blame] | 73 | try: |
| 74 | ifname = get_my_primary_interface() if ifname is None else ifname |
| 75 | addresses = ni.ifaddresses(ifname) |
| 76 | ipv4 = addresses[AF_INET][0]['addr'] |
| 77 | return ipv4 |
| 78 | except Exception as e: |
| 79 | return None |
Zsolt Haraszti | 86be6f1 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 80 | |
Zsolt Haraszti | 86be6f1 | 2016-09-27 09:56:49 -0700 | [diff] [blame] | 81 | if __name__ == '__main__': |
| 82 | print get_my_primary_local_ipv4() |