예제 #1
0
파일: net.cpp 프로젝트: JBouron/polybar
  /**
   * Construct network interface
   */
  network::network(string interface) : m_log(logger::make()), m_interface(move(interface)) {
    if (if_nametoindex(m_interface.c_str()) == 0) {
      throw network_error("Invalid network interface \"" + m_interface + "\"");
    }

    m_socketfd = file_util::make_file_descriptor(socket(AF_INET, SOCK_DGRAM, 0));
    if (!*m_socketfd) {
      throw network_error("Failed to open socket");
    }

    check_tuntap();
  }
예제 #2
0
// <-1: invalid id
// -1: invalid length
// 0: unknown length, buffer too short
// >0: actual length
int ProtocolDef::PacketLength(const unsigned char* const buf, const unsigned bufLength) const
{
	if (bufLength == 0)
		return 0;
	
	unsigned char msgid = buf[0];
	int len = msg[msgid].Length;

	if (len > 0)
		return len;
	if (len == 0)
		return -2;
	if (len == -1) {
		if (bufLength < 2)
			return 0;
		return (buf[1] >= 2) ? buf[1] : -1;
	}
	if (len == -2) {
		if (bufLength < 3)
			return 0;
		unsigned short slen = *((unsigned short*)(buf + 1));
		return (slen >= 3) ? slen : -1;
	}

	throw network_error(str( boost::format("Invalid Message Length: %1%") %(unsigned int)msgid ));
}
void CRemoteConnection::SendRawPacket(const unsigned char* data, const unsigned length, const int packetNum)
{
	if (!active)
		return;

	const unsigned hsize = 9;
	unsigned char tempbuf[NETWORK_BUFFER_SIZE];
	*(int*)tempbuf = packetNum;
	*(int*)(tempbuf+4) = lastInOrder;
	if(!waitingPackets.empty() && waitingPackets.find(lastInOrder+1)==waitingPackets.end()){
		int nak = (waitingPackets.begin()->first-1) - lastInOrder;
		assert(nak >= 0);
		if (nak <= 255)
			*(unsigned char*)(tempbuf+8) = (unsigned char)nak;
		else
			*(unsigned char*)(tempbuf+8) = 255;
	}
	else {
		*(unsigned char*)(tempbuf+8) = 0;
	}

	memcpy(&tempbuf[hsize],data,length);

	if(sendto(*mySocket,(char*)tempbuf,length+hsize,0,(sockaddr*)&addr,sizeof(addr))==SOCKET_ERROR){
		if (IsFakeError())
			return;
		throw network_error("Error sending data: "+GetErrorMsg());
	}
	dataSent += length;
	sentOverhead += hsize;
}
예제 #4
0
void UDPConnectedSocket::Send(const unsigned char* const buf, const unsigned dataLength) const
{
	int error = send(mySocket, (char*)buf, dataLength, 0);
	if (error == SOCKET_ERROR && !IsFakeError())
	{
		throw network_error(std::string("Error sending data to socket: ") + GetErrorMsg());
	}
}
예제 #5
0
CLocalConnection::CLocalConnection()
{
	if (instances > 1) {
		throw network_error("Opening a third local connection is not allowed");
	}
	instance = instances;
	instances++;
}
void UDPConnection::SendData(boost::shared_ptr<const RawPacket> data)
{
	if(outgoingLength + data->length >= UDPBufferSize){
		throw network_error("Buffer overflow in UDPConnection (SendData)");
	}
	memcpy(&outgoingData[outgoingLength], data->data, data->length);
	outgoingLength += data->length;
}
예제 #7
0
UDPConnectedSocket::UDPConnectedSocket(const std::string& server, const unsigned remoteport)
: Socket(DATAGRAM)
{
	sockaddr_in remoteAddr = ResolveHost(server, remoteport);
	if (connect(mySocket, (sockaddr*)&remoteAddr, sizeof(remoteAddr)) == SOCKET_ERROR)
	{
		throw network_error(std::string("Error while connecting: ") + GetErrorMsg());
	}
	SetBlocking(false);
}
예제 #8
0
bool ProtocolDef::HasFixedLength(const unsigned char id) const
{
	if (msg[id].Length > 0)
		return true;
	else if (msg[id].Length < 0)
		return false;
	else
	{
		throw network_error(str( boost::format("Unbound Message Type: %1%") %(unsigned int)id ));
	}
}
예제 #9
0
actor_addr remote_actor_impl(std::set<std::string> ifs,
                             std::string host, uint16_t port) {
  auto mm = get_middleman_actor();
  actor_addr result;
  scoped_actor self;
  self->sync_send(mm, connect_atom::value, std::move(host), port).await(
    [&](ok_atom, const node_id&, actor_addr res, std::set<std::string>& xs) {
      if (!res)
        throw network_error("no actor published at given port");
      if (! (xs.empty() && ifs.empty())
          && ! std::includes(xs.begin(), xs.end(), ifs.begin(), ifs.end()))
        throw network_error("expected signature does not "
                            "comply to found signature");
      result = std::move(res);
    },
    [&](error_atom, std::string& msg) {
      throw network_error(std::move(msg));
    }
  );
  return result;
}
예제 #10
0
CLocalConnection::CLocalConnection()
{
	if (instances > 1) {
		throw network_error("Opening a third local connection is not allowed");
	}

	instance = instances;
	instances++;

	// make sure protocoldef is initialized
	CBaseNetProtocol::Get();
}
예제 #11
0
udp_client::udp_client(std::string host, uint16_t port)
    : connected_(false)
{
    boost::lock_guard<boost::mutex> lock(host_mutex_);
    host_ = enet_host_create(nullptr, 1, UDP_CHANNELS, 0, 0);
    if (host_ == nullptr)
        throw network_error("could not set up UDP host");

    if (host.empty()) {
#ifdef ENET_IPV6
        host = "::1";
#else
        host = "127.0.0.1";
#endif
    }

    enet_address_set_host(&address_, host.c_str());
    address_.port = port;

    log_msg("udp_client set up peer to host %1%", host);
    peer_ = enet_host_connect(host_, &address_, UDP_CHANNELS, 0);
    if (peer_ == nullptr)
        throw network_error("could not set up UDP peer");
}
예제 #12
0
unsigned UDPConnectedSocket::Recv(unsigned char* buf, const unsigned bufLength) const
{
	const int data = recv(mySocket,(char*)buf,bufLength,0);
	if (data == SOCKET_ERROR)
	{
		if (IsFakeError())
		{
			return 0;
		}
		else
		{
			throw network_error(std::string("Error receiving data from socket: ") + GetErrorMsg());
		}
	}
	return (unsigned)data;
}