Qt 4.8
Macros | Functions
qnativesocketengine_win.cpp File Reference
#include <winsock2.h>
#include <ws2tcpip.h>
#include "qnativesocketengine_p.h"
#include <qabstracteventdispatcher.h>
#include <qsocketnotifier.h>
#include <qdebug.h>
#include <qdatetime.h>
#include <qnetworkinterface.h>

Go to the source code of this file.

Macros

#define _WSAIOW(x, y)   (IOC_IN|(x)|(y))
 
#define AF_INET6   23 /* Internetwork Version 6 */
 
#define IOC_VENDOR   0x18000000
 
#define QT_SOCKLEN_T   int
 
#define QT_SOCKOPTLEN_T   int
 
#define SIO_UDP_CONNRESET   _WSAIOW(IOC_VENDOR,12)
 
#define SO_EXCLUSIVEADDRUSE   ((int)(~SO_REUSEADDR)) /* disallow local address reuse */
 
#define WS_ERROR_DEBUG(x)   Q_UNUSED(x)
 

Functions

static bool multicastMembershipHelper (QNativeSocketEnginePrivate *d, int how6, int how4, const QHostAddress &groupAddress, const QNetworkInterface &iface)
 
static int qt_socket_getMaxMsgSize (int socketDescriptor)
 
static void qt_socket_getPortAndAddress (SOCKET socketDescriptor, const qt_sockaddr *sa, quint16 *port, QHostAddress *address)
 
static QAbstractSocket::SocketType qt_socket_getType (int socketDescriptor)
 
static void qt_socket_setPortAndAddress (SOCKET socketDescriptor, sockaddr_in *sockAddrIPv4, qt_sockaddr_in6 *sockAddrIPv6, quint16 port, const QHostAddress &address, sockaddr **sockAddrPtr, QT_SOCKLEN_T *sockAddrSize)
 Sets the port and address to a sockaddr. More...
 

Macro Definition Documentation

◆ _WSAIOW

#define _WSAIOW (   x,
 
)    (IOC_IN|(x)|(y))

Definition at line 298 of file qnativesocketengine_win.cpp.

◆ AF_INET6

#define AF_INET6   23 /* Internetwork Version 6 */

◆ IOC_VENDOR

#define IOC_VENDOR   0x18000000

Definition at line 295 of file qnativesocketengine_win.cpp.

◆ QT_SOCKLEN_T

#define QT_SOCKLEN_T   int

◆ QT_SOCKOPTLEN_T

#define QT_SOCKOPTLEN_T   int

Definition at line 164 of file qnativesocketengine_win.cpp.

Referenced by multicastMembershipHelper().

◆ SIO_UDP_CONNRESET

#define SIO_UDP_CONNRESET   _WSAIOW(IOC_VENDOR,12)

Definition at line 300 of file qnativesocketengine_win.cpp.

◆ SO_EXCLUSIVEADDRUSE

#define SO_EXCLUSIVEADDRUSE   ((int)(~SO_REUSEADDR)) /* disallow local address reuse */

Definition at line 159 of file qnativesocketengine_win.cpp.

◆ WS_ERROR_DEBUG

#define WS_ERROR_DEBUG (   x)    Q_UNUSED(x)

Function Documentation

◆ multicastMembershipHelper()

static bool multicastMembershipHelper ( QNativeSocketEnginePrivate d,
int  how6,
int  how4,
const QHostAddress groupAddress,
const QNetworkInterface iface 
)
static

Definition at line 840 of file qnativesocketengine_win.cpp.

