Qt 4.8
Functions
qnativesocketengine_unix.cpp File Reference
#include "qnativesocketengine_p.h"
#include "private/qnet_unix_p.h"
#include "qiodevice.h"
#include "qhostaddress.h"
#include "qelapsedtimer.h"
#include "qvarlengtharray.h"
#include "qnetworkinterface.h"
#include <time.h>
#include <errno.h>
#include <fcntl.h>
#include <net/if.h>
#include <netinet/tcp.h>

Go to the source code of this file.

Functions

static bool multicastMembershipHelper (QNativeSocketEnginePrivate *d, int how6, int how4, const QHostAddress &groupAddress, const QNetworkInterface &interface)
 
static void qt_socket_getPortAndAddress (const qt_sockaddr *s, quint16 *port, QHostAddress *addr)
 

Function Documentation

◆ multicastMembershipHelper()

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

Definition at line 588 of file qnativesocketengine_unix.cpp.

Referenced by QNativeSocketEnginePrivate::nativeJoinMulticastGroup(), and QNativeSocketEnginePrivate::nativeLeaveMulticastGroup().

593 {
594  int level = 0;
595  int sockOpt = 0;
596  void *sockArg;
597  int sockArgSize;
598 
599  ip_mreq mreq4;
600 #ifndef QT_NO_IPV6
601  ipv6_mreq mreq6;
602 
603  if (groupAddress.protocol() == QAbstractSocket::IPv6Protocol) {
604  level = IPPROTO_IPV6;
605  sockOpt = how6;
606  sockArg = &mreq6;
607  sockArgSize = sizeof(mreq6);
608  memset(&mreq6, 0, sizeof(mreq6));
609  Q_IPV6ADDR ip6 = groupAddress.toIPv6Address();
610  memcpy(&mreq6.ipv6mr_multiaddr, &ip6, sizeof(ip6));
611  mreq6.ipv6mr_interface = interface.index();
612  } else
613 #endif
614  if (groupAddress.protocol() == QAbstractSocket::IPv4Protocol) {
615  level = IPPROTO_IP;
616  sockOpt = how4;
617  sockArg = &mreq4;
618  sockArgSize = sizeof(mreq4);
619  memset(&mreq4, 0, sizeof(mreq4));
620  mreq4.imr_multiaddr.s_addr = htonl(groupAddress.toIPv4Address());
621 
622  if (interface.isValid()) {
623  QList<QNetworkAddressEntry> addressEntries = interface.addressEntries();
624  if (!addressEntries.isEmpty()) {
625  QHostAddress firstIP = addressEntries.first().ip();
626  mreq4.imr_interface.s_addr = htonl(firstIP.toIPv4Address());
627  } else {
630  return false;
631  }
632  } else {
633  mreq4.imr_interface.s_addr = INADDR_ANY;
634  }
635  } else {
636  // unreachable
639  return false;
640  }
641 
642  int res = setsockopt(d->socketDescriptor, level, sockOpt, sockArg, sockArgSize);
643  if (res == -1) {
644  switch (errno) {
645  case ENOPROTOOPT:
648  break;
649  case EADDRNOTAVAIL:
652  break;
653  default:
656  break;
657  }
658  return false;
659  }
660  return true;
661 }
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...
int errno

◆ qt_socket_getPortAndAddress()

static void qt_socket_getPortAndAddress ( const qt_sockaddr s,
quint16 port,
QHostAddress addr 
)
inlinestatic

Definition at line 108 of file qnativesocketengine_unix.cpp.

Referenced by QNativeSocketEnginePrivate::fetchConnectionParameters(), and QNativeSocketEnginePrivate::nativeReceiveDatagram().

109 {
110 #if !defined(QT_NO_IPV6)
111  if (s->a.sa_family == AF_INET6) {
112  Q_IPV6ADDR tmp;
113  memcpy(&tmp, &s->a6.sin6_addr, sizeof(tmp));
114  if (addr) {
115  QHostAddress tmpAddress;
116  tmpAddress.setAddress(tmp);
117  *addr = tmpAddress;
118 #ifndef QT_NO_IPV6IFNAME
119  char scopeid[IFNAMSIZ];
120  if (::if_indextoname(s->a6.sin6_scope_id, scopeid)) {
121  addr->setScopeId(QLatin1String(scopeid));
122  } else
123 #endif
125  }
126  if (port)
127  *port = ntohs(s->a6.sin6_port);
128  return;
129  }
130 #endif
131  if (port)
132  *port = ntohs(s->a4.sin_port);
133  if (addr) {
134  QHostAddress tmpAddress;
135  tmpAddress.setAddress(ntohl(s->a4.sin_addr.s_addr));
136  *addr = tmpAddress;
137  }
138 }
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.
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
struct qt_in6_addr sin6_addr
void setAddress(quint32 ip4Addr)
Set the IPv4 address specified by ip4Addr.
qt_sockaddr_in6 a6
The QHostAddress class provides an IP address.
Definition: qhostaddress.h:70
#define AF_INET6