// calculate the broadcast address
quint32 ip = entry.ip().toIPv4Address();
quint32 mask = entry.netmask().toIPv4Address();
quint32 broadcast = (ip & mask) | (0xFFFFFFFFU & ~mask);
// calculate the broadcast address
quint32 ip = entry.ip().toIPv4Address();
quint32 mask = entry.netmask().toIPv4Address();
quint32 broadcast = (ip & mask) | (0xFFFFFFFFU & ~mask);
To copy to clipboard, switch view to plain text mode
static QList<QNetworkInterfacePrivate *> interfaceListingWinXP()
{
QList<QNetworkInterfacePrivate *> interfaces;
IP_ADAPTER_ADDRESSES staticBuf[2]; // 2 is arbitrary
PIP_ADAPTER_ADDRESSES pAdapter = staticBuf;
ULONG bufSize = sizeof staticBuf;
ULONG flags = GAA_FLAG_INCLUDE_ALL_INTERFACES |
GAA_FLAG_INCLUDE_PREFIX |
GAA_FLAG_SKIP_DNS_SERVER |
GAA_FLAG_SKIP_MULTICAST;
ULONG retval = ptrGetAdaptersAddresses(AF_UNSPEC, flags, NULL, pAdapter, &bufSize);
if (retval == ERROR_BUFFER_OVERFLOW) {
// need more memory
pAdapter = (IP_ADAPTER_ADDRESSES *)qMalloc(bufSize);
// try again
if (ptrGetAdaptersAddresses(AF_UNSPEC, flags, NULL, pAdapter, &bufSize) != ERROR_SUCCESS) {
qFree(pAdapter);
return interfaces;
}
} else if (retval != ERROR_SUCCESS) {
// error
return interfaces;
}
// iterate over the list and add the entries to our listing
for (PIP_ADAPTER_ADDRESSES ptr = pAdapter; ptr; ptr = ptr->Next) {
QNetworkInterfacePrivate *iface = new QNetworkInterfacePrivate;
interfaces << iface;
iface->index = 0;
if (ptr->Length >= offsetof(IP_ADAPTER_ADDRESSES, Ipv6IfIndex))
iface->index = ptr->Ipv6IfIndex;
else if (ptr->IfIndex != 0)
iface->index = ptr->IfIndex;
if (ptr->OperStatus == IfOperStatusUp)
if ((ptr->Flags & IP_ADAPTER_NO_MULTICAST) == 0)
iface
->name
= QString::fromLocal8Bit(ptr
->AdapterName
);
if (ptr->PhysicalAddressLength)
iface->hardwareAddress = iface->makeHwAddress(ptr->PhysicalAddressLength,
ptr->PhysicalAddress);
else
// loopback if it has no address
// The GetAdaptersAddresses call has an interesting semantic:
// It can return a number N of addresses and a number M of prefixes.
// But if you have IPv6 addresses, generally N > M.
// I cannot find a way to relate the Address to the Prefix, aside from stopping
// the iteration at the last Prefix entry and assume that it applies to all addresses
// from that point on.
PIP_ADAPTER_PREFIX pprefix = 0;
if (ptr->Length >= offsetof(IP_ADAPTER_ADDRESSES, FirstPrefix))
pprefix = ptr->FirstPrefix;
for (PIP_ADAPTER_UNICAST_ADDRESS addr = ptr->FirstUnicastAddress; addr; addr = addr->Next) {
entry.setIp(addressFromSockaddr(addr->Address.lpSockaddr));
if (pprefix) {
entry.setNetmask(netmaskFromPrefixLength(pprefix->PrefixLength, addr->Address.lpSockaddr->sa_family));
pprefix = pprefix->Next ? pprefix->Next : pprefix;
}
iface->addressEntries << entry;
}
}
if (pAdapter != staticBuf)
qFree(pAdapter);
return interfaces;
}
static QList<QNetworkInterfacePrivate *> interfaceListingWinXP()
{
QList<QNetworkInterfacePrivate *> interfaces;
IP_ADAPTER_ADDRESSES staticBuf[2]; // 2 is arbitrary
PIP_ADAPTER_ADDRESSES pAdapter = staticBuf;
ULONG bufSize = sizeof staticBuf;
ULONG flags = GAA_FLAG_INCLUDE_ALL_INTERFACES |
GAA_FLAG_INCLUDE_PREFIX |
GAA_FLAG_SKIP_DNS_SERVER |
GAA_FLAG_SKIP_MULTICAST;
ULONG retval = ptrGetAdaptersAddresses(AF_UNSPEC, flags, NULL, pAdapter, &bufSize);
if (retval == ERROR_BUFFER_OVERFLOW) {
// need more memory
pAdapter = (IP_ADAPTER_ADDRESSES *)qMalloc(bufSize);
// try again
if (ptrGetAdaptersAddresses(AF_UNSPEC, flags, NULL, pAdapter, &bufSize) != ERROR_SUCCESS) {
qFree(pAdapter);
return interfaces;
}
} else if (retval != ERROR_SUCCESS) {
// error
return interfaces;
}
// iterate over the list and add the entries to our listing
for (PIP_ADAPTER_ADDRESSES ptr = pAdapter; ptr; ptr = ptr->Next) {
QNetworkInterfacePrivate *iface = new QNetworkInterfacePrivate;
interfaces << iface;
iface->index = 0;
if (ptr->Length >= offsetof(IP_ADAPTER_ADDRESSES, Ipv6IfIndex))
iface->index = ptr->Ipv6IfIndex;
else if (ptr->IfIndex != 0)
iface->index = ptr->IfIndex;
iface->flags = QNetworkInterface::CanBroadcast;
if (ptr->OperStatus == IfOperStatusUp)
iface->flags |= QNetworkInterface::IsUp | QNetworkInterface::IsRunning;
if ((ptr->Flags & IP_ADAPTER_NO_MULTICAST) == 0)
iface->flags |= QNetworkInterface::CanMulticast;
iface->name = QString::fromLocal8Bit(ptr->AdapterName);
if (ptr->PhysicalAddressLength)
iface->hardwareAddress = iface->makeHwAddress(ptr->PhysicalAddressLength,
ptr->PhysicalAddress);
else
// loopback if it has no address
iface->flags |= QNetworkInterface::IsLoopBack;
// The GetAdaptersAddresses call has an interesting semantic:
// It can return a number N of addresses and a number M of prefixes.
// But if you have IPv6 addresses, generally N > M.
// I cannot find a way to relate the Address to the Prefix, aside from stopping
// the iteration at the last Prefix entry and assume that it applies to all addresses
// from that point on.
PIP_ADAPTER_PREFIX pprefix = 0;
if (ptr->Length >= offsetof(IP_ADAPTER_ADDRESSES, FirstPrefix))
pprefix = ptr->FirstPrefix;
for (PIP_ADAPTER_UNICAST_ADDRESS addr = ptr->FirstUnicastAddress; addr; addr = addr->Next) {
QNetworkAddressEntry entry;
entry.setIp(addressFromSockaddr(addr->Address.lpSockaddr));
if (pprefix) {
entry.setNetmask(netmaskFromPrefixLength(pprefix->PrefixLength, addr->Address.lpSockaddr->sa_family));
pprefix = pprefix->Next ? pprefix->Next : pprefix;
}
iface->addressEntries << entry;
}
}
if (pAdapter != staticBuf)
qFree(pAdapter);
return interfaces;
}
To copy to clipboard, switch view to plain text mode
Bookmarks