VirtualBox

Changeset 72316 in vbox for trunk/src


Ignore:
Timestamp:
May 24, 2018 9:28:48 AM (7 years ago)
Author:
vboxsync
Message:

valkit/netutils.py: Fixes to the host IP getting code.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/ValidationKit/common/netutils.py

    r72297 r72316  
    3737
    3838
     39def getPrimaryHostIpByUdp(sPeerIp = '255.255.255.255'):
     40    """
     41    Worker for getPrimaryHostIp.
     42
     43    The method is opening a UDP socket targetting a random port on a
     44    limited (local LAN) broadcast address.  We then use getsockname() to
     45    obtain our own IP address, which should then be the primary IP.
     46
     47    Unfortunately, this doesn't always work reliably on Solaris.  When for
     48    instance our host only is configured, which interface we end up on seems
     49    to be totally random.
     50    """
     51
     52    try:    oSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM);
     53    except: oSocket = None;
     54    if oSocket is not None:
     55        try:
     56            oSocket.connect((sPeerIp, 1984));
     57            sHostIp = oSocket.getsockname()[0];
     58        except:
     59            sHostIp = None;
     60        oSocket.close();
     61        if sHostIp is not None:
     62            return sHostIp;
     63    return '127.0.0.1';
     64
     65
     66def getPrimaryHostIpByHostname():
     67    """
     68    Worker for getPrimaryHostIp.
     69
     70    Attempts to resolve the hostname.
     71    """
     72    try:
     73        return socket.gethostbyname(getHostnameFqdn());
     74    except:
     75        return '127.0.0.1';
     76
     77
    3978def getPrimaryHostIp():
    4079    """
     
    4685
    4786    #
    48     # The first gambit is opening a UDP socket targetting a random port on a
    49     # limited (local LAN) broadcast address.  We then use getsockname() to
    50     # obtain our own IP address, which should then be the primary IP.
     87    # This isn't quite as easy as one would think.  Doing a UDP connect to
     88    # 255.255.255.255 turns out to be problematic on solaris with more than one
     89    # network interface (IP is random selected it seems), as well as linux
     90    # where we've seen 127.0.1.1 being returned on some hosts.
    5191    #
    52     try:    oSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM);
    53     except: oSocket = None; print('#1');
    54     if oSocket is not None:
    55         try:
    56             oSocket.connect(('255.255.255.255', 1984));
    57             sHostIp = oSocket.getsockname()[0];
    58         except:
    59             sHostIp = None;
    60         oSocket.close();
    61         if sHostIp is not None:
    62             return sHostIp;
    63 
     92    # So a modified algorithm first try a known public IP address, ASSUMING
     93    # that the primary interface is the one that gets us onto the internet.
     94    # If that fails, due to routing or whatever, we try 255.255.255.255 and
     95    # then finally hostname resolution.
    6496    #
    65     # The second attempt is resolving the hostname.
    66     #
    67     try:
    68         return socket.gethostbyname(getHostnameFqdn());
    69     except:
    70         pass;
    71 
    72     return '127.0.0.1';
    73 
     97    sHostIp = getPrimaryHostIpByUdp('8.8.8.8');
     98    if sHostIp.startswith('127.'):
     99        sHostIp = getPrimaryHostIpByUdp('255.255.255.255');
     100        if sHostIp.startswith('127.'):
     101            sHostIp = getPrimaryHostIpByHostname();
     102    return sHostIp;
    74103
    75104def getHostnameFqdn():
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette