Example #1
0
def get_adapters(include_unconfigured=False):

    # Call GetAdaptersAddresses() with error and buffer size handling

    addressbuffersize = wintypes.ULONG(15*1024)
    retval = ERROR_BUFFER_OVERFLOW
    while retval == ERROR_BUFFER_OVERFLOW:
        addressbuffer = ctypes.create_string_buffer(addressbuffersize.value)
        retval = iphlpapi.GetAdaptersAddresses(wintypes.ULONG(AF_UNSPEC),
                                      wintypes.ULONG(0),
                                      None,
                                      ctypes.byref(addressbuffer),
                                      ctypes.byref(addressbuffersize))
    if retval != NO_ERROR:
        raise ctypes.WinError()

    # Iterate through adapters fill array
    address_infos = []
    address_info = IP_ADAPTER_ADDRESSES.from_buffer(addressbuffer)
    while True:
        address_infos.append(address_info)
        if not address_info.Next:
            break
        address_info = address_info.Next[0]


    # Iterate through unicast addresses
    result = []
    for adapter_info in address_infos:

        name = adapter_info.AdapterName
        if sys.version_info[0] > 2:
            # We don't expect non-ascii characters here, so encoding shouldn't matter
            name = name.decode()
        nice_name = adapter_info.Description
        index = adapter_info.IfIndex

        if adapter_info.FirstUnicastAddress:
            ips = enumerate_interfaces_of_adapter(adapter_info.FriendlyName, adapter_info.FirstUnicastAddress[0])
            ips = list(ips)
            result.append(shared.Adapter(name, nice_name, ips,
                                         index=index))
        elif include_unconfigured:
            result.append(shared.Adapter(name, nice_name, [],
                                         index=index))

    return result
Example #2
0
 def add_ip(adapter_name, ip):
     if not adapter_name in ips:
         try:
             index = socket.if_nametoindex(adapter_name)
         except (OSError, AttributeError):
             index = None
         ips[adapter_name] = shared.Adapter(adapter_name,
                                            adapter_name, [],
                                            index=index)
     ips[adapter_name].ips.append(ip)
Example #3
0
 def add_ip(adapter_name: str, ip: Optional[shared.IP]) -> None:
     if adapter_name not in ips:
         index = None  # type: Optional[int]
         try:
             index = socket.if_nametoindex(adapter_name)
         except (OSError, AttributeError):
             pass
         ips[adapter_name] = shared.Adapter(adapter_name,
                                            adapter_name, [],
                                            index=index)
     if ip is not None:
         ips[adapter_name].ips.append(ip)
Example #4
0
def get_adapters():
    
    # Call GetAdaptersAddresses() with error and buffer size handling

    addressbuffersize = wintypes.ULONG(15*1024)
    retval = ERROR_BUFFER_OVERFLOW
    while retval == ERROR_BUFFER_OVERFLOW:
        addressbuffer = ctypes.create_string_buffer(addressbuffersize.value)
        retval = iphlpapi.GetAdaptersAddresses(wintypes.ULONG(AF_UNSPEC),
                                      wintypes.ULONG(0),
                                      None,
                                      ctypes.byref(addressbuffer),
                                      ctypes.byref(addressbuffersize))
    if retval != NO_ERROR:
        raise ctypes.WinError()
    
    # Iterate through adapters fill array
    address_infos = []
    address_info = IP_ADAPTER_ADDRESSES.from_buffer(addressbuffer)
    while True:
        address_infos.append(address_info)
        if not address_info.Next:
            break
        address_info = address_info.Next[0]
    
    
    # Iterate through unicast addresses
    result = []
    for adapter_info in address_infos:
        
        name = adapter_info.AdapterName
        nice_name = adapter_info.Description
        
        if adapter_info.FirstUnicastAddress:
            ips = enumerate_interfaces_of_adapter(adapter_info.FriendlyName, adapter_info.FirstUnicastAddress[0])
            ips = list(ips)
            result.append(shared.Adapter(name, nice_name, ips))

    return result
Example #5
0
 def add_ip(adapter_name, ip):
     if not adapter_name in ips:
         ips[adapter_name] = shared.Adapter(adapter_name, adapter_name, [])
     ips[adapter_name].ips.append(ip)