Qt 4.8
qhostaddress.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtNetwork module of the Qt Toolkit.
7 **
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.
16 **
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.
24 **
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.
28 **
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.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qhostaddress.h"
43 #include "qhostaddress_p.h"
44 #include "qdebug.h"
45 #include "qplatformdefs.h"
46 #include "qstringlist.h"
47 #include "qendian.h"
48 #ifndef QT_NO_DATASTREAM
49 #include <qdatastream.h>
50 #endif
51 #if defined(Q_OS_WINCE)
52 #include <winsock.h>
53 #endif
54 
55 #ifdef QT_LINUXBASE
56 # include <arpa/inet.h>
57 #endif
58 
60 
61 #define QT_ENSURE_PARSED(a) \
62  do { \
63  if (!(a)->d->isParsed) \
64  (a)->d->parse(); \
65  } while (0)
66 
67 #ifdef Q_OS_WIN
68 # if !defined (QT_NO_IPV6)
69 // sockaddr_in6 size changed between old and new SDK
70 // Only the new version is the correct one, so always
71 // use this structure.
72 #if defined(Q_OS_WINCE)
73 # if !defined(u_char)
74 # define u_char unsigned char
75 # endif
76 # if !defined(u_short)
77 # define u_short unsigned short
78 # endif
79 # if !defined(u_long)
80 # define u_long unsigned long
81 # endif
82 #endif
83 struct qt_in6_addr {
85 };
86 typedef struct {
87  short sin6_family; /* AF_INET6 */
88  u_short sin6_port; /* Transport level port number */
89  u_long sin6_flowinfo; /* IPv6 flow information */
90  struct qt_in6_addr sin6_addr; /* IPv6 address */
91  u_long sin6_scope_id; /* set of interfaces for a scope */
93 # else
94 typedef void * qt_sockaddr_in6 ;
95 # endif
96 # ifndef AF_INET6
97 # define AF_INET6 23 /* Internetwork Version 6 */
98 # endif
99 #else
100 #define qt_sockaddr_in6 sockaddr_in6
101 #define qt_s6_addr s6_addr
102 #endif
103 
104 
106 {
107 public:
109 
110  void setAddress(quint32 a_ = 0);
111  void setAddress(const quint8 *a_);
112  void setAddress(const Q_IPV6ADDR &a_);
113 
114  bool parse();
115  void clear();
116 
117  quint32 a; // IPv4 address
118  Q_IPV6ADDR a6; // IPv6 address
120 
122  bool isParsed;
124 
125  friend class QHostAddress;
126 };
127 
129  : a(0), protocol(QAbstractSocket::UnknownNetworkLayerProtocol), isParsed(true)
130 {
131  memset(&a6, 0, sizeof(a6));
132 }
133 
135 {
136  a = a_;
138  isParsed = true;
139 }
140 
142 {
143  for (int i = 0; i < 16; i++)
144  a6[i] = a_[i];
146  isParsed = true;
147 }
148 
150 {
151  a6 = a_;
152  a = 0;
154  isParsed = true;
155 }
156 
157 static bool parseIp4(const QString& address, quint32 *addr)
158 {
159  QStringList ipv4 = address.split(QLatin1String("."));
160  if (ipv4.count() != 4)
161  return false;
162 
163  quint32 ipv4Address = 0;
164  for (int i = 0; i < 4; ++i) {
165  bool ok = false;
166  uint byteValue = ipv4.at(i).toUInt(&ok);
167  if (!ok || byteValue > 255)
168  return false;
169 
170  ipv4Address <<= 8;
171  ipv4Address += byteValue;
172  }
173 
174  *addr = ipv4Address;
175  return true;
176 }
177 
178 static bool parseIp6(const QString &address, quint8 *addr, QString *scopeId)
179 {
180  QString tmp = address;
181  int scopeIdPos = tmp.lastIndexOf(QLatin1Char('%'));
182  if (scopeIdPos != -1) {
183  *scopeId = tmp.mid(scopeIdPos + 1);
184  tmp.chop(tmp.size() - scopeIdPos);
185  } else {
186  scopeId->clear();
187  }
188 
189  QStringList ipv6 = tmp.split(QLatin1String(":"));
190  int count = ipv6.count();
191  if (count < 3 || count > 8)
192  return false;
193 
194  int colonColon = tmp.count(QLatin1String("::"));
195  if(count == 8 && colonColon > 1)
196  return false;
197 
198  // address can be compressed with a "::", but that
199  // may only appear once (see RFC 1884)
200  // the statement below means:
201  // if(shortened notation is not used AND
202  // ((pure IPv6 notation AND less than 8 parts) OR
203  // ((mixed IPv4/6 notation AND less than 7 parts)))
204  if(colonColon != 1 && count < (tmp.contains(QLatin1Char('.')) ? 7 : 8))
205  return false;
206 
207  int mc = 16;
208  int fillCount = 9 - count; // number of 0 words to fill in the middle
209  for (int i = count - 1; i >= 0; --i) {
210  if (mc <= 0)
211  return false;
212 
213  if (ipv6.at(i).isEmpty()) {
214  if (i == count - 1) {
215  // special case: ":" is last character
216  if (!ipv6.at(i - 1).isEmpty())
217  return false;
218  addr[--mc] = 0;
219  addr[--mc] = 0;
220  } else if (i == 0) {
221  // special case: ":" is first character
222  if (!ipv6.at(i + 1).isEmpty())
223  return false;
224  addr[--mc] = 0;
225  addr[--mc] = 0;
226  } else {
227  for (int j = 0; j < fillCount; ++j) {
228  if (mc <= 0)
229  return false;
230  addr[--mc] = 0;
231  addr[--mc] = 0;
232  }
233  }
234  } else {
235  bool ok = false;
236  uint byteValue = ipv6.at(i).toUInt(&ok, 16);
237  if (ok && byteValue <= 0xffff) {
238  addr[--mc] = byteValue & 0xff;
239  addr[--mc] = (byteValue >> 8) & 0xff;
240  } else {
241  if (i != count - 1)
242  return false;
243 
244  // parse the ipv4 part of a mixed type
245  quint32 maybeIp4;
246  if (!parseIp4(ipv6.at(i), &maybeIp4))
247  return false;
248 
249  addr[--mc] = maybeIp4 & 0xff;
250  addr[--mc] = (maybeIp4 >> 8) & 0xff;
251  addr[--mc] = (maybeIp4 >> 16) & 0xff;
252  addr[--mc] = (maybeIp4 >> 24) & 0xff;
253  --fillCount;
254  }
255  }
256  }
257 
258  return true;
259 }
260 
262 {
263  isParsed = true;
266 
267  // All IPv6 addresses contain a ':', and may contain a '.'.
268  if (a.contains(QLatin1Char(':'))) {
269  quint8 maybeIp6[16];
270  if (parseIp6(a, maybeIp6, &scopeId)) {
271  setAddress(maybeIp6);
273  return true;
274  }
275  }
276 
277  // All IPv4 addresses contain a '.'.
278  if (a.contains(QLatin1Char('.'))) {
279  quint32 maybeIp4 = 0;
280  if (parseIp4(a, &maybeIp4)) {
281  setAddress(maybeIp4);
283  return true;
284  }
285  }
286 
287  return false;
288 }
289 
291 {
292  a = 0;
294  isParsed = true;
295  memset(&a6, 0, sizeof(a6));
296 }
297 
298 
300 {
301  length = -1;
302  QHostAddress other;
303  return other.setAddress(address) && setAddress(other);
304 }
305 
307 {
308  static const quint8 zeroes[16] = { 0 };
309  union {
310  quint32 v4;
311  quint8 v6[16];
312  } ip;
313 
314  int netmask = 0;
315  quint8 *ptr = ip.v6;
316  quint8 *end;
317  length = -1;
318 
319  QHostAddress::operator=(address);
320 
321  if (d->protocol == QAbstractSocket::IPv4Protocol) {
322  ip.v4 = qToBigEndian(d->a);
323  end = ptr + 4;
324  } else if (d->protocol == QAbstractSocket::IPv6Protocol) {
325  memcpy(ip.v6, d->a6.c, 16);
326  end = ptr + 16;
327  } else {
328  d->clear();
329  return false;
330  }
331 
332  while (ptr < end) {
333  switch (*ptr) {
334  case 255:
335  netmask += 8;
336  ++ptr;
337  continue;
338 
339  default:
340  d->clear();
341  return false; // invalid IP-style netmask
342 
343  // the rest always falls through
344  case 254:
345  ++netmask;
346  case 252:
347  ++netmask;
348  case 248:
349  ++netmask;
350  case 240:
351  ++netmask;
352  case 224:
353  ++netmask;
354  case 192:
355  ++netmask;
356  case 128:
357  ++netmask;
358  case 0:
359  break;
360  }
361  break;
362  }
363 
364  // confirm that the rest is only zeroes
365  if (ptr < end && memcmp(ptr + 1, zeroes, end - ptr - 1) != 0) {
366  d->clear();
367  return false;
368  }
369 
370  length = netmask;
371  return true;
372 }
373 
374 static void clearBits(quint8 *where, int start, int end)
375 {
376  Q_ASSERT(end == 32 || end == 128);
377  if (start == end)
378  return;
379 
380  // for the byte where 'start' is, clear the lower bits only
381  quint8 bytemask = 256 - (1 << (8 - (start & 7)));
382  where[start / 8] &= bytemask;
383 
384  // for the tail part, clear everything
385  memset(where + (start + 7) / 8, 0, end / 8 - (start + 7) / 8);
386 }
387 
389 {
390  return length;
391 }
392 
394 {
395  length = newLength;
396  if (length < 0 || length > (proto == QAbstractSocket::IPv4Protocol ? 32 :
397  proto == QAbstractSocket::IPv6Protocol ? 128 : -1)) {
398  // invalid information, reject
400  length = -1;
401  return;
402  }
403 
404  d->protocol = proto;
405  if (d->protocol == QAbstractSocket::IPv4Protocol) {
406  if (length == 0) {
407  d->a = 0;
408  } else if (length == 32) {
409  d->a = quint32(0xffffffff);
410  } else {
411  d->a = quint32(0xffffffff) >> (32 - length) << (32 - length);
412  }
413  } else {
414  memset(d->a6.c, 0xFF, sizeof(d->a6));
415  clearBits(d->a6.c, length, 128);
416  }
417 }
418 
461  : d(new QHostAddressPrivate)
462 {
463 }
464 
469  : d(new QHostAddressPrivate)
470 {
471  setAddress(ip4Addr);
472 }
473 
481  : d(new QHostAddressPrivate)
482 {
483  setAddress(ip6Addr);
484 }
485 
490  : d(new QHostAddressPrivate)
491 {
492  setAddress(ip6Addr);
493 }
494 
502  : d(new QHostAddressPrivate)
503 {
504  d->ipString = address;
505  d->isParsed = false;
506 }
507 
516 QHostAddress::QHostAddress(const struct sockaddr *sockaddr)
517  : d(new QHostAddressPrivate)
518 {
519  if (sockaddr->sa_family == AF_INET)
520  setAddress(htonl(((sockaddr_in *)sockaddr)->sin_addr.s_addr));
521 #ifndef QT_NO_IPV6
522  else if (sockaddr->sa_family == AF_INET6)
523  setAddress(((qt_sockaddr_in6 *)sockaddr)->sin6_addr.qt_s6_addr);
524 #endif
525 }
526 
531  : d(new QHostAddressPrivate(*address.d.data()))
532 {
533 }
534 
539  : d(new QHostAddressPrivate)
540 {
541  switch (address) {
542  case Null:
543  break;
544  case Broadcast:
545  setAddress(QLatin1String("255.255.255.255"));
546  break;
547  case LocalHost:
548  setAddress(QLatin1String("127.0.0.1"));
549  break;
550  case LocalHostIPv6:
551  setAddress(QLatin1String("::1"));
552  break;
553  case Any:
554  setAddress(QLatin1String("0.0.0.0"));
555  break;
556  case AnyIPv6:
557  setAddress(QLatin1String("::"));
558  break;
559  }
560 }
561 
566 {
567 }
568 
574 {
575  *d.data() = *address.d.data();
576  return *this;
577 }
578 
586 {
587  setAddress(address);
588  return *this;
589 }
590 
616 {
617  d->clear();
618 }
619 
624 {
625  d->setAddress(ip4Addr);
626 }
627 
640 {
641  d->setAddress(ip6Addr);
642 }
643 
653 {
654  d->setAddress(ip6Addr);
655 }
656 
668 bool QHostAddress::setAddress(const QString &address)
669 {
670  d->ipString = address;
671  return d->parse();
672 }
673 
682 void QHostAddress::setAddress(const struct sockaddr *sockaddr)
683 {
684  clear();
685  if (sockaddr->sa_family == AF_INET)
686  setAddress(htonl(((sockaddr_in *)sockaddr)->sin_addr.s_addr));
687 #ifndef QT_NO_IPV6
688  else if (sockaddr->sa_family == AF_INET6)
689  setAddress(((qt_sockaddr_in6 *)sockaddr)->sin6_addr.qt_s6_addr);
690 #endif
691 }
692 
705 {
706  QT_ENSURE_PARSED(this);
707  return d->a;
708 }
709 
714 {
715  QT_ENSURE_PARSED(this);
716  return d->protocol;
717 }
718 
731 {
732  QT_ENSURE_PARSED(this);
733  return d->a6;
734 }
735 
746 {
747  QT_ENSURE_PARSED(this);
749  quint32 i = toIPv4Address();
750  QString s;
751  s.sprintf("%d.%d.%d.%d", (i>>24) & 0xff, (i>>16) & 0xff,
752  (i >> 8) & 0xff, i & 0xff);
753  return s;
754  }
755 
757  quint16 ugle[8];
758  for (int i = 0; i < 8; i++) {
759  ugle[i] = (quint16(d->a6[2*i]) << 8) | quint16(d->a6[2*i+1]);
760  }
761  QString s;
762  QString temp;
763  bool zeroDetected = false;
764  bool zeroShortened = false;
765  for (int i = 0; i < 8; i++) {
766  if ((ugle[i] != 0) || zeroShortened) {
767  temp.sprintf("%X", ugle[i]);
768  s.append(temp);
769  if (zeroDetected)
770  zeroShortened = true;
771  } else {
772  if (!zeroDetected) {
773  if (i<7 && (ugle[i+1] == 0)) {
774  s.append(QLatin1Char(':'));
775  zeroDetected = true;
776  } else {
777  temp.sprintf("%X", ugle[i]);
778  s.append(temp);
779  if (i<7)
780  s.append(QLatin1Char(':'));
781  }
782  }
783  }
784  if (i<7 && ((ugle[i] != 0) || zeroShortened || (i==0 && zeroDetected)))
785  s.append(QLatin1Char(':'));
786  }
787 
788  if (!d->scopeId.isEmpty())
789  s.append(QLatin1Char('%') + d->scopeId);
790  return s;
791  }
792 
793  return QString();
794 }
795 
841 {
842  QT_ENSURE_PARSED(this);
844 }
845 
856 {
857  QT_ENSURE_PARSED(this);
859  d->scopeId = id;
860 }
861 
866 bool QHostAddress::operator==(const QHostAddress &other) const
867 {
868  QT_ENSURE_PARSED(this);
869  QT_ENSURE_PARSED(&other);
870 
872  return other.d->protocol == QAbstractSocket::IPv4Protocol && d->a == other.d->a;
874  return other.d->protocol == QAbstractSocket::IPv6Protocol
875  && memcmp(&d->a6, &other.d->a6, sizeof(Q_IPV6ADDR)) == 0;
876  }
877  return d->protocol == other.d->protocol;
878 }
879 
885 {
886  QT_ENSURE_PARSED(this);
887  QHostAddress otherAddress(other);
888  QT_ENSURE_PARSED(&otherAddress);
889 
891  return otherAddress.d->protocol == QAbstractSocket::IPv4Protocol && d->a == otherAddress.d->a;
893  return otherAddress.d->protocol == QAbstractSocket::IPv6Protocol
894  && memcmp(&d->a6, &otherAddress.d->a6, sizeof(Q_IPV6ADDR)) == 0;
895  }
896  return int(other) == int(Null);
897 }
898 
905 {
906  QT_ENSURE_PARSED(this);
908 }
909 
968 bool QHostAddress::isInSubnet(const QHostAddress &subnet, int netmask) const
969 {
970  QT_ENSURE_PARSED(this);
971  if (subnet.protocol() != d->protocol || netmask < 0)
972  return false;
973 
974  union {
975  quint32 ip;
976  quint8 data[4];
977  } ip4, net4;
978  const quint8 *ip;
979  const quint8 *net;
981  if (netmask > 32)
982  netmask = 32;
983  ip4.ip = qToBigEndian(d->a);
984  net4.ip = qToBigEndian(subnet.d->a);
985  ip = ip4.data;
986  net = net4.data;
987  } else if (d->protocol == QAbstractSocket::IPv6Protocol) {
988  if (netmask > 128)
989  netmask = 128;
990  ip = d->a6.c;
991  net = subnet.d->a6.c;
992  } else {
993  return false;
994  }
995 
996  if (netmask >= 8 && memcmp(ip, net, netmask / 8) != 0)
997  return false;
998  if ((netmask & 7) == 0)
999  return true;
1000 
1001  // compare the last octet now
1002  quint8 bytemask = 256 - (1 << (8 - (netmask & 7)));
1003  quint8 ipbyte = ip[netmask / 8];
1004  quint8 netbyte = net[netmask / 8];
1005  return (ipbyte & bytemask) == (netbyte & bytemask);
1006 }
1007 
1021 {
1022  return isInSubnet(subnet.first, subnet.second);
1023 }
1024 
1025 
1054 {
1055  // We support subnets in the form:
1056  // ddd.ddd.ddd.ddd/nn
1057  // ddd.ddd.ddd/nn
1058  // ddd.ddd/nn
1059  // ddd/nn
1060  // ddd.ddd.ddd.
1061  // ddd.ddd.ddd
1062  // ddd.ddd.
1063  // ddd.ddd
1064  // ddd.
1065  // ddd
1066  // <ipv6-address>/nn
1067  //
1068  // where nn can be an IPv4-style netmask for the IPv4 forms
1069 
1070  const QPair<QHostAddress, int> invalid = qMakePair(QHostAddress(), -1);
1071  if (subnet.isEmpty())
1072  return invalid;
1073 
1074  int slash = subnet.indexOf(QLatin1Char('/'));
1075  QString netStr = subnet;
1076  if (slash != -1)
1077  netStr.truncate(slash);
1078 
1079  int netmask = -1;
1080  bool isIpv6 = netStr.contains(QLatin1Char(':'));
1081 
1082  if (slash != -1) {
1083  // is the netmask given in IP-form or in bit-count form?
1084  if (!isIpv6 && subnet.indexOf(QLatin1Char('.'), slash + 1) != -1) {
1085  // IP-style, convert it to bit-count form
1086  QNetmaskAddress parser;
1087  if (!parser.setAddress(subnet.mid(slash + 1)))
1088  return invalid;
1089  netmask = parser.prefixLength();
1090  } else {
1091  bool ok;
1092  netmask = subnet.mid(slash + 1).toUInt(&ok);
1093  if (!ok)
1094  return invalid; // failed to parse the subnet
1095  }
1096  }
1097 
1098  if (isIpv6) {
1099  // looks like it's an IPv6 address
1100  if (netmask > 128)
1101  return invalid; // invalid netmask
1102  if (netmask < 0)
1103  netmask = 128;
1104 
1105  QHostAddress net;
1106  if (!net.setAddress(netStr))
1107  return invalid; // failed to parse the IP
1108 
1109  clearBits(net.d->a6.c, netmask, 128);
1110  return qMakePair(net, netmask);
1111  }
1112 
1113  if (netmask > 32)
1114  return invalid; // invalid netmask
1115 
1116  // parse the address manually
1117  QStringList parts = netStr.split(QLatin1Char('.'));
1118  if (parts.isEmpty() || parts.count() > 4)
1119  return invalid; // invalid IPv4 address
1120 
1121  if (parts.last().isEmpty())
1122  parts.removeLast();
1123 
1124  quint32 addr = 0;
1125  for (int i = 0; i < parts.count(); ++i) {
1126  bool ok;
1127  uint byteValue = parts.at(i).toUInt(&ok);
1128  if (!ok || byteValue > 255)
1129  return invalid; // invalid IPv4 address
1130 
1131  addr <<= 8;
1132  addr += byteValue;
1133  }
1134  addr <<= 8 * (4 - parts.count());
1135  if (netmask == -1) {
1136  netmask = 8 * parts.count();
1137  } else if (netmask == 0) {
1138  // special case here
1139  // x86's instructions "shr" and "shl" do not operate when
1140  // their argument is 32, so the code below doesn't work as expected
1141  addr = 0;
1142  } else if (netmask != 32) {
1143  // clear remaining bits
1144  quint32 mask = quint32(0xffffffff) >> (32 - netmask) << (32 - netmask);
1145  addr &= mask;
1146  }
1147 
1148  return qMakePair(QHostAddress(addr), netmask);
1149 }
1150 
1151 #ifndef QT_NO_DEBUG_STREAM
1153 {
1154  d.maybeSpace() << "QHostAddress(" << address.toString() << ')';
1155  return d.space();
1156 }
1157 #endif
1158 
1160 {
1161  return qHash(key.toString());
1162 }
1163 
1164 #ifndef QT_NO_DATASTREAM
1165 
1177 {
1178  qint8 prot;
1179  prot = qint8(address.protocol());
1180  out << prot;
1181  switch (address.protocol()) {
1183  break;
1185  out << address.toIPv4Address();
1186  break;
1188  {
1189  Q_IPV6ADDR ipv6 = address.toIPv6Address();
1190  for (int i = 0; i < 16; ++i)
1191  out << ipv6[i];
1192  out << address.scopeId();
1193  }
1194  break;
1195  }
1196  return out;
1197 }
1198 
1210 {
1211  qint8 prot;
1212  in >> prot;
1213  switch (QAbstractSocket::NetworkLayerProtocol(prot)) {
1215  address.clear();
1216  break;
1218  {
1219  quint32 ipv4;
1220  in >> ipv4;
1221  address.setAddress(ipv4);
1222  }
1223  break;
1225  {
1226  Q_IPV6ADDR ipv6;
1227  for (int i = 0; i < 16; ++i)
1228  in >> ipv6[i];
1229  address.setAddress(ipv6);
1230 
1231  QString scope;
1232  in >> scope;
1233  address.setScopeId(scope);
1234  }
1235  break;
1236  default:
1237  address.clear();
1239  }
1240  return in;
1241 }
1242 
1243 #endif //QT_NO_DATASTREAM
1244 
void setScopeId(const QString &id)
Sets the IPv6 scope ID of the address to id.
QBool contains(QChar c, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.h:904
The QDebug class provides an output stream for debugging information.
Definition: qdebug.h:62
double d
Definition: qnumeric_p.h:62
QString scopeId() const
Returns the scope ID of an IPv6 address.
quint8 c[16]
Definition: qhostaddress.h:65
QString & sprintf(const char *format,...)
Safely builds a formatted string from the format string cformat and an arbitrary list of arguments...
Definition: qstring.cpp:5567
bool isNull() const
Returns true if this host address is null (INADDR_ANY or in6addr_any).
Q_IPV6ADDR toIPv6Address() const
Returns the IPv6 address as a Q_IPV6ADDR structure.
QHostAddress & operator=(const QHostAddress &other)
Assigns another host address to this object, and returns a reference to this object.
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
T * data() const
Returns the value of the pointer referenced by this object.
QScopedPointer< QHostAddressPrivate > d
Definition: qhostaddress.h:134
void chop(int n)
Removes n characters from the end of the string.
Definition: qstring.cpp:4623
T1 first
Definition: qpair.h:65
T2 second
Definition: qpair.h:66
static void clear(QVariant::Private *d)
Definition: qvariant.cpp:197
void removeLast()
Removes the last item in the list.
Definition: qlist.h:287
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
QAbstractSocket::NetworkLayerProtocol protocol
long ASN1_INTEGER_get ASN1_INTEGER * a
void setStatus(Status status)
Sets the status of the data stream to the status given.
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
unsigned char quint8
Definition: qglobal.h:934
quint32 toIPv4Address() const
Returns the IPv4 address as a number.
NetworkLayerProtocol
This enum describes the network layer protocol values used in Qt.
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
QDataStream & operator>>(QDataStream &in, QHostAddress &address)
Reads a host address into address from the stream in and returns a reference to the stream...
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
Definition: qlist.h:152
#define u_char
void clear()
Sets the host address to 0.0.0.0.
signed char qint8
Definition: qglobal.h:933
uint qHash(const QHostAddress &key)
bool operator==(const QHostAddress &address) const
Returns true if this host address is the same as the other address given; otherwise returns false...
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
#define u_long
void truncate(int pos)
Truncates the string at the given position index.
Definition: qstring.cpp:4603
~QHostAddress()
Destroys the host address object.
int size() const
Returns the number of characters in this string.
Definition: qstring.h:102
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
void setAddress(quint32 a_=0)
static QPair< QHostAddress, int > parseSubnet(const QString &subnet)
Parses the IP and subnet information contained in subnet and returns the network prefix for that netw...
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
T qToBigEndian(T source)
Definition: qendian.h:337
unsigned short quint16
Definition: qglobal.h:936
static const char * data(const QByteArray &arr)
unsigned int uint
Definition: qglobal.h:996
int indexOf(QChar c, int from=0, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.cpp:2838
#define u_short
QString toString() const
Returns the address as a string.
const T * ptr(const T &t)
QDebug & maybeSpace()
Writes a space character to the debug stream, depending on the last character sent to the stream...
Definition: qdebug.h:93
static bool parseIp4(const QString &address, quint32 *addr)
static bool parseIp6(const QString &address, quint8 *addr, QString *scopeId)
int count() const
Definition: qstring.h:103
bool isInSubnet(const QHostAddress &subnet, int netmask) const
Returns true if this IP is in the subnet described by the network prefix subnet and netmask netmask...
QHostAddress()
Constructs a host address object with the IP address 0.0.0.0.
QString mid(int position, int n=-1) const Q_REQUIRED_RESULT
Returns a string that contains n characters of this string, starting at the specified position index...
Definition: qstring.cpp:3706
QString & append(QChar c)
Definition: qstring.cpp:1777
QAbstractSocket::NetworkLayerProtocol protocol() const
Returns the network layer protocol of the host address.
void setAddress(quint32 ip4Addr)
Set the IPv4 address specified by ip4Addr.
#define QT_ENSURE_PARSED(a)
u_char qt_s6_addr[16]
QString simplified() const Q_REQUIRED_RESULT
Returns a string that has whitespace removed from the start and the end, and that has each sequence o...
Definition: qstring.cpp:4415
void clear()
Clears the contents of the string and makes it empty.
Definition: qstring.h:723
int lastIndexOf(QChar c, int from=-1, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.cpp:3000
QDataStream & operator<<(QDataStream &out, const QHostAddress &address)
Writes host address address to the stream out and returns a reference to the stream.
Q_OUTOFLINE_TEMPLATE QPair< T1, T2 > qMakePair(const T1 &x, const T2 &y)
Definition: qpair.h:102
int key
T & last()
Returns a reference to the last item in the list.
Definition: qlist.h:284
unsigned int quint32
Definition: qglobal.h:938
uint toUInt(bool *ok=0, int base=10) const
Returns the string converted to an unsigned int using base base, which is 10 by default and must be b...
Definition: qstring.cpp:6120
static void clearBits(quint8 *where, int start, int end)
QStringList split(const QString &sep, SplitBehavior behavior=KeepEmptyParts, Qt::CaseSensitivity cs=Qt::CaseSensitive) const Q_REQUIRED_RESULT
Splits the string into substrings wherever sep occurs, and returns the list of those strings...
Definition: qstring.cpp:6526
The QDataStream class provides serialization of binary data to a QIODevice.
Definition: qdatastream.h:71
The QHostAddress class provides an IP address.
Definition: qhostaddress.h:70
void setPrefixLength(QAbstractSocket::NetworkLayerProtocol proto, int len)
static const KeyPair *const end
QDebug & space()
Writes a space character to the debug stream and returns a reference to the stream.
Definition: qdebug.h:91
int prefixLength() const
The QAbstractSocket class provides the base functionality common to all socket types.
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
bool setAddress(const QString &address)
#define AF_INET6