1 | /* $Id: UINetworkManagerUtils.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - UINetworkManagerUtils namespace implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2017-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 | /* GUI includes: */
|
---|
29 | #include "UINetworkManagerUtils.h"
|
---|
30 |
|
---|
31 |
|
---|
32 | quint32 UINetworkManagerUtils::ipv4FromQStringToQuint32(const QString &strAddress)
|
---|
33 | {
|
---|
34 | quint32 uAddress = 0;
|
---|
35 | foreach (const QString &strPart, strAddress.split('.'))
|
---|
36 | {
|
---|
37 | uAddress = uAddress << 8;
|
---|
38 | bool fOk = false;
|
---|
39 | uint uPart = strPart.toUInt(&fOk);
|
---|
40 | if (fOk)
|
---|
41 | uAddress += uPart;
|
---|
42 | }
|
---|
43 | return uAddress;
|
---|
44 | }
|
---|
45 |
|
---|
46 | QString UINetworkManagerUtils::ipv4FromQuint32ToQString(quint32 uAddress)
|
---|
47 | {
|
---|
48 | QStringList address;
|
---|
49 | while (uAddress)
|
---|
50 | {
|
---|
51 | uint uPart = uAddress & 0xFF;
|
---|
52 | address.prepend(QString::number(uPart));
|
---|
53 | uAddress = uAddress >> 8;
|
---|
54 | }
|
---|
55 | return address.join('.');
|
---|
56 | }
|
---|
57 |
|
---|
58 | quint32 UINetworkManagerUtils::incrementNetworkAddress(quint32 uAddress)
|
---|
59 | {
|
---|
60 | return advanceNetworkAddress(uAddress, true /* forward */);
|
---|
61 | }
|
---|
62 |
|
---|
63 | quint32 UINetworkManagerUtils::decrementNetworkAddress(quint32 uAddress)
|
---|
64 | {
|
---|
65 | return advanceNetworkAddress(uAddress, false /* forward */);
|
---|
66 | }
|
---|
67 |
|
---|
68 | quint32 UINetworkManagerUtils::advanceNetworkAddress(quint32 uAddress, bool fForward)
|
---|
69 | {
|
---|
70 | /* Success by default: */
|
---|
71 | bool fSuccess = true;
|
---|
72 | do
|
---|
73 | {
|
---|
74 | /* Just advance address: */
|
---|
75 | if (fForward)
|
---|
76 | ++uAddress;
|
---|
77 | else
|
---|
78 | --uAddress;
|
---|
79 | /* And treat it as success initially: */
|
---|
80 | fSuccess = true;
|
---|
81 | /* Iterate the resulting bytes: */
|
---|
82 | uint uByteIndex = 0;
|
---|
83 | quint32 uIterator = uAddress;
|
---|
84 | while (fSuccess && uIterator)
|
---|
85 | {
|
---|
86 | /* Get current byte: */
|
---|
87 | const quint32 uCurrentByte = uIterator & 0xFF;
|
---|
88 | /* Advance iterator early: */
|
---|
89 | uIterator = uIterator >> 8;
|
---|
90 | // We know that .0. and .255. are legal these days
|
---|
91 | // but still prefer to exclude them from
|
---|
92 | // being proposed to an end user.
|
---|
93 | /* If current byte equal to 255
|
---|
94 | * or first byte equal to 0,
|
---|
95 | * let's try again: */
|
---|
96 | if ( uCurrentByte == 0xFF
|
---|
97 | || (uCurrentByte == 0x00 && uByteIndex == 0))
|
---|
98 | fSuccess = false;
|
---|
99 | /* Advance byte index: */
|
---|
100 | ++uByteIndex;
|
---|
101 | }
|
---|
102 | }
|
---|
103 | while (!fSuccess);
|
---|
104 | return uAddress;
|
---|
105 | }
|
---|
106 |
|
---|
107 | QStringList UINetworkManagerUtils::makeDhcpServerProposal(const QString &strInterfaceAddress, const QString &strInterfaceMask)
|
---|
108 | {
|
---|
109 | /* Convert interface address/mask into digital form and calculate inverted interface mask: */
|
---|
110 | const quint32 uAddress = ipv4FromQStringToQuint32(strInterfaceAddress);
|
---|
111 | const quint32 uMaskDirect = ipv4FromQStringToQuint32(strInterfaceMask);
|
---|
112 | const quint32 uMaskInvert = ~uMaskDirect;
|
---|
113 | //printf("Direct mask: %s (%u)\n", ipv4FromQuint32ToQString(uMaskDirect).toUtf8().constData(), uMaskDirect);
|
---|
114 | //printf("Inverted mask: %s (%u)\n", ipv4FromQuint32ToQString(uMaskInvert).toUtf8().constData(), uMaskInvert);
|
---|
115 |
|
---|
116 | /* Split the interface address into left and right parts: */
|
---|
117 | const quint32 uPartL = uAddress & uMaskDirect;
|
---|
118 | const quint32 uPartR = uAddress & uMaskInvert;
|
---|
119 | //printf("Left part: %s (%u)\n", ipv4FromQuint32ToQString(uPartL).toUtf8().constData(), uPartL);
|
---|
120 | //printf("Right part: %s (%u)\n", ipv4FromQuint32ToQString(uPartR).toUtf8().constData(), uPartR);
|
---|
121 |
|
---|
122 | /* Prepare DHCP server proposal:" */
|
---|
123 | quint32 uServerProposedAddress = 0;
|
---|
124 | quint32 uServerProposedAddressL = 0;
|
---|
125 | quint32 uServerProposedAddressU = 0;
|
---|
126 | if (uPartR < uMaskInvert / 2)
|
---|
127 | {
|
---|
128 | /* Make DHCP server proposal from right scope: */
|
---|
129 | //printf("Make DHCP server proposal from right scope:\n");
|
---|
130 | uServerProposedAddress = uPartL + incrementNetworkAddress(uPartR);
|
---|
131 | uServerProposedAddressL = uPartL + incrementNetworkAddress(incrementNetworkAddress(uPartR));
|
---|
132 | uServerProposedAddressU = uPartL + (uMaskInvert & 0xFEFEFEFE) /* decrementNetworkAddress(uMaskInvert) */;
|
---|
133 | }
|
---|
134 | else
|
---|
135 | {
|
---|
136 | /* Make DHCP server proposal from left scope: */
|
---|
137 | //printf("Make DHCP server proposal from left scope:\n");
|
---|
138 | uServerProposedAddress = uPartL + 1 /* incrementNetworkAddress(0) */;
|
---|
139 | uServerProposedAddressL = uPartL + 2 /* incrementNetworkAddress(incrementNetworkAddress(0)) */;
|
---|
140 | uServerProposedAddressU = uPartL + decrementNetworkAddress(uPartR);
|
---|
141 | }
|
---|
142 | //printf("DHCP server address: %s (%u)\n", ipv4FromQuint32ToQString(uServerProposedAddress).toUtf8().constData(), uServerProposedAddress);
|
---|
143 | //printf("DHCP server lower address: %s (%u)\n", ipv4FromQuint32ToQString(uServerProposedAddressL).toUtf8().constData(), uServerProposedAddressL);
|
---|
144 | //printf("DHCP server upper address: %s (%u)\n", ipv4FromQuint32ToQString(uServerProposedAddressU).toUtf8().constData(), uServerProposedAddressU);
|
---|
145 |
|
---|
146 | /* Pack and return result: */
|
---|
147 | return QStringList() << ipv4FromQuint32ToQString(uServerProposedAddress)
|
---|
148 | << ipv4FromQuint32ToQString(uMaskDirect)
|
---|
149 | << ipv4FromQuint32ToQString(uServerProposedAddressL)
|
---|
150 | << ipv4FromQuint32ToQString(uServerProposedAddressU);
|
---|
151 | }
|
---|
152 |
|
---|