示例#1
0
	bool Database::_GetChatroom(Statement &s, Chatroom &Out)
	{
		Nullable<std::string> Name;
		Nullable<std::string> OwnerUsername;
		Nullable<std::string> Password;
		Nullable<std::string> Description;
		Nullable<int> ServerFamily;
		Nullable<std::string> ServerIP;
		Nullable<int> ServerPort;

		if (!s.GetString("Name", Name) || !s.GetString("OwnerUsername", OwnerUsername) || !s.GetString("Password", Password) ||
				!s.GetString("Description", Description) || !s.GetInt("ServerFamily", ServerFamily) ||
				!s.GetString("ServerIP", ServerIP) || !s.GetInt("ServerPort", ServerPort))
			return false;

		if (Name.Null || OwnerUsername.Null || ServerIP.Null || ServerFamily.Null || ServerPort.Null)
			return false;

		Net::Address Address;
		Address.Load(ServerFamily.Value, ServerIP.Value, ServerPort.Value);
		Out.Name = Name.Value;
		Out.OwnerUsername = OwnerUsername.Value;
		Out.ServerAddress = Address;
		Out.Password = Password;
		Out.Description = Description;

		return true;
	}
示例#2
0
    bool IPBlockList::blocked(const net::Address& addr) const
    {
        if (addr.protocol() == QAbstractSocket::IPv6Protocol || blocks.empty())
            return false;

        // Binary search the list of blocks which are sorted
        quint32 ip = addr.toIPv4Address();
        int begin = 0;
        int end = blocks.size() - 1;
        while (true)
        {
            if (begin == end)
                return blocks[begin].constains(ip);
            else if (begin == end - 1)
                return blocks[begin].constains(ip) || blocks[end].constains(ip);

            int pivot = begin + (end - begin) / 2;
            if (blocks[pivot].constains(ip))
                return true;
            else if (ip < blocks[pivot].ip1)
                end = pivot - 1; // continue in the range [begin, pivot - 1]
            else // ip > blocks[pivot].ip2
                begin = pivot + 1; // continue in the range [pivot + 1, end]
        }
        return false;
    }
示例#3
0
void TCPClient::sendData(const char* data, std::size_t size, const net::Address& peerAddress) 
{
	TraceL << "Send data to " << peerAddress << endl;

	// Ensure permissions exist for the peer.
	if (!hasPermission(peerAddress.host()))	
		throw std::runtime_error("No permission exists for peer: " + peerAddress.host());	

	auto conn = connections().get(peerAddress, nullptr);
	if (!conn)	
		throw std::runtime_error("No peer exists for: " + peerAddress.toString());
	
	conn->send(data, size);
}
示例#4
0
	void HttpServer::newConnection(int fd, const net::Address& addr)
	{
		HttpClientHandler* handler = new HttpClientHandler(this,fd);
		connect(handler,SIGNAL(closed()),this,SLOT(slotConnectionClosed()));
		Out(SYS_WEB|LOG_NOTICE) << "connection from "<< addr.toString()  << endl;
		clients.append(handler);
	}
