Qt 4.8
Typedefs | Functions | Variables
qnetworkinterface_win.cpp File Reference
#include "qnetworkinterface.h"
#include "qnetworkinterface_p.h"
#include "qnetworkinterface_win_p.h"
#include <qhostinfo.h>
#include <qhash.h>
#include <qurl.h>
#include <private/qsystemlibrary_p.h>

Go to the source code of this file.

Typedefs

typedef ULONG(WINAPI * PtrGetAdaptersAddresses) (ULONG, ULONG, PVOID, PIP_ADAPTER_ADDRESSES, PULONG)
 
typedef DWORD(WINAPI * PtrGetAdaptersInfo) (PIP_ADAPTER_INFO, PULONG)
 
typedef DWORD(WINAPI * PtrGetNetworkParams) (PFIXED_INFO, PULONG)
 

Functions

static QHostAddress addressFromSockaddr (sockaddr *sa)
 
static QList< QNetworkInterfacePrivate * > interfaceListing ()
 
static QList< QNetworkInterfacePrivate * > interfaceListingWin2k ()
 
static QList< QNetworkInterfacePrivate * > interfaceListingWinXP ()
 
static QHash< QHostAddress, QHostAddressipv4Netmasks ()
 
static void resolveLibs ()
 

Variables

static PtrGetAdaptersAddresses ptrGetAdaptersAddresses = 0
 
static PtrGetAdaptersInfo ptrGetAdaptersInfo = 0
 
static PtrGetNetworkParams ptrGetNetworkParams = 0
 

Typedef Documentation

◆ PtrGetAdaptersAddresses

typedef ULONG(WINAPI * PtrGetAdaptersAddresses) (ULONG, ULONG, PVOID, PIP_ADAPTER_ADDRESSES, PULONG)

Definition at line 57 of file qnetworkinterface_win.cpp.

◆ PtrGetAdaptersInfo

typedef DWORD(WINAPI * PtrGetAdaptersInfo) (PIP_ADAPTER_INFO, PULONG)

Definition at line 55 of file qnetworkinterface_win.cpp.

◆ PtrGetNetworkParams

typedef DWORD(WINAPI * PtrGetNetworkParams) (PFIXED_INFO, PULONG)

Definition at line 59 of file qnetworkinterface_win.cpp.

Function Documentation

◆ addressFromSockaddr()

static QHostAddress addressFromSockaddr ( sockaddr *  sa)
static

Definition at line 77 of file qnetworkinterface_win.cpp.

Referenced by interfaceListingWinXP().

78 {
79  QHostAddress address;
80  if (!sa)
81  return address;
82 
83  if (sa->sa_family == AF_INET)
84  address.setAddress(htonl(((sockaddr_in *)sa)->sin_addr.s_addr));
85  else if (sa->sa_family == AF_INET6) {
86  address.setAddress(((qt_sockaddr_in6 *)sa)->sin6_addr.qt_s6_addr);
87  int scope = ((qt_sockaddr_in6 *)sa)->sin6_scope_id;
88  if (scope)
89  address.setScopeId(QString::number(scope));
90  } else
91  qWarning("Got unknown socket family %d", sa->sa_family);
92  return address;
93 
94 }
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.
Q_CORE_EXPORT void qWarning(const char *,...)
void setAddress(quint32 ip4Addr)
Set the IPv4 address specified by ip4Addr.
The QHostAddress class provides an IP address.
Definition: qhostaddress.h:70
#define AF_INET6

◆ interfaceListing()

static QList<QNetworkInterfacePrivate *> interfaceListing ( )
static

Definition at line 274 of file qnetworkinterface_win.cpp.

275 {
276  resolveLibs();
277  if (ptrGetAdaptersAddresses != NULL)
278  return interfaceListingWinXP();
279  else if (ptrGetAdaptersInfo != NULL)
280  return interfaceListingWin2k();
281 
282  // failed
284 }
static PtrGetAdaptersInfo ptrGetAdaptersInfo
static void resolveLibs()
static QList< QNetworkInterfacePrivate * > interfaceListingWinXP()
static PtrGetAdaptersAddresses ptrGetAdaptersAddresses
static QList< QNetworkInterfacePrivate * > interfaceListingWin2k()
The QList class is a template class that provides lists.
Definition: qdatastream.h:62

◆ interfaceListingWin2k()

static QList<QNetworkInterfacePrivate *> interfaceListingWin2k ( )
static

Definition at line 220 of file qnetworkinterface_win.cpp.

