summaryrefslogtreecommitdiff
path: root/src/VBox/ValidationKit/common/netutils.py
blob: 1304b827accf8f93e6f80413525fff7268497f32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# -*- coding: utf-8 -*-
# $Id$
# pylint: disable=too-many-lines

"""
Common Network Utility Functions.
"""

from __future__ import print_function;

__copyright__ = \
"""
Copyright (C) 2012-2022 Oracle and/or its affiliates.

This file is part of VirtualBox base platform packages, as
available from https://www.virtualbox.org.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation, in version 3 of the
License.

This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, see <https://www.gnu.org/licenses>.

The contents of this file may alternatively be used under the terms
of the Common Development and Distribution License Version 1.0
(CDDL), a copy of it is provided in the "COPYING.CDDL" file included
in the VirtualBox distribution, in which case the provisions of the
CDDL are applicable instead of those of the GPL.

You may elect to license modified versions of this file under the
terms and conditions of either the GPL or the CDDL or both.

SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
"""
__version__ = "$Revision$"


# Standard Python imports.
import socket;


def getPrimaryHostIpByUdp(sPeerIp = '255.255.255.255'):
    """
    Worker for getPrimaryHostIp.

    The method is opening a UDP socket targetting a random port on a
    limited (local LAN) broadcast address.  We then use getsockname() to
    obtain our own IP address, which should then be the primary IP.

    Unfortunately, this doesn't always work reliably on Solaris.  When for
    instance our host only is configured, which interface we end up on seems
    to be totally random.
    """

    try:    oSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM);
    except: oSocket = None;
    if oSocket is not None:
        try:
            oSocket.connect((sPeerIp, 1984));
            sHostIp = oSocket.getsockname()[0];
        except:
            sHostIp = None;
        oSocket.close();
        if sHostIp is not None:
            return sHostIp;
    return '127.0.0.1';


def getPrimaryHostIpByHostname():
    """
    Worker for getPrimaryHostIp.

    Attempts to resolve the hostname.
    """
    try:
        return socket.gethostbyname(getHostnameFqdn());
    except:
        return '127.0.0.1';


def getPrimaryHostIp():
    """
    Tries to figure out the primary (the one with default route), local
    IPv4 address.

    Returns the IP address on success and otherwise '127.0.0.1'.
    """

    #
    # This isn't quite as easy as one would think.  Doing a UDP connect to
    # 255.255.255.255 turns out to be problematic on solaris with more than one
    # network interface (IP is random selected it seems), as well as linux
    # where we've seen 127.0.1.1 being returned on some hosts.
    #
    # So a modified algorithm first try a known public IP address, ASSUMING
    # that the primary interface is the one that gets us onto the internet.
    # If that fails, due to routing or whatever, we try 255.255.255.255 and
    # then finally hostname resolution.
    #
    sHostIp = getPrimaryHostIpByUdp('8.8.8.8');
    if sHostIp.startswith('127.'):
        sHostIp = getPrimaryHostIpByUdp('255.255.255.255');
        if sHostIp.startswith('127.'):
            sHostIp = getPrimaryHostIpByHostname();
    return sHostIp;


def getHostnameFqdn():
    """
    Wrapper around getfqdn.

    Returns the fully qualified hostname, None if not found.
    """

    try:
        sHostname = socket.getfqdn();
    except:
        return None;

    if '.' in sHostname or sHostname.startswith('localhost'):
        return sHostname;

    #
    # Somewhat misconfigured system, needs expensive approach to guessing FQDN.
    # Get address information on the hostname and do a reverse lookup from that.
    #
    try:
        aAddressInfo = socket.getaddrinfo(sHostname, None);
    except:
        return sHostname;

    for aAI in aAddressInfo:
        try:    sName, _ = socket.getnameinfo(aAI[4], 0);
        except: continue;
        if '.' in sName and not set(sName).issubset(set('0123456789.')) and not sName.startswith('localhost'):
            return sName;

    return sHostname;