Пример #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
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;
}
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
/*!	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;
}
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;
}
Пример #6
0
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;
}
Пример #7
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;
}
Пример #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
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);
}
Пример #10
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;
}
Пример #11
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;
}
Пример #12
0
bool
configure_wireless(const char* name, char* const* args, int32 argCount)
{
	enum {
		NONE,
		LIST,
		JOIN,
		LEAVE
	} mode = NONE;

	if (!strcmp(args[0], "list") || !strcmp(args[0], "scan"))
		mode = LIST;
	else if (!strcmp(args[0], "join"))
		mode = JOIN;
	else if (!strcmp(args[0], "leave"))
		mode = LEAVE;

	if (mode == NONE)
		return false;

	BNetworkDevice device(name);
	if (!device.Exists()) {
		fprintf(stderr, "%s: \"%s\" does not exist!\n", kProgramName, name);
		exit(1);
	}
	if (!device.IsWireless()) {
		fprintf(stderr, "%s: \"%s\" is not a WLAN device!\n", kProgramName,
			name);
		exit(1);
	}

	args++;
	argCount--;

	switch (mode) {
		case LIST:
		{
			// list wireless network(s)

			bool verbose = false;
			if (argCount > 0 && !strcmp(args[0], "-v")) {
				verbose = true;
				args++;
				argCount--;
			}
			show_wireless_network_header(verbose);

			if (argCount > 0) {
				// list the named entries
				for (int32 i = 0; i < argCount; i++) {
					wireless_network network;
					BNetworkAddress link;
					status_t status;
					if (link.SetTo(AF_LINK, args[i]) == B_OK)
						status = device.GetNetwork(link, network);
					else
						status = device.GetNetwork(args[i], network);
					if (status != B_OK) {
						fprintf(stderr, "%s: Getting network failed: %s\n",
							kProgramName, strerror(status));
					} else
						show_wireless_network(network, verbose);
				}
			} else {
				// list them all
				wireless_network network;
				uint32 cookie = 0;
				while (device.GetNextNetwork(cookie, network) == B_OK)
					show_wireless_network(network, verbose);
			}
			break;
		}

		case JOIN:
		{
			// join a wireless network
			if (argCount > 2) {
				fprintf(stderr, "usage: %s %s join <network> [<password>]\n",
					kProgramName, name);
				exit(1);
			}

			const char* password = NULL;
			if (argCount == 2)
				password = args[1];

			BNetworkAddress link;
			status_t status;
			if (link.SetTo(AF_LINK, args[0]) == B_OK)
				status = device.JoinNetwork(link, password);
			else
				status = device.JoinNetwork(args[0], password);
			if (status != B_OK) {
				fprintf(stderr, "%s: Joining network failed: %s\n",
					kProgramName, strerror(status));
				exit(1);
			}
			break;
		}

		case LEAVE:
		{
			// leave a wireless network
			if (argCount != 1) {
				fprintf(stderr, "usage: %s %s leave <network>\n", kProgramName,
					name);
				exit(1);
			}

			BNetworkAddress link;
			status_t status;
			if (link.SetTo(AF_LINK, args[0]) == B_OK)
				status = device.LeaveNetwork(link);
			else
				status = device.LeaveNetwork(args[0]);
			if (status != B_OK) {
				fprintf(stderr, "%s: Leaving network failed: %s\n",
					kProgramName, strerror(status));
				exit(1);
			}
			break;
		}

		case NONE:
			break;
	}

	return true;
}
Пример #13
0
int
main(int argc, char** argv)
{
	if (argc < 2)
		usage();

	BNetworkDevice device(argv[1]);
	if (!device.Exists()) {
		fprintf(stderr, "\"%s\" does not exist!\n", argv[1]);
		return 1;
	}
	if (!device.IsWireless()) {
		fprintf(stderr, "\"%s\" is not a WLAN device!\n", argv[1]);
		return 1;
	}

	if (argc > 2) {
		if (!strcmp(argv[2], "join")) {
			if (argc < 4)
				usage();
			const char* password = NULL;
			if (argc == 5)
				password = argv[4];

			status_t status = device.JoinNetwork(argv[3], password);
			if (status != B_OK) {
				fprintf(stderr, "joining network failed: %s\n",
					strerror(status));
				return 1;
			}
		} else if (!strcmp(argv[2], "leave")) {
			if (argc < 4)
				usage();

			status_t status = device.LeaveNetwork(argv[3]);
			if (status != B_OK) {
				fprintf(stderr, "leaving network failed: %s\n",
					strerror(status));
				return 1;
			}
		} else if (!strcmp(argv[2], "list")) {
			if (argc == 4) {
				// list the named entry
				wireless_network network;
				BNetworkAddress link;
				status_t status = link.SetTo(AF_LINK, argv[3]);
				if (status == B_OK)
					status = device.GetNetwork(link, network);
				else
					status = device.GetNetwork(argv[3], network);
				if (status != B_OK) {
					fprintf(stderr, "getting network failed: %s\n",
						strerror(status));
					return 1;
				}
				show(network);
			} else {
				// list all
				wireless_network network;
				uint32 cookie = 0;
				while (device.GetNextNetwork(cookie, network) == B_OK)
					show(network);
			}
		} else
			usage();
	} else {
		// show associated networks
		wireless_network network;
		uint32 cookie = 0;
		while (device.GetNextAssociatedNetwork(cookie, network) == B_OK) {
			show(network);
		}
		if (cookie == 0)
			puts("no associated networks found.");
	}

	return 0;
}
Пример #14
0
void
list_routes(int socket, const char *interfaceName, route_entry &route)
{
	// get a list of all routes

	ifconf config;
	config.ifc_len = sizeof(config.ifc_value);
	if (ioctl(socket, SIOCGRTSIZE, &config, sizeof(struct ifconf)) < 0)
		return;

	uint32 size = (uint32)config.ifc_value;
	if (size == 0)
		return;

	void *buffer = malloc(size);
	if (buffer == NULL) {
		fprintf(stderr, "%s: Out of memory.\n", kProgramName);
		return;
	}

	config.ifc_len = size;
	config.ifc_buf = buffer;
	if (ioctl(socket, SIOCGRTTABLE, &config, sizeof(struct ifconf)) < 0)
		return;

	ifreq *interface = (ifreq*)buffer;
	ifreq *end = (ifreq*)((uint8*)buffer + size);

	// find family (we use the family of the first address as this is
	// called on a socket for a single family)
	const address_family *family = NULL;
	for (int32 i = 0; kFamilies[i].family >= 0; i++) {
		if (interface->ifr_route.destination->sa_family
				== kFamilies[i].family) {
			family = &kFamilies[i];
			break;
		}
	}

	int addressLength = family->maxAddressLength;

	printf("%s routing table:\n", family->name);

	if (family->preferredPrefixFormat == PREFIX_PREFER_NETMASK) {
		printf("%*s %*s %*s Flags  Interface\n", addressLength, "Destination",
			addressLength, "Netmask", addressLength, "Gateway");
	} else {
		printf("%*s     %*s Flags  Interface\n", addressLength, "Destination",
			addressLength, "Gateway");
	}

	while (interface < end) {
		route_entry& route = interface->ifr_route;

		// apply filters
		if (interfaceName == NULL
			|| !strcmp(interfaceName, interface->ifr_name)) {

			if (family != NULL) {
				BNetworkAddress destination(*route.destination);
				printf("%*s", addressLength, destination.ToString().String());
				if (route.mask != NULL) {
					BNetworkAddress mask;
					mask.SetTo(*route.mask);
					if (family->preferredPrefixFormat
							== PREFIX_PREFER_NETMASK) {
						printf(" %*s ", addressLength,
							mask.ToString().String());
					} else {
						printf("/%-3zd ", mask.PrefixLength());
					}
				} else {
					if (family->preferredPrefixFormat
							== PREFIX_PREFER_NETMASK) {
						printf(" %*s ", addressLength, "-");
					} else
						printf("     ");
				}

				if ((route.flags & RTF_GATEWAY) != 0) {
					BNetworkAddress gateway;
					if (route.gateway != NULL)
						gateway.SetTo(*route.gateway);
					printf("%*s ", addressLength, gateway.ToString().String());
				} else
					printf("%*s ", family->maxAddressLength, "-");
			} else
				printf("unknown family ");

			if (route.flags != 0) {
				const struct {
					int			value;
					const char	*name;
				} kFlags[] = {
					{RTF_DEFAULT, "D"},
					{RTF_REJECT, "R"},
					{RTF_HOST, "H"},
					{RTF_LOCAL, "L"},
					{RTF_DYNAMIC, "D"},
					{RTF_MODIFIED, "M"},
				};
				for (uint32 i = 0; i < sizeof(kFlags) / sizeof(kFlags[0]);
					i++) {
					if ((route.flags & kFlags[i].value) != 0) {
						printf(kFlags[i].name);
					} else
						putchar('-');
				}
				printf(" ");
			} else
				printf("------ ");

			printf("%s", interface->ifr_name);
			putchar('\n');
		}

		int32 addressSize = 0;
		if (route.destination != NULL)
			addressSize += route.destination->sa_len;
		if (route.mask != NULL)
			addressSize += route.mask->sa_len;
		if (route.gateway != NULL)
			addressSize += route.gateway->sa_len;

		interface = (ifreq*)((addr_t)interface + IF_NAMESIZE
			+ sizeof(route_entry) + addressSize);
	}

	putchar('\n');
	free(buffer);
}