Referenced by interfaceListing().

221 {
223  IP_ADAPTER_INFO staticBuf[2]; // 2 is arbitrary
224  PIP_ADAPTER_INFO pAdapter = staticBuf;
225  ULONG bufSize = sizeof staticBuf;
226 
227  DWORD retval = ptrGetAdaptersInfo(pAdapter, &bufSize);
228  if (retval == ERROR_BUFFER_OVERFLOW) {
229  // need more memory
230  pAdapter = (IP_ADAPTER_INFO *)qMalloc(bufSize);
231  if (!pAdapter)
232  return interfaces;
233  // try again
234  if (ptrGetAdaptersInfo(pAdapter, &bufSize) != ERROR_SUCCESS) {
235  qFree(pAdapter);
236  return interfaces;
237  }
238  } else if (retval != ERROR_SUCCESS) {
239  // error
240  return interfaces;
241  }
242 
243  // iterate over the list and add the entries to our listing
244  for (PIP_ADAPTER_INFO ptr = pAdapter; ptr; ptr = ptr->Next) {
246  interfaces << iface;
247 
248  iface->index = ptr->Index;
250  if (ptr->Type == MIB_IF_TYPE_PPP)
251  iface->flags |= QNetworkInterface::IsPointToPoint;
252  else
253  iface->flags |= QNetworkInterface::CanBroadcast;
254  iface->name = QString::fromLocal8Bit(ptr->AdapterName);
255  iface->hardwareAddress = QNetworkInterfacePrivate::makeHwAddress(ptr->AddressLength,
256  ptr->Address);
257 
258  for (PIP_ADDR_STRING addr = &ptr->IpAddressList; addr; addr = addr->Next) {
259  QNetworkAddressEntry entry;
260  entry.setIp(QHostAddress(QLatin1String(addr->IpAddress.String)));
261  entry.setNetmask(QHostAddress(QLatin1String(addr->IpMask.String)));
262  // broadcast address is set on postProcess()
263 
264  iface->addressEntries << entry;
265  }
266  }
267 
268  if (pAdapter != staticBuf)
269  qFree(pAdapter);
270 
271  return interfaces;
272 }
static PtrGetAdaptersInfo ptrGetAdaptersInfo
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_CORE_EXPORT void qFree(void *ptr)
Definition: qmalloc.cpp:58
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
Q_CORE_EXPORT void * qMalloc(size_t size)
Definition: qmalloc.cpp:53
static QString makeHwAddress(int len, uchar *data)
const T * ptr(const T &t)
#define MIB_IF_TYPE_PPP
void setIp(const QHostAddress &newIp)
Sets the IP address the QNetworkAddressEntry object contains to newIp.
void setNetmask(const QHostAddress &newNetmask)
Sets the netmask that this QNetworkAddressEntry object contains to newNetmask.
The QHostAddress class provides an IP address.
Definition: qhostaddress.h:70
The QNetworkAddressEntry class stores one IP address supported by a network interface, along with its associated netmask and broadcast address.
The QList class is a template class that provides lists.
Definition: qdatastream.h:62

◆ interfaceListingWinXP()

static QList<QNetworkInterfacePrivate *> interfaceListingWinXP ( )
static

Definition at line 135 of file qnetworkinterface_win.cpp.

Referenced by interfaceListing().

