def convert_rt_msg(msg): ret = rtmsg() ret['header']['type'] = (RTNL_NEWROUTE if msg['header']['type'] == RTM_ADD else RTNL_DELROUTE) ret['family'] = msg['DST']['header']['family'] ret['attrs'] = [] if 'address' in msg['DST']: ret['attrs'].append(['RTA_DST', msg['DST']['address']]) if ('NETMASK' in msg and msg['NETMASK']['header']['family'] == ret['family']): ret['dst_len'] = dqn2int(msg['NETMASK']['address'], ret['family']) if 'GATEWAY' in msg: if msg['GATEWAY']['header']['family'] not in (AF_INET, AF_INET6): # interface routes, table 255 # discard for now return None ret['attrs'].append(['RTA_GATEWAY', msg['GATEWAY']['address']]) if 'IFA' in msg: ret['attrs'].append(['RTA_SRC', msg['IFA']['address']]) if 'IFP' in msg: ret['attrs'].append(['RTA_OIF', msg['IFP']['index']]) elif msg['rtm_index'] != 0: ret['attrs'].append(['RTA_OIF', msg['rtm_index']]) del ret['value'] return ret
def _GetAdaptersInfo(self): ret = {'interfaces': [], 'addresses': []} # prepare buffer buf = ctypes.create_string_buffer(15000) buf_len = ctypes.c_ulong(15000) ( ctypes.windll.iphlpapi.GetAdaptersInfo( ctypes.byref(buf), ctypes.byref(buf_len) ) ) adapter = IP_ADAPTER_INFO.from_address(ctypes.addressof(buf)) while True: mac = ':'.join(['%02x' % x for x in adapter.Address][:6]) ifname = ctypes.string_at( ctypes.addressof(adapter.AdapterName) ).decode('utf-8') spec = { 'index': adapter.Index, 'attrs': (['IFLA_ADDRESS', mac], ['IFLA_IFNAME', ifname]), } msg = ifinfmsg().load(spec) del msg['value'] ret['interfaces'].append(msg) ipaddr = adapter.IpAddressList while True: addr = ctypes.string_at( ctypes.addressof(ipaddr.IpAddress) ).decode('utf-8') mask = ctypes.string_at( ctypes.addressof(ipaddr.IpMask) ).decode('utf-8') spec = { 'index': adapter.Index, 'family': AF_INET, 'prefixlen': dqn2int(mask), 'attrs': ( ['IFA_ADDRESS', addr], ['IFA_LOCAL', addr], ['IFA_LABEL', ifname], ), } msg = ifaddrmsg().load(spec) del msg['value'] ret['addresses'].append(msg) if ipaddr.Next: ipaddr = ipaddr.Next.contents else: break if adapter.Next: adapter = adapter.Next.contents else: break return ret
def convert_ifa_msg(msg): ret = ifaddrmsg() ret['header']['type'] = RTNL_NEWADDR if \ msg['header']['type'] == RTM_NEWADDR else \ RTNL_DELADDR ret['index'] = msg['IFP']['index'] ret['family'] = msg['IFA']['header']['family'] ret['prefixlen'] = dqn2int(msg['NETMASK']['address'], ret['family']) ret['attrs'] = [['IFA_ADDRESS', msg['IFA']['address']], ['IFA_BROADCAST', msg['BRD']['address']], ['IFA_LABEL', msg['IFP']['ifname']]] del ret['value'] return ret
def norm_mask(value): if isinstance(value, basestring): if '.' in value: value = dqn2int(value) value = int(value) return value