Exemplo n.º 1
0
void STUNClient::sendTestRequest(
  boost::asio::ip::udp::socket& sock,
  OSS::IPAddress& dest,
  int testNum)
{
  //std::cout << "Sending TEST " << testNum << " " << sock.local_endpoint().address().to_string()
  //  << "->" << dest.toIpPortString() << std::endl;
  StunAtrString username;
  StunAtrString password;
  username.sizeValue = 0;
  password.sizeValue = 0;

  bool changePort=false;
  bool changeIP=false;

  switch (testNum)
  {
    case 1:
    case 10:
    case 11:
       break;
    case 2:
       //changePort=true;
       changeIP=true;
       break;
    case 3:
       changePort=true;
       break;
    case 4:
       changeIP=true;
       break;
    case 5:
       break;
    default:
       OSS_ASSERT(false);
  }

  StunMessage req;
  memset(&req, 0, sizeof(StunMessage));

  stunBuildReqSimple(&req, username, changePort , changeIP , testNum);

  char buf[STUN_MAX_MESSAGE_SIZE];
  int len = STUN_MAX_MESSAGE_SIZE;

  len = stunEncodeMessage(req, buf, len, password, false);

  sock.async_send_to(boost::asio::buffer(buf, len),
    boost::asio::ip::udp::endpoint(dest.address(), dest.getPort()),
    boost::bind(&STUNClient::handleWrite, shared_from_this(),
                      boost::asio::placeholders::error));

  sock.async_receive_from(boost::asio::buffer(_buffer), _senderEndPoint,
      boost::bind(&STUNClient::handleReadNatType, shared_from_this(),
        boost::asio::placeholders::error,
          boost::asio::placeholders::bytes_transferred));

  _semReadEvent.tryWait(10);
}
Exemplo n.º 2
0
bool STUNClient::createSingleSocket(
  const std::string& stunServer,
  boost::asio::ip::udp::socket& socket,
  const OSS::IPAddress& lAddr,
  OSS::IPAddress& externalAddress)
{
  OSS::mutex_critic_sec_lock globalLock(_csGlobal);

  _test1Responded = false;
  _test2Responded = false;
  _test3Responded = false;
  _test10Responded = false;
  _test1ChangedAddr = OSS::IPAddress();
  _test1MappedAddr = OSS::IPAddress();
  _test10MappedAddr = OSS::IPAddress();
  _sendCount = 0;

  OSS::IPAddress localAddress = lAddr;
  OSS::IPAddress targetAddress = OSS::IPAddress::fromV4IPPort(stunServer.c_str());
  if (!targetAddress.isValid() || !localAddress.isValid())
    return false;

  if (!targetAddress.getPort())
    targetAddress.setPort(STUN_PORT);

  if (!socket.is_open())
  {
    socket.open(boost::asio::ip::udp::v4());
    boost::asio::ip::udp::endpoint ep;

    ep = boost::asio::ip::udp::endpoint(localAddress.address(), localAddress.getPort());
    boost::system::error_code ec;
    socket.bind(ep, ec);
    if (ec)
      return false;
  }

  while (_sendCount < 15)
  {
    _sendCount++;
    if (!_test1Responded)
      sendTestRequest(socket,  targetAddress, 1);
    else
      break;
  }

  if (_test1Responded)
  {
    externalAddress = _test1MappedAddr;
    return externalAddress.isValid();
  }

  return false;
}
Exemplo n.º 3
0
 interfaces() {
     memset(data,0,MAX_LINKS*sizeof(struct info));
     memset(buf,0,MAX_BUF);
     lsock.open(boost::asio::ip::udp::v4());
     lsock.bind(boost::asio::ip::udp::endpoint(boost::asio::ip::address::from_string("127.0.0.1"),10000));
     lsock.async_receive(
             boost::asio::buffer(buf,MAX_BUF),
             boost::bind(&interfaces::handle_receive, this,
                 boost::asio::placeholders::error,
                 boost::asio::placeholders::bytes_transferred));
     boost::thread t = boost::thread (boost::bind(&boost::asio::io_service::run, &iol));
 }
Exemplo n.º 4
0
void session::echo(std::vector<uint8_t> const& request)
{
	auto reply = request;

	socket_.send_to(boost::asio::buffer(request), endpoint_);

	std::size_t reply_length = socket_.receive_from(boost::asio::buffer(reply), endpoint_);

	if (reply_length != request.size())
		throw std::logic_error("Different size of reply and request");
	if (reply != request)
		throw std::logic_error("Different data value of request and reply");
}
Exemplo n.º 5
0
 void receive_next() {
     m_multicast_socket.async_receive_from
         (boost::asio::buffer(m_frame_data, sizeof(m_frame_data)), m_frame_sender,
          boost::bind(&garmin_radar::handle_frame, this,
                      boost::asio::placeholders::error,
                      boost::asio::placeholders::bytes_transferred));
 }
Exemplo n.º 6
0
void Detection::TimeOutHandler(int packet_type, const boost::system::error_code& error, boost::asio::ip::udp::socket& socket, int tracker_index)
{
    if(!error)//超时
    {
        std::ofstream fout("text.txt", std::ofstream::ate|std::ofstream::app);
        if (packet_type == 0 && !has_tracker_list)  // bs
        {
            fout << "bs回包超时" << std::endl;
            fout.close();
            m_progress_pos_ += 3;
        }
        else if (packet_type == 1)  // tracker
        {
            std::string result = "";
            result.append("tracker(");
            result.append(m_tracker_infos[tracker_index].ip);
            result.append(":");
            boost::uint16_t port = m_tracker_infos[tracker_index].port;
            char tmp[10];
            itoa(m_tracker_infos[tracker_index].port, tmp, 10);
            result.append(tmp);
            result.append( ")回包超时");
            result_trackers.push_back(result);
            //fout.close();
            m_progress_pos_ += 1;
        }
        socket.close();
    }
}
Exemplo n.º 7
0
 void send_control_uchar(unsigned int type, unsigned char data) {
     frame_control_uchar frame;
     frame.frame_type = type;
     frame.len1 = 1;
     frame.data = data;
     m_control_socket.send(boost::asio::buffer(&frame, sizeof(frame)));
 }
Exemplo n.º 8
0
 void send_control_ushort(unsigned int type, unsigned short data) {
     frame_control_ushort frame;
     frame.frame_type = type;
     frame.len1 = 2;
     frame.data = data;
     m_control_socket.send(boost::asio::buffer(&frame, sizeof(frame)));
 }
Exemplo n.º 9
0
 void send_request(){
     socket.async_send_to(boost::asio::buffer(send_arr, 5), receiver_endpoint,
         boost::bind(&sAudioReceiver::handle_send, this,
             boost::asio::placeholders::error,
             boost::asio::placeholders::bytes_transferred
         )
     );
 }
Exemplo n.º 10
0
    sAudioReceiver(boost::asio::io_service& io_service, sAudioBuffer* audiobuf, char* host, int port) : audiobuf(audiobuf), socket(io_service), ping_interval(boost::posix_time::seconds(1)) {
        printf("Creating sAudioReceiver on %s:%d\n", host, port);
        socket.open(boost::asio::ip::udp::v4());

        receiver_endpoint = boost::asio::ip::udp::endpoint(boost::asio::ip::address::from_string(host), port);


        std::string thx("ihazo");
        std::copy(thx.begin(), thx.end(), send_arr.begin());
        printf("Created sAudioReceiver\n");
        send_request();
        start_receive();
        printf("Sent request\n");

        ping_timer = new boost::asio::deadline_timer(socket.get_io_service(), ping_interval);
        start_timer();
    }
Exemplo n.º 11
0
Arquivo: main.cpp Projeto: CCJY/coliru
void Client::send(Peer peer) {
    boost::asio::streambuf request;
    std::ostream os(&request);
    os << "Client";
    boost::system::error_code error;
    auto bytes_transferred = socket_.send_to(request.data(), peer, 0, error);
    std::cout << "Client sent: " << bytes_transferred << " bytes"
                 " with " << error.message() << std::endl;      
}
Exemplo n.º 12
0
        void handle_receive(const boost::system::error_code& error, size_t received_bytes) {
            this->sendto(buf,received_bytes);
            lsock.async_receive(
                    boost::asio::buffer(buf,MAX_BUF),
                    boost::bind(&interfaces::handle_receive, this,
                        boost::asio::placeholders::error,
                        boost::asio::placeholders::bytes_transferred));

        }
Exemplo n.º 13
0
void DiscoveryServer::HandleReceive(const boost::system::error_code& error) {
    if (!error &&
        std::string(m_recv_buffer.begin(), m_recv_buffer.end()) == DISCOVERY_QUESTION) {
        m_socket.send_to(
            boost::asio::buffer(DISCOVERY_ANSWER + boost::asio::ip::host_name()),
            m_remote_endpoint);
    }
    Listen();
}
Exemplo n.º 14
0
    blocking_t(const std::string& host, std::uint16_t port) :
        io_service(),
        socket(io_service)
    {
        boost::asio::ip::udp::resolver resolver(io_service);
        boost::asio::ip::udp::resolver::query query(host, boost::lexical_cast<std::string>(port),
            boost::asio::ip::udp::resolver::query::flags::numeric_service);
        endpoint = *resolver.resolve(query);

        socket.open(endpoint.protocol());
    }
Exemplo n.º 15
0
 void start_receive(){
     socket.async_receive_from(
         boost::asio::buffer(recv_arr), receiver_endpoint,
         boost::bind(
             &sAudioReceiver::handle_receive,
             this,
             boost::asio::placeholders::error,
             boost::asio::placeholders::bytes_transferred
         )
     );
 }
Exemplo n.º 16
0
	SC_UdpInPort(struct World * world, int inPortNum):
		mWorld(world), mPortNum(inPortNum), udpSocket(ioService)
	{
		using namespace boost::asio;
		BOOST_AUTO(protocol, ip::udp::v4());
		udpSocket.open(protocol);

		udpSocket.bind(ip::udp::endpoint(protocol, inPortNum));

		boost::asio::socket_base::send_buffer_size option(65536);
		udpSocket.set_option(option);

#ifdef USE_RENDEZVOUS
		if (world->mRendezvous) {
			thread thread( boost::bind( PublishPortToRendezvous, kSCRendezvous_UDP, sc_htons(mPortNum) ) );
			mRendezvousThread = std::move(thread);
		}
#endif

		startReceiveUDP();
	}
Exemplo n.º 17
0
  receiver(boost::asio::io_service& io_service,
      const boost::asio::ip::address& listen_address,
      const boost::asio::ip::address& multicast_address)
    : socket_(io_service)
  {
    // Create the socket so that multiple may be bound to the same address.
    boost::asio::ip::udp::endpoint listen_endpoint(
        listen_address, multicast_port);
    socket_.open(listen_endpoint.protocol());
    socket_.set_option(boost::asio::ip::udp::socket::reuse_address(true));
    socket_.bind(listen_endpoint);

    // Join the multicast group.
    socket_.set_option(
        boost::asio::ip::multicast::join_group(multicast_address));

    socket_.async_receive_from(
        boost::asio::buffer(data_, max_length), sender_endpoint_,
        boost::bind(&receiver::handle_receive_from, this,
            boost::asio::placeholders::error,
            boost::asio::placeholders::bytes_transferred));
  }
Exemplo n.º 18
0
Arquivo: main.cpp Projeto: CCJY/coliru
void Server::receive() {
    Peer peer;
    boost::system::error_code error;
    auto bytes_transferred =
      socket_.receive_from(boost::asio::buffer(recv_buffer_), peer, 0, error);
    std::cout << "Server received " << bytes_transferred << " bytes"
                 " from " << peer
              << " with " << error.message() << std::endl;
    if (!error) {
      std::cout << "  >> ";
      std::cout.write(recv_buffer_.data(), bytes_transferred);
      std::cout << std::endl;
    }
    receive();
}
Exemplo n.º 19
0
  void handle_receive_from(
      const boost::system::error_code& error,
      size_t bytes_recvd)
  {
    if (!error)
    {
      std::cout.write(data_, bytes_recvd);
      std::cout << std::endl;

      socket_.async_receive_from(
          boost::asio::buffer(data_, max_length), sender_endpoint_,
          boost::bind(&receiver::handle_receive_from, this,
              boost::asio::placeholders::error,
            boost::asio::placeholders::bytes_transferred));
    }
  }
Exemplo n.º 20
0
 bool SendPacket(const P &packet) {
   boost::system::error_code ec;
   return socket.send_to(boost::asio::buffer(&packet, sizeof(packet)),
                         endpoint, 0, ec) == sizeof(packet) && !ec;
 }