845 {
846  int level = 0;
847  int sockOpt = 0;
848  char *sockArg;
849  int sockArgSize;
850 
851  struct ip_mreq mreq4;
852 #ifndef QT_NO_IPV6
853  struct ipv6_mreq mreq6;
854 
855  if (groupAddress.protocol() == QAbstractSocket::IPv6Protocol) {
856  level = IPPROTO_IPV6;
857  sockOpt = how6;
858  sockArg = reinterpret_cast<char *>(&mreq6);
859  sockArgSize = sizeof(mreq6);
860  memset(&mreq6, 0, sizeof(mreq6));
861  Q_IPV6ADDR ip6 = groupAddress.toIPv6Address();
862  memcpy(&mreq6.ipv6mr_multiaddr, &ip6, sizeof(ip6));
863  mreq6.ipv6mr_interface = iface.index();
864  } else
865 #endif
866  if (groupAddress.protocol() == QAbstractSocket::IPv4Protocol) {
867  level = IPPROTO_IP;
868  sockOpt = how4;
869  sockArg = reinterpret_cast<char *>(&mreq4);
870  sockArgSize = sizeof(mreq4);
871  memset(&mreq4, 0, sizeof(mreq4));
872  mreq4.imr_multiaddr.s_addr = htonl(groupAddress.toIPv4Address());
873 
874  if (iface.isValid()) {
875  QList<QNetworkAddressEntry> addressEntries = iface.addressEntries();
876  if (!addressEntries.isEmpty()) {
877  QHostAddress firstIP = addressEntries.first().ip();
878  mreq4.imr_interface.s_addr = htonl(firstIP.toIPv4Address());
879  } else {
882  return false;
883  }
884  } else {
885  mreq4.imr_interface.s_addr = INADDR_ANY;
886  }
887  } else {
888  // unreachable
891  return false;
892  }
893 
894  int res = setsockopt(d->socketDescriptor, level, sockOpt, sockArg, sockArgSize);
895  if (res == -1) {
898  return false;
899  }
900  return true;
901 }
Q_IPV6ADDR toIPv6Address() const
Returns the IPv6 address as a Q_IPV6ADDR structure.
QHostAddress ip() const
This function returns one IPv4 or IPv6 address found, that was found in a network interface...
quint32 toIPv4Address() const
Returns the IPv4 address as a number.
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
Definition: qlist.h:152
bool isValid() const
Returns true if this QNetworkInterface object contains valid information about a network interface...
T & first()
Returns a reference to the first item in the list.
Definition: qlist.h:282
QAbstractSocket::NetworkLayerProtocol protocol() const
Returns the network layer protocol of the host address.
void setError(QAbstractSocket::SocketError error, ErrorString errorString) const
Sets the error and error string if not set already.
int index() const
Returns the interface system index, if known.
The QHostAddress class provides an IP address.
Definition: qhostaddress.h:70
QList< QNetworkAddressEntry > addressEntries() const
Returns the list of IP addresses that this interface possesses along with their associated netmasks a...

◆ qt_socket_getMaxMsgSize()

static int qt_socket_getMaxMsgSize ( int  socketDescriptor)
inlinestatic
Warning
This function is not part of the public interface.

Definition at line 259 of file qnativesocketengine_win.cpp.

260 {
261  int value = 0;
262  QT_SOCKLEN_T valueSize = sizeof(value);
263  if (::getsockopt(socketDescriptor, SOL_SOCKET, SO_MAX_MSG_SIZE, (char *) &value, &valueSize) != 0) {
264  WS_ERROR_DEBUG(WSAGetLastError());
265  }
266  return value;
267 }
#define WS_ERROR_DEBUG(x)
#define QT_SOCKLEN_T

◆ qt_socket_getPortAndAddress()

static void qt_socket_getPortAndAddress ( SOCKET  socketDescriptor,
const qt_sockaddr sa,
quint16 port,
QHostAddress address 
)
inlinestatic

Definition at line 171 of file qnativesocketengine_win.cpp.

Referenced by multicastMembershipHelper().

172 {
173 #if !defined (QT_NO_IPV6)
174  if (sa->a.sa_family == AF_INET6) {
175  const qt_sockaddr_in6 *sa6 = &sa->a6;
176  Q_IPV6ADDR tmp;
177  for (int i = 0; i < 16; ++i)
178  tmp.c[i] = sa6->sin6_addr.qt_s6_addr[i];
179  if (address) {
180  QHostAddress a;
181  a.setAddress(tmp);
183  *address = a;
184  }
185  if (port)
186  WSANtohs(socketDescriptor, sa6->sin6_port, port);
187  } else
188 #endif
189  if (sa->a.sa_family == AF_INET) {
190  const sockaddr_in *sa4 = &sa->a4;
191  unsigned long addr;
192  WSANtohl(socketDescriptor, sa4->sin_addr.s_addr, &addr);
193  QHostAddress a;
194  a.setAddress(addr);
195  if (address)
196  *address = a;
197  if (port)
198  WSANtohs(socketDescriptor, sa4->sin_port, port);
199  }
200 }
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
struct qt_in6_addr sin6_addr
long ASN1_INTEGER_get ASN1_INTEGER * a
#define AF_INET6
void setAddress(quint32 ip4Addr)
Set the IPv4 address specified by ip4Addr.
u_char qt_s6_addr[16]
qt_sockaddr_in6 a6
if(void) toggleToolbarShown
The QHostAddress class provides an IP address.
Definition: qhostaddress.h:70

