46 #include "private/qnet_unix_p.h" 48 #ifndef QT_NO_NETWORKINTERFACE 50 #define IP_MULTICAST // make AIX happy and define IFF_MULTICAST 52 #include <sys/types.h> 53 #include <sys/socket.h> 56 # include <sys/sockio.h> 60 #ifndef QT_NO_GETIFADDRS 65 # include <arpa/inet.h> 66 # ifndef SIOCGIFBRDADDR 67 # define SIOCGIFBRDADDR 0x8919 69 #endif // QT_LINUXBASE 71 #include <qplatformdefs.h> 81 if (sa->sa_family == AF_INET)
82 address.
setAddress(htonl(((sockaddr_in *)sa)->sin_addr.s_addr));
84 else if (sa->sa_family ==
AF_INET6) {
85 address.
setAddress(((sockaddr_in6 *)sa)->sin6_addr.s6_addr);
86 int scope = ((sockaddr_in6 *)sa)->sin6_scope_id;
88 #ifndef QT_NO_IPV6IFNAME 89 char scopeid[IFNAMSIZ];
90 if (::if_indextoname(scope, scopeid)) {
104 QNetworkInterface::InterfaceFlags flags = 0;
109 #ifdef IFF_POINTOPOINT //cygwin doesn't define IFF_POINTOPOINT 119 #ifdef QT_NO_GETIFADDRS 122 static const int STORAGEBUFFER_GROWTH = 256;
127 #ifdef QT_NO_IPV6IFNAME 129 struct ifconf interfaceList;
133 storageBuffer.
resize(storageBuffer.
size() + STORAGEBUFFER_GROWTH);
134 interfaceList.ifc_buf = storageBuffer.
data();
135 interfaceList.ifc_len = storageBuffer.
size();
138 if (
qt_safe_ioctl(socket, SIOCGIFCONF, &interfaceList) >= 0) {
139 if (
int(interfaceList.ifc_len +
sizeof(ifreq) + 64) < storageBuffer.
size()) {
141 storageBuffer.
resize(interfaceList.ifc_len);
148 if (storageBuffer.
size() > 100000) {
154 int interfaceCount = interfaceList.ifc_len /
sizeof(ifreq);
155 for (
int i = 0; i < interfaceCount; ++i) {
166 struct if_nameindex *interfaceList = ::if_nameindex();
167 for (
struct if_nameindex *
ptr = interfaceList;
ptr &&
ptr->if_name; ++
ptr)
168 result <<
ptr->if_name;
170 if_freenameindex(interfaceList);
181 #ifndef QT_NO_IPV6IFNAME 183 ifindex = if_nametoindex(req.ifr_name);
187 for ( ; if_it != interfaces.
end(); ++if_it)
188 if ((*if_it)->index == ifindex) {
196 for ( ; if_it != interfaces.
end(); ++if_it)
207 iface->
index = ifindex;
217 memcpy(req.ifr_name, oldName, qMin<int>(oldName.
length() + 1,
sizeof(req.ifr_name) - 1));
234 iface->hardwareAddress = iface->makeHwAddress(6, addr);
247 if ((socket =
qt_safe_socket(AF_INET, SOCK_STREAM, IPPROTO_IP)) == -1)
254 memset(&req, 0,
sizeof(ifreq));
255 memcpy(req.ifr_name, *it, qMin<int>(it->length() + 1,
sizeof(req.ifr_name) - 1));
263 sockaddr *sa = &req.ifr_addr;
264 if (sa->sa_family == AF_INET)
271 sockaddr *sa = &req.ifr_addr;
277 sockaddr *sa = &req.ifr_addr;
281 iface->addressEntries << entry;
294 # include <features.h> 298 # if defined(Q_OS_LINUX) && __GLIBC__ - 0 >= 2 && __GLIBC_MINOR__ - 0 >= 1 299 # include <netpacket/packet.h> 305 for (ifaddrs *
ptr = rawList;
ptr;
ptr =
ptr->ifa_next) {
306 if ( !
ptr->ifa_addr )
310 int ifindex = if_nametoindex(
ptr->ifa_name);
314 for ( ; if_it != interfaces.
end(); ++if_it)
315 if ((*if_it)->index == ifindex) {
317 if (
ptr->ifa_addr->sa_family == AF_PACKET
318 && (*if_it)->hardwareAddress.isEmpty()) {
319 sockaddr_ll *sll = (sockaddr_ll *)
ptr->ifa_addr;
320 (*if_it)->hardwareAddress = (*if_it)->makeHwAddress(sll->sll_halen, (
uchar*)sll->sll_addr);
324 if ( if_it != interfaces.
end() )
329 iface->
index = ifindex;
333 if (
ptr->ifa_addr->sa_family == AF_PACKET ) {
334 sockaddr_ll *sll = (sockaddr_ll *)
ptr->ifa_addr;
335 iface->hardwareAddress = iface->makeHwAddress(sll->sll_halen, (
uchar*)sll->sll_addr);
342 # elif defined(Q_OS_BSD4) 344 # include <net/if_dl.h> 354 if (
ptr->ifa_addr &&
ptr->ifa_addr->sa_family == AF_LINK) {
358 sockaddr_dl *sdl = (sockaddr_dl *)
ptr->ifa_addr;
359 iface->index = sdl->sdl_index;
362 iface->hardwareAddress = iface->makeHwAddress(sdl->sdl_alen, (
uchar*)LLADDR(sdl));
368 # else // Generic version 375 for (ifaddrs *
ptr = rawList;
ptr;
ptr =
ptr->ifa_next) {
377 int ifindex = if_nametoindex(
ptr->ifa_name);
380 for ( ; if_it != interfaces.
end(); ++if_it)
381 if ((*if_it)->index == ifindex)
385 if (if_it == interfaces.
end()) {
390 iface->
index = ifindex;
407 if ((socket =
qt_safe_socket(AF_INET, SOCK_STREAM, IPPROTO_IP)) == -1)
411 if (getifaddrs(&interfaceListing) == -1) {
418 for (ifaddrs *
ptr = interfaceListing;
ptr;
ptr =
ptr->ifa_next) {
420 int ifindex = if_nametoindex(
ptr->ifa_name);
423 for ( ; if_it != interfaces.
end(); ++if_it)
424 if ((*if_it)->index == ifindex) {
447 freeifaddrs(interfaceListing);
460 #endif // QT_NO_NETWORKINTERFACE
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
void setScopeId(const QString &id)
Sets the IPv6 scope ID of the address to id.
bool isNull() const
Returns true if this host address is null (INADDR_ANY or in6addr_any).
#define QT_END_NAMESPACE
This macro expands to.
QHostAddress ip() const
This function returns one IPv4 or IPv6 address found, that was found in a network interface...
char * data()
Returns a pointer to the data stored in the byte array.
#define it(className, varName)
const_iterator constEnd() const
The QByteArray class provides an array of bytes.
iterator begin()
Returns an STL-style iterator pointing to the first item in the list.
#define QT_END_INCLUDE_NAMESPACE
This macro is equivalent to QT_BEGIN_NAMESPACE.
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
static int qt_safe_ioctl(int sockfd, int request, T arg)
InterfaceFlag
Specifies the flags associated with this network interface.
static QNetworkInterface::InterfaceFlags convertFlags(uint rawFlags)
#define QT_BEGIN_NAMESPACE
This macro expands to.
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the list...
QList< QNetworkInterfacePrivate * > scan()
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal...
const T * ptr(const T &t)
The QList::iterator class provides an STL-style non-const iterator for QList and QQueue.
static QList< QNetworkInterfacePrivate * > interfaceListing()
int length() const
Same as size().
void setAddress(quint32 ip4Addr)
Set the IPv4 address specified by ip4Addr.
static int qt_safe_socket(int domain, int type, int protocol, int flags=0)
void setIp(const QHostAddress &newIp)
Sets the IP address the QNetworkAddressEntry object contains to newIp.
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
static QList< QNetworkInterfacePrivate * > createInterfaces(ifaddrs *rawList)
QList< QNetworkAddressEntry > addressEntries
void resize(int size)
Sets the size of the byte array to size bytes.
const_iterator constBegin() const
int size() const
Returns the number of bytes in this byte array.
#define QT_BEGIN_INCLUDE_NAMESPACE
This macro is equivalent to QT_END_NAMESPACE.
void setNetmask(const QHostAddress &newNetmask)
Sets the netmask that this QNetworkAddressEntry object contains to newNetmask.
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
The QHostAddress class provides an IP address.
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
QNetworkInterface::InterfaceFlags flags
The QNetworkAddressEntry class stores one IP address supported by a network interface, along with its associated netmask and broadcast address.
static QHostAddress addressFromSockaddr(sockaddr *sa)
The QList class is a template class that provides lists.
void setBroadcast(const QHostAddress &newBroadcast)
Sets the broadcast IP address of this QNetworkAddressEntry object to newBroadcast.
#define forever
This macro is provided for convenience for writing infinite loops.