khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1 | # |
| 2 | # Copyright 2017 the original author or authors. |
| 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 |
| 24 | import netaddr |
| 25 | |
| 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(): |
| 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 | |
| 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 | addresses = ni.ifaddresses(iface) |
| 52 | if AF_INET in addresses: |
| 53 | m_ip = addresses[AF_INET][0]['addr'] |
| 54 | _ip = netaddr.IPAddress(m_ip).value |
| 55 | m_network = netaddr.IPNetwork(inter_core_subnet) |
| 56 | if _ip >= m_network.first and _ip <= m_network.last: |
| 57 | return m_ip |
| 58 | return None |
| 59 | |
| 60 | |
| 61 | def get_my_primary_interface(pon_subnet=None): |
| 62 | if not pon_subnet: |
| 63 | return _get_my_primary_interface() |
| 64 | # My interface should have an IP that belongs to the specified subnet |
| 65 | for iface in ni.interfaces(): |
| 66 | addresses = ni.ifaddresses(iface) |
| 67 | if AF_INET in addresses: |
| 68 | m_ip = addresses[AF_INET][0]['addr'] |
| 69 | m_ip = netaddr.IPAddress(m_ip).value |
| 70 | m_network = netaddr.IPNetwork(pon_subnet) |
| 71 | if m_ip >= m_network.first and m_ip <= m_network.last: |
| 72 | return iface |
| 73 | return None |
| 74 | |
William Kurkian | daa6bb2 | 2019-03-07 12:26:28 -0500 | [diff] [blame] | 75 | def mac_str_to_tuple(mac): |
| 76 | return tuple(int(d, 16) for d in mac.split(':')) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 77 | |
| 78 | def _get_my_primary_local_ipv4(ifname=None): |
| 79 | try: |
| 80 | ifname = get_my_primary_interface() if ifname is None else ifname |
| 81 | addresses = ni.ifaddresses(ifname) |
| 82 | ipv4 = addresses[AF_INET][0]['addr'] |
| 83 | return ipv4 |
| 84 | except Exception as e: |
| 85 | return None |
| 86 | |
| 87 | if __name__ == '__main__': |
| 88 | print get_my_primary_local_ipv4() |