136 {
138  IP_ADAPTER_ADDRESSES staticBuf[2]; // 2 is arbitrary
139  PIP_ADAPTER_ADDRESSES pAdapter = staticBuf;
140  ULONG bufSize = sizeof staticBuf;
141 
142  const QHash<QHostAddress, QHostAddress> &ipv4netmasks = ipv4Netmasks();
143  ULONG flags = GAA_FLAG_INCLUDE_PREFIX |
146  ULONG retval = ptrGetAdaptersAddresses(AF_UNSPEC, flags, NULL, pAdapter, &bufSize);
147  if (retval == ERROR_BUFFER_OVERFLOW) {
148  // need more memory
149  pAdapter = (IP_ADAPTER_ADDRESSES *)qMalloc(bufSize);
150  if (!pAdapter)
151  return interfaces;
152  // try again
153  if (ptrGetAdaptersAddresses(AF_UNSPEC, flags, NULL, pAdapter, &bufSize) != ERROR_SUCCESS) {
154  qFree(pAdapter);
155  return interfaces;
156  }
157  } else if (retval != ERROR_SUCCESS) {
158  // error
159  return interfaces;
160  }
161 
162  // iterate over the list and add the entries to our listing
163  for (PIP_ADAPTER_ADDRESSES ptr = pAdapter; ptr; ptr = ptr->Next) {
165  interfaces << iface;
166 
167  iface->index = 0;
168  if (ptr->Length >= offsetof(IP_ADAPTER_ADDRESSES, Ipv6IfIndex) && ptr->Ipv6IfIndex != 0)
169  iface->index = ptr->Ipv6IfIndex;
170  else if (ptr->IfIndex != 0)
171  iface->index = ptr->IfIndex;
172 
173  iface->flags = QNetworkInterface::CanBroadcast;
174  if (ptr->OperStatus == IfOperStatusUp)
176  if ((ptr->Flags & IP_ADAPTER_NO_MULTICAST) == 0)
177  iface->flags |= QNetworkInterface::CanMulticast;
178 
179  iface->name = QString::fromLocal8Bit(ptr->AdapterName);
180  iface->friendlyName = QString::fromWCharArray(ptr->FriendlyName);
181  if (ptr->PhysicalAddressLength)
182  iface->hardwareAddress = iface->makeHwAddress(ptr->PhysicalAddressLength,
183  ptr->PhysicalAddress);
184  else
185  // loopback if it has no address
186  iface->flags |= QNetworkInterface::IsLoopBack;
187 
188  // The GetAdaptersAddresses call has an interesting semantic:
189  // It can return a number N of addresses and a number M of prefixes.
190  // But if you have IPv6 addresses, generally N > M.
191  // I cannot find a way to relate the Address to the Prefix, aside from stopping
192  // the iteration at the last Prefix entry and assume that it applies to all addresses
193  // from that point on.
194  PIP_ADAPTER_PREFIX pprefix = 0;
195  if (ptr->Length >= offsetof(IP_ADAPTER_ADDRESSES, FirstPrefix))
196  pprefix = ptr->FirstPrefix;
197  for (PIP_ADAPTER_UNICAST_ADDRESS addr = ptr->FirstUnicastAddress; addr; addr = addr->Next) {
198  QNetworkAddressEntry entry;
199  entry.setIp(addressFromSockaddr(addr->Address.lpSockaddr));
200  if (pprefix) {
201  if (entry.ip().protocol() == QAbstractSocket::IPv4Protocol) {
202  entry.setNetmask(ipv4netmasks[entry.ip()]);
203 
204  // broadcast address is set on postProcess()
205  } else { //IPV6
206  entry.setPrefixLength(pprefix->PrefixLength);
207  }
208  pprefix = pprefix->Next ? pprefix->Next : pprefix;
209  }
210  iface->addressEntries << entry;
211  }
212  }
213 
214  if (pAdapter != staticBuf)
215  qFree(pAdapter);
216 
217  return interfaces;
218 }
static QString fromWCharArray(const wchar_t *, int size=-1)
Returns a copy of the string, where the encoding of string depends on the size of wchar...
Definition: qstring.cpp:1019
static QHash< QHostAddress, QHostAddress > ipv4Netmasks()
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
#define IP_ADAPTER_NO_MULTICAST
QHostAddress ip() const
This function returns one IPv4 or IPv6 address found, that was found in a network interface...
Q_CORE_EXPORT void qFree(void *ptr)
Definition: qmalloc.cpp:58
void setPrefixLength(int length)
Sets the prefix length of this IP address to length.
Q_CORE_EXPORT void * qMalloc(size_t size)
Definition: qmalloc.cpp:53
The QHash class is a template class that provides a hash-table-based dictionary.
Definition: qdatastream.h:66
static QHostAddress addressFromSockaddr(sockaddr *sa)
const T * ptr(const T &t)
#define GAA_FLAG_SKIP_MULTICAST
QAbstractSocket::NetworkLayerProtocol protocol() const
Returns the network layer protocol of the host address.
struct _IP_ADAPTER_PREFIX * Next
void setIp(const QHostAddress &newIp)
Sets the IP address the QNetworkAddressEntry object contains to newIp.
#define GAA_FLAG_INCLUDE_PREFIX
static PtrGetAdaptersAddresses ptrGetAdaptersAddresses
void setNetmask(const QHostAddress &newNetmask)
Sets the netmask that this QNetworkAddressEntry object contains to newNetmask.
#define GAA_FLAG_SKIP_DNS_SERVER
The QNetworkAddressEntry class stores one IP address supported by a network interface, along with its associated netmask and broadcast address.
The QList class is a template class that provides lists.
Definition: qdatastream.h:62

