Qt 4.8
Static Public Functions | List of all members
QHostInfoAgent Class Reference

#include <qhostinfo_p.h>

Inheritance diagram for QHostInfoAgent:
QObject

Static Public Functions

static QHostInfo fromName (const QString &hostName)
 
static QHostInfo fromName (const QString &hostName, QSharedPointer< QNetworkSession > networkSession)
 
- Static Public Functions inherited from QObject
static bool connect (const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
 Creates a connection of the given type from the signal in the sender object to the method in the receiver object. More...
 
static bool connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *member)
 Disconnects signal in object sender from method in object receiver. More...
 
static bool disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &member)
 
static uint registerUserData ()
 
static QString tr (const char *sourceText, const char *comment=0, int n=-1)
 
static QString trUtf8 (const char *sourceText, const char *comment=0, int n=-1)
 

Additional Inherited Members

- Public Slots inherited from QObject
void deleteLater ()
 Schedules this object for deletion. More...
 
- Signals inherited from QObject
void destroyed (QObject *=0)
 This signal is emitted immediately before the object obj is destroyed, and can not be blocked. More...
 
- Public Functions inherited from QObject
bool blockSignals (bool b)
 If block is true, signals emitted by this object are blocked (i.e., emitting a signal will not invoke anything connected to it). More...
 
const QObjectListchildren () const
 Returns a list of child objects. More...
 
bool connect (const QObject *sender, const char *signal, const char *member, Qt::ConnectionType type=Qt::AutoConnection) const
 
bool disconnect (const char *signal=0, const QObject *receiver=0, const char *member=0)
 
bool disconnect (const QObject *receiver, const char *member=0)
 
void dumpObjectInfo ()
 Dumps information about signal connections, etc. More...
 
void dumpObjectTree ()
 Dumps a tree of children to the debug output. More...
 
QList< QByteArraydynamicPropertyNames () const
 Returns the names of all properties that were dynamically added to the object using setProperty(). More...
 
virtual bool event (QEvent *)
 This virtual function receives events to an object and should return true if the event e was recognized and processed. More...
 
virtual bool eventFilter (QObject *, QEvent *)
 Filters events if this object has been installed as an event filter for the watched object. More...
 
template<typename T >
findChild (const QString &aName=QString()) const
 Returns the child of this object that can be cast into type T and that is called name, or 0 if there is no such object. More...
 
template<typename T >
QList< T > findChildren (const QString &aName=QString()) const
 Returns all children of this object with the given name that can be cast to type T, or an empty list if there are no such objects. More...
 
template<typename T >
QList< T > findChildren (const QRegExp &re) const
 
bool inherits (const char *classname) const
 Returns true if this object is an instance of a class that inherits className or a QObject subclass that inherits className; otherwise returns false. More...
 
void installEventFilter (QObject *)
 Installs an event filter filterObj on this object. More...
 
bool isWidgetType () const
 Returns true if the object is a widget; otherwise returns false. More...
 
void killTimer (int id)
 Kills the timer with timer identifier, id. More...
 
virtual const QMetaObjectmetaObject () const
 Returns a pointer to the meta-object of this object. More...
 
void moveToThread (QThread *thread)
 Changes the thread affinity for this object and its children. More...
 
QString objectName () const
 
QObjectparent () const
 Returns a pointer to the parent object. More...
 
QVariant property (const char *name) const
 Returns the value of the object's name property. More...
 
Q_INVOKABLE QObject (QObject *parent=0)
 Constructs an object with parent object parent. More...
 
void removeEventFilter (QObject *)
 Removes an event filter object obj from this object. More...
 
void setObjectName (const QString &name)
 
void setParent (QObject *)
 Makes the object a child of parent. More...
 
bool setProperty (const char *name, const QVariant &value)
 Sets the value of the object's name property to value. More...
 
void setUserData (uint id, QObjectUserData *data)
 
bool signalsBlocked () const
 Returns true if signals are blocked; otherwise returns false. More...
 
int startTimer (int interval)
 Starts a timer and returns a timer identifier, or returns zero if it could not start a timer. More...
 
QThreadthread () const
 Returns the thread in which the object lives. More...
 
QObjectUserDatauserData (uint id) const
 
virtual ~QObject ()
 Destroys the object, deleting all its child objects. More...
 
- Static Public Variables inherited from QObject
static const QMetaObject staticMetaObject
 This variable stores the meta-object for the class. More...
 
