Ejemplo n.º 1
0
void
list_interface_addresses(BNetworkInterface& interface, uint32 flags)
{
	int32 count = interface.CountAddresses();
	for (int32 i = 0; i < count; i++) {
		BNetworkInterfaceAddress address;
		if (interface.GetAddressAt(i, address) != B_OK)
			break;

		const address_family* family
			= address_family_for(address.Address().sa_family);

		printf("\t%s addr: %s", family->name,
			BNetworkAddress(address.Address()).ToString().String());

		if ((flags & IFF_BROADCAST) != 0) {
			printf(", Bcast: %s",
				BNetworkAddress(address.Broadcast()).ToString().String());
		}

		switch (family->preferred_format) {
			case PREFER_OUTPUT_MASK:
				printf(", Mask: %s",
					BNetworkAddress(address.Mask()).ToString().String());
				break;
			case PREFER_OUTPUT_PREFIX_LENGTH:
				printf(", Prefix Length: %zu",
					BNetworkAddress(address.Mask()).PrefixLength());
				break;
		}

		putchar('\n');
	}
}
Ejemplo n.º 2
0
status_t
InterfacesListView::_InitList()
{
	BNetworkRoster& roster = BNetworkRoster::Default();
	BNetworkInterface interface;
	uint32 cookie = 0;

	while (roster.GetNextInterface(&cookie, interface) == B_OK) {
		if (strncmp(interface.Name(), "loop", 4) && interface.Name()[0])
			AddItem(new InterfaceListItem(interface.Name()));
	}

	return B_OK;
}
Ejemplo n.º 3
0
void
NetPulseView::MouseDown(BPoint where)
{
	int32 buttons;
	int32 clicks;
	if (Window()->CurrentMessage()->FindInt32("buttons", &buttons) != B_OK
		|| Window()->CurrentMessage()->FindInt32("clicks", &clicks) != B_OK) {
		return;
	}

	if (buttons == B_SECONDARY_MOUSE_BUTTON) {	
		BPopUpMenu* popUpMenu = new BPopUpMenu("NetPulseMenu");

		popUpMenu->AddItem(new BMenuItem("Connect",
			new BMessage(kMsgConnect)));
		popUpMenu->AddItem(new BMenuItem("Disconnect",
			new BMessage(kMsgDisconnect)));
		popUpMenu->AddSeparatorItem();

		popUpMenu->AddItem(new BMenuItem("Statistics...",
			new BMessage(kMsgStatistics)));
		popUpMenu->AddSeparatorItem();

		uint32 cookie = 0;
		BNetworkInterface interface;
		BNetworkRoster& roster = BNetworkRoster::Default();
		while (roster.GetNextInterface(&cookie, interface) == B_OK) {
			const char* name = interface.Name();
			if (strncmp(name, "loop", 4) != 0) {
				BMenuItem* menuItem = new BMenuItem(name,
					new BMessage(kMsgChangeInterface + cookie));
				menuItem->SetMarked(cookie == fCookie);
				popUpMenu->AddItem(menuItem);
			}
		}

		popUpMenu->AddSeparatorItem();
		popUpMenu->AddItem(new BMenuItem("Quit", new BMessage(kMsgQuit)));
		popUpMenu->SetTargetForItems(this);

		popUpMenu->FindItem(kMsgConnect)->SetEnabled(!fEnable && fCookie != 0);
		popUpMenu->FindItem(kMsgDisconnect)->SetEnabled(fEnable && fCookie != 0);
		popUpMenu->FindItem(kMsgStatistics)->SetEnabled(fEnable && fCookie != 0);

		popUpMenu->Go(ConvertToScreen(BPoint(0, 0)), true, false, false);
		delete popUpMenu;
	} else if (buttons == B_PRIMARY_MOUSE_BUTTON && clicks == 2)
		be_roster->Launch("application/x-vnd.Haiku-Network");
}
Ejemplo n.º 4
0
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;
}
Ejemplo n.º 5
0
void
EthernetSettingsView::_GatherInterfaces()
{
    // iterate over all interfaces and retrieve minimal status

    fInterfaces.MakeEmpty();

    BNetworkRoster& roster = BNetworkRoster::Default();
    BNetworkInterface interface;
    uint32 cookie = 0;

    while (roster.GetNextInterface(&cookie, interface) == B_OK) {
        if (strncmp(interface.Name(), "loop", 4) && interface.Name()[0]) {
            fInterfaces.AddItem(new BString(interface.Name()));
            fSettings.AddItem(new Settings(interface.Name()));
        }
    }
}
Ejemplo n.º 6
0
void
list_interfaces(const char* name)
{
	if (name != NULL) {
		list_interface(name);
		return;
	}

	// get a list of all interfaces

	BNetworkRoster& roster = BNetworkRoster::Default();

	BNetworkInterface interface;
	uint32 cookie = 0;

	while (roster.GetNextInterface(&cookie, interface) == B_OK) {
		list_interface(interface.Name());
	}
}
Ejemplo n.º 7
0
status_t
BNetworkRoster::GetNextInterface(uint32* cookie,
                                 BNetworkInterface& interface) const
{
    // TODO: think about caching the interfaces!

    if (cookie == NULL)
        return B_BAD_VALUE;

    // get a list of all interfaces

    int socket = ::socket(AF_INET, SOCK_DGRAM, 0);
    if (socket < 0)
        return errno;

    FileDescriptorCloser closer(socket);

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

    size_t count = (size_t)config.ifc_value;
    if (count == 0)
        return B_BAD_VALUE;

    char* buffer = (char*)malloc(count * sizeof(struct ifreq));
    if (buffer == NULL)
        return B_NO_MEMORY;

    MemoryDeleter deleter(buffer);

    config.ifc_len = count * sizeof(struct ifreq);
    config.ifc_buf = buffer;
    if (ioctl(socket, SIOCGIFCONF, &config, sizeof(struct ifconf)) < 0)
        return errno;

    ifreq* interfaces = (ifreq*)buffer;
    ifreq* end = (ifreq*)(buffer + config.ifc_len);

    for (uint32 i = 0; interfaces < end; i++) {
        interface.SetTo(interfaces[0].ifr_name);
        if (i == *cookie) {
            (*cookie)++;
            return B_OK;
        }

        interfaces = (ifreq*)((uint8*)interfaces
                              + _SIZEOF_ADDR_IFREQ(interfaces[0]));
    }

    return B_BAD_VALUE;
}
Ejemplo n.º 8
0
bool
BNetworkAddress::IsLocal() const
{
	BNetworkRoster& roster = BNetworkRoster::Default();

	BNetworkInterface interface;
	uint32 cookie = 0;

	while (roster.GetNextInterface(&cookie, interface) == B_OK) {
		int32 count = interface.CountAddresses();
		for (int32 j = 0; j < count; j++) {
			BNetworkInterfaceAddress address;
			if (interface.GetAddressAt(j, address) != B_OK)
				break;

			if (Equals(address.Address(), false))
				return true;
		}
	}

	return false;
}
Ejemplo n.º 9
0
void
NetworkWindow::_ScanInterfaces()
{
	// Try existing devices first
	BNetworkRoster& roster = BNetworkRoster::Default();
	BNetworkInterface interface;
	uint32 cookie = 0;

	while (roster.GetNextInterface(&cookie, interface) == B_OK) {
		if ((interface.Flags() & IFF_LOOPBACK) != 0)
			continue;

		InterfaceListItem* item = new InterfaceListItem(interface.Name());
		item->SetExpanded(true);

		fInterfaceItemMap.insert(std::pair<BString, InterfaceListItem*>(
			BString(interface.Name()), item));
		fListView->AddItem(item);
	}

	// TODO: Then consider those from the settings (for example, for USB)
}
Ejemplo n.º 10
0
void
SystemInfo::_RetrieveNetwork()
{
	if (fRetrievedNetwork)
		return;

	fBytesReceived = 0;
	fBytesSent = 0;
	fRetrievedNetwork = true;

	BNetworkRoster& roster = BNetworkRoster::Default();

	BNetworkInterface interface;
	uint32 cookie = 0;
	while (roster.GetNextInterface(&cookie, interface) == B_OK) {
		ifreq_stats stats;
		if (interface.GetStats(stats) == B_OK) {
			fBytesReceived += stats.receive.bytes;
			fBytesSent += stats.send.bytes;
		}
	}
}
Ejemplo n.º 11
0
int32
NetworkStatusView::_DetermineInterfaceStatus(
	const BNetworkInterface& interface)
{
	uint32 flags = interface.Flags();
	int32 status = kStatusNoLink;

	// TODO: no kStatusLinkNoConfig yet

	if (flags & IFF_CONFIGURING)
		status = kStatusConnecting;
	else if ((flags & (IFF_UP | IFF_LINK)) == (IFF_UP | IFF_LINK))
		status = kStatusReady;

	return status;
}
Ejemplo n.º 12
0
void
NetworkStatusView::_Update(bool force)
{
	BNetworkRoster& roster = BNetworkRoster::Default();
	BNetworkInterface interface;
	uint32 cookie = 0;

	while (roster.GetNextInterface(&cookie, interface) == B_OK) {
		if ((interface.Flags() & IFF_LOOPBACK) == 0) {
			int32 oldStatus = kStatusUnknown;
			if (fInterfaceStatuses.find(interface.Name())
				!= fInterfaceStatuses.end()) {
				oldStatus = fInterfaceStatuses[interface.Name()];
			}
			int32 status = _DetermineInterfaceStatus(interface);
			if (oldStatus != status) {
				BNotification notification(B_INFORMATION_NOTIFICATION);
				notification.SetGroup(B_TRANSLATE("Network Status"));
				notification.SetTitle(interface.Name());
				notification.SetMessageID(interface.Name());
				notification.SetIcon(fNotifyIcons[status]);
				if (status == kStatusConnecting
					|| (status == kStatusReady
						&& oldStatus == kStatusConnecting)
					|| (status == kStatusNoLink
						&& oldStatus == kStatusReady)
					|| (status == kStatusNoLink
						&& oldStatus == kStatusConnecting)) {
					// A significant state change, raise notification.
					notification.SetContent(kStatusDescriptions[status]);
					notification.Send();
				}
				Invalidate();
			}
			fInterfaceStatuses[interface.Name()] = status;
		}
	}
}
Ejemplo n.º 13
0
status_t
BNetworkRoster::RemoveInterface(const BNetworkInterface& interface)
{
    return RemoveInterface(interface.Name());
}
Ejemplo n.º 14
0
status_t
BNetworkRoster::AddInterface(const BNetworkInterface& interface)
{
    return AddInterface(interface.Name());
}
Ejemplo n.º 15
0
status_t
BGeolocation::LocateSelf(float& latitude, float& longitude)
{
	// Enumerate wifi network and build JSON message
	BNetworkRoster& roster = BNetworkRoster::Default();
	uint32 interfaceCookie = 0;
	BNetworkInterface interface;

	BString query("{\n\t\"wifiAccessPoints\": [");
	int32 count = 0;

	while (roster.GetNextInterface(&interfaceCookie, interface) == B_OK) {
		uint32 networkCookie = 0;
		wireless_network network;

		BNetworkDevice device(interface.Name());
			// TODO is that the correct way to enumerate devices?

		while (device.GetNextNetwork(networkCookie, network) == B_OK) {
			if (count != 0)
				query += ',';

			count++;

			query += "\n\t\t{ \"macAddress\": \"";
			query += network.address.ToString().ToUpper();
			query += "\", \"signalStrength\": ";
			query << (int)network.signal_strength;
			query += ", \"signalToNoiseRatio\": ";
			query << (int)network.noise_level;
			query += " }";
		}

	}

	query += "\n\t]\n}\n";

	// Check that we have enough data (we need at least 2 networks)
	if (count < 2)
		return B_DEVICE_NOT_FOUND;

	class GeolocationListener: public BUrlProtocolListener
	{
		public:
			virtual	~GeolocationListener() {};

			void	DataReceived(BUrlRequest*, const char* data, off_t position,
						ssize_t size) {
				result.WriteAt(position, data, size);
			}
			BMallocIO result;
	};

	GeolocationListener listener;

	// Send Request (POST JSON message)
	BUrlRequest* request = BUrlProtocolRoster::MakeRequest(fService, &listener);
	if (request == NULL)
		return B_BAD_DATA;

	BHttpRequest* http = dynamic_cast<BHttpRequest*>(request);
	if (http == NULL) {
		delete request;
		return B_BAD_DATA;
	}

	http->SetMethod(B_HTTP_POST);
	BMemoryIO* io = new BMemoryIO(query.String(), query.Length());
	http->AdoptInputData(io, query.Length());

	status_t result = http->Run();
	if (result < 0) {
		delete http;
		return result;
	}

	while (http->IsRunning())
		snooze(10000);

	// Parse reply
	const BHttpResult& reply = (const BHttpResult&)http->Result();
	if (reply.StatusCode() != 200) {
		delete http;
		return B_ERROR;
	}

	BMessage data;
	result = BJson::Parse(data, (char*)listener.result.Buffer());
	delete http;
	if (result != B_OK) {
		return result;
	}

	BMessage location;
	result = data.FindMessage("location", &location);
	if (result != B_OK)
		return result;

	double lat, lon;
	result = location.FindDouble("lat", &lat);
	if (result == B_OK)
		result = location.FindDouble("lng", &lon);

	latitude = lat;
	longitude = lon;

	return result;
}