1
/****************************************************************************
3
** Copyright (C) 2012 Intel Corporation
4
** Contact: http://www.qt-project.org/legal
6
** This file is part of the QtCore module of the Qt Toolkit.
8
** $QT_BEGIN_LICENSE:LGPL$
9
** Commercial License Usage
10
** Licensees holding valid commercial Qt licenses may use this file in
11
** accordance with the commercial license agreement provided with the
12
** Software or, alternatively, in accordance with the terms contained in
13
** a written agreement between you and Digia. For licensing terms and
14
** conditions see http://qt.digia.com/licensing. For further information
15
** use the contact form at http://qt.digia.com/contact-us.
17
** GNU Lesser General Public License Usage
18
** Alternatively, this file may be used under the terms of the GNU Lesser
19
** General Public License version 2.1 as published by the Free Software
20
** Foundation and appearing in the file LICENSE.LGPL included in the
21
** packaging of this file. Please review the following information to
22
** ensure the GNU Lesser General Public License version 2.1 requirements
23
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25
** In addition, as a special exception, Digia gives you certain additional
26
** rights. These rights are described in the Digia Qt LGPL Exception
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29
** GNU General Public License Usage
30
** Alternatively, this file may be used under the terms of the GNU
31
** General Public License version 3.0 as published by the Free Software
32
** Foundation and appearing in the file LICENSE.GPL included in the
33
** packaging of this file. Please review the following information to
34
** ensure the GNU General Public License version 3.0 requirements will be
35
** met: http://www.gnu.org/copyleft/gpl.html.
40
****************************************************************************/
42
#include "qipaddress_p.h"
43
#include "private/qlocale_tools_p.h"
44
#include "qvarlengtharray.h"
47
namespace QIPAddressUtils {
49
static QString number(quint8 val, int base = 10)
52
return val ? qulltoa(val, base, zero) : zero;
55
typedef QVarLengthArray<char, 64> Buffer;
56
static bool checkedToAscii(Buffer &buffer, const QChar *begin, const QChar *end)
58
const ushort *const ubegin = reinterpret_cast<const ushort *>(begin);
59
const ushort *const uend = reinterpret_cast<const ushort *>(end);
60
const ushort *src = ubegin;
62
buffer.resize(uend - ubegin + 1);
63
char *dst = buffer.data();
74
static bool parseIp4Internal(IPv4Address &address, const char *ptr, bool acceptLeadingZero);
75
bool parseIp4(IPv4Address &address, const QChar *begin, const QChar *end)
77
Q_ASSERT(begin != end);
79
if (!checkedToAscii(buffer, begin, end))
82
const char *ptr = buffer.data();
83
return parseIp4Internal(address, ptr, true);
86
static bool parseIp4Internal(IPv4Address &address, const char *ptr, bool acceptLeadingZero)
90
while (dotCount < 4) {
91
if (!acceptLeadingZero && *ptr == '0' &&
92
ptr[1] != '.' && ptr[1] != '\0')
97
quint64 ll = qstrtoull(ptr, &endptr, 0, &ok);
99
if (!ok || endptr == ptr || ll != x)
102
if (*endptr == '.' || dotCount == 3) {
106
} else if (dotCount == 2) {
110
} else if (dotCount == 1) {
117
if (dotCount == 3 && *endptr != '\0')
119
else if (dotCount == 3 || *endptr == '\0')
127
void toString(QString &appendTo, IPv4Address address)
129
// reconstructing is easy
130
// use the fast operator% that pre-calculates the size
131
appendTo += number(address >> 24)
133
% number(address >> 16)
135
% number(address >> 8)
140
bool parseIp6(IPv6Address &address, const QChar *begin, const QChar *end)
142
Q_ASSERT(begin != end);
144
if (!checkedToAscii(buffer, begin, end))
147
const char *ptr = buffer.data();
159
// IPv4-in-IPv6 addresses are stricter in what they accept
160
if (dotCount != 0 && dotCount != 3)
163
memset(address, 0, sizeof address);
164
if (colonCount == 2 && end - begin == 2) // "::"
167
// if there's a double colon ("::"), this is how many zeroes it means
171
// there are two cases where 8 colons are allowed: at the ends
172
// so test that before the colon-count test
173
if ((ptr[0] == ':' && ptr[1] == ':') ||
174
(ptr[end - begin - 2] == ':' && ptr[end - begin - 1] == ':')) {
175
zeroWordsToFill = 9 - colonCount;
176
} else if (colonCount < 2 || colonCount > 7) {
179
zeroWordsToFill = 8 - colonCount;
188
quint64 ll = qstrtoull(ptr, &endptr, 16, &ok);
192
// empty field, we hope it's "::"
193
if (zeroWordsToFill < 1)
195
if (pos == 0 || pos == colonCount * 2) {
196
if (ptr[0] == '\0' || ptr[1] != ':')
200
pos += zeroWordsToFill * 2;
208
if (*endptr == '.') {
209
// this could be an IPv4 address
210
// it's only valid in the last element
215
if (!parseIp4Internal(ip4, ptr, false))
218
address[12] = ip4 >> 24;
219
address[13] = ip4 >> 16;
220
address[14] = ip4 >> 8;
225
address[pos++] = x >> 8;
226
address[pos++] = x & 0xff;
237
static inline QChar toHex(uchar c)
239
return ushort(c > 9 ? c + 'a' - 0xA : c + '0');
242
void toString(QString &appendTo, IPv6Address address)
244
// the longest IPv6 address possible is:
245
// "1111:2222:3333:4444:5555:6666:255.255.255.255"
246
// however, this function never generates that. The longest it does
247
// generate without an IPv4 address is:
248
// "1111:2222:3333:4444:5555:6666:7777:8888"
249
// and the longest with an IPv4 address is:
250
// "::ffff:255.255.255.255"
251
static const int Ip6AddressMaxLen = sizeof "1111:2222:3333:4444:5555:6666:7777:8888";
252
static const int Ip6WithIp4AddressMaxLen = sizeof "::ffff:255.255.255.255";
254
// check for the special cases
255
const quint64 zeroes[] = { 0, 0 };
256
bool embeddedIp4 = false;
258
// we consider embedded IPv4 for:
260
// ::x.x.x.y except if the x are 0 too
261
if (memcmp(address, zeroes, 10) == 0) {
262
if (address[10] == 0xff && address[11] == 0xff) {
264
} else if (address[10] == 0 && address[11] == 0) {
265
if (address[12] != 0 || address[13] != 0 || address[14] != 0) {
267
} else if (address[15] == 0) {
268
appendTo.append(QLatin1String("::"));
274
// QString::reserve doesn't shrink, so it's fine to us
275
appendTo.reserve(appendTo.size() +
276
(embeddedIp4 ? Ip6WithIp4AddressMaxLen : Ip6AddressMaxLen));
278
// for finding where to place the "::"
279
int zeroRunLength = 0; // in octets
280
int zeroRunOffset = 0; // in octets
281
for (int i = 0; i < 16; i += 2) {
282
if (address[i] == 0 && address[i + 1] == 0) {
283
// found a zero, scan forward to see how many more there are
285
for (j = i; j < 16; j += 2) {
286
if (address[j] != 0 || address[j+1] != 0)
290
if (j - i > zeroRunLength) {
291
zeroRunLength = j - i;
298
const QChar colon = ushort(':');
299
if (zeroRunLength < 4)
301
else if (zeroRunOffset == 0)
302
appendTo.append(colon);
304
for (int i = 0; i < 16; i += 2) {
305
if (i == zeroRunOffset) {
306
appendTo.append(colon);
307
i += zeroRunLength - 2;
311
if (i == 12 && embeddedIp4) {
312
IPv4Address ip4 = address[12] << 24 |
316
toString(appendTo, ip4);
321
if (address[i] >> 4) {
322
appendTo.append(toHex(address[i] >> 4));
323
appendTo.append(toHex(address[i] & 0xf));
324
appendTo.append(toHex(address[i + 1] >> 4));
325
appendTo.append(toHex(address[i + 1] & 0xf));
326
} else if (address[i] & 0xf) {
327
appendTo.append(toHex(address[i] & 0xf));
328
appendTo.append(toHex(address[i + 1] >> 4));
329
appendTo.append(toHex(address[i + 1] & 0xf));
331
} else if (address[i + 1] >> 4) {
332
appendTo.append(toHex(address[i + 1] >> 4));
333
appendTo.append(toHex(address[i + 1] & 0xf));
335
appendTo.append(toHex(address[i + 1] & 0xf));
339
appendTo.append(colon);