- Protected Functions inherited from QObject
virtual void childEvent (QChildEvent *)
 This event handler can be reimplemented in a subclass to receive child events. More...
 
virtual void connectNotify (const char *signal)
 This virtual function is called when something has been connected to signal in this object. More...
 
virtual void customEvent (QEvent *)
 This event handler can be reimplemented in a subclass to receive custom events. More...
 
virtual void disconnectNotify (const char *signal)
 This virtual function is called when something has been disconnected from signal in this object. More...
 
 QObject (QObjectPrivate &dd, QObject *parent=0)
 
int receivers (const char *signal) const
 Returns the number of receivers connected to the signal. More...
 
QObjectsender () const
 Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; otherwise it returns 0. More...
 
int senderSignalIndex () const
 
virtual void timerEvent (QTimerEvent *)
 This event handler can be reimplemented in a subclass to receive timer events for the object. More...
 
- Protected Variables inherited from QObject
QScopedPointer< QObjectDatad_ptr
 
- Static Protected Variables inherited from QObject
static const QMetaObject staticQtMetaObject
 

Detailed Description

Definition at line 99 of file qhostinfo_p.h.

Functions

◆ fromName() [1/2]

QHostInfo QHostInfoAgent::fromName ( const QString hostName)
static

Definition at line 124 of file qhostinfo_unix.cpp.

Referenced by QHostInfo::fromName(), fromName(), QHostInfoPrivate::fromName(), QHostInfoRunnable::run(), and translateWSAError().