◆ qt_socket_getType()

static QAbstractSocket::SocketType qt_socket_getType ( int  socketDescriptor)
inlinestatic
Warning
This function is not part of the public interface.

Definition at line 241 of file qnativesocketengine_win.cpp.

242 {
243  int value = 0;
244  QT_SOCKLEN_T valueSize = sizeof(value);
245  if (::getsockopt(socketDescriptor, SOL_SOCKET, SO_TYPE, (char *) &value, &valueSize) != 0) {
246  WS_ERROR_DEBUG(WSAGetLastError());
247  } else {
248  if (value == SOCK_STREAM)
250  else if (value == SOCK_DGRAM)
252  }
254 }
#define WS_ERROR_DEBUG(x)
#define QT_SOCKLEN_T

◆ qt_socket_setPortAndAddress()

static void qt_socket_setPortAndAddress ( SOCKET  socketDescriptor,
sockaddr_in *  sockAddrIPv4,
qt_sockaddr_in6 sockAddrIPv6,
quint16  port,
const QHostAddress address,
sockaddr **  sockAddrPtr,
QT_SOCKLEN_T sockAddrSize 
)
inlinestatic

Sets the port and address to a sockaddr.

Warning
This function is not part of the public interface.

Requires that sa point to the IPv6 struct if the address is IPv6.

Definition at line 210 of file qnativesocketengine_win.cpp.

Referenced by multicastMembershipHelper().

212 {
213 #if !defined(QT_NO_IPV6)
214  if (address.protocol() == QAbstractSocket::IPv6Protocol) {
215  memset(sockAddrIPv6, 0, sizeof(qt_sockaddr_in6));
216  sockAddrIPv6->sin6_family = AF_INET6;
217  sockAddrIPv6->sin6_scope_id = address.scopeId().toInt();
218  WSAHtons(socketDescriptor, port, &(sockAddrIPv6->sin6_port));
219  Q_IPV6ADDR tmp = address.toIPv6Address();
220  memcpy(&(sockAddrIPv6->sin6_addr.qt_s6_addr), &tmp, sizeof(tmp));
221  *sockAddrSize = sizeof(qt_sockaddr_in6);
222  *sockAddrPtr = (struct sockaddr *) sockAddrIPv6;
223  } else
224 #endif
225  if (address.protocol() == QAbstractSocket::IPv4Protocol
227  memset(sockAddrIPv4, 0, sizeof(sockaddr_in));
228  sockAddrIPv4->sin_family = AF_INET;
229  WSAHtons(socketDescriptor, port, &(sockAddrIPv4->sin_port));
230  WSAHtonl(socketDescriptor, address.toIPv4Address(), &(sockAddrIPv4->sin_addr.s_addr));
231  *sockAddrSize = sizeof(sockaddr_in);
232  *sockAddrPtr = (struct sockaddr *) sockAddrIPv4;
233  } else {
234  // unreachable
235  }
236 }
QString scopeId() const
Returns the scope ID of an IPv6 address.
Q_IPV6ADDR toIPv6Address() const
Returns the IPv6 address as a Q_IPV6ADDR structure.
int toInt(bool *ok=0, int base=10) const
Returns the string converted to an int using base base, which is 10 by default and must be between 2 ...
Definition: qstring.cpp:6090
struct qt_in6_addr sin6_addr
quint32 toIPv4Address() const
Returns the IPv4 address as a number.
#define AF_INET6
QAbstractSocket::NetworkLayerProtocol protocol() const
Returns the network layer protocol of the host address.
u_char qt_s6_addr[16]