コード例 #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;
}
コード例 #2
0
ファイル: ProxySecureSocket.cpp プロジェクト: bhanug/haiku
status_t
BProxySecureSocket::Connect(const BNetworkAddress& peer, bigtime_t timeout)
{
	status_t status = InitCheck();
	if (status != B_OK)
		return status;

	BSocket::Connect(fProxyAddress, timeout);
	if (status != B_OK)
		return status;

	BString connectRequest;
	connectRequest.SetToFormat("CONNECT %s:%d HTTP/1.0\r\n\r\n",
		peer.HostName().String(), peer.Port());
	BSocket::Write(connectRequest.String(), connectRequest.Length());

	char buffer[256];
	ssize_t length = BSocket::Read(buffer, sizeof(buffer) - 1);
	if (length <= 0)
		return length;

	buffer[length] = '\0';
	int httpStatus = 0;
	int matches = scanf(buffer, "HTTP/1.0 %d %*[^\r\n]\r\n\r\n", httpStatus);
	if (matches != 2)
		return B_BAD_DATA;

	if (httpStatus < 200 || httpStatus > 299)
		return B_BAD_VALUE;

	return _Setup();
}
コード例 #3
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) {
		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;
}
コード例 #4
0
ファイル: NetworkAddress.cpp プロジェクト: looncraz/haiku
bool
BNetworkAddress::Equals(const BNetworkAddress& other, bool includePort) const
{
	if (IsEmpty() && other.IsEmpty())
		return true;

	if (Family() != other.Family()
			|| (includePort && Port() != other.Port())) {
		return false;
	}

	switch (fAddress.ss_family) {
		case AF_INET:
		{
			sockaddr_in& address = (sockaddr_in&)fAddress;
			sockaddr_in& otherAddress = (sockaddr_in&)other.fAddress;
			return memcmp(&address.sin_addr, &otherAddress.sin_addr,
				sizeof(address.sin_addr)) == 0;
		}

		case AF_INET6:
		{
			sockaddr_in6& address = (sockaddr_in6&)fAddress;
			sockaddr_in6& otherAddress = (sockaddr_in6&)other.fAddress;
			return memcmp(&address.sin6_addr, &otherAddress.sin6_addr,
				sizeof(address.sin6_addr)) == 0;
		}

		default:
			if (fAddress.ss_len != other.fAddress.ss_len)
				return false;

			return memcmp(&fAddress, &other.fAddress, fAddress.ss_len);
	}
}
コード例 #5
0
/*!	Parses the \a argument as network \a address for the specified \a family.
	If \a family is \c AF_UNSPEC, \a family will be overwritten with the family
	of the successfully parsed address.
*/
bool
parse_address(int& family, const char* argument, BNetworkAddress& address)
{
	if (argument == NULL)
		return false;

	status_t status = address.SetTo(family, argument, (uint16)0,
		B_NO_ADDRESS_RESOLUTION);
	if (status != B_OK)
		return false;

	if (family == AF_UNSPEC) {
		// Test if we support the resulting address family
		bool supported = false;

		for (int32 i = 0; kFamilies[i].family >= 0; i++) {
			if (kFamilies[i].family == address.Family()) {
				supported = true;
				break;
			}
		}
		if (!supported)
			return false;

		// Take over family from address
		family = address.Family();
	}

	return true;
}
コード例 #6
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);
}
コード例 #7
0
const char*
NetworkSettings::HardwareAddress()
{
	BNetworkAddress macAddress;
	if (fNetworkInterface->GetHardwareAddress(macAddress) == B_OK)
		return macAddress.ToString();

	return NULL;
}
コード例 #8
0
void
IPAddressControl::_UpdateMark()
{
	if (TextLength() == 0) {
		MarkAsInvalid(!fAllowEmpty);
		return;
	}

	BNetworkAddress address;
	bool success = address.SetTo(fFamily, Text()) == B_OK;

	MarkAsInvalid(!success);
}
コード例 #9
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;
}
コード例 #10
0
ファイル: Socket.cpp プロジェクト: SummerSnail2014/haiku
void
BSocket::_SetTo(int fd, const BNetworkAddress& local,
	const BNetworkAddress& peer)
{
	Disconnect();

	fInitStatus = B_OK;
	fSocket = fd;
	fLocal = local;
	fPeer = peer;

	TRACE("%p: accepted from %s to %s\n", this, local.ToString().c_str(),
		peer.ToString().c_str());
}
コード例 #11
0
ファイル: IPAddressControl.cpp プロジェクト: AmirAbrams/haiku
void
IPAddressControl::_UpdateMark()
{
	if (TextLength() == 0) {
		MarkAsInvalid(!fAllowEmpty);
		return;
	}

	BNetworkAddress address;
	bool success = address.SetTo(fFamily, Text(), (char*)NULL,
		B_NO_ADDRESS_RESOLUTION) == B_OK;

	MarkAsInvalid(!success);
}
コード例 #12
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;
}
コード例 #13
0
status_t
BNetworkAddressResolver::GetNextAddress(int family, uint32* cookie,
	BNetworkAddress& address) const
{
	if (fStatus != B_OK)
		return fStatus;

	// Skip previous info entries, and those that have a non-matching family

	addrinfo* info = fInfo;
	int32 first = *cookie;
	for (int32 index = 0; index < first && info != NULL; index++) {
		while (info != NULL && info->ai_family != family)
			info = info->ai_next;
	}

	if (info == NULL)
		return B_BAD_VALUE;

	// Return current

	address.SetTo(*info->ai_addr, info->ai_addrlen);
	(*cookie)++;

	return B_OK;
}
コード例 #14
0
status_t
BNetworkAddressResolver::GetNextAddress(uint32* cookie,
	BNetworkAddress& address) const
{
	if (fStatus != B_OK)
		return fStatus;

	// Skip previous info entries

	addrinfo* info = fInfo;
	int32 first = *cookie;
	for (int32 index = 0; index < first && info != NULL; index++) {
		info = info->ai_next;
	}

	if (info == NULL)
		return B_BAD_VALUE;

	// Return current

	address.SetTo(*info->ai_addr, info->ai_addrlen);
	(*cookie)++;

	return B_OK;
}
コード例 #15
0
status_t
BNetworkDevice::JoinNetwork(const BNetworkAddress& address,
	const char* password)
{
	if (address.InitCheck() != B_OK)
		return B_BAD_VALUE;

	BMessage message(kMsgJoinNetwork);
	status_t status = message.AddString("device", Name());

	if (status == B_OK) {
		status = message.AddFlat("address",
			const_cast<BNetworkAddress*>(&address));
	}
	if (status == B_OK && password != NULL)
		status = message.AddString("password", password);
	if (status != B_OK)
		return status;

	// Send message to the net_server

	BMessenger networkServer(kNetServerSignature);
	BMessage reply;
	status = networkServer.SendMessage(&message, &reply);
	if (status == B_OK)
		reply.FindInt32("status", &status);

	return status;
}
コード例 #16
0
void
Settings::ReadConfiguration()
{
	BNetworkInterface interface(fName);
	BNetworkAddress hardwareAddress;
	if (interface.GetHardwareAddress(hardwareAddress) != B_OK)
		return;

	fHardwareAddress = hardwareAddress.ToString();
	BNetworkInterfaceAddress address;

	// TODO: We only get the first address
	if (interface.GetAddressAt(0, address) != B_OK)
		return;

	fIP = address.Address().ToString();
	fNetmask = address.Mask().ToString();

	int family = AF_INET;
	if (address.Address().Family() != AF_UNSPEC)
		family = address.Address().Family();

	BNetworkAddress gatewayAddress;
	if (interface.GetDefaultRoute(family, gatewayAddress) != B_OK)
		return;

	fGateway = gatewayAddress.ToString();

	uint32 flags = interface.Flags();

	fAuto = (flags & (IFF_AUTO_CONFIGURED | IFF_CONFIGURING)) != 0;
	fDisabled = (flags & IFF_UP) == 0;

	// read resolv.conf for the dns.
	fNameServers.MakeEmpty();

	res_init();
	res_state state = __res_state();

	if (state != NULL) {
		for (int i = 0; i < state->nscount; i++) {
			fNameServers.AddItem(
				new BString(inet_ntoa(state->nsaddr_list[i].sin_addr)));
		}
		fDomain = state->dnsrch[0];
	}
}
コード例 #17
0
status_t
BNetworkInterface::AddAddress(const BNetworkAddress& local)
{
	BNetworkInterfaceAddress address;
	address.SetAddress(local.SockAddr());

	return do_ifaliasreq(Name(), B_SOCKET_ADD_ALIAS, address);
}
コード例 #18
0
ファイル: tunconfig.cpp プロジェクト: mmuman/haiku
static
status_t
show_all()
{
	BNetworkRoster& roster = BNetworkRoster::Default();

	BNetworkInterface interface;
	uint32 cookie = 0;

	while (roster.GetNextInterface(&cookie, interface) == B_OK) {
		BNetworkAddress linkAddress;
		status_t status = interface.GetHardwareAddress(linkAddress);
		if (status == B_OK && linkAddress.LinkLevelType() == IFT_TUN)
			show_interface(interface.Name());
	}
	return B_OK;
}
コード例 #19
0
ファイル: route.cpp プロジェクト: AmirAbrams/haiku
bool
parse_address(int32 familyIndex, const char* argument, BNetworkAddress& address)
{
	if (argument == NULL)
		return false;

	return address.SetTo(kFamilies[familyIndex].family, argument,
		(uint16)0, B_NO_ADDRESS_RESOLUTION) == B_OK;
}
コード例 #20
0
status_t
BNetworkDevice::GetNetwork(const BNetworkAddress& address,
	wireless_network& network)
{
	if (address.Family() != AF_LINK)
		return B_BAD_VALUE;

	return get_network(Name(), network, UINT32_MAX, &address, NULL);
}
コード例 #21
0
bool
prefix_length_to_mask(int family, const char* argument, BNetworkAddress& mask)
{
	char *end;
	uint32 prefixLength = strtoul(argument, &end, 10);
	if (end == argument)
		return false;

	return mask.SetToMask(family, prefixLength) == B_OK;
}
コード例 #22
0
status_t
BNetworkInterface::AddDefaultRoute(const BNetworkAddress& gateway)
{
	route_entry route;
	memset(&route, 0, sizeof(route_entry));
	route.flags = RTF_STATIC | RTF_DEFAULT | RTF_GATEWAY;
	route.gateway = const_cast<sockaddr*>(&gateway.SockAddr());

	return AddRoute(route);
}
コード例 #23
0
status_t
BNetworkDevice::GetHardwareAddress(BNetworkAddress& address)
{
	ifreq request;
	status_t status = do_request(request, Name(), SIOCGIFADDR);
	if (status != B_OK)
		return status;

	address.SetTo(request.ifr_addr);
	return B_OK;
}
コード例 #24
0
ファイル: SecureSocket.cpp プロジェクト: looncraz/haiku
status_t
BSecureSocket::Connect(const BNetworkAddress& peer, bigtime_t timeout)
{
	status_t status = InitCheck();
	if (status != B_OK)
		return status;

	status = BSocket::Connect(peer, timeout);
	if (status != B_OK)
		return status;

	return _SetupConnect(peer.HostName().String());
}
コード例 #25
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;
}
コード例 #26
0
ファイル: route.cpp プロジェクト: AmirAbrams/haiku
bool
prefix_length_to_mask(int32 familyIndex, const char* argument,
	BNetworkAddress& mask)
{
	if (argument == NULL)
		return false;

	char* end;
	uint32 prefixLength = strtoul(argument, &end, 10);
	if (end == argument)
		return false;

	return mask.SetToMask(kFamilies[familyIndex].family, prefixLength) == B_OK;
}
コード例 #27
0
status_t
BNetworkInterface::GetHardwareAddress(BNetworkAddress& address)
{
	int socket = ::socket(AF_LINK, SOCK_DGRAM, 0);
	if (socket < 0)
		return errno;

	FileDescriptorCloser closer(socket);

	ifreq request;
	strlcpy(request.ifr_name, Name(), IF_NAMESIZE);

	if (ioctl(socket, SIOCGIFADDR, &request, sizeof(struct ifreq)) < 0)
		return errno;

	address.SetTo(request.ifr_addr);
	return B_OK;
}
コード例 #28
0
status_t
BNetworkInterface::GetDefaultRoute(int family, BNetworkAddress& gateway) const
{
	BObjectList<route_entry> routes(1, true);
	status_t status = GetRoutes(family, routes);
	if (status != B_OK)
		return status;

	for (int32 i = routes.CountItems() - 1; i >= 0; i--) {
		route_entry* entry = routes.ItemAt(i);
		if (entry->flags & RTF_DEFAULT) {
			gateway.SetTo(*entry->gateway);
			break;
		}
	}

	return B_OK;
}
コード例 #29
0
ファイル: NetworkAddress.cpp プロジェクト: looncraz/haiku
bool
BNetworkAddress::operator<(const BNetworkAddress& other) const
{
	if (Family() < other.Family())
		return true;
	if (Family() > other.Family())
		return false;

	int compare;

	switch (fAddress.ss_family) {
		default:
		case AF_INET:
		{
			sockaddr_in& address = (sockaddr_in&)fAddress;
			sockaddr_in& otherAddress = (sockaddr_in&)other.fAddress;
			compare = memcmp(&address.sin_addr, &otherAddress.sin_addr,
				sizeof(address.sin_addr));
			break;
		}

		case AF_INET6:
		{
			sockaddr_in6& address = (sockaddr_in6&)fAddress;
			sockaddr_in6& otherAddress = (sockaddr_in6&)other.fAddress;
			compare = memcmp(&address.sin6_addr, &otherAddress.sin6_addr,
				sizeof(address.sin6_addr));
			break;
		}

		case AF_LINK:
			if (LinkLevelAddressLength() < other.LinkLevelAddressLength())
				return true;
			if (LinkLevelAddressLength() > other.LinkLevelAddressLength())
				return true;

			// TODO: could compare index, and name, too
			compare = memcmp(LinkLevelAddress(), other.LinkLevelAddress(),
				LinkLevelAddressLength());
			break;
	}

	if (compare < 0)
		return true;
	if (compare > 0)
		return false;

	return Port() < other.Port();
}
コード例 #30
0
status_t
BNetworkDevice::GetNextAssociatedNetwork(uint32& cookie,
	BNetworkAddress& address)
{
	// We currently support only a single associated network
	if (cookie != 0)
		return B_ENTRY_NOT_FOUND;

	uint8 mac[IEEE80211_ADDR_LEN];
	int32 length = IEEE80211_ADDR_LEN;
	status_t status = get_80211(Name(), IEEE80211_IOC_BSSID, mac, length);
	if (status != B_OK)
		return status;

	if (mac[0] == 0 && mac[1] == 0 && mac[2] == 0 && mac[3] == 0 && mac[4] == 0
			&& mac[5] == 0) {
		return B_ENTRY_NOT_FOUND;
	}

	address.SetToLinkLevel(mac, IEEE80211_ADDR_LEN);
	cookie++;
	return B_OK;
}