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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
|
/****************************************************************************
**
** Implementation of TQHostAddress class.
**
** Created : 979899
**
** Copyright (C) 1997-2008 Trolltech ASA. All rights reserved.
**
** This file is part of the network module of the TQt GUI Toolkit.
**
** This file may be used under the terms of the GNU General
** Public License versions 2.0 or 3.0 as published by the Free
** Software Foundation and appearing in the files LICENSE.GPL2
** and LICENSE.GPL3 included in the packaging of this file.
** Alternatively you may (at your option) use any later version
** of the GNU General Public License if such license has been
** publicly approved by Trolltech ASA (or its successors, if any)
** and the KDE Free TQt Foundation.
**
** Please review the following information to ensure GNU General
** Public Licensing requirements will be met:
** http://trolltech.com/products/qt/licenses/licensing/opensource/.
** If you are unsure which license is appropriate for your use, please
** review the following information:
** http://trolltech.com/products/qt/licenses/licensing/licensingoverview
** or contact the sales department at sales@trolltech.com.
**
** This file may be used under the terms of the Q Public License as
** defined by Trolltech ASA and appearing in the file LICENSE.TQPL
** included in the packaging of this file. Licensees holding valid TQt
** Commercial licenses may use this file in accordance with the TQt
** Commercial License Agreement provided with the Software.
**
** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted
** herein.
**
**********************************************************************/
#include "ntqhostaddress.h"
#include "ntqstringlist.h"
#ifndef TQT_NO_NETWORK
class TQHostAddressPrivate
{
public:
TQHostAddressPrivate( TQ_UINT32 a_=0 ) : a(a_), isIp4(TRUE)
{
}
TQHostAddressPrivate( TQ_UINT8 *a_ );
TQHostAddressPrivate(const Q_IPV6ADDR &a_);
~TQHostAddressPrivate()
{
}
TQHostAddressPrivate & operator=( const TQHostAddressPrivate &from )
{
a = from.a;
isIp4 = from.isIp4;
a6 = from.a6;
return *this;
}
private:
TQ_UINT32 a; // ip 4 address
Q_IPV6ADDR a6; // ip 6 address
bool isIp4;
friend class TQHostAddress;
};
TQHostAddressPrivate::TQHostAddressPrivate(TQ_UINT8 *a_) : a(0), isIp4(FALSE)
{
for ( int i=0; i<16; i++ ) {
a6.c[i] = a_[i];
}
}
TQHostAddressPrivate::TQHostAddressPrivate(const Q_IPV6ADDR &a_) : a(0), isIp4(FALSE)
{
a6 = a_;
}
/*!
\class TQHostAddress ntqhostaddress.h
\brief The TQHostAddress class provides an IP address.
\if defined(commercial)
It is part of the <a href="commercialeditions.html">TQt Enterprise Edition</a>.
\endif
\ingroup io
\module network
This class contains an IP address in a platform and protocol
independent manner. It stores both IPv4 and IPv6 addresses in a
way that you can easily access on any platform.
TQHostAddress is normally used with the classes TQSocket,
TQServerSocket and TQSocketDevice to set up a server or to connect
to a host.
Host addresses may be set with setAddress() and retrieved with
ip4Addr() or toString().
\sa TQSocket, TQServerSocket, TQSocketDevice
*/
/*!
Creates a host address object with the IP address 0.0.0.0.
*/
TQHostAddress::TQHostAddress()
: d( new TQHostAddressPrivate )
{
}
/*!
Creates a host address object for the IPv4 address \a ip4Addr.
*/
TQHostAddress::TQHostAddress( TQ_UINT32 ip4Addr )
: d( new TQHostAddressPrivate( ip4Addr ) )
{
}
/*!
Creates a host address object with the specified IPv6 address.
\a ip6Addr must be a 16 byte array in network byte order
(high-order byte first).
*/
TQHostAddress::TQHostAddress( TQ_UINT8 *ip6Addr )
: d( new TQHostAddressPrivate( ip6Addr ) )
{
}
/*!
Creates a host address object with the IPv6 address, \a ip6Addr.
*/
TQHostAddress::TQHostAddress(const Q_IPV6ADDR &ip6Addr)
: d(new TQHostAddressPrivate(ip6Addr))
{
}
// ### DOC: Can only make this public if we specify precisely the
// format of the address string.
/*!
\internal
*/
TQHostAddress::TQHostAddress(const TQString &address)
: d( new TQHostAddressPrivate )
{
setAddress( address );
}
/*!
Creates a copy of \a address.
*/
TQHostAddress::TQHostAddress( const TQHostAddress &address )
: d( new TQHostAddressPrivate )
{
*d = *(address.d);
}
/*!
Destroys the host address object.
*/
TQHostAddress::~TQHostAddress()
{
delete d;
}
/*!
Assigns another host address object \a address to this object and
returns a reference to this object.
*/
TQHostAddress & TQHostAddress::operator=( const TQHostAddress & address )
{
*d = *(address.d);
return *this;
}
/*!
Set the IPv4 address specified by \a ip4Addr.
*/
void TQHostAddress::setAddress( TQ_UINT32 ip4Addr )
{
delete d;
d = new TQHostAddressPrivate( ip4Addr );
}
/*!
\overload
Set the IPv6 address specified by \a ip6Addr.
\a ip6Addr must be a 16 byte array in network byte order
(high-order byte first).
*/
void TQHostAddress::setAddress( TQ_UINT8 *ip6Addr )
{
delete d;
d = new TQHostAddressPrivate( ip6Addr );
}
#ifndef TQT_NO_STRINGLIST
static bool parseIp4(const TQString& address, TQ_UINT32 *addr)
{
TQStringList ipv4 = TQStringList::split(".", address, FALSE);
if (ipv4.count() == 4) {
int i = 0;
bool ok = TRUE;
while(ok && i < 4) {
uint byteValue = ipv4[i].toUInt(&ok);
if (byteValue > 255)
ok = FALSE;
if (ok)
*addr = (*addr << 8) + byteValue;
++i;
}
if (ok)
return TRUE;
}
return FALSE;
}
/*!
\overload
Sets the IPv4 or IPv6 address specified by the string
representation \a address (e.g. "127.0.0.1"). Returns TRUE and
sets the address if the address was successfully parsed; otherwise
returns FALSE and leaves the address unchanged.
*/
bool TQHostAddress::setAddress(const TQString& address)
{
TQString a = address.simplifyWhiteSpace();
// try ipv4
TQ_UINT32 maybeIp4 = 0;
if (parseIp4(address, &maybeIp4)) {
setAddress(maybeIp4);
return TRUE;
}
// try ipv6
TQStringList ipv6 = TQStringList::split(":", a, TRUE);
int count = (int)ipv6.count();
if (count < 3)
return FALSE; // there must be at least two ":"
if (count > 8)
return FALSE; // maximum of seven ":" exceeded
TQ_UINT8 maybeIp6[16];
int mc = 16;
int fillCount = 9 - count;
for (int i=count-1; i>=0; --i) {
if ( mc <= 0 )
return FALSE;
if (ipv6[i].isEmpty()) {
if (i==count-1) {
// special case: ":" is last character
if (!ipv6[i-1].isEmpty())
return FALSE;
maybeIp6[--mc] = 0;
maybeIp6[--mc] = 0;
} else if (i==0) {
// special case: ":" is first character
if (!ipv6[i+1].isEmpty())
return FALSE;
maybeIp6[--mc] = 0;
maybeIp6[--mc] = 0;
} else {
for (int j=0; j<fillCount; ++j) {
if ( mc <= 0 )
return FALSE;
maybeIp6[--mc] = 0;
maybeIp6[--mc] = 0;
}
}
} else {
bool ok = FALSE;
uint byteValue = ipv6[i].toUInt(&ok, 16);
if (ok && byteValue <= 0xffff) {
maybeIp6[--mc] = byteValue & 0xff;
maybeIp6[--mc] = (byteValue >> 8) & 0xff;
} else {
if (i == count-1) {
// parse the ipv4 part of a mixed type
if (!parseIp4(ipv6[i], &maybeIp4))
return FALSE;
maybeIp6[--mc] = maybeIp4 & 0xff;
maybeIp6[--mc] = (maybeIp4 >> 8) & 0xff;
maybeIp6[--mc] = (maybeIp4 >> 16) & 0xff;
maybeIp6[--mc] = (maybeIp4 >> 24) & 0xff;
--fillCount;
} else {
return FALSE;
}
}
}
}
if (mc == 0) {
setAddress(maybeIp6);
return TRUE;
}
return FALSE;
}
#endif
/*!
\obsolete
Use isIPv4Address() instead.
*/
bool TQHostAddress::isIp4Addr() const
{
return isIPv4Address();
}
/*!
Returns TRUE if the host address represents an IPv4 address;
otherwise returns FALSE.
*/
bool TQHostAddress::isIPv4Address() const
{
return d->isIp4;
}
/*!
\obsolete
Use toIPv4Address() instead.
*/
TQ_UINT32 TQHostAddress::ip4Addr() const
{
return toIPv4Address();
}
/*!
Returns the IPv4 address as a number.
For example, if the address is 127.0.0.1, the returned value is
2130706433 (i.e. 0x7f000001).
This value is only valid when isIp4Addr() returns TRUE.
\sa toString()
*/
TQ_UINT32 TQHostAddress::toIPv4Address() const
{
return d->a;
}
/*!
Returns TRUE if the host address represents an IPv6 address;
otherwise returns FALSE.
*/
bool TQHostAddress::isIPv6Address() const
{
return !d->isIp4;
}
/*!
Returns the IPv6 address as a Q_IPV6ADDR structure. The structure
consists of 16 unsigned characters.
\code
Q_IPV6ADDR addr = hostAddr.ip6Addr();
// addr.c[] contains 16 unsigned characters
for (int i = 0; i < 16; ++i) {
// process addr.c[i]
}
\endcode
This value is only valid when isIPv6Address() returns TRUE.
\sa toString()
*/
Q_IPV6ADDR TQHostAddress::toIPv6Address() const
{
return d->a6;
}
#ifndef TQT_NO_SPRINTF
/*!
Returns the address as a string.
For example, if the address is the IPv4 address 127.0.0.1, the
returned string is "127.0.0.1".
\sa ip4Addr()
*/
TQString TQHostAddress::toString() const
{
if ( d->isIp4 ) {
TQ_UINT32 i = ip4Addr();
TQString s;
s.sprintf( "%d.%d.%d.%d", (i>>24) & 0xff, (i>>16) & 0xff,
(i >> 8) & 0xff, i & 0xff );
return s;
} else {
TQ_UINT16 ugle[8];
for ( int i=0; i<8; i++ ) {
ugle[i] = ( (TQ_UINT16)( d->a6.c[2*i] ) << 8 ) |
( (TQ_UINT16)( d->a6.c[2*i+1] ) );
}
TQString s;
s.sprintf( "%X:%X:%X:%X:%X:%X:%X:%X",
ugle[0], ugle[1], ugle[2], ugle[3],
ugle[4], ugle[5], ugle[6], ugle[7] );
return s;
}
}
#endif
/*!
Returns TRUE if this host address is the same as \a other;
otherwise returns FALSE.
*/
bool TQHostAddress::operator==( const TQHostAddress & other ) const
{
return d->a == other.d->a;
}
/*!
Returns TRUE if this host address is null (INADDR_ANY or in6addr_any). The
default constructor creates a null address, and that address isn't valid
for any particular host or interface.
*/
bool TQHostAddress::isNull() const
{
if ( d->isIp4 )
return d->a == 0;
int i = 0;
while( i < 16 ) {
if ( d->a6.c[i++] != 0 )
return FALSE;
}
return TRUE;
}
#endif //TQT_NO_NETWORK
|