◆ ipv4Netmasks()

static QHash<QHostAddress, QHostAddress> ipv4Netmasks ( )
static

Definition at line 96 of file qnetworkinterface_win.cpp.

Referenced by interfaceListingWinXP().

97 {
98  //Retrieve all the IPV4 addresses & netmasks
99  IP_ADAPTER_INFO staticBuf[2]; // 2 is arbitrary
100  PIP_ADAPTER_INFO pAdapter = staticBuf;
101  ULONG bufSize = sizeof staticBuf;
102  QHash<QHostAddress, QHostAddress> ipv4netmasks;
103 
104  DWORD retval = ptrGetAdaptersInfo(pAdapter, &bufSize);
105  if (retval == ERROR_BUFFER_OVERFLOW) {
106  // need more memory
107  pAdapter = (IP_ADAPTER_INFO *)qMalloc(bufSize);
108  if (!pAdapter)
109  return ipv4netmasks;
110  // try again
111  if (ptrGetAdaptersInfo(pAdapter, &bufSize) != ERROR_SUCCESS) {
112  qFree(pAdapter);
113  return ipv4netmasks;
114  }
115  } else if (retval != ERROR_SUCCESS) {
116  // error
117  return ipv4netmasks;
118  }
119 
120  // iterate over the list and add the entries to our listing
121  for (PIP_ADAPTER_INFO ptr = pAdapter; ptr; ptr = ptr->Next) {
122  for (PIP_ADDR_STRING addr = &ptr->IpAddressList; addr; addr = addr->Next) {
123  QHostAddress address(QLatin1String(addr->IpAddress.String));
124  QHostAddress mask(QLatin1String(addr->IpMask.String));
125  ipv4netmasks[address] = mask;
126  }
127  }
128  if (pAdapter != staticBuf)
129  qFree(pAdapter);
130 
131  return ipv4netmasks;
132 
133 }
static PtrGetAdaptersInfo ptrGetAdaptersInfo
Q_CORE_EXPORT void qFree(void *ptr)
Definition: qmalloc.cpp:58
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
Q_CORE_EXPORT void * qMalloc(size_t size)
Definition: qmalloc.cpp:53
The QHash class is a template class that provides a hash-table-based dictionary.
Definition: qdatastream.h:66
const T * ptr(const T &t)
The QHostAddress class provides an IP address.
Definition: qhostaddress.h:70

◆ resolveLibs()

static void resolveLibs ( )
static

Definition at line 62 of file qnetworkinterface_win.cpp.

Referenced by interfaceListing().

63 {
64  // try to find the functions we need from Iphlpapi.dll
65  static bool done = false;
66  if (!done) {
67  QSystemLibrary iphlpapi(QLatin1String("iphlpapi"));
68  if (iphlpapi.load()) {
69  ptrGetAdaptersInfo = (PtrGetAdaptersInfo)iphlpapi.resolve("GetAdaptersInfo");
70  ptrGetAdaptersAddresses = (PtrGetAdaptersAddresses)iphlpapi.resolve("GetAdaptersAddresses");
71  ptrGetNetworkParams = (PtrGetNetworkParams)iphlpapi.resolve("GetNetworkParams");
72  }
73  done = true;
74  }
75 }
static PtrGetAdaptersInfo ptrGetAdaptersInfo
DWORD(WINAPI * PtrGetNetworkParams)(PFIXED_INFO, PULONG)
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
DWORD(WINAPI * PtrGetAdaptersInfo)(PIP_ADAPTER_INFO, PULONG)
ULONG(WINAPI * PtrGetAdaptersAddresses)(ULONG, ULONG, PVOID, PIP_ADAPTER_ADDRESSES, PULONG)
static PtrGetAdaptersAddresses ptrGetAdaptersAddresses
static PtrGetNetworkParams ptrGetNetworkParams

Variable Documentation

◆ ptrGetAdaptersAddresses

PtrGetAdaptersAddresses ptrGetAdaptersAddresses = 0
static

Definition at line 58 of file qnetworkinterface_win.cpp.

Referenced by interfaceListing(), interfaceListingWinXP(), and resolveLibs().

◆ ptrGetAdaptersInfo

PtrGetAdaptersInfo ptrGetAdaptersInfo = 0
static

◆ ptrGetNetworkParams

PtrGetNetworkParams ptrGetNetworkParams = 0
static

Definition at line 60 of file qnetworkinterface_win.cpp.

Referenced by interfaceListing(), and resolveLibs().