예제 #1
0
status_t
SocketConnection::Connect(const char* server, uint32 port)
{
	if (fSocket >= 0)
		Disconnect();

	TRACE("SocketConnection to server %s:%i\n", server, (int)port);

	BNetworkAddress address;
	status_t status = address.SetTo(server, port);
	if (status != B_OK)
		return status;

	fSocket = socket(address.Family(), SOCK_STREAM, 0);
	if (fSocket < 0)
		return errno;

	int result = connect(fSocket, address, address.Length());
	if (result < 0) {
		close(fSocket);
		return errno;
	}

	TRACE("SocketConnection: connected\n");

	return B_OK;
}
status_t
SocketConnection::Connect(const char* server, uint32 port)
{
	if (fSocket >= 0)
		Disconnect();

	TRACE("SocketConnection to server %s:%i\n", server, (int)port);

	BNetworkAddress address;
	status_t status = address.SetTo(server, port);
	if (status != B_OK) {
		TRACE("%s: Address Error: %s\n", __func__, strerror(status));
		return status;
	}

	TRACE("Server resolves to %s\n", address.ToString().String());

	fSocket = socket(address.Family(), SOCK_STREAM, 0);
	if (fSocket < 0) {
		TRACE("%s: Socket Error: %s\n", __func__, strerror(errno));
		return errno;
	}

	int result = connect(fSocket, address, address.Length());
	if (result < 0) {
		TRACE("%s: Connect Error: %s\n", __func__, strerror(errno));
		close(fSocket);
		return errno;
	}

	TRACE("SocketConnection: connected\n");

	return B_OK;
}
예제 #3
0
status_t
BAbstractSocket::Connect(const BNetworkAddress& peer, int type,
	bigtime_t timeout)
{
	Disconnect();

	fInitStatus = _OpenIfNeeded(peer.Family(), type);
	if (fInitStatus == B_OK)
		fInitStatus = SetTimeout(timeout);

	if (fInitStatus == B_OK && !IsBound()) {
		BNetworkAddress local;
		local.SetToWildcard(peer.Family());
		fInitStatus = Bind(local);
	}
	if (fInitStatus != B_OK)
		return fInitStatus;

	BNetworkAddress normalized = peer;
	if (connect(fSocket, normalized, normalized.Length()) != 0) {
		TRACE("%p: connecting to %s: %s\n", this,
			normalized.ToString().c_str(), strerror(errno));
		return fInitStatus = errno;
	}

	fIsConnected = true;
	fPeer = normalized;
	_UpdateLocalAddress();

	TRACE("%p: connected to %s (local %s)\n", this, peer.ToString().c_str(),
		fLocal.ToString().c_str());

	return fInitStatus = B_OK;
}
예제 #4
0
status_t
BNetworkInterface::RemoveAddress(const BNetworkAddress& address)
{
	ifreq request;
	memcpy(&request.ifr_addr, &address.SockAddr(), address.Length());

	return do_request(address.Family(), request, Name(), B_SOCKET_REMOVE_ALIAS);
}
예제 #5
0
status_t
BAbstractSocket::Bind(const BNetworkAddress& local, int type)
{
	fInitStatus = _OpenIfNeeded(local.Family(), type);
	if (fInitStatus != B_OK)
		return fInitStatus;

	if (bind(fSocket, local, local.Length()) != 0)
		return fInitStatus = errno;

	fIsBound = true;
	_UpdateLocalAddress();
	return B_OK;
}
예제 #6
0
int32
BNetworkInterface::FindAddress(const BNetworkAddress& address)
{
	int socket = ::socket(address.Family(), SOCK_DGRAM, 0);
	if (socket < 0)
		return -1;

	FileDescriptorCloser closer(socket);

	ifaliasreq request;
	memset(&request, 0, sizeof(ifaliasreq));

	strlcpy(request.ifra_name, Name(), IF_NAMESIZE);
	request.ifra_index = -1;
	memcpy(&request.ifra_addr, &address.SockAddr(), address.Length());

	if (ioctl(socket, B_SOCKET_GET_ALIAS, &request, sizeof(struct ifaliasreq))
			< 0) {
		return -1;
	}

	return request.ifra_index;
}