QT_BEGIN_NAMESPACE static QHostAddress addressFromSockaddr(sockaddr *sa) { QHostAddress address; if (!sa) return address; if (sa->sa_family == AF_INET) address.setAddress(htonl(((sockaddr_in *)sa)->sin_addr.s_addr)); #ifndef QT_NO_IPV6 else if (sa->sa_family == AF_INET6) { address.setAddress(((sockaddr_in6 *)sa)->sin6_addr.s6_addr); int scope = ((sockaddr_in6 *)sa)->sin6_scope_id; if (scope) { #ifndef QT_NO_IPV6IFNAME char scopeid[IFNAMSIZ]; if (::if_indextoname(scope, scopeid)) { address.setScopeId(QLatin1String(scopeid)); } else #endif address.setScopeId(QString::number(scope)); } } #endif return address; }
QT_BEGIN_NAMESPACE static QHostAddress addressFromSockaddr(sockaddr *sa, int ifindex = 0, const QString &ifname = QString()) { QHostAddress address; if (!sa) return address; if (sa->sa_family == AF_INET) address.setAddress(htonl(((sockaddr_in *)sa)->sin_addr.s_addr)); else if (sa->sa_family == AF_INET6) { address.setAddress(((sockaddr_in6 *)sa)->sin6_addr.s6_addr); int scope = ((sockaddr_in6 *)sa)->sin6_scope_id; if (scope && scope == ifindex) { // this is the most likely scenario: // a scope ID in a socket is that of the interface this address came from address.setScopeId(ifname); } else if (scope) { #ifndef QT_NO_IPV6IFNAME char scopeid[IFNAMSIZ]; if (::if_indextoname(scope, scopeid)) { address.setScopeId(QLatin1String(scopeid)); } else #endif address.setScopeId(QString::number(uint(scope))); } } return address; }
/* Extracts the port and address from a sockaddr, and stores them in \a port and \a addr if they are non-null. */ static inline void qt_socket_getPortAndAddress(SOCKET socketDescriptor, const qt_sockaddr *sa, quint16 *port, QHostAddress *address) { #if !defined (QT_NO_IPV6) if (sa->a.sa_family == AF_INET6) { const qt_sockaddr_in6 *sa6 = &sa->a6; Q_IPV6ADDR tmp; for (int i = 0; i < 16; ++i) tmp.c[i] = sa6->sin6_addr.qt_s6_addr[i]; if (address) { QHostAddress a; a.setAddress(tmp); a.setScopeId(QString::number(sa6->sin6_scope_id)); *address = a; } if (port) WSANtohs(socketDescriptor, sa6->sin6_port, port); } else #endif if (sa->a.sa_family == AF_INET) { const sockaddr_in *sa4 = &sa->a4; unsigned long addr; WSANtohl(socketDescriptor, sa4->sin_addr.s_addr, &addr); QHostAddress a; a.setAddress(addr); if (address) *address = a; if (port) WSANtohs(socketDescriptor, sa4->sin_port, port); } }
static QHostAddress addressFromSockaddr(sockaddr *sa) { QHostAddress address; if (!sa) return address; if (sa->sa_family == AF_INET) address.setAddress(htonl(((sockaddr_in *)sa)->sin_addr.s_addr)); else if (sa->sa_family == AF_INET6) { address.setAddress(((qt_sockaddr_in6 *)sa)->sin6_addr.qt_s6_addr); int scope = ((qt_sockaddr_in6 *)sa)->sin6_scope_id; if (scope) address.setScopeId(QString::number(scope)); } else qWarning("Got unknown socket family %d", sa->sa_family); return address; }
Transport::Transport(JingleContent *content) : JingleTransport(content) { QList<QHostAddress> addresses; foreach (const QNetworkInterface &networkInterface, QNetworkInterface::allInterfaces()) { if (networkInterface.flags() & QNetworkInterface::IsLoopBack) continue; foreach (const QNetworkAddressEntry &addressEntry, networkInterface.addressEntries()) { QHostAddress address = addressEntry.ip(); if(address.protocol() == QAbstractSocket::IPv6Protocol && XMPP::Ice176::isIPv6LinkLocalAddress(address)) address.setScopeId(networkInterface.name()); addresses << address; } } qSort(addresses.begin(), addresses.end(), addressLessThen); QList<XMPP::Ice176::LocalAddress> localAddresses; foreach (const QHostAddress &address, addresses) { XMPP::Ice176::LocalAddress localAddress; localAddress.addr = address; localAddresses << localAddress; }
static PyObject *meth_QHostAddress_setScopeId(PyObject *sipSelf, PyObject *sipArgs) { PyObject *sipParseErr = NULL; { const QString* a0; int a0State = 0; QHostAddress *sipCpp; if (sipParseArgs(&sipParseErr, sipArgs, "BJ1", &sipSelf, sipType_QHostAddress, &sipCpp, sipType_QString,&a0, &a0State)) { sipCpp->setScopeId(*a0); sipReleaseType(const_cast<QString *>(a0),sipType_QString,a0State); Py_INCREF(Py_None); return Py_None; } } /* Raise an exception if the arguments couldn't be parsed. */ sipNoMethod(sipParseErr, sipName_QHostAddress, sipName_setScopeId, doc_QHostAddress_setScopeId); return NULL; }
QHostInfo QHostInfoAgent::fromName(const QString &hostName) { QHostInfo results; #if defined(QHOSTINFO_DEBUG) qDebug("QHostInfoAgent::fromName(%s) looking up...", hostName.toLatin1().constData()); #endif // Load res_init on demand. static volatile bool triedResolve = false; if (!triedResolve) { QMutexLocker locker(QMutexPool::globalInstanceGet(&local_res_init)); if (!triedResolve) { resolveLibrary(); triedResolve = true; } } // If res_init is available, poll it. if (local_res_init) local_res_init(); QHostAddress address; if (address.setAddress(hostName)) { // Reverse lookup // Reverse lookups using getnameinfo are broken on darwin, use gethostbyaddr instead. #if !defined (QT_NO_GETADDRINFO) && !defined (Q_OS_DARWIN) sockaddr_in sa4; #ifndef QT_NO_IPV6 sockaddr_in6 sa6; #endif sockaddr *sa = 0; QT_SOCKLEN_T saSize = 0; if (address.protocol() == QAbstractSocket::IPv4Protocol) { sa = (sockaddr *)&sa4; saSize = sizeof(sa4); memset(&sa4, 0, sizeof(sa4)); sa4.sin_family = AF_INET; sa4.sin_addr.s_addr = htonl(address.toIPv4Address()); } #ifndef QT_NO_IPV6 else { sa = (sockaddr *)&sa6; saSize = sizeof(sa6); memset(&sa6, 0, sizeof(sa6)); sa6.sin6_family = AF_INET6; memcpy(sa6.sin6_addr.s6_addr, address.toIPv6Address().c, sizeof(sa6.sin6_addr.s6_addr)); } #endif char hbuf[NI_MAXHOST]; if (sa && getnameinfo(sa, saSize, hbuf, sizeof(hbuf), 0, 0, 0) == 0) results.setHostName(QString::fromLatin1(hbuf)); #else 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.setHostName(QString::fromLatin1(ent->h_name)); #endif if (results.hostName().isEmpty()) results.setHostName(address.toString()); results.setAddresses(QList<QHostAddress>() << address); return results; } // IDN support QByteArray aceHostname = QUrl::toAce(hostName); results.setHostName(hostName); if (aceHostname.isEmpty()) { results.setError(QHostInfo::HostNotFound); results.setErrorString(hostName.isEmpty() ? QCoreApplication::translate("QHostInfoAgent", "No host name given") : QCoreApplication::translate("QHostInfoAgent", "Invalid hostname")); return results; } #if !defined (QT_NO_GETADDRINFO) // Call getaddrinfo, and place all IPv4 addresses at the start and // the IPv6 addresses at the end of the address list in results. addrinfo *res = 0; struct addrinfo hints; memset(&hints, 0, sizeof(hints)); hints.ai_family = PF_UNSPEC; #ifdef Q_ADDRCONFIG hints.ai_flags = Q_ADDRCONFIG; #endif int result = getaddrinfo(aceHostname.constData(), 0, &hints, &res); # ifdef Q_ADDRCONFIG if (result == EAI_BADFLAGS) { // if the lookup failed with AI_ADDRCONFIG set, try again without it hints.ai_flags = 0; result = getaddrinfo(aceHostname.constData(), 0, &hints, &res); } # endif if (result == 0) { addrinfo *node = res; QList<QHostAddress> addresses; while (node) { #ifdef QHOSTINFO_DEBUG qDebug() << "getaddrinfo node: flags:" << node->ai_flags << "family:" << node->ai_family << "ai_socktype:" << node->ai_socktype << "ai_protocol:" << node->ai_protocol << "ai_addrlen:" << node->ai_addrlen; #endif if (node->ai_family == AF_INET) { QHostAddress addr; addr.setAddress(ntohl(((sockaddr_in *) node->ai_addr)->sin_addr.s_addr)); if (!addresses.contains(addr)) addresses.append(addr); } #ifndef QT_NO_IPV6 else if (node->ai_family == AF_INET6) { QHostAddress addr; sockaddr_in6 *sa6 = (sockaddr_in6 *) node->ai_addr; addr.setAddress(sa6->sin6_addr.s6_addr); if (sa6->sin6_scope_id) addr.setScopeId(QString::number(sa6->sin6_scope_id)); if (!addresses.contains(addr)) addresses.append(addr); } #endif node = node->ai_next; } if (addresses.isEmpty() && node == 0) { // Reached the end of the list, but no addresses were found; this // means the list contains one or more unknown address types. results.setError(QHostInfo::UnknownError); results.setErrorString(tr("Unknown address type")); } results.setAddresses(addresses); freeaddrinfo(res); } else if (result == EAI_NONAME || result == EAI_FAIL #ifdef EAI_NODATA // EAI_NODATA is deprecated in RFC 3493 || result == EAI_NODATA #endif ) { results.setError(QHostInfo::HostNotFound); results.setErrorString(tr("Host not found")); } else { results.setError(QHostInfo::UnknownError); results.setErrorString(QString::fromLocal8Bit(gai_strerror(result))); } #else // Fall back to gethostbyname for platforms that don't define // getaddrinfo. gethostbyname does not support IPv6, and it's not // reentrant on all platforms. For now this is okay since we only // use one QHostInfoAgent, but if more agents are introduced, locking // must be provided. QMutexLocker locker(::getHostByNameMutex()); hostent *result = gethostbyname(aceHostname.constData()); if (result) { if (result->h_addrtype == AF_INET) { QList<QHostAddress> addresses; for (char **p = result->h_addr_list; *p != 0; p++) { QHostAddress addr; addr.setAddress(ntohl(*((quint32 *)*p))); if (!addresses.contains(addr)) addresses.prepend(addr); } results.setAddresses(addresses); } else { results.setError(QHostInfo::UnknownError); results.setErrorString(tr("Unknown address type")); } #if !defined(Q_OS_VXWORKS) } else if (h_errno == HOST_NOT_FOUND || h_errno == NO_DATA || h_errno == NO_ADDRESS) { results.setError(QHostInfo::HostNotFound); results.setErrorString(tr("Host not found")); #endif } else { results.setError(QHostInfo::UnknownError); results.setErrorString(tr("Unknown error")); } #endif // !defined (QT_NO_GETADDRINFO) #if defined(QHOSTINFO_DEBUG) if (results.error() != QHostInfo::NoError) { qDebug("QHostInfoAgent::fromName(): error #%d %s", h_errno, results.errorString().toLatin1().constData()); } else { QString tmp; QList<QHostAddress> addresses = results.addresses(); for (int i = 0; i < addresses.count(); ++i) { if (i != 0) tmp += ", "; tmp += addresses.at(i).toString(); } qDebug("QHostInfoAgent::fromName(): found %i entries for \"%s\": {%s}", addresses.count(), hostName.toLatin1().constData(), tmp.toLatin1().constData()); } #endif return results; }
void ServerPool::SelectDefaultListen(bool force) { if (!force) { QReadLocker rlock(&naLock); if (!naList_4.isEmpty() || !naList_6.isEmpty()) // lists are already populated, do nothing return; } QWriteLocker wlock(&naLock); naList_4.clear(); naList_6.clear(); // populate stored IPv4 and IPv6 addresses QHostAddress config_v4(gCoreContext->resolveSettingAddress( "BackendServerIP", QString(), gCoreContext->ResolveIPv4, true)); bool v4IsSet = config_v4.isNull() ? true : false; #if !defined(QT_NO_IPV6) QHostAddress config_v6(gCoreContext->resolveSettingAddress( "BackendServerIP6", QString(), gCoreContext->ResolveIPv6, true)); bool v6IsSet = config_v6.isNull() ? true : false; #endif bool allowLinkLocal = gCoreContext->GetNumSetting("AllowLinkLocal", true) > 0; // loop through all available interfaces QList<QNetworkInterface> IFs = QNetworkInterface::allInterfaces(); QList<QNetworkInterface>::const_iterator qni; for (qni = IFs.begin(); qni != IFs.end(); ++qni) { if ((qni->flags() & QNetworkInterface::IsRunning) == 0) continue; QList<QNetworkAddressEntry> IPs = qni->addressEntries(); QList<QNetworkAddressEntry>::iterator qnai; for (qnai = IPs.begin(); qnai != IPs.end(); ++qnai) { QHostAddress ip = qnai->ip(); #if !defined(QT_NO_IPV6) if (ip.protocol() == QAbstractSocket::IPv4Protocol) { #endif if (naList_4.contains(*qnai)) // already defined, skip continue; else if (!config_v4.isNull() && (ip == config_v4)) { // IPv4 address is defined, add it LOG(VB_GENERAL, LOG_DEBUG, QString("Adding BackendServerIP to address list.")); naList_4.append(*qnai); v4IsSet = true; } else if (ip == QHostAddress::LocalHost) { // always listen on LocalHost LOG(VB_GENERAL, LOG_DEBUG, QString("Adding IPv4 loopback to address list.")); naList_4.append(*qnai); if (!v4IsSet && (config_v4 == ip)) v4IsSet = true; } else if (ip.isInSubnet(kLinkLocal) && allowLinkLocal) { // optionally listen on linklocal // the next clause will enable it anyway if no IP address // has been set LOG(VB_GENERAL, LOG_DEBUG, QString("Adding link-local '%1' to address list.") .arg(PRETTYIP_(ip))); naList_4.append(*qnai); } else if (config_v4.isNull()) { // IPv4 address is not defined, populate one // restrict autoconfiguration to RFC1918 private networks static QPair<QHostAddress, int> privNet1 = QHostAddress::parseSubnet("10.0.0.0/8"), privNet2 = QHostAddress::parseSubnet("172.16.0.0/12"), privNet3 = QHostAddress::parseSubnet("192.168.0.0/16"); if (ip.isInSubnet(privNet1) || ip.isInSubnet(privNet2) || ip.isInSubnet(privNet3)) { LOG(VB_GENERAL, LOG_DEBUG, QString("Adding '%1' to address list.") .arg(PRETTYIP_(ip))); naList_4.append(*qnai); } else if (ip.isInSubnet(kLinkLocal)) { LOG(VB_GENERAL, LOG_DEBUG, QString("Adding link-local '%1' to address list.") .arg(PRETTYIP_(ip))); naList_4.append(*qnai); } else LOG(VB_GENERAL, LOG_DEBUG, QString("Skipping " "non-private address during IPv4 autoselection: %1") .arg(PRETTYIP_(ip))); } else LOG(VB_GENERAL, LOG_DEBUG, QString("Skipping address: %1") .arg(PRETTYIP_(ip))); #if !defined(QT_NO_IPV6) } else { if (ip.isInSubnet(kLinkLocal6)) { // set scope id for link local address ip.setScopeId(qni->name()); qnai->setIp(ip); } if (naList_6.contains(*qnai)) // already defined, skip continue; else if ((!config_v6.isNull()) && (ip == config_v6)) { // IPv6 address is defined, add it LOG(VB_GENERAL, LOG_DEBUG, QString("Adding BackendServerIP6 to address list.")); naList_6.append(*qnai); v6IsSet = true; } else if (ip == QHostAddress::LocalHostIPv6) { // always listen on LocalHost LOG(VB_GENERAL, LOG_DEBUG, QString("Adding IPv6 loopback to address list.")); naList_6.append(*qnai); if (!v6IsSet && (config_v6 == ip)) v6IsSet = true; } else if (ip.isInSubnet(kLinkLocal6) && allowLinkLocal) { // optionally listen on linklocal // the next clause will enable it anyway if no IP address // has been set LOG(VB_GENERAL, LOG_DEBUG, QString("Adding link-local '%1' to address list.") .arg(ip.toString())); naList_6.append(*qnai); } else if (config_v6.isNull()) { if (ip.isInSubnet(kLinkLocal6)) LOG(VB_GENERAL, LOG_DEBUG, QString("Adding link-local '%1' to address list.") .arg(PRETTYIP_(ip))); else LOG(VB_GENERAL, LOG_DEBUG, QString("Adding '%1' to address list.") .arg(PRETTYIP_(ip))); naList_6.append(*qnai); } else LOG(VB_GENERAL, LOG_DEBUG, QString("Skipping address: %1") .arg(PRETTYIP_(ip))); } #endif } } if (!v4IsSet && (config_v4 != QHostAddress::LocalHost) && !naList_4.isEmpty()) { LOG(VB_GENERAL, LOG_CRIT, LOC + QString("Host is configured to listen " "on %1, but address is not used on any local network " "interfaces.").arg(config_v4.toString())); } #if !defined(QT_NO_IPV6) if (!v6IsSet && (config_v6 != QHostAddress::LocalHostIPv6) && !naList_6.isEmpty()) { LOG(VB_GENERAL, LOG_CRIT, LOC + QString("Host is configured to listen " "on %1, but address is not used on any local network " "interfaces.").arg(PRETTYIP_(config_v6))); } #endif // NOTE: there is no warning for the case where both defined addresses // are localhost, and neither are found. however this would also // mean there is no configured network at all, and should be // sufficiently rare a case as to not worry about it. }
QHostInfo QHostInfoAgent::fromName(const QString &hostName) { QHostInfo results; #if defined(QHOSTINFO_DEBUG) qDebug("QHostInfoAgent::fromName(%s) looking up...", hostName.toLatin1().constData()); #endif // Load res_init on demand. resolveLibrary(); // If res_init is available, poll it. if (local_res_init) local_res_init(); QHostAddress address; if (address.setAddress(hostName)) { // Reverse lookup sockaddr_in sa4; sockaddr_in6 sa6; sockaddr *sa = 0; QT_SOCKLEN_T saSize = 0; if (address.protocol() == QAbstractSocket::IPv4Protocol) { sa = (sockaddr *)&sa4; saSize = sizeof(sa4); memset(&sa4, 0, sizeof(sa4)); sa4.sin_family = AF_INET; sa4.sin_addr.s_addr = htonl(address.toIPv4Address()); } else { sa = (sockaddr *)&sa6; saSize = sizeof(sa6); memset(&sa6, 0, sizeof(sa6)); sa6.sin6_family = AF_INET6; memcpy(sa6.sin6_addr.s6_addr, address.toIPv6Address().c, sizeof(sa6.sin6_addr.s6_addr)); } char hbuf[NI_MAXHOST]; if (sa && getnameinfo(sa, saSize, hbuf, sizeof(hbuf), 0, 0, 0) == 0) results.setHostName(QString::fromLatin1(hbuf)); if (results.hostName().isEmpty()) results.setHostName(address.toString()); results.setAddresses(QList<QHostAddress>() << address); return results; } // IDN support QByteArray aceHostname = QUrl::toAce(hostName); results.setHostName(hostName); if (aceHostname.isEmpty()) { results.setError(QHostInfo::HostNotFound); results.setErrorString(hostName.isEmpty() ? QCoreApplication::translate("QHostInfoAgent", "No host name given") : QCoreApplication::translate("QHostInfoAgent", "Invalid hostname")); return results; } // Call getaddrinfo, and place all IPv4 addresses at the start and // the IPv6 addresses at the end of the address list in results. addrinfo *res = 0; struct addrinfo hints; memset(&hints, 0, sizeof(hints)); hints.ai_family = PF_UNSPEC; #ifdef Q_ADDRCONFIG hints.ai_flags = Q_ADDRCONFIG; #endif int result = getaddrinfo(aceHostname.constData(), 0, &hints, &res); # ifdef Q_ADDRCONFIG if (result == EAI_BADFLAGS) { // if the lookup failed with AI_ADDRCONFIG set, try again without it hints.ai_flags = 0; result = getaddrinfo(aceHostname.constData(), 0, &hints, &res); } # endif if (result == 0) { addrinfo *node = res; QList<QHostAddress> addresses; while (node) { #ifdef QHOSTINFO_DEBUG qDebug() << "getaddrinfo node: flags:" << node->ai_flags << "family:" << node->ai_family << "ai_socktype:" << node->ai_socktype << "ai_protocol:" << node->ai_protocol << "ai_addrlen:" << node->ai_addrlen; #endif if (node->ai_family == AF_INET) { QHostAddress addr; addr.setAddress(ntohl(((sockaddr_in *) node->ai_addr)->sin_addr.s_addr)); if (!addresses.contains(addr)) addresses.append(addr); } else if (node->ai_family == AF_INET6) { QHostAddress addr; sockaddr_in6 *sa6 = (sockaddr_in6 *) node->ai_addr; addr.setAddress(sa6->sin6_addr.s6_addr); if (sa6->sin6_scope_id) addr.setScopeId(QString::number(sa6->sin6_scope_id)); if (!addresses.contains(addr)) addresses.append(addr); } node = node->ai_next; } if (addresses.isEmpty() && node == 0) { // Reached the end of the list, but no addresses were found; this // means the list contains one or more unknown address types. results.setError(QHostInfo::UnknownError); results.setErrorString(tr("Unknown address type")); } results.setAddresses(addresses); freeaddrinfo(res); } else if (result == EAI_NONAME || result == EAI_FAIL #ifdef EAI_NODATA // EAI_NODATA is deprecated in RFC 3493 || result == EAI_NODATA #endif ) { results.setError(QHostInfo::HostNotFound); results.setErrorString(tr("Host not found")); } else { results.setError(QHostInfo::UnknownError); results.setErrorString(QString::fromLocal8Bit(gai_strerror(result))); } #if defined(QHOSTINFO_DEBUG) if (results.error() != QHostInfo::NoError) { qDebug("QHostInfoAgent::fromName(): error #%d %s", h_errno, results.errorString().toLatin1().constData()); } else { QString tmp; QList<QHostAddress> addresses = results.addresses(); for (int i = 0; i < addresses.count(); ++i) { if (i != 0) tmp += ", "; tmp += addresses.at(i).toString(); } qDebug("QHostInfoAgent::fromName(): found %i entries for \"%s\": {%s}", addresses.count(), hostName.toLatin1().constData(), tmp.toLatin1().constData()); } #endif return results; }
void UPnpNotifyTask::SendNotifyMsg( MSocketDevice *pSocket, QString sNT, QString sUDN ) { QString sUSN; if ( sUDN.length() > 0) sUSN = sUDN + "::" + sNT; else sUSN = sNT; QString sData = QString ( "Server: %1\r\n" "NTS: %3\r\n" "NT: %4\r\n" "USN: %5\r\n" "CACHE-CONTROL: max-age=%6\r\n" "Content-Length: 0\r\n\r\n" ) .arg( HttpServer::GetServerVersion() ) .arg( GetNTSString() ) .arg( sNT ) .arg( sUSN ) .arg( m_nMaxAge ); LOG(VB_UPNP, LOG_INFO, QString("UPnpNotifyTask::SendNotifyMsg : %1:%2 : %3 : %4") .arg(pSocket->address().toString()) .arg(pSocket->port()) .arg(sNT) .arg(sUSN)); QMutexLocker qml(&m_mutex); // for addressList // ------------------------------------------------------------------- // Refresh IP Address List in case of changes // ------------------------------------------------------------------- QList<QHostAddress> addressList = UPnp::g_IPAddrList; for ( QList<QHostAddress>::Iterator it = addressList.begin(); it != addressList.end(); ++it ) { if ((*it).toString().isEmpty()) { LOG(VB_GENERAL, LOG_ERR, "UPnpNotifyTask::SendNotifyMsg - NULL in address list"); continue; } QHostAddress ip = *it; // Descope the Link Local address. The scope is only valid // on the server sending the announcement, not the clients // that receive it ip.setScopeId(QString()); QString ipaddress = ip.toString(); // If this looks like an IPv6 address, then enclose it in []'s if (ipaddress.contains(":")) ipaddress = "[" + ipaddress + "]"; QString sHeader = QString("NOTIFY * HTTP/1.1\r\n" "HOST: %1:%2\r\n" "LOCATION: http://%3:%4/getDeviceDesc\r\n") .arg(pSocket->address().toString()) .arg(pSocket->port()) .arg(ipaddress) .arg(m_nServicePort); QString sPacket = sHeader + sData; QByteArray scPacket = sPacket.toUtf8(); // --------------------------------------------------------------- // Send Packet to Socket (Send same packet twice) // --------------------------------------------------------------- pSocket->writeBlock( scPacket, scPacket.length(), pSocket->address(), pSocket->port() ); std::this_thread::sleep_for(std::chrono::milliseconds(random() % 250)); pSocket->writeBlock( scPacket, scPacket.length(), pSocket->address(), pSocket->port() ); } }
void UPnpSearchTask::SendMsg( MSocketDevice *pSocket, QString sST, QString sUDN ) { QString sUSN; if (( sUDN.length() > 0) && ( sUDN != sST )) sUSN = sUDN + "::" + sST; else sUSN = sST; QString sDate = MythDate::current().toString( "d MMM yyyy hh:mm:ss" ); QString sData = QString ( "CACHE-CONTROL: max-age=%1\r\n" "DATE: %2\r\n" "EXT:\r\n" "Server: %3\r\n" "ST: %4\r\n" "USN: %5\r\n" "Content-Length: 0\r\n\r\n" ) .arg( m_nMaxAge ) .arg( sDate ) .arg( HttpServer::GetServerVersion() ) .arg( sST ) .arg( sUSN ); #if 0 LOG(VB_UPNP, LOG_DEBUG, QString("UPnpSearchTask::SendMsg : %1 : %2 ") .arg(sST) .arg(sUSN)); LOG(VB_UPNP, LOG_DEBUG, QString("UPnpSearchTask::SendMsg m_PeerAddress = %1 Port=%2") .arg(m_PeerAddress.toString()) .arg(m_nPeerPort)); #endif for ( QList<QHostAddress>::Iterator it = m_addressList.begin(); it != m_addressList.end(); ++it ) { QString ipaddress; // Avoid announcing the localhost address if (*it == QHostAddress::LocalHost || *it == QHostAddress::LocalHostIPv6 || *it == QHostAddress::AnyIPv4 || *it == QHostAddress::AnyIPv6) continue; QHostAddress ip = *it; // Descope the Link Local address. The scope is only valid // on the server sending the announcement, not the clients // that receive it ip.setScopeId(QString()); // If this looks like an IPv6 address, then enclose it in []'s if (ip.protocol() == QAbstractSocket::IPv6Protocol) ipaddress = "[" + ip.toString() + "]"; else ipaddress = ip.toString(); QString sHeader = QString ( "HTTP/1.1 200 OK\r\n" "LOCATION: http://%1:%2/getDeviceDesc\r\n" ) .arg( ipaddress ) .arg( m_nServicePort); QString sPacket = sHeader + sData; QByteArray scPacket = sPacket.toUtf8(); // ------------------------------------------------------------------ // Send Packet to UDP Socket (Send same packet twice) // ------------------------------------------------------------------ pSocket->writeBlock( scPacket, scPacket.length(), m_PeerAddress, m_nPeerPort ); std::this_thread::sleep_for( std::chrono::milliseconds( random() % 250 )); pSocket->writeBlock( scPacket, scPacket.length(), m_PeerAddress, m_nPeerPort ); } }