Esempio n. 1
0
std::shared_ptr<Peer> MockKnownPeers::lookupAddr(const socket_address& addr) const
{
	_history.call("lookupAddr", addr.toString());
	std::string ip = addr.toString();
	auto it = _ips.find(ip);
	return it != _ips.end()? it->second : NULL;
}
Esempio n. 2
0
bool tcp_socket::bind(const socket_address& addr)
{
	struct sockaddr_in local;
	memset((char*)&local, 0, sizeof(local));

	int accept_from = addr.address().empty()? INADDR_ANY : INADDR_LOOPBACK;
	local.sin_addr.s_addr = htonl(accept_from);
	local.sin_port = htons(addr.port());
	local.sin_family = AF_INET;
	return ::bind(_sock, (struct sockaddr*)&local, sizeof(local)) != -1;
}
Esempio n. 3
0
bool udt_socket::connect(const socket_address& endpoint)
{
	struct sockaddr_in remote;
	remote.sin_family = AF_INET;
	remote.sin_port = htons(endpoint.port());
	inet_aton(endpoint.address().c_str(), &remote.sin_addr);

	int res = UDT::connect(_sock, (const sockaddr*)&remote, sizeof(remote));
	if (res == UDT::ERROR)
		_sock = UDT::INVALID_SOCK;
	else
		setAsyncWrites(_sock, true);
	return res == 0;
}
Esempio n. 4
0
bool net::socket::connect(type type, const socket_address& addr, int timeout)
{
	// Create socket.
	if (!create(addr.ss_family, type)) {
		return false;
	}

	// Connect.
	if ((::connect(_M_fd, reinterpret_cast<const struct sockaddr*>(&addr), addr.size()) < 0) && (errno != EINPROGRESS)) {
		close();
		return false;
	}

	if (timeout != 0) {
		if (!wait_writable(timeout)) {
			close();
			return false;
		}

		int error;
		if ((!get_socket_error(error)) || (error != 0)) {
			close();
			return false;
		}
	}

	return true;
}
Esempio n. 5
0
bool udt_socket::bind(const socket_address& addr)
{
	struct sockaddr_in local;
	memset((char*)&local, 0, sizeof(local));

	local.sin_family = AF_INET;
	local.sin_port = htons(addr.port());
	local.sin_addr.s_addr = htonl(INADDR_ANY); // currently ignores addr.address()...
	return UDT::bind(_sock, (struct sockaddr*)&local, sizeof(local)) != UDT::ERROR;
}
Esempio n. 6
0
bool udp_socket::bind(const socket_address& addr)
{
	struct sockaddr_in local;
	memset((char*)&local, 0, sizeof(local));

	local.sin_family = AF_INET;
	local.sin_port = htons(addr.port());
	local.sin_addr.s_addr = htonl(INADDR_ANY);
	return ::bind(_sock, (struct sockaddr*)&local, sizeof(local)) != -1;
}
Esempio n. 7
0
bool net::socket::listen(const socket_address& addr)
{
	// Create socket.
	if (!create(addr.ss_family, STREAM)) {
		return false;
	}

	// Reuse address.
	int optval = 1;
	if (setsockopt(_M_fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(int)) < 0) {
		close();
		return false;
	}

	// Bind.
	if (bind(_M_fd, reinterpret_cast<const struct sockaddr*>(&addr), addr.size()) < 0) {
		if (addr.ss_family == AF_UNIX) {
			unlink(reinterpret_cast<const local_address*>(&addr)->sun_path);
		}

		close();
		return false;
	}

	// Listen.
	if (::listen(_M_fd, BACKLOG) < 0) {
		if (addr.ss_family == AF_UNIX) {
			unlink(reinterpret_cast<const local_address*>(&addr)->sun_path);
		}

		close();
		return false;
	}

	if (addr.ss_family == AF_UNIX) {
		unlink(reinterpret_cast<const local_address*>(&addr)->sun_path);
	}

	return true;
}
Esempio n. 8
0
// ----------------------------------------------------------------------------
void socket::connect(const socket_address& addr)
{
  connect(addr.sockaddr_ptr(), addr.sockaddr_len());
}
Esempio n. 9
0
// ----------------------------------------------------------------------------
socket socket::accept(socket_address& remote_addr)
{
  socklen_t remote_addr_len = remote_addr.sockaddr_len();
  return accept(remote_addr.sockaddr_ptr(), &remote_addr_len);
}
Esempio n. 10
0
// ----------------------------------------------------------------------------
void socket::bind(const socket_address& addr)
{
  bind(addr.sockaddr_ptr(), addr.sockaddr_len());
}
Esempio n. 11
0
/* This code is subject to the terms of the Mozilla Public License, v.2.0. http://mozilla.org/MPL/2.0/. */
#include "unittest.h"

#include "socket_address.h"

TEST_CASE( "socket_addressTest/testConstructors", "[unit]" )
{
	{
		socket_address addr("1.2.3.4", 5);
		assertEquals( "1.2.3.4", addr.address() );
		assertEquals( 5, addr.port() );
		assertEquals( "1.2.3.4:5", addr.toString() );
	}
	{
		socket_address addr("localhost", 50000);
		assertEquals( "localhost", addr.address() );
		assertEquals( 50000, addr.port() );
		assertEquals( "localhost:50000", addr.toString() );
	}
	{
		socket_address addr;
		assertEquals( "", addr.address() );
		assertEquals( 0, addr.port() );
		assertEquals( ":0", addr.toString() );
	}
}

TEST_CASE( "socket_addressTest/testFromString", "[unit]" )
{
	socket_address addr;
	assertTrue( addr.fromString("1.2.3.4:5") );
Esempio n. 12
0
bool udp_socket::setEndpoint(const socket_address& endpoint)
{
	_endpoint.sin_family = AF_INET;
	_endpoint.sin_port = htons(endpoint.port());
	return inet_aton(endpoint.address().c_str(), &_endpoint.sin_addr) != 0;
}