示例#5
0
void UDPAllocation::onPeerDataReceived(void*, const MutableBuffer& buffer, const net::Address& peerAddress)
{	
	//auto source = reinterpret_cast<net::PacketInfo*>(packet.info);
	TraceL << "Received UDP Datagram from " << peerAddress << endl;	
	
	if (!hasPermission(peerAddress.host())) {
		TraceL << "No Permission: " << peerAddress.host() << endl;	
		return;
	}

	updateUsage(buffer.size());
	
	// Check that we have not exceeded out lifetime and bandwidth quota.
	if (IAllocation::deleted())
		return;
	
	stun::Message message(stun::Message::Indication, stun::Message::DataIndication);
		
	// Try to use the externalIP value for the XorPeerAddress 
	// attribute to overcome proxy and NAT issues.
	std::string peerHost(server().options().externalIP);
	if (peerHost.empty()) {
		peerHost.assign(peerAddress.host());
		assert(0 && "external IP not set");
	}
	
	auto peerAttr = new stun::XorPeerAddress;
	peerAttr->setAddress(net::Address(peerHost, peerAddress.port()));
	message.add(peerAttr);

	auto dataAttr = new stun::Data;
	dataAttr->copyBytes(bufferCast<const char*>(buffer), buffer.size());
	message.add(dataAttr);
	
	//Mutex::ScopedLock lock(_mutex);

	TraceL << "Send data indication:" 
		<< "\n\tFrom: " << peerAddress
		<< "\n\tTo: " << _tuple.remote()
		//<< "\n\tData: " << std::string(packet.data(), packet.size())
		<< endl;
		
	server().udpSocket().sendPacket(message, _tuple.remote());
	
	//net::Address tempAddress("58.7.41.244", _tuple.remote().port());
	//server().udpSocket().send(message, tempAddress);
}
示例#6
0
void TCPSocket::bind(const net::Address& address, unsigned flags) 
{
    TraceLS(this) << "Binding on " << address << endl;
    init();
    int r;
    switch (address.af()) {
    case AF_INET:
        r = uv_tcp_bind(ptr<uv_tcp_t>(), address.addr(), flags);
        break;
    //case AF_INET6:
    //    r = uv_tcp_bind6(ptr<uv_tcp_t>(), *reinterpret_cast<const sockaddr_in6*>(address.addr()));
    //    break;
    default:
        throw std::runtime_error("Unexpected address family");
    }
    if (r) setAndThrowError("TCP bind failed", r);
}
示例#7
0
void TCPSocket::connect(const net::Address& peerAddress) 
{
    TraceLS(this) << "Connecting to " << peerAddress << endl;
    init();
    auto req = new uv_connect_t;
    req->data = this;
    int r = uv_tcp_connect(req, ptr<uv_tcp_t>(), peerAddress.addr(), internal::onConnect);
    if (r) setAndThrowError("TCP connect failed", r);
}
示例#8
0
	bool Database::_GetServer(Statement &s, Server &Out)
	{
		Nullable<std::string> Name;
		Nullable<int> Family;
		Nullable<std::string> IP;
		Nullable<int> Port;

		if (!s.GetString("Name", Name) || !s.GetInt("Family", Family) || !s.GetString("IP", IP) || !s.GetInt("Port", Port))
			return false;

		if (Family.Null || IP.Null || Port.Null || Name.Null)
			return false;

		Net::Address Address;
		Address.Load(Family.Value, IP.Value, Port.Value);
		Out.Address = Address;
		Out.Name = Name.Value;

		return true;
	}
示例#9
0
		virtual void dataReceived(bt::Buffer::Ptr ptr, const net::Address& addr)
		{
			try
			{
				// read and decode the packet
				BDecoder bdec(ptr->get(), ptr->size(), false);
				boost::scoped_ptr<BNode> n(bdec.decode());

				if (!n || n->getType() != BNode::DICT)
					return;

				// try to make a RPCMsg of it
				RPCMsg::Ptr msg = factory.build((BDictNode*)n.get(), this);
				if (msg)
				{
					if (addr.ipVersion() == 6 && addr.isIPv4Mapped())
						msg->setOrigin(addr.convertIPv4Mapped());
					else
						msg->setOrigin(addr);
					
					msg->apply(dh_table);
					// erase an existing call
					if (msg->getType() == RSP_MSG && calls.contains(msg->getMTID()))
					{
						// delete the call, but first notify it off the response
						RPCCall* c = calls.find(msg->getMTID());
						c->response(msg);
						calls.erase(msg->getMTID());
						c->deleteLater();
						doQueuedCalls();
					}
				}
			}
			catch (bt::Error & err)
			{
				Out(SYS_DHT | LOG_DEBUG) << "Error happened during parsing : " << err.toString() << endl;
			}
		}
bool ClientHasAlreadyConnected( net::Address & clientAddress )
{
    bool clientHasAlreadyConnected = false;

    map<net::Address, net::ServerSocket*>::iterator itr = serverConnections.find( clientAddress );

    if( itr != serverConnections.end() )
    {
        clientHasAlreadyConnected = true;
        printf( "The client %s has already connected\n", clientAddress.ToString().c_str() );
    }

    return clientHasAlreadyConnected;
}
示例#11
0
bool GUIHandler::sendPacket(const ssl_visualizer_packet &p)
{
    ParameterManager* pm = ParameterManager::getInstance();
    string buffer;    
    p.SerializeToString(&buffer);
    Net::Address multiaddr;
    multiaddr.setHost(pm->get<string>("network.VISUALIZER_ADDRESS").c_str(),
                      pm->get<int>("network.VISUALIZER_PORT"));
    bool result;
    mtx_.lock();
    result = this->send(buffer.c_str(), buffer.length(), multiaddr);

    mtx_.unlock();
    if (result==false)
    {
        cerr << "Sending Visualizer data failed (maybe too large?). Size was: " << buffer.length() << endl;
    }
    else
    {
//        cout << buffer.length() << " Bytes of ( Visualizer Packet ) sent." << endl;
    }
    return(result);
}
示例#12
0
	void runUDPSocketTest() 
	{
		// Notes: Sending over home wireless network via
		// ADSL to US server round trip stays around 200ms
		// when sending 1450kb packets at 50ms intervals.
		// At 40ms send intervals latency increated to around 400ms.

		TraceL << "UDP Socket Test: Starting" << endl;
		
		//UDPPacketSize = 10000;
		UDPPacketSize = 1450;
		UDPNumPacketsWanted = 100;
		UDPNumPacketsReceived = 0;
		
		//serverBindAddr.swap(net::Address("0.0.0.0", 1337));	 //
		udpServerAddr.swap(net::Address("74.207.248.97", 1337));	 //
		//udpServerAddr.swap(net::Address("127.0.0.1", 1337));	 //

		//clientBindAddr.swap(net::Address("0.0.0.0", 1338));	
		//clientSendAddr.swap(net::Address("58.7.41.244", 1337));	 //
		//clientSendAddr.swap(net::Address("127.0.0.1", 1337));	 //

		//net::UDPSocket serverSock;
		//serverSock.Recv += sdelegate(this, &Tests::onUDPSocketServerRecv);
		//serverSock.bind(serverBindAddr);
		//this->serverSock = &serverSock;
		
		net::UDPSocket clientSock;
		//clientSock.Recv += sdelegate(this, &Tests::onUDPClientSocketRecv);		
		assert(0 && "fixme");
		clientSock.bind(net::Address("0.0.0.0", 0));	
		clientSock.connect(udpServerAddr);	
		this->udpClientSock = &clientSock;

		//for (unsigned i = 0; i < UDPNumPacketsWanted; i++)
		//	clientSock.send("bounce", 6, serverBindAddr);		

		// Start the send timer
		Timer timer;
		timer.Timeout += sdelegate(this, &Tests::onUDPClientSendTimer);
		timer.start(50, 50);
		timer.handle().ref();
			
		runLoop();
		
		//this->serverSock = nullptr;
		this->udpClientSock = nullptr;
	}
示例#13
0
	bool UTPServer::Private::bind(const net::Address& addr)
	{
		net::ServerSocket::Ptr sock(new net::ServerSocket(this));
		if (!sock->bind(addr))
		{
			return false;
		}
		else
		{
			Out(SYS_UTP | LOG_NOTICE) << "UTP: bound to " << addr.toString() << endl;
			sock->setTOS(tos);
			sock->setReadNotificationsEnabled(false);
			sock->setWriteNotificationsEnabled(false);
			sockets.append(sock);
			return true;
		}
	}
示例#14
0
	bool Database::GetServer(const Net::Address Address, Server &Out)
	{
		const std::string Query = "SELECT * FROM Server WHERE Family = :Family AND IP = :IP AND Port = :Port";

		Statement s;
		if (!s.Prepare(Inner, Query))
			return false;

		if (!s.Bind(":Family", Address.Family) || !s.Bind(":IP", Address.GetPrintableIP()) || !s.Bind(":Port", Address.Port))
			return false;

		if (!s.Step())
			return false;

		if (!_GetServer(s, Out))
			return false;

		return true;
	}