1 | # -*- coding: utf-8 -*-
|
---|
2 | # $Id: netutils.py 98103 2023-01-17 14:15:46Z vboxsync $
|
---|
3 | # pylint: disable=too-many-lines
|
---|
4 |
|
---|
5 | """
|
---|
6 | Common Network Utility Functions.
|
---|
7 | """
|
---|
8 |
|
---|
9 | from __future__ import print_function;
|
---|
10 |
|
---|
11 | __copyright__ = \
|
---|
12 | """
|
---|
13 | Copyright (C) 2012-2023 Oracle and/or its affiliates.
|
---|
14 |
|
---|
15 | This file is part of VirtualBox base platform packages, as
|
---|
16 | available from https://www.virtualbox.org.
|
---|
17 |
|
---|
18 | This program is free software; you can redistribute it and/or
|
---|
19 | modify it under the terms of the GNU General Public License
|
---|
20 | as published by the Free Software Foundation, in version 3 of the
|
---|
21 | License.
|
---|
22 |
|
---|
23 | This program is distributed in the hope that it will be useful, but
|
---|
24 | WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
25 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
26 | General Public License for more details.
|
---|
27 |
|
---|
28 | You should have received a copy of the GNU General Public License
|
---|
29 | along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
30 |
|
---|
31 | The contents of this file may alternatively be used under the terms
|
---|
32 | of the Common Development and Distribution License Version 1.0
|
---|
33 | (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
34 | in the VirtualBox distribution, in which case the provisions of the
|
---|
35 | CDDL are applicable instead of those of the GPL.
|
---|
36 |
|
---|
37 | You may elect to license modified versions of this file under the
|
---|
38 | terms and conditions of either the GPL or the CDDL or both.
|
---|
39 |
|
---|
40 | SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
41 | """
|
---|
42 | __version__ = "$Revision: 98103 $"
|
---|
43 |
|
---|
44 |
|
---|
45 | # Standard Python imports.
|
---|
46 | import socket;
|
---|
47 |
|
---|
48 |
|
---|
49 | def getPrimaryHostIpByUdp(sPeerIp = '255.255.255.255'):
|
---|
50 | """
|
---|
51 | Worker for getPrimaryHostIp.
|
---|
52 |
|
---|
53 | The method is opening a UDP socket targetting a random port on a
|
---|
54 | limited (local LAN) broadcast address. We then use getsockname() to
|
---|
55 | obtain our own IP address, which should then be the primary IP.
|
---|
56 |
|
---|
57 | Unfortunately, this doesn't always work reliably on Solaris. When for
|
---|
58 | instance our host only is configured, which interface we end up on seems
|
---|
59 | to be totally random.
|
---|
60 | """
|
---|
61 |
|
---|
62 | try: oSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM);
|
---|
63 | except: oSocket = None;
|
---|
64 | if oSocket is not None:
|
---|
65 | try:
|
---|
66 | oSocket.connect((sPeerIp, 1984));
|
---|
67 | sHostIp = oSocket.getsockname()[0];
|
---|
68 | except:
|
---|
69 | sHostIp = None;
|
---|
70 | oSocket.close();
|
---|
71 | if sHostIp is not None:
|
---|
72 | return sHostIp;
|
---|
73 | return '127.0.0.1';
|
---|
74 |
|
---|
75 |
|
---|
76 | def getPrimaryHostIpByHostname():
|
---|
77 | """
|
---|
78 | Worker for getPrimaryHostIp.
|
---|
79 |
|
---|
80 | Attempts to resolve the hostname.
|
---|
81 | """
|
---|
82 | try:
|
---|
83 | return socket.gethostbyname(getHostnameFqdn());
|
---|
84 | except:
|
---|
85 | return '127.0.0.1';
|
---|
86 |
|
---|
87 |
|
---|
88 | def getPrimaryHostIp():
|
---|
89 | """
|
---|
90 | Tries to figure out the primary (the one with default route), local
|
---|
91 | IPv4 address.
|
---|
92 |
|
---|
93 | Returns the IP address on success and otherwise '127.0.0.1'.
|
---|
94 | """
|
---|
95 |
|
---|
96 | #
|
---|
97 | # This isn't quite as easy as one would think. Doing a UDP connect to
|
---|
98 | # 255.255.255.255 turns out to be problematic on solaris with more than one
|
---|
99 | # network interface (IP is random selected it seems), as well as linux
|
---|
100 | # where we've seen 127.0.1.1 being returned on some hosts.
|
---|
101 | #
|
---|
102 | # So a modified algorithm first try a known public IP address, ASSUMING
|
---|
103 | # that the primary interface is the one that gets us onto the internet.
|
---|
104 | # If that fails, due to routing or whatever, we try 255.255.255.255 and
|
---|
105 | # then finally hostname resolution.
|
---|
106 | #
|
---|
107 | sHostIp = getPrimaryHostIpByUdp('8.8.8.8');
|
---|
108 | if sHostIp.startswith('127.'):
|
---|
109 | sHostIp = getPrimaryHostIpByUdp('255.255.255.255');
|
---|
110 | if sHostIp.startswith('127.'):
|
---|
111 | sHostIp = getPrimaryHostIpByHostname();
|
---|
112 | return sHostIp;
|
---|
113 |
|
---|
114 |
|
---|
115 | def getHostnameFqdn():
|
---|
116 | """
|
---|
117 | Wrapper around getfqdn.
|
---|
118 |
|
---|
119 | Returns the fully qualified hostname, None if not found.
|
---|
120 | """
|
---|
121 |
|
---|
122 | try:
|
---|
123 | sHostname = socket.getfqdn();
|
---|
124 | except:
|
---|
125 | return None;
|
---|
126 |
|
---|
127 | if '.' in sHostname or sHostname.startswith('localhost'):
|
---|
128 | return sHostname;
|
---|
129 |
|
---|
130 | #
|
---|
131 | # Somewhat misconfigured system, needs expensive approach to guessing FQDN.
|
---|
132 | # Get address information on the hostname and do a reverse lookup from that.
|
---|
133 | #
|
---|
134 | try:
|
---|
135 | aAddressInfo = socket.getaddrinfo(sHostname, None);
|
---|
136 | except:
|
---|
137 | return sHostname;
|
---|
138 |
|
---|
139 | for aAI in aAddressInfo:
|
---|
140 | try: sName, _ = socket.getnameinfo(aAI[4], 0);
|
---|
141 | except: continue;
|
---|
142 | if '.' in sName and not set(sName).issubset(set('0123456789.')) and not sName.startswith('localhost'):
|
---|
143 | return sName;
|
---|
144 |
|
---|
145 | return sHostname;
|
---|
146 |
|
---|