void ListenSocket::updateInfo()
{
  Socket::updateInfo();

  _ip = ipAddr(_addr);
  _port = portNumber(_addr);
}
void NetworkConnectorEngine::sendBroadCast() {
    if ( iTimeOfLastBroadCast + 60 < QDateTime::currentDateTimeUtc().toTime_t()) {
        QByteArray dataToSend ;
        dataToSend.append('c') ;
        dataToSend.append('a') ;
        bool hasIpv6 =
            !Connection::Ipv6AddressesEqual(iController->getNode().ipv6Addr(),
                                            KNullIpv6Addr) ;
        if ( hasIpv6 ) {
            dataToSend.append('6') ;
        } else {
            dataToSend.append('4') ;
        }

        quint32 nodeHash1NetworkBO ( htonl ( iController->getNode().nodeFingerPrint().iHash160bits[0]) ) ;
        quint32 nodeHash2NetworkBO ( htonl ( iController->getNode().nodeFingerPrint().iHash160bits[1]) ) ;
        quint32 nodeHash3NetworkBO ( htonl ( iController->getNode().nodeFingerPrint().iHash160bits[2]) ) ;
        quint32 nodeHash4NetworkBO ( htonl ( iController->getNode().nodeFingerPrint().iHash160bits[3]) ) ;
        quint32 nodeHash5NetworkBO ( htonl ( iController->getNode().nodeFingerPrint().iHash160bits[4]) ) ;

        dataToSend.append((const char *)(&nodeHash1NetworkBO), sizeof(quint32) ) ;
        dataToSend.append((const char *)(&nodeHash2NetworkBO), sizeof(quint32) ) ;
        dataToSend.append((const char *)(&nodeHash3NetworkBO), sizeof(quint32) ) ;
        dataToSend.append((const char *)(&nodeHash4NetworkBO), sizeof(quint32) ) ;
        dataToSend.append((const char *)(&nodeHash5NetworkBO), sizeof(quint32) ) ;

        quint32 portNumber  ( iController->getNode().port() ) ;
        if ( portNumber > 0 ) {
            quint32 portNumberNetworkBO ( htonl ( portNumber ) ) ;
            dataToSend.append((const char *)(&portNumberNetworkBO), sizeof(quint32) ) ;

            if ( hasIpv6 ) {
                // include also our ipv6-addr in broadcase as in ipv6
                // case the sender of the UDP packet at the receiving
                // end is our link-local addr FE:80:.. and it may be
                // difficult to connect as network interface must be
                // specified too .. so , include our advertised addr:
                for ( int i = 0 ; i < 16 ; i++ ) {
                    dataToSend.append(iController->getNode().ipv6Addr().c[i]) ;
                }
            }
            iTimeOfLastBroadCast = QDateTime::currentDateTimeUtc().toTime_t() ;
            // hmm, send broadcast only over ipv4. there seems to be
            // some trouble in binding qudpsocket into ipv6 interface
            // in the receiving end..
            iBroadCastSocket->writeDatagram(dataToSend.data(), dataToSend.size(),
                                            QHostAddress::Broadcast, KBroadCastAdvertismentPort);

        }
    }
}
示例#3
0
void m_dccchat::dccConnect(std::string server, std::string nick, std::string ip, std::string port) {
	Socket* dccSocket = new Socket();
	std::istringstream portNumber (port);
	unsigned short dccPort;
	portNumber >> dccPort;
	dccSocket->connectServer(ip, dccPort);
	activeConnections.insert(std::pair<std::string, Socket*> (server + "/" + nick, dccSocket));
	reportingModules.insert(std::pair<std::string, std::vector<std::string> > (server + "/" + nick, std::vector<std::string> ()));
	dccListenArg listenData;
	listenData.modPtr = this;
	listenData.id = server + "/" + nick;
	listenData.sockPtr = dccSocket;
	pthread_t newThread;
	threads.push_back(&newThread);
	void* listenArgs = (void*) &listenData;
	pthread_create(&newThread, NULL, &dccListen_thread, listenArgs);
	sleep(1); // make the arguments passed to the thread not die before the thread function can read them.
}
Network::Identity ListenSocket::recvFrom(Network::Buffer& data)
{
  if (_socktype != ASocket::SockType::UDP)
    throw std::runtime_error("recvFrom not implemented for non UDP socket.");

  int 						ret;
  struct sockaddr_storage	raddr;
  socklen_t				    raddrlen = sizeof(decltype(raddr));
  std::unique_ptr<char[]> 	buff(new char[_recvFromSize]);

  std::memset(&raddr, 0, raddrlen);
  ret = recvfrom(_socket, buff.get(), _recvFromSize, 0,
                 reinterpret_cast<struct sockaddr*>(&raddr), &raddrlen);
  if (ret == -1)
    throw Error(strerror(errno));
  data.resize(ret);
  data.assign(buff.get(), ret);
  return Network::Identity(ipAddr(raddr), portNumber(raddr));
}
示例#5
0
const char* RpcStringBinding::formatted (bool includePortNumber) const
    {
    char*& p = includePortNumber ? m_strFormat : m_strFormatNoPortNumber;

    if (p == 0 && m_ipAddress != 0)
	{
	char binding [MAXHOSTNAMELEN]; // XXX

	int n = 0;

	n = ::sprintf (binding, "%c%s", protocolSequence (), ipAddress ());

	if (n && includePortNumber)
	    ::sprintf (&binding[n], "[%d]", portNumber ());

	p = copyString (binding);
	}

    return p;
    }
示例#6
0
QString Options::newPortNumber()
{
   portNumber_.clear();
   return portNumber();
}