125 {
126  QHostInfo results;
127 
128 #if defined(QHOSTINFO_DEBUG)
129  qDebug("QHostInfoAgent::fromName(%s) looking up...",
130  hostName.toLatin1().constData());
131 #endif
132 
133  // Load res_init on demand.
134  static volatile bool triedResolve = false;
135  if (!triedResolve) {
137  if (!triedResolve) {
138  resolveLibrary();
139  triedResolve = true;
140  }
141  }
142 
143  // If res_init is available, poll it.
144  if (local_res_init)
145  local_res_init();
146 
147  QHostAddress address;
148  if (address.setAddress(hostName)) {
149  // Reverse lookup
150 // Reverse lookups using getnameinfo are broken on darwin, use gethostbyaddr instead.
151 #if !defined (QT_NO_GETADDRINFO) && !defined (Q_OS_DARWIN)
152  sockaddr_in sa4;
153 #ifndef QT_NO_IPV6
154  sockaddr_in6 sa6;
155 #endif
156  sockaddr *sa = 0;
157  QT_SOCKLEN_T saSize = 0;
158  if (address.protocol() == QAbstractSocket::IPv4Protocol) {
159  sa = (sockaddr *)&sa4;
160  saSize = sizeof(sa4);
161  memset(&sa4, 0, sizeof(sa4));
162  sa4.sin_family = AF_INET;
163  sa4.sin_addr.s_addr = htonl(address.toIPv4Address());
164  }
165 #ifndef QT_NO_IPV6
166  else {
167  sa = (sockaddr *)&sa6;
168  saSize = sizeof(sa6);
169  memset(&sa6, 0, sizeof(sa6));
170  sa6.sin6_family = AF_INET6;
171  memcpy(sa6.sin6_addr.s6_addr, address.toIPv6Address().c, sizeof(sa6.sin6_addr.s6_addr));
172  }
173 #endif
174 
175  char hbuf[NI_MAXHOST];
176  if (sa && getnameinfo(sa, saSize, hbuf, sizeof(hbuf), 0, 0, 0) == 0)
177  results.setHostName(QString::fromLatin1(hbuf));
178 #else
179  in_addr_t inetaddr = qt_safe_inet_addr(hostName.toLatin1().constData());
180  struct hostent *ent = gethostbyaddr((const char *)&inetaddr, sizeof(inetaddr), AF_INET);
181  if (ent)
182  results.setHostName(QString::fromLatin1(ent->h_name));
183 #endif
184 
185  if (results.hostName().isEmpty())
186  results.setHostName(address.toString());
187  results.setAddresses(QList<QHostAddress>() << address);
188  return results;
189  }
190 
191  // IDN support
192  QByteArray aceHostname = QUrl::toAce(hostName);
193  results.setHostName(hostName);
194  if (aceHostname.isEmpty()) {
196  results.setErrorString(hostName.isEmpty() ?
197  QCoreApplication::translate("QHostInfoAgent", "No host name given") :
198  QCoreApplication::translate("QHostInfoAgent", "Invalid hostname"));
199  return results;
200  }
201 
202 #if !defined (QT_NO_GETADDRINFO)
203  // Call getaddrinfo, and place all IPv4 addresses at the start and
204  // the IPv6 addresses at the end of the address list in results.
205  addrinfo *res = 0;
206  struct addrinfo hints;
207  memset(&hints, 0, sizeof(hints));
208  hints.ai_family = PF_UNSPEC;
209 #ifdef Q_ADDRCONFIG
210  hints.ai_flags = Q_ADDRCONFIG;
211 #endif
212 
213  int result = getaddrinfo(aceHostname.constData(), 0, &hints, &res);
214 # ifdef Q_ADDRCONFIG
215  if (result == EAI_BADFLAGS) {
216  // if the lookup failed with AI_ADDRCONFIG set, try again without it
217  hints.ai_flags = 0;
218  result = getaddrinfo(aceHostname.constData(), 0, &hints, &res);
219  }
220 # endif
221 
222  if (result == 0) {
223  addrinfo *node = res;
224  QList<QHostAddress> addresses;
225  while (node) {
226 #ifdef QHOSTINFO_DEBUG
227  qDebug() << "getaddrinfo node: flags:" << node->ai_flags << "family:" << node->ai_family << "ai_socktype:" << node->ai_socktype << "ai_protocol:" << node->ai_protocol << "ai_addrlen:" << node->ai_addrlen;
228 #endif
229  if (node->ai_family == AF_INET) {
230  QHostAddress addr;
231  addr.setAddress(ntohl(((sockaddr_in *) node->ai_addr)->sin_addr.s_addr));
232  if (!addresses.contains(addr))
233  addresses.append(addr);
234  }
235 #ifndef QT_NO_IPV6
236  else if (node->ai_family == AF_INET6) {
237  QHostAddress addr;
238  sockaddr_in6 *sa6 = (sockaddr_in6 *) node->ai_addr;
239  addr.setAddress(sa6->sin6_addr.s6_addr);
240  if (sa6->sin6_scope_id)
241  addr.setScopeId(QString::number(sa6->sin6_scope_id));
242  if (!addresses.contains(addr))
243  addresses.append(addr);
244  }
245 #endif
246  node = node->ai_next;
247  }
248  if (addresses.isEmpty() && node == 0) {
249  // Reached the end of the list, but no addresses were found; this
250  // means the list contains one or more unknown address types.
252  results.setErrorString(tr("Unknown address type"));
253  }
254 
255  results.setAddresses(addresses);
256  freeaddrinfo(res);
257  } else if (result == EAI_NONAME
258  || result == EAI_FAIL
259 #ifdef EAI_NODATA
260  // EAI_NODATA is deprecated in RFC 3493
261  || result == EAI_NODATA
262 #endif
263  ) {
265  results.setErrorString(tr("Host not found"));
266  } else {
268  results.setErrorString(QString::fromLocal8Bit(gai_strerror(result)));
269  }
270 
271 #else
272  // Fall back to gethostbyname for platforms that don't define
273  // getaddrinfo. gethostbyname does not support IPv6, and it's not
274  // reentrant on all platforms. For now this is okay since we only
275  // use one QHostInfoAgent, but if more agents are introduced, locking
276  // must be provided.
277  QMutexLocker locker(::getHostByNameMutex());
278  hostent *result = gethostbyname(aceHostname.constData());
279  if (result) {
280  if (result->h_addrtype == AF_INET) {
281  QList<QHostAddress> addresses;
282  for (char **p = result->h_addr_list; *p != 0; p++) {
283  QHostAddress addr;
284  addr.setAddress(ntohl(*((quint32 *)*p)));
285  if (!addresses.contains(addr))
286  addresses.prepend(addr);
287  }
288  results.setAddresses(addresses);
289  } else {
291  results.setErrorString(tr("Unknown address type"));
292  }
293 #if !defined(Q_OS_VXWORKS)
294  } else if (h_errno == HOST_NOT_FOUND || h_errno == NO_DATA
295  || h_errno == NO_ADDRESS) {
297  results.setErrorString(tr("Host not found"));
298 #endif
299  } else {
301  results.setErrorString(tr("Unknown error"));
302  }
303 #endif // !defined (QT_NO_GETADDRINFO)
304 
305 #if defined(QHOSTINFO_DEBUG)
306  if (results.error() != QHostInfo::NoError) {
307  qDebug("QHostInfoAgent::fromName(): error #%d %s",
308  h_errno, results.errorString().toLatin1().constData());
309  } else {
310  QString tmp;
311  QList<QHostAddress> addresses = results.addresses();
312  for (int i = 0; i < addresses.count(); ++i) {
313  if (i != 0) tmp += ", ";
314  tmp += addresses.at(i).toString();
315  }
316  qDebug("QHostInfoAgent::fromName(): found %i entries for \"%s\": {%s}",
317  addresses.count(), hostName.toLatin1().constData(),
318  tmp.toLatin1().constData());
319  }
320 #endif
321  return results;
322 }
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qstring.cpp:6448
void setScopeId(const QString &id)
Sets the IPv6 scope ID of the address to id.
quint8 c[16]
Definition: qhostaddress.h:65
static QString fromLocal8Bit(const char *, int size=-1)
Returns a QString initialized with the first size characters of the 8-bit string str.
Definition: qstring.cpp:4245
Q_IPV6ADDR toIPv6Address() const
Returns the IPv6 address as a Q_IPV6ADDR structure.
void setErrorString(const QString &errorString)
Sets the human readable description of the error that occurred to str if the lookup failed...
Definition: qhostinfo.cpp:465
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
HostInfoError error() const
Returns the type of error that occurred if the host name lookup failed; otherwise returns NoError...
Definition: qhostinfo.cpp:413
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
quint32 toIPv4Address() const
Returns the IPv4 address as a number.
The QString class provides a Unicode character string.
Definition: qstring.h:83
QList< QHostAddress > addresses() const
Returns the list of IP addresses associated with hostName().
Definition: qhostinfo.cpp:372
static void resolveLibrary()
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
Definition: qlist.h:152
static QString translate(const char *context, const char *key, const char *disambiguation=0, Encoding encoding=CodecForTr)
#define QT_SOCKLEN_T
Q_CORE_EXPORT void qDebug(const char *,...)
static QByteArray toAce(const QString &)
Returns the ASCII Compatible Encoding of the given domain name domain.
Definition: qurl.cpp:6158
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
QBool contains(const T &t) const
Returns true if the list contains an occurrence of value; otherwise returns false.
Definition: qlist.h:880
void setHostName(const QString &name)
Sets the host name of this QHostInfo to hostName.
Definition: qhostinfo.cpp:402
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
void setAddresses(const QList< QHostAddress > &addresses)
Sets the list of addresses in this QHostInfo to addresses.
Definition: qhostinfo.cpp:382
QString hostName() const
Returns the name of the host whose IP addresses were looked up.
Definition: qhostinfo.cpp:392
void prepend(const T &t)
Inserts value at the beginning of the list.
Definition: qlist.h:541
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
The QHostInfo class provides static functions for host name lookups.
Definition: qhostinfo.h:58
QString toString() const
Returns the address as a string.
QByteArray toLatin1() const Q_REQUIRED_RESULT
Returns a Latin-1 representation of the string as a QByteArray.
Definition: qstring.cpp:3993
static res_init_proto local_res_init
void setError(HostInfoError error)
Sets the error type of this QHostInfo to error.
Definition: qhostinfo.cpp:423
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
The QCoreApplication class provides an event loop for console Qt applications.
The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes...
Definition: qmutex.h:101
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 NI_MAXHOST
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188
unsigned int quint32
Definition: qglobal.h:938
static in_addr_t qt_safe_inet_addr(const char *cp)
Definition: qnet_unix_p.h:177
QString errorString() const
If the lookup failed, this function returns a human readable description of the error; otherwise "Unk...
Definition: qhostinfo.cpp:454
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421
The QHostAddress class provides an IP address.
Definition: qhostaddress.h:70
static QMutex * globalInstanceGet(const void *address)
Returns a QMutex from the global mutex pool.
Definition: qmutexpool.cpp:150
#define AF_INET6

◆ fromName() [2/2]

QHostInfo QHostInfoAgent::fromName ( const QString hostName,
QSharedPointer< QNetworkSession networkSession 
)
static

Definition at line 302 of file qhostinfo.cpp.

303 {
304  return QHostInfoAgent::fromName(hostName);
305 }
static QHostInfo fromName(const QString &hostName)

The documentation for this class was generated from the following files: