VirtualBox

source: vbox/trunk/src/VBox/ValidationKit/common/netutils.py@ 78339

Last change on this file since 78339 was 76553, checked in by vboxsync, 6 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.1 KB
Line 
1# -*- coding: utf-8 -*-
2# $Id: netutils.py 76553 2019-01-01 01:45:53Z vboxsync $
3# pylint: disable=C0302
4
5"""
6Common Network Utility Functions.
7"""
8
9from __future__ import print_function;
10
11__copyright__ = \
12"""
13Copyright (C) 2012-2019 Oracle Corporation
14
15This file is part of VirtualBox Open Source Edition (OSE), as
16available from http://www.virtualbox.org. This file is free software;
17you can redistribute it and/or modify it under the terms of the GNU
18General Public License (GPL) as published by the Free Software
19Foundation, in version 2 as it comes in the "COPYING" file of the
20VirtualBox OSE distribution. VirtualBox OSE is distributed in the
21hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
22
23The contents of this file may alternatively be used under the terms
24of the Common Development and Distribution License Version 1.0
25(CDDL) only, as it comes in the "COPYING.CDDL" file of the
26VirtualBox OSE distribution, in which case the provisions of the
27CDDL are applicable instead of those of the GPL.
28
29You may elect to license modified versions of this file under the
30terms and conditions of either the GPL or the CDDL or both.
31"""
32__version__ = "$Revision: 76553 $"
33
34
35# Standard Python imports.
36import socket;
37
38
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
78def getPrimaryHostIp():
79 """
80 Tries to figure out the primary (the one with default route), local
81 IPv4 address.
82
83 Returns the IP address on success and otherwise '127.0.0.1'.
84 """
85
86 #
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.
91 #
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.
96 #
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;
103
104
105def getHostnameFqdn():
106 """
107 Wrapper around getfqdn.
108
109 Returns the fully qualified hostname, None if not found.
110 """
111
112 try:
113 sHostname = socket.getfqdn();
114 except:
115 return None;
116
117 if '.' in sHostname or sHostname.startswith('localhost'):
118 return sHostname;
119
120 #
121 # Somewhat misconfigured system, needs expensive approach to guessing FQDN.
122 # Get address information on the hostname and do a reverse lookup from that.
123 #
124 try:
125 aAddressInfo = socket.getaddrinfo(sHostname, None);
126 except:
127 return sHostname;
128
129 for aAI in aAddressInfo:
130 try: sName, _ = socket.getnameinfo(aAI[4], 0);
131 except: continue;
132 if '.' in sName and not set(sName).issubset(set('0123456789.')) and not sName.startswith('localhost'):
133 return sName;
134
135 return sHostname;
136
Note: See TracBrowser for help on using the repository browser.

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