From 93db2c44b7b01eb08ef6f447d4a84198b2dcd1e3 Mon Sep 17 00:00:00 2001 From: Robert Griebl Date: Tue, 14 Jul 2009 14:56:27 +0200 Subject: Socket function cleanup: replacing direct POSIX calls with qt_safe_(). Reviewed-By: Thiago --- src/network/kernel/qhostinfo_unix.cpp | 4 +--- src/network/kernel/qnetworkinterface_unix.cpp | 19 ++++++++-------- src/network/socket/qnativesocketengine_p.h | 29 ------------------------- src/network/socket/qnativesocketengine_unix.cpp | 6 ++--- src/network/socket/qnet_unix_p.h | 28 ++++++++++++++++++++++++ 5 files changed, 42 insertions(+), 44 deletions(-) diff --git a/src/network/kernel/qhostinfo_unix.cpp b/src/network/kernel/qhostinfo_unix.cpp index 032e575..78df510 100644 --- a/src/network/kernel/qhostinfo_unix.cpp +++ b/src/network/kernel/qhostinfo_unix.cpp @@ -53,12 +53,10 @@ static const int RESOLVER_TIMEOUT = 2000; #include #include -extern "C" { #include #include #include #include -} #if defined (QT_NO_GETADDRINFO) #include @@ -180,7 +178,7 @@ QHostInfo QHostInfoAgent::fromName(const QString &hostName) } results.setHostName(QString::fromLatin1(hbuf)); #else - in_addr_t inetaddr = inet_addr(hostName.toLatin1().constData()); + in_addr_t inetaddr = qt_safe_inet_addr(hostName.toLatin1().constData()); struct hostent *ent = gethostbyaddr((const char *)&inetaddr, sizeof(inetaddr), AF_INET); if (!ent) { results.setError(QHostInfo::HostNotFound); diff --git a/src/network/kernel/qnetworkinterface_unix.cpp b/src/network/kernel/qnetworkinterface_unix.cpp index aa27726..4efbe45 100644 --- a/src/network/kernel/qnetworkinterface_unix.cpp +++ b/src/network/kernel/qnetworkinterface_unix.cpp @@ -43,6 +43,7 @@ #include "qnetworkinterface.h" #include "qnetworkinterface_p.h" #include "qalgorithms.h" +#include "private/qnet_unix_p.h" #ifndef QT_NO_NETWORKINTERFACE @@ -123,7 +124,7 @@ static QSet interfaceNames(int socket) interfaceList.ifc_len = storageBuffer.size(); // get the interface list - if (::ioctl(socket, SIOCGIFCONF, &interfaceList) >= 0) { + if (qt_safe_ioctl(socket, SIOCGIFCONF, &interfaceList) >= 0) { if (int(interfaceList.ifc_len + sizeof(ifreq) + 64) < storageBuffer.size()) { // if the buffer was big enough, break storageBuffer.resize(interfaceList.ifc_len); @@ -198,7 +199,7 @@ static QNetworkInterfacePrivate *findInterface(int socket, QList= 0) { + if (qt_safe_ioctl(socket, SIOCGIFNAME, &req) >= 0) { iface->name = QString::fromLatin1(req.ifr_name); // reset the name: @@ -211,13 +212,13 @@ static QNetworkInterfacePrivate *findInterface(int socket, QList= 0) { + if (qt_safe_ioctl(socket, SIOCGIFFLAGS, &req) >= 0) { iface->flags = convertFlags(req.ifr_flags); } #ifdef SIOCGIFHWADDR // Get the HW address - if (::ioctl(socket, SIOCGIFHWADDR, &req) >= 0) { + if (qt_safe_ioctl(socket, SIOCGIFHWADDR, &req) >= 0) { uchar *addr = (uchar *)&req.ifr_addr; iface->hardwareAddress = iface->makeHwAddress(6, addr); } @@ -232,7 +233,7 @@ static QList interfaceListing() QList interfaces; int socket; - if ((socket = ::socket(AF_INET, SOCK_STREAM, IPPROTO_IP)) == -1) + if ((socket = qt_safe_socket(AF_INET, SOCK_STREAM, IPPROTO_IP)) == -1) return interfaces; // error QSet names = interfaceNames(socket); @@ -247,7 +248,7 @@ static QList interfaceListing() // Get the interface broadcast address QNetworkAddressEntry entry; if (iface->flags & QNetworkInterface::CanBroadcast) { - if (::ioctl(socket, SIOCGIFBRDADDR, &req) >= 0) { + if (qt_safe_ioctl(socket, SIOCGIFBRDADDR, &req) >= 0) { sockaddr *sa = &req.ifr_addr; if (sa->sa_family == AF_INET) entry.setBroadcast(addressFromSockaddr(sa)); @@ -255,13 +256,13 @@ static QList interfaceListing() } // Get the interface netmask - if (::ioctl(socket, SIOCGIFNETMASK, &req) >= 0) { + if (qt_safe_ioctl(socket, SIOCGIFNETMASK, &req) >= 0) { sockaddr *sa = &req.ifr_addr; entry.setNetmask(addressFromSockaddr(sa)); } // Get the address of the interface - if (::ioctl(socket, SIOCGIFADDR, &req) >= 0) { + if (qt_safe_ioctl(socket, SIOCGIFADDR, &req) >= 0) { sockaddr *sa = &req.ifr_addr; entry.setIp(addressFromSockaddr(sa)); } @@ -392,7 +393,7 @@ static QList interfaceListing() QList interfaces; int socket; - if ((socket = ::socket(AF_INET, SOCK_STREAM, IPPROTO_IP)) == -1) + if ((socket = qt_safe_socket(AF_INET, SOCK_STREAM, IPPROTO_IP)) == -1) return interfaces; // error ifaddrs *interfaceListing; diff --git a/src/network/socket/qnativesocketengine_p.h b/src/network/socket/qnativesocketengine_p.h index 7811635..a9479d3 100644 --- a/src/network/socket/qnativesocketengine_p.h +++ b/src/network/socket/qnativesocketengine_p.h @@ -63,35 +63,6 @@ QT_BEGIN_NAMESPACE -#ifndef Q_OS_WIN -// Almost always the same. If not, specify in qplatformdefs.h. -#if !defined(QT_SOCKOPTLEN_T) -# define QT_SOCKOPTLEN_T QT_SOCKLEN_T -#endif - -// Tru64 redefines accept -> _accept with _XOPEN_SOURCE_EXTENDED -static inline int qt_socket_accept(int s, struct sockaddr *addr, QT_SOCKLEN_T *addrlen) -{ return ::accept(s, addr, static_cast(addrlen)); } -#if defined(accept) -# undef accept -#endif - -// UnixWare 7 redefines listen -> _listen -static inline int qt_socket_listen(int s, int backlog) -{ return ::listen(s, backlog); } -#if defined(listen) -# undef listen -#endif - -// UnixWare 7 redefines socket -> _socket -static inline int qt_socket_socket(int domain, int type, int protocol) -{ return ::socket(domain, type, protocol); } -#if defined(socket) -# undef socket -#endif - -#endif - // Use our own defines and structs which we know are correct # define QT_SS_MAXSIZE 128 # define QT_SS_ALIGNSIZE (sizeof(qint64)) diff --git a/src/network/socket/qnativesocketengine_unix.cpp b/src/network/socket/qnativesocketengine_unix.cpp index 0c1fa19..3991ae6 100644 --- a/src/network/socket/qnativesocketengine_unix.cpp +++ b/src/network/socket/qnativesocketengine_unix.cpp @@ -508,7 +508,7 @@ qint64 QNativeSocketEnginePrivate::nativeBytesAvailable() const int nbytes = 0; // gives shorter than true amounts on Unix domain sockets. qint64 available = 0; - if (::ioctl(socketDescriptor, FIONREAD, (char *) &nbytes) >= 0) + if (qt_safe_ioctl(socketDescriptor, FIONREAD, (char *) &nbytes) >= 0) available = (qint64) nbytes; #if defined (QNATIVESOCKETENGINE_DEBUG) @@ -634,8 +634,8 @@ qint64 QNativeSocketEnginePrivate::nativeSendDatagram(const char *data, qint64 l ssize_t sentBytes; do { - sentBytes = ::sendto(socketDescriptor, data, len, - 0, sockAddrPtr, sockAddrSize); + sentBytes = qt_safe_sendto(socketDescriptor, data, len, + 0, sockAddrPtr, sockAddrSize); } while (sentBytes == -1 && errno == EINTR); if (sentBytes < 0) { diff --git a/src/network/socket/qnet_unix_p.h b/src/network/socket/qnet_unix_p.h index 392c1e2..b33d225 100644 --- a/src/network/socket/qnet_unix_p.h +++ b/src/network/socket/qnet_unix_p.h @@ -59,6 +59,12 @@ #include #include +// for inet_addr +#include +#include +#include + + QT_BEGIN_NAMESPACE // Almost always the same. If not, specify in qplatformdefs.h. @@ -148,6 +154,28 @@ static inline int qt_safe_connect(int sockfd, const struct sockaddr *addr, QT_SO # undef listen #endif +template +static inline int qt_safe_ioctl(int sockfd, int request, T arg) +{ + return ::ioctl(sockfd, request, arg); +} + +static inline in_addr_t qt_safe_inet_addr(const char *cp) +{ + return ::inet_addr(cp); +} + +static inline int qt_safe_sendto(int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *to, QT_SOCKLEN_T tolen) +{ +#ifdef MSG_NOSIGNAL + flags |= MSG_NOSIGNAL; +#endif + + register int ret; + EINTR_LOOP(ret, ::sendto(sockfd, buf, len, flags, to, tolen)); + return ret; +} + QT_END_NAMESPACE #endif // QNET_UNIX_P_H -- cgit v0.12