Qt 4.8
Classes | Macros | Functions
qhostaddress.cpp File Reference
#include "qhostaddress.h"
#include "qhostaddress_p.h"
#include "qdebug.h"
#include "qplatformdefs.h"
#include "qstringlist.h"
#include "qendian.h"
#include <qdatastream.h>
#include <winsock.h>

Go to the source code of this file.

Classes

class  QHostAddressPrivate
 
struct  qt_in6_addr
 
struct  qt_sockaddr_in6
 

Macros

#define AF_INET6   23 /* Internetwork Version 6 */
 
#define QT_ENSURE_PARSED(a)
 
#define u_char   unsigned char
 
#define u_long   unsigned long
 
#define u_short   unsigned short
 

Functions

static void clearBits (quint8 *where, int start, int end)
 
QDebug operator<< (QDebug d, const QHostAddress &address)
 
static bool parseIp4 (const QString &address, quint32 *addr)
 
static bool parseIp6 (const QString &address, quint8 *addr, QString *scopeId)
 
uint qHash (const QHostAddress &key)
 

Macro Definition Documentation

◆ AF_INET6

#define AF_INET6   23 /* Internetwork Version 6 */

◆ QT_ENSURE_PARSED

#define QT_ENSURE_PARSED (   a)
Value:
do { \
if (!(a)->d->isParsed) \
(a)->d->parse(); \
} while (0)
double d
Definition: qnumeric_p.h:62
long ASN1_INTEGER_get ASN1_INTEGER * a

Definition at line 61 of file qhostaddress.cpp.

Referenced by QHostAddress::isInSubnet(), QHostAddress::isNull(), QHostAddress::operator==(), QHostAddress::protocol(), QHostAddress::scopeId(), QHostAddress::setScopeId(), QHostAddress::toIPv4Address(), QHostAddress::toIPv6Address(), and QHostAddress::toString().

◆ u_char

#define u_char   unsigned char

Definition at line 74 of file qhostaddress.cpp.

◆ u_long

#define u_long   unsigned long

Definition at line 80 of file qhostaddress.cpp.

◆ u_short

#define u_short   unsigned short

Definition at line 77 of file qhostaddress.cpp.

Function Documentation

◆ clearBits()

static void clearBits ( quint8 where,
int  start,
int  end 
)
static

Definition at line 374 of file qhostaddress.cpp.

Referenced by QHostAddress::parseSubnet(), and QNetmaskAddress::setPrefixLength().

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 }
unsigned char quint8
Definition: qglobal.h:934
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
static const KeyPair *const end

◆ operator<<()

QDebug operator<< ( QDebug  d,
const QHostAddress address 
)

Definition at line 1152 of file qhostaddress.cpp.

Referenced by operator==().

1153 {
1154  d.maybeSpace() << "QHostAddress(" << address.toString() << ')';
1155  return d.space();
1156 }
QString toString() const
Returns the address as a string.
QDebug & maybeSpace()
Writes a space character to the debug stream, depending on the last character sent to the stream...
Definition: qdebug.h:93
QDebug & space()
Writes a space character to the debug stream and returns a reference to the stream.
Definition: qdebug.h:91

◆ parseIp4()

static bool parseIp4 ( const QString address,
quint32 addr 
)
static

Definition at line 157 of file qhostaddress.cpp.

Referenced by QHostAddressPrivate::parse(), and parseIp6().

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 }
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
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
unsigned int uint
Definition: qglobal.h:996
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
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

◆ parseIp6()

static bool parseIp6 ( const QString address,
quint8 addr,
QString scopeId 
)
static

Definition at line 178 of file qhostaddress.cpp.

Referenced by QHostAddressPrivate::parse().

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 }
QBool contains(QChar c, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.h:904
void chop(int n)
Removes n characters from the end of the string.
Definition: qstring.cpp:4623
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
The QString class provides a Unicode character string.
Definition: qstring.h:83
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
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
unsigned int uint
Definition: qglobal.h:996
static bool parseIp4(const QString &address, quint32 *addr)
int count() const
Definition: qstring.h:103
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
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
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
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 QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55

◆ qHash()

uint qHash ( const QHostAddress key)

Definition at line 1159 of file qhostaddress.cpp.

Referenced by operator==(), and qHash().

1160 {
1161  return qHash(key.toString());
1162 }
QString toString() const
Returns the address as a string.
uint qHash(const QUrl &url)
Definition: qurl.h:285