Exemplo n.º 21
0
inline bool
UdpChannel::isListening() const
{
  return m_socket.is_open();
}
Exemplo n.º 22
0
 void SendPacket(const P &packet) {
   const ScopeLock protect(mutex);
   socket.send_to(boost::asio::buffer(&packet, sizeof(packet)),
                  endpoint, 0);
 }
Exemplo n.º 23
0
 boost::asio::io_service &get_io_service() {
   return socket.get_io_service();
 }
Exemplo n.º 24
0
 bool IsDefined() const {
   const ScopeLock protect(mutex);
   return resolving || socket.is_open();
 }
Exemplo n.º 25
0
 Drone(const std::string& address, boost::asio::io_service& ios) : Pitch_(0.0), Roll_(0.0), Yaw_(0.0), H_(0.0), ios_(ios), 
     remote_(boost::asio::ip::address::from_string(address), 7777) ,
     socket_(ios_)
 {
     socket_.connect(remote_);
 }
Exemplo n.º 26
0
void DiscoveryServer::Listen() {
    m_socket.async_receive_from(
        boost::asio::buffer(m_recv_buffer), m_remote_endpoint,
        boost::bind(&DiscoveryServer::HandleReceive, this,
                    boost::asio::placeholders::error));
}
Exemplo n.º 27
0
 gcc_pure
 bool IsConnected() const {
   const ScopeLock protect(mutex);
   return socket.is_open();
 }
Exemplo n.º 28
0
std::string AutohostInterface::TryBindSocket(
			boost::asio::ip::udp::socket& socket,
			const std::string& remoteIP, int remotePort,
			const std::string& localIP, int localPort)
{
	std::string errorMsg = "";

	ip::address localAddr;
	ip::address remoteAddr;
	boost::system::error_code err;
	try {
		socket.open(ip::udp::v6(), err); // test IP v6 support
		const bool supportsIPv6 = !err;

		remoteAddr = netcode::WrapIP(remoteIP, &err);
		if (err) {
			throw std::runtime_error("Failed to parse address " + remoteIP + ": " + err.message());
		}

		if (!supportsIPv6 && remoteAddr.is_v6()) {
			throw std::runtime_error("IP v6 not supported, can not use address " + remoteAddr.to_string());
		}

		if (localIP.empty()) {
			// use the "any" address as local "from"
			if (remoteAddr.is_v6()) {
				localAddr = ip::address_v6::any();
			} else {
				if (supportsIPv6) {
					socket.close();
				}
				socket.open(ip::udp::v4());
				localAddr = ip::address_v4::any();
			}
		} else {
			localAddr = netcode::WrapIP(localIP, &err);
			if (err) {
				throw std::runtime_error("Failed to parse local IP " + localIP + ": " + err.message());
			}
			if (localAddr.is_v6() != remoteAddr.is_v6()) {
				throw std::runtime_error("Local IP " + localAddr.to_string() + " and remote IP " + remoteAddr.to_string() + " are IP v4/v6 mixed");
			}
		}

		socket.bind(ip::udp::endpoint(localAddr, localPort));

		boost::asio::socket_base::non_blocking_io command(true);
		socket.io_control(command);

		// A similar, slighly less verbose message is already in GameServer
		//LogObject() << "[AutohostInterface] Connecting (UDP) to IP "
		//		<<  (remoteAddr.is_v6() ? "(v6)" : "(v4)") << " " << remoteAddr
		//		<< " Port " << remotePort;
		socket.connect(ip::udp::endpoint(remoteAddr, remotePort));
	} catch (std::runtime_error& e) { // includes also boost::system::system_error, as it inherits from runtime_error
		socket.close();
		errorMsg = e.what();
		if (errorMsg.empty()) {
			errorMsg = "Unknown problem";
		}
	}

	return errorMsg;
}
Exemplo n.º 29
0
 void SendCmd()
 {
     socket_.send(boost::asio::buffer(boost::str(boost::format("s,%1%,%2%,%3%,%4%") % Pitch_ % Roll_ % Yaw_ % H_)));
 }
Exemplo n.º 30
0
 bool IsDefined() const {
   return socket.is_open();
 }