diff --git a/src/preferences/network/InterfacesAddOn/InterfaceView.cpp b/src/preferences/network/InterfacesAddOn/InterfaceView.cpp deleted file mode 100644 index b33393214c..0000000000 --- a/src/preferences/network/InterfacesAddOn/InterfaceView.cpp +++ /dev/null @@ -1,97 +0,0 @@ -/* - * Copyright 2004-2013 Haiku, Inc. All rights reserved. - * Distributed under the terms of the MIT License. - * - * Authors: - * Alexander von Gluck, kallisti5@unixzen.com - * John Scipione, jscipione@gmail.com - */ - - -#include "InterfaceView.h" - -#include -#include -#include -#include -#include -#include - - -#undef B_TRANSLATION_CONTEXT -#define B_TRANSLATION_CONTEXT "InterfaceWindow" - - -InterfaceView::InterfaceView(NetworkSettings* settings) - : - BTabView("settings_tabs") -{ - fNetworkSettings = settings; - SetTabWidth(B_WIDTH_FROM_LABEL); - _PopulateTabs(); -} - - -InterfaceView::~InterfaceView() -{ -} - - -void -InterfaceView::Revert() -{ - protocols* supportedFamilies = fNetworkSettings->ProtocolVersions(); - - for (int index = 0; index < MAX_PROTOCOLS; index++) { - if (supportedFamilies[index].present) { - int inet_id = supportedFamilies[index].inet_id; - fTabIPView[inet_id]->Revert(); - } - } -} - - -void -InterfaceView::Apply() -{ - protocols* supportedFamilies = fNetworkSettings->ProtocolVersions(); - - for (int index = 0; index < MAX_PROTOCOLS; index++) { - if (supportedFamilies[index].present) { - int inet_id = supportedFamilies[index].inet_id; - fTabIPView[inet_id]->Save(); - } - } -} - - -status_t -InterfaceView::_PopulateTabs() -{ - protocols* supportedFamilies = fNetworkSettings->ProtocolVersions(); - - BTab* hardwareTab = new BTab; - fTabHardwareView = new InterfaceHardwareView(fNetworkSettings); - AddTab(fTabHardwareView, hardwareTab); - hardwareTab->SetLabel(B_TRANSLATE("Interface")); - - for (int index = 0; index < MAX_PROTOCOLS; index++) { - if (supportedFamilies[index].present) { - int inet_id = supportedFamilies[index].inet_id; - fTabIPView[inet_id] = new InterfaceAddressView(inet_id, - fNetworkSettings); - BTab* tab = new BTab; - AddTab(fTabIPView[inet_id], tab); - tab->SetLabel(supportedFamilies[index].name); - } - } - - return B_OK; -} - - -bool -InterfaceView::QuitRequested() -{ - return true; -} diff --git a/src/preferences/network/InterfacesAddOn/InterfaceView.h b/src/preferences/network/InterfacesAddOn/InterfaceView.h deleted file mode 100644 index 51a5fbb4fb..0000000000 --- a/src/preferences/network/InterfacesAddOn/InterfaceView.h +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright 2004-2013 Haiku, Inc. All rights reserved. - * Distributed under the terms of the MIT License. - * - * Authors: - * Alexander von Gluck, kallisti5@unixzen.com - * John Scipione, jscipione@gmail.com - */ -#ifndef INTERFACE_WINDOW_H -#define INTERFACE_WINDOW_H - - -#include "NetworkSettings.h" -#include "InterfaceAddressView.h" -#include "InterfaceHardwareView.h" - -#include - -#include -#include - - -enum { - kMsgNetwork = 'netw' -}; - - -typedef std::map IPViewMap; - - -class BButton; -class BTabView; - -class InterfaceView : public BTabView { -public: - InterfaceView(NetworkSettings* settings); - virtual ~InterfaceView(); - - virtual bool QuitRequested(); - - void Apply(); - void Revert(); - - NetworkSettings* Settings() { return fNetworkSettings; } - -private: - status_t _PopulateTabs(); - - NetworkSettings* fNetworkSettings; - - IPViewMap fTabIPView; - InterfaceHardwareView* fTabHardwareView; -}; - - -#endif // INTERFACE_WINDOW_H diff --git a/src/preferences/network/InterfacesAddOn/InterfacesAddOn.h b/src/preferences/network/InterfacesAddOn/InterfacesAddOn.h deleted file mode 100644 index 5363c3ab22..0000000000 --- a/src/preferences/network/InterfacesAddOn/InterfacesAddOn.h +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright 2004-2013 Haiku, Inc. All rights reserved. - * Distributed under the terms of the MIT License. - * - * Authors: - * Alexander von Gluck, - * Philippe Houdoin - * Fredrik Modéen - * John Scipione, jscipione@gmail.com - */ -#ifndef INTERFACES_ADDON_H -#define INTERFACES_ADDON_H - - -#include - -#include "NetworkSetupAddOn.h" -#include "InterfacesListView.h" - - -static const uint32 kMsgInterfaceSelected = 'ifce'; - - -class BButton; -class BView; - -class InterfaceView; - -class InterfacesAddOn : public NetworkSetupAddOn, public BBox -{ -public: - InterfacesAddOn(image_id addon_image); - ~InterfacesAddOn(); - - status_t Save(); - status_t Revert(); - - BView* CreateView(); - - void AttachedToWindow(); - void MessageReceived(BMessage* msg); - void Show(); - -private: - InterfaceView* _SettingsView(); - void _ShowPanel(); -private: - InterfacesListView* fListView; -}; - - -#endif // INTERFACES_ADDON_H - diff --git a/src/preferences/network/InterfacesAddOn/InterfacesListView.cpp b/src/preferences/network/InterfacesAddOn/InterfacesListView.cpp deleted file mode 100644 index b87c568958..0000000000 --- a/src/preferences/network/InterfacesAddOn/InterfacesListView.cpp +++ /dev/null @@ -1,590 +0,0 @@ -/* - * Copyright 2004-2013 Haiku, Inc. All rights reserved. - * Distributed under the terms of the MIT License. - * - * Authors: - * Alexander von Gluck IV, kallisti5@unixzen.com - * Philippe Houdoin - * Fredrik Modéen - * John Scipione, jscipione@gmail.com - */ - - -#include "InterfacesListView.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include - -#include "InterfacesAddOn.h" -#include "InterfaceView.h" - - -#define ICON_SIZE 37 - - -#undef B_TRANSLATION_CONTEXT -#define B_TRANSLATION_CONTEXT "InterfacesListView" - - -// #pragma mark - our_image function - - -status_t -our_image(image_info& image) -{ - int32 cookie = 0; - while (get_next_image_info(B_CURRENT_TEAM, &cookie, &image) == B_OK) { - if ((char *)our_image >= (char *)image.text - && (char *)our_image <= (char *)image.text + image.text_size) - return B_OK; - } - - return B_ERROR; -} - - -// #pragma mark - InterfaceListItem - - -InterfaceListItem::InterfaceListItem(const char* name) - : - BListItem(0, false), - fIcon(NULL) -{ - fInterface.SetTo(name); - _Init(); -} - - -InterfaceListItem::~InterfaceListItem() -{ - delete fIcon; - delete fSettings; -} - - -// #pragma mark - InterfaceListItem public methods - - -void -InterfaceListItem::DrawItem(BView* owner, BRect /*bounds*/, bool complete) -{ - BListView* list = dynamic_cast(owner); - - if (list == NULL) - return; - - owner->PushState(); - - BRect bounds = list->ItemFrame(list->IndexOf(this)); - - //rgb_color highColor = list->HighColor(); - rgb_color lowColor = list->LowColor(); - - if (IsSelected() || complete) { - if (IsSelected()) { - list->SetHighColor(ui_color(B_LIST_SELECTED_BACKGROUND_COLOR)); - list->SetLowColor(list->HighColor()); - } else - list->SetHighColor(lowColor); - - list->FillRect(bounds); - } - - BString interfaceState; - BBitmap* stateIcon(NULL); - - if (fSettings->IsDisabled()) { - interfaceState = "disabled"; - stateIcon = fIconOffline; - } else if (!fInterface.HasLink()) { - interfaceState = "no link"; - stateIcon = fIconOffline; - } else if ((fSettings->IPAddr(AF_INET).IsEmpty() - && fSettings->IPAddr(AF_INET6).IsEmpty()) - && (fSettings->AutoConfigure(AF_INET) - || fSettings->AutoConfigure(AF_INET6))) { - interfaceState = "connecting" B_UTF8_ELLIPSIS; - stateIcon = fIconPending; - } else { - interfaceState = "connected"; - stateIcon = fIconOnline; - } - - // Set the initial bounds of item contents - BPoint iconPt = bounds.LeftTop(); - BPoint namePt = bounds.LeftTop(); - BPoint line2Pt = bounds.LeftTop(); - BPoint line3Pt = bounds.LeftTop(); - BPoint statePt = bounds.RightTop(); - - iconPt += BPoint(4, 4); - statePt += BPoint(0, fFirstlineOffset); - namePt += BPoint(ICON_SIZE + 12, fFirstlineOffset); - line2Pt += BPoint(ICON_SIZE + 12, fSecondlineOffset); - line3Pt += BPoint(ICON_SIZE + 12, fThirdlineOffset); - - statePt -= BPoint( - be_plain_font->StringWidth(interfaceState.String()) + 4.0f, 0); - - if (fSettings->IsDisabled()) { - list->SetDrawingMode(B_OP_ALPHA); - list->SetBlendingMode(B_CONSTANT_ALPHA, B_ALPHA_OVERLAY); - list->SetHighColor(0, 0, 0, 32); - } else - list->SetDrawingMode(B_OP_OVER); - - list->DrawBitmapAsync(fIcon, iconPt); - list->DrawBitmapAsync(stateIcon, iconPt); - - if (fSettings->IsDisabled()) { - rgb_color textColor; - if (IsSelected()) - textColor = ui_color(B_LIST_SELECTED_ITEM_TEXT_COLOR); - else - textColor = ui_color(B_LIST_ITEM_TEXT_COLOR); - - if (textColor.red + textColor.green + textColor.blue > 128 * 3) - list->SetHighColor(tint_color(textColor, B_DARKEN_1_TINT)); - else - list->SetHighColor(tint_color(textColor, B_LIGHTEN_1_TINT)); - } else { - if (IsSelected()) - list->SetHighColor(ui_color(B_LIST_SELECTED_ITEM_TEXT_COLOR)); - else - list->SetHighColor(ui_color(B_LIST_ITEM_TEXT_COLOR)); - } - - list->SetFont(be_bold_font); - - BString name = Name(); - name.RemoveFirst("/dev/net/"); - - list->DrawString(name, namePt); - list->SetFont(be_plain_font); - list->DrawString(interfaceState, statePt); - - if (!fSettings->IsDisabled()) { - // Render IPv4 Address - BString ipv4Str(B_TRANSLATE_COMMENT("IP:", "IPv4 address label")); - if (fSettings->IPAddr(AF_INET).IsEmpty()) - ipv4Str << " " << B_TRANSLATE("None"); - else - ipv4Str << " " << BString(fSettings->IP(AF_INET)); - - list->DrawString(ipv4Str, line2Pt); - } - - // Render IPv6 Address (if present) - if (!fSettings->IsDisabled() - && !fSettings->IPAddr(AF_INET6).IsEmpty()) { - BString ipv6Str(B_TRANSLATE_COMMENT("IPv6:", "IPv6 address label")); - ipv6Str << " " << BString(fSettings->IP(AF_INET6)); - - list->DrawString(ipv6Str, line3Pt); - } - - owner->PopState(); -} - - -void -InterfaceListItem::Update(BView* owner, const BFont* font) -{ - BListItem::Update(owner, font); - font_height height; - font->GetHeight(&height); - - float lineHeight = ceilf(height.ascent) + ceilf(height.descent) - + ceilf(height.leading); - - fFirstlineOffset = 2 + ceilf(height.ascent + height.leading / 2); - fSecondlineOffset = fFirstlineOffset + lineHeight; - fThirdlineOffset = fFirstlineOffset + (lineHeight * 2); - - SetHeight(std::max(3 * lineHeight + 4, fIcon->Bounds().Height() + 8)); - // either to the text height or icon height, whichever is taller -} - - -// #pragma mark - InterfaceListItem private methods - - -void -InterfaceListItem::_Init() -{ - fSettings = new NetworkSettings(Name()); - - const char* mediaTypeName = NULL; - - BNetworkDevice device(Name()); - if (device.IsWireless()) - mediaTypeName = "wifi"; - else if (device.IsEthernet()) - mediaTypeName = "ether"; - - printf("%s is a %s device\n", Name(), mediaTypeName); - - _PopulateBitmaps(mediaTypeName); - // Load the interface icons -} - - -void -InterfaceListItem::_PopulateBitmaps(const char* mediaType) { - - const uint8* interfaceHVIF; - const uint8* offlineHVIF; - const uint8* pendingHVIF; - const uint8* onlineHVIF; - - BBitmap* interfaceBitmap = NULL; - - /* Load interface icons */ - image_info info; - if (our_image(info) != B_OK) - return; - - BFile resourcesFile(info.name, B_READ_ONLY); - if (resourcesFile.InitCheck() < B_OK) - return; - - BResources addonResources(&resourcesFile); - - if (addonResources.InitCheck() < B_OK) - return; - - size_t iconSize; - - // Try specific interface icon? - interfaceHVIF = (const uint8*)addonResources.LoadResource( - B_VECTOR_ICON_TYPE, Name(), &iconSize); - - if (interfaceHVIF == NULL && mediaType != NULL) - // Not found, try interface media type? - interfaceHVIF = (const uint8*)addonResources.LoadResource( - B_VECTOR_ICON_TYPE, mediaType, &iconSize); - if (interfaceHVIF == NULL) - // Not found, try default interface icon? - interfaceHVIF = (const uint8*)addonResources.LoadResource( - B_VECTOR_ICON_TYPE, "ether", &iconSize); - - if (interfaceHVIF) { - // Now build the bitmap - interfaceBitmap = new BBitmap(BRect(0, 0, ICON_SIZE, ICON_SIZE), - 0, B_RGBA32); - if (BIconUtils::GetVectorIcon(interfaceHVIF, - iconSize, interfaceBitmap) == B_OK) - fIcon = interfaceBitmap; - else - delete interfaceBitmap; - } - - // Load possible state icons - offlineHVIF = (const uint8*)addonResources.LoadResource( - B_VECTOR_ICON_TYPE, "offline", &iconSize); - - if (offlineHVIF) { - fIconOffline = new BBitmap(BRect(0, 0, ICON_SIZE, ICON_SIZE), - 0, B_RGBA32); - BIconUtils::GetVectorIcon(offlineHVIF, iconSize, fIconOffline); - } - - pendingHVIF = (const uint8*)addonResources.LoadResource( - B_VECTOR_ICON_TYPE, "pending", &iconSize); - - if (pendingHVIF) { - fIconPending = new BBitmap(BRect(0, 0, ICON_SIZE, ICON_SIZE), - 0, B_RGBA32); - BIconUtils::GetVectorIcon(pendingHVIF, iconSize, fIconPending); - } - - onlineHVIF = (const uint8*)addonResources.LoadResource( - B_VECTOR_ICON_TYPE, "online", &iconSize); - - if (onlineHVIF) { - fIconOnline = new BBitmap(BRect(0, 0, ICON_SIZE, ICON_SIZE), - 0, B_RGBA32); - BIconUtils::GetVectorIcon(onlineHVIF, iconSize, fIconOnline); - } -} - - -// #pragma mark - InterfaceListView - - -InterfacesListView::InterfacesListView(const char* name) - : - BListView(name, B_SINGLE_SELECTION_LIST, - B_WILL_DRAW | B_NAVIGABLE | B_FRAME_EVENTS | B_FULL_UPDATE_ON_RESIZE) -{ - fContextMenu = new BPopUpMenu("context menu", false, false); -} - - -InterfacesListView::~InterfacesListView() -{ -} - - -// #pragma mark - InterfaceListView protected methods - - -void -InterfacesListView::AttachedToWindow() -{ - BListView::AttachedToWindow(); - - _InitList(); - - start_watching_network( - B_WATCH_NETWORK_INTERFACE_CHANGES | B_WATCH_NETWORK_LINK_CHANGES, this); - - Select(0); - // Select the first item in the list -} - - -void -InterfacesListView::DetachedFromWindow() -{ - BListView::DetachedFromWindow(); - - stop_watching_network(this); - - // free all items, they will be retrieved again in AttachedToWindow() - for (int32 i = CountItems(); i-- > 0;) - delete ItemAt(i); - - MakeEmpty(); -} - - -void -InterfacesListView::MessageReceived(BMessage* message) -{ - switch (message->what) { - case B_NETWORK_MONITOR: - _HandleNetworkMessage(message); - break; - - default: - BListView::MessageReceived(message); - } -} - - -void -InterfacesListView::MouseDown(BPoint where) -{ - int32 buttons = 0; - Window()->CurrentMessage()->FindInt32("buttons", &buttons); - - if ((B_SECONDARY_MOUSE_BUTTON & buttons) == 0) { - // If not secondary mouse button do the default - BListView::MouseDown(where); - return; - } - - InterfaceListItem* item = FindItem(where); - if (item == NULL) - return; - - // Remove all items from the menu - for (int32 i = fContextMenu->CountItems(); i >= 0; --i) { - BMenuItem* menuItem = fContextMenu->RemoveItem(i); - delete menuItem; - } - - // Now add the ones we want - if (item->GetSettings()->IsDisabled()) { - fContextMenu->AddItem(new BMenuItem(B_TRANSLATE("Enable"), - new BMessage(kMsgInterfaceToggle))); - } else { - if (item->GetSettings()->AutoConfigure(AF_INET) - || item->GetSettings()->AutoConfigure(AF_INET6)) { - fContextMenu->AddItem(new BMenuItem( - B_TRANSLATE("Renegotiate address"), - new BMessage(kMsgInterfaceRenegotiate))); - } - fContextMenu->AddItem(new BSeparatorItem()); - fContextMenu->AddItem(new BMenuItem(B_TRANSLATE("Disable"), - new BMessage(kMsgInterfaceToggle))); - } - - fContextMenu->ResizeToPreferred(); - BMenuItem* selected = fContextMenu->Go(ConvertToScreen(where)); - if (selected == NULL) - return; - - switch (selected->Message()->what) { - case kMsgInterfaceToggle: - item->SetDisabled(!item->IsDisabled()); - Invalidate(); - break; - - case kMsgInterfaceRenegotiate: - item->GetSettings()->RenegotiateAddresses(); - break; - } -} - - -// #pragma mark - InterfaceListView public methods - - -InterfaceListItem* -InterfacesListView::FindItem(const char* name) -{ - for (int32 i = CountItems(); i-- > 0;) { - InterfaceListItem* item = dynamic_cast(ItemAt(i)); - if (item == NULL) - continue; - - if (strcmp(item->Name(), name) == 0) - return item; - } - - return NULL; -} - - -InterfaceListItem* -InterfacesListView::FindItem(BPoint where) -{ - for (int32 i = CountItems(); i-- > 0;) { - InterfaceListItem* item = dynamic_cast(ItemAt(i)); - if (item == NULL) - continue; - - if (ItemFrame(i).Contains(where)) - return item; - } - - return NULL; -} - - -status_t -InterfacesListView::SaveItems(BString& settingsData) -{ - // Grab each fSettings instance and write it's settings - for (int32 i = CountItems(); i-- > 0;) { - InterfaceListItem* item = dynamic_cast(ItemAt(i)); - if (item == NULL) - continue; - - if (strcmp(item->Name(), "loop") == 0) - continue; - - NetworkSettings* ns = item->GetSettings(); - ns->SetConfiguration(); - settingsData << ns->GenerateConfiguration(); - } - - return B_OK; -} - - -// #pragma mark - InterfaceListView private methods - - -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())); - } - - // FIXME compute this according to the actual size of the largest item - // instead. - SetExplicitMinSize(BSize(10 + B_LARGE_ICON - + StringWidth("/dev/net/MMMMMMMMMMM xxxxxxxxxx"), B_SIZE_UNSET)); - return B_OK; -} - - -status_t -InterfacesListView::_UpdateList() -{ - // TODO - return B_OK; -} - - -void -InterfacesListView::_HandleNetworkMessage(BMessage* message) -{ - const char* name; - int32 opcode; - - if (message->FindInt32("opcode", &opcode) != B_OK) - return; - - if (message->FindString("interface", &name) != B_OK - && message->FindString("device", &name) != B_OK) - return; - - if (strcmp(name, "loop") == 0) - return; - - InterfaceListItem* item = FindItem(name); - if (item == NULL) - printf("InterfaceListItem %s not found!\n", name); - - switch (opcode) { - case B_NETWORK_INTERFACE_CHANGED: - case B_NETWORK_DEVICE_LINK_CHANGED: - if (item != NULL) { - // Make sure the item reflects the current state - item->GetSettings()->ReadConfiguration(); - InvalidateItem(IndexOf(item)); - } - break; - - case B_NETWORK_INTERFACE_ADDED: - if (item != NULL) - InvalidateItem(IndexOf(item)); - else - AddItem(new InterfaceListItem(name)); - break; - - case B_NETWORK_INTERFACE_REMOVED: - if (item != NULL) { - RemoveItem(item); - delete item; - } - break; - } -} diff --git a/src/preferences/network/InterfacesAddOn/InterfacesListView.h b/src/preferences/network/InterfacesAddOn/InterfacesListView.h deleted file mode 100644 index baceaded28..0000000000 --- a/src/preferences/network/InterfacesAddOn/InterfacesListView.h +++ /dev/null @@ -1,92 +0,0 @@ -/* - * Copyright 2004-2013 Haiku, Inc. All rights reserved. - * Distributed under the terms of the MIT License. - * - * Authors: - * Alexander von Gluck, kallisti5@unixzen.com - * Philippe Houdoin - * Fredrik Modéen - * John Scipione, jscipione@gmail.com - */ -#ifndef INTERFACES_LIST_VIEW_H -#define INTERFACES_LIST_VIEW_H - - -#include -#include - -#include "NetworkSettings.h" - - -class BBitmap; -class BMenuItem; -class BNetworkInterface; -class BPoint; -class BPopUpMenu; -class BSeparatorItem; -class BString; - -class InterfaceListItem : public BListItem { -public: - InterfaceListItem(const char* name); - ~InterfaceListItem(); - - void DrawItem(BView* owner, - BRect bounds, bool complete); - void Update(BView* owner, const BFont* font); - - inline const char* Name() {return fInterface.Name();} - inline bool IsDisabled() { - return fSettings->IsDisabled();} - inline void SetDisabled(bool disable) { - fSettings->SetDisabled(disable);} - inline NetworkSettings* GetSettings() {return fSettings;} - -private: - void _Init(); - void _PopulateBitmaps(const char* mediaType); - - BBitmap* fIcon; - BBitmap* fIconOffline; - BBitmap* fIconPending; - BBitmap* fIconOnline; - - NetworkSettings* fSettings; - // Interface configuration - - BNetworkInterface fInterface; - // Hardware Interface - - float fFirstlineOffset; - float fSecondlineOffset; - float fThirdlineOffset; -}; - - -class InterfacesListView : public BListView { -public: - InterfacesListView(const char* name); - virtual ~InterfacesListView(); - - InterfaceListItem* FindItem(const char* name); - InterfaceListItem* FindItem(BPoint where); - status_t SaveItems(BString& settingsData); - -protected: - virtual void AttachedToWindow(); - virtual void DetachedFromWindow(); - - virtual void MessageReceived(BMessage* message); - virtual void MouseDown(BPoint where); - -private: - // Context menu - BPopUpMenu* fContextMenu; - - status_t _InitList(); - status_t _UpdateList(); - void _HandleNetworkMessage(BMessage* message); -}; - - -#endif // INTERFACES_LIST_VIEW_H diff --git a/src/preferences/network/InterfacesAddOn/NetworkSettings.cpp b/src/preferences/network/InterfacesAddOn/NetworkSettings.cpp deleted file mode 100644 index 5330062849..0000000000 --- a/src/preferences/network/InterfacesAddOn/NetworkSettings.cpp +++ /dev/null @@ -1,459 +0,0 @@ -/* - * Copyright 2004-2013 Haiku, Inc. All rights reserved. - * Distributed under the terms of the MIT License. - * - * Authors: - * Axel Dörfler, axeld@pinc-software.de. - * Andre Alves Garzia, andre@andregarzia.com - * Alexander von Gluck, kallisti5@unixzen.com - * John Scipione, jscipione@gmail.com - * Vegard Wærp, vegarwa@online.no - */ - - -#include "NetworkSettings.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include - - -NetworkSettings::NetworkSettings(const char* name) - : - fDisabled(false), - fNameServers(5, true) -{ - fName = name; - _DetectProtocols(); - - fNetworkDevice = new BNetworkDevice(fName); - fNetworkInterface = new BNetworkInterface(fName); - - ReadConfiguration(); -} - - -NetworkSettings::~NetworkSettings() -{ - unsigned int index; - for (index = 0; index < MAX_PROTOCOLS; index++) - { - int socket_id = fProtocols[index].socket_id; - if (socket_id < 0) - continue; - - close(socket_id); - } - - delete fNetworkInterface; -} - - -status_t -NetworkSettings::_DetectProtocols() -{ - for (int index = 0; index < MAX_PROTOCOLS; index++) { - fProtocols[index].name = NULL; - fProtocols[index].present = false; - fProtocols[index].socket_id = -1; - fProtocols[index].inet_id = -1; - } - - // First we populate the struct with the possible - // protocols we could configure for an interface - // (size limit of MAX_PROTOCOLS) - fProtocols[0].name = "IPv4"; - fProtocols[0].inet_id = AF_INET; - fProtocols[1].name = "IPv6"; - fProtocols[1].inet_id = AF_INET6; - - // Loop through each of the possible protocols and - // ensure they are functional - for (int index = 0; index < MAX_PROTOCOLS; index++) { - int inet_id = fProtocols[index].inet_id; - if (inet_id > 0) - { - fProtocols[index].socket_id = socket(inet_id, SOCK_DGRAM, 0); - if (fProtocols[index].socket_id < 0) { - printf("Protocol %s : not present\n", fProtocols[index].name); - fProtocols[index].present = false; - } else { - printf("Protocol %s : present\n", fProtocols[index].name); - fProtocols[index].present = true; - } - } - } - - return B_OK; -} - - -// -- Interface address read code - - -/*! ReadConfiguration pulls the current interface settings - from the interfaces via BNetworkInterface and friends - and populates this classes private settings BAddresses - with them. -*/ -void -NetworkSettings::ReadConfiguration() -{ - fDisabled = (fNetworkInterface->Flags() & IFF_UP) == 0; - - for (int index = 0; index < MAX_PROTOCOLS; index++) { - int inet_id = fProtocols[index].inet_id; - - if (fProtocols[index].present) { - // --- Obtain IP Addresses - int32 zeroAddr = fNetworkInterface->FindFirstAddress(inet_id); - if (zeroAddr >= 0) { - fNetworkInterface->GetAddressAt(zeroAddr, - fInterfaceAddressMap[inet_id]); - fAddress[inet_id].SetTo( - fInterfaceAddressMap[inet_id].Address()); - fNetmask[inet_id].SetTo( - fInterfaceAddressMap[inet_id].Mask()); - } - - // --- Obtain gateway - // TODO : maybe in the future no ioctls? - ifconf config; - config.ifc_len = sizeof(config.ifc_value); - // Populate config with size of routing table - if (ioctl(fProtocols[index].socket_id, SIOCGRTSIZE, - &config, sizeof(config)) < 0) - return; - - uint32 size = (uint32)config.ifc_value; - if (size == 0) - return; - - // Malloc a buffer the size of the routing table - void* buffer = malloc(size); - if (buffer == NULL) - return; - - MemoryDeleter bufferDeleter(buffer); - config.ifc_len = size; - config.ifc_buf = buffer; - - if (ioctl(fProtocols[index].socket_id, SIOCGRTTABLE, - &config, sizeof(config)) < 0) - return; - - ifreq* interface = (ifreq*)buffer; - ifreq* end = (ifreq*)((uint8*)buffer + size); - - while (interface < end) { - route_entry& route = interface->ifr_route; - - if ((route.flags & RTF_GATEWAY) != 0) { - if (inet_id == AF_INET) { - char addressOut[INET_ADDRSTRLEN]; - sockaddr_in* socketAddr - = (sockaddr_in*)route.gateway; - - inet_ntop(inet_id, &socketAddr->sin_addr, - addressOut, INET_ADDRSTRLEN); - - fGateway[inet_id].SetTo(addressOut); - - } else if (inet_id == AF_INET6) { - char addressOut[INET6_ADDRSTRLEN]; - sockaddr_in6* socketAddr - = (sockaddr_in6*)route.gateway; - - inet_ntop(inet_id, &socketAddr->sin6_addr, - addressOut, INET6_ADDRSTRLEN); - - fGateway[inet_id].SetTo(addressOut); - - } else { - printf("Cannot pull routes for unknown protocol: %d\n", - inet_id); - fGateway[inet_id].SetTo(""); - } - - } - - 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); - } - - // --- Obtain selfconfiguration options - // TODO : This needs to be determined by protocol flags - // AutoConfiguration on the IP level doesn't exist yet - // ( fInterfaceAddressMap[AF_INET].Flags() ) - if (fProtocols[index].socket_id >= 0) { - fAutoConfigure[inet_id] = (fNetworkInterface->Flags() - & (IFF_AUTO_CONFIGURED | IFF_CONFIGURING)) != 0; - } - } - } - - // Read wireless network from interfaces - fWirelessNetwork.SetTo(NULL); - - BPath path; - find_directory(B_SYSTEM_SETTINGS_DIRECTORY, &path); - path.Append("network"); - path.Append("interfaces"); - - void* handle = load_driver_settings(path.Path()); - if (handle != NULL) { - const driver_settings* settings = get_driver_settings(handle); - if (settings != NULL) { - for (int32 i = 0; i < settings->parameter_count; i++) { - driver_parameter& top = settings->parameters[i]; - if (!strcmp(top.name, "interface")) { - // The name of the interface can either be the value of - // the "interface" parameter, or a separate "name" parameter - const char* name = NULL; - if (top.value_count > 0) { - name = top.values[0]; - if (fName != name) - continue; - } - - // search "network" parameter - for (int32 j = 0; j < top.parameter_count; j++) { - driver_parameter& sub = top.parameters[j]; - if (name == NULL && !strcmp(sub.name, "name") - && sub.value_count > 0) { - name = sub.values[0]; - if (fName != sub.values[0]) - break; - } - - if (!strcmp(sub.name, "network") - && sub.value_count > 0) { - fWirelessNetwork.SetTo(sub.values[0]); - break; - } - } - - // We found our interface - if (fName == name) - break; - } - } - } - unload_driver_settings(handle); - } - - // 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]; - } -} - - -/*! SetConfiguration sets this classes current BNetworkAddress settings - to the interface directly via BNetworkInterface and friends -*/ -void -NetworkSettings::SetConfiguration() -{ - printf("Setting %s\n", Name()); - - fNetworkDevice->JoinNetwork(WirelessNetwork()); - - for (int index = 0; index < MAX_PROTOCOLS; index++) { - int inet_id = fProtocols[index].inet_id; - if (fProtocols[index].present) { - int32 zeroAddr = fNetworkInterface->FindFirstAddress(inet_id); - if (zeroAddr >= 0) { - BNetworkInterfaceAddress interfaceConfig; - fNetworkInterface->GetAddressAt(zeroAddr, - interfaceConfig); - interfaceConfig.SetAddress(fAddress[inet_id]); - interfaceConfig.SetMask(fNetmask[inet_id]); - fNetworkInterface->SetAddress(interfaceConfig); - } else { - // TODO : test this case (no address set for this protocol) - printf("no zeroAddr found for %s(%d), found %" B_PRIu32 "\n", - fProtocols[index].name, inet_id, zeroAddr); - BNetworkInterfaceAddress interfaceConfig; - interfaceConfig.SetAddress(fAddress[inet_id]); - interfaceConfig.SetMask(fNetmask[inet_id]); - fNetworkInterface->AddAddress(interfaceConfig); - } - - // FIXME these flags shouldn't be interface-global, but specific to - // each protocol. Only set them for AF_INET otherwise there is - // confusion and freezes. - if (fProtocols[index].inet_id == AF_INET) { - int32 flags = fNetworkInterface->Flags(); - if (AutoConfigure(fProtocols[index].inet_id)) { - flags |= IFF_AUTO_CONFIGURED; - fNetworkInterface->SetFlags(flags); - fNetworkInterface->AutoConfigure(fProtocols[index].inet_id); - } else { - flags &= ~(IFF_AUTO_CONFIGURED | IFF_CONFIGURING); - fNetworkInterface->SetFlags(flags); - } - } - } - } -} - - -/*! LoadConfiguration reads the current interface configuration - file that NetServer looks for and populates this class with it -*/ -void -NetworkSettings::LoadConfiguration() -{ - -} - - -/*! GenerateConfiguration reads this classes settings and write them to - a BString. This can then be put in the interfaces settings file to make - the settings persistant. -*/ -BString -NetworkSettings::GenerateConfiguration() -{ - // TODO should use ProtocolVersions to know which protocols are there - bool autoConfigure = true; - if (!IsDisabled()) { - for (int i = 0; i < MAX_PROTOCOLS; i++) { - if (fProtocols[i].present && strlen(IP(fProtocols[i].inet_id)) != 0 - && !AutoConfigure(fProtocols[i].inet_id)) { - autoConfigure = false; - } - } - } - - // If the interface is fully autoconfigured, don't include it in the - // settings file. - if (autoConfigure) - return BString(); - - BString result; - - result.SetToFormat("interface %s {\n", Name()); - - if (IsDisabled()) - result << "\tdisabled\ttrue\n"; - else { - for (int i = 0; i < MAX_PROTOCOLS; i++) { - if (fProtocols[i].present && strlen(IP(fProtocols[i].inet_id)) != 0 - && !AutoConfigure(fProtocols[i].inet_id)) { - result << "\taddress {\n"; - - result << "\t\tfamily\t"; - if (fProtocols[i].inet_id == AF_INET) - result << "inet\n"; - else if (fProtocols[i].inet_id == AF_INET6) - result << "inet6\n"; - else { - // FIXME the struct protocols should know the name to use. - debugger("Unknown protocol found!"); - } - - result << "\t\taddress\t" << IP(fProtocols[i].inet_id) << "\n"; - result << "\t\tgateway\t" << Gateway(fProtocols[i].inet_id) - << "\n"; - result << "\t\tmask\t" << Netmask(fProtocols[i].inet_id) - << "\n"; - result << "\t}\n"; - } - } - } - -#if 0 - if (!networks.empty()) { - for (size_t index = 0; index < networks.size(); index++) { - wireless_network& network = networks[index]; - fprintf(fp, "\tnetwork %s {\n", network.name); - fprintf(fp, "\t\tmac\t%s\n", - network.address.ToString().String()); - fprintf(fp, "\t}\n"); - } - } -#endif - - result << "}\n\n"; - - return result; -} - - -/*! RenegotiateAddresses performs a address renegotiation in an attempt to fix - connectivity problems -*/ -status_t -NetworkSettings::RenegotiateAddresses() -{ - for (int index = 0; index < MAX_PROTOCOLS; index++) { - int inet_id = fProtocols[index].inet_id; - if (fProtocols[index].present - && AutoConfigure(inet_id)) { - // If protocol is active, and set to auto - fNetworkInterface->AutoConfigure(inet_id); - // Perform AutoConfiguration - } - } - - return B_OK; -} - - -const char* -NetworkSettings::HardwareAddress() -{ - BNetworkAddress macAddress; - if (fNetworkInterface->GetHardwareAddress(macAddress) == B_OK) - return macAddress.ToString(); - - return NULL; -} - - -status_t -NetworkSettings::GetNextAssociatedNetwork(uint32& cookie, - BNetworkAddress& address) -{ - return fNetworkDevice->GetNextAssociatedNetwork(cookie, address); -} - - -status_t -NetworkSettings::GetNextNetwork(uint32& cookie, wireless_network& network) -{ - return fNetworkDevice->GetNextNetwork(cookie, network); -} - diff --git a/src/preferences/network/InterfacesAddOn/NetworkSettings.h b/src/preferences/network/InterfacesAddOn/NetworkSettings.h deleted file mode 100644 index 4ba77cf2d0..0000000000 --- a/src/preferences/network/InterfacesAddOn/NetworkSettings.h +++ /dev/null @@ -1,141 +0,0 @@ -/* - * Copyright 2004-2013 Haiku, Inc. All rights reserved. - * Distributed under the terms of the MIT License. - * - * Authors: - * Andre Alves Garzia, andre@andregarzia.com - * Alexander von Gluck, kallisti5@unixzen.com - * John Scipione, jscipione@gmail.com - * Vegard Wærp, vegarwa@online.no - */ -#ifndef SETTINGS_H -#define SETTINGS_H - - -#include - -#include -#include -#include - - -#define MAX_PROTOCOLS 7 - // maximum number of protocols that could be configured - - -typedef std::map AddressMap; -typedef std::map InterfaceAddressMap; -typedef std::map BoolMap; - -typedef struct _protocols { - const char* name; - // Eg. "IPv4", used for user descriptions - bool present; - // Does the OS have the proper addon? - int inet_id; - // Eg. AF_INET - int socket_id; - // Socket ID used for ioctls to this proto -} protocols; - - -class BString; - -class NetworkSettings { -public: - NetworkSettings(const char* name); - virtual ~NetworkSettings(); - - protocols* ProtocolVersions() - { return fProtocols; } - - void SetIP(int family, const char* ip) - { fAddress[family].SetTo(family, ip); } - void SetNetmask(int family, const char* mask) - { fNetmask[family].SetTo(family, mask); } - void SetGateway(int family, const char* ip) - { fGateway[family].SetTo(family, ip); } - void SetAutoConfigure(int family, bool autoConf) - { fAutoConfigure[family] = autoConf; } - - void SetDisabled(bool disabled) - { fDisabled = disabled; } - - void SetWirelessNetwork(const char* name) - { fWirelessNetwork.SetTo(name); } -// void SetDomain(const BString& domain) -// { fDomain = domain; } - - bool AutoConfigure(int family) - { return fAutoConfigure[family]; } - BNetworkAddress IPAddr(int family) - { return fAddress[family]; } - const char* IP(int family) - { return fAddress[family].ToString(); } - const char* Netmask(int family) - { return fNetmask[family].ToString(); } - const char* Gateway(int family) - { return fGateway[family].ToString(); } - int32 PrefixLen(int family) - { return fNetmask[family].PrefixLength(); } - - status_t RenegotiateAddresses(); - - const char* Name() { return fName.String(); } - const char* Domain() { return fDomain.String(); } - status_t Stats(ifreq_stats* ptr) - { return fNetworkInterface->GetStats(*ptr); } - - bool IsDisabled() { return fDisabled; } - - bool IsWireless() { - return fNetworkDevice->IsWireless(); } - bool IsEthernet() { - return fNetworkDevice->IsEthernet(); } - bool HasLink() { - return fNetworkDevice->HasLink(); } - - const char* HardwareAddress(); - - const BString& WirelessNetwork() { return fWirelessNetwork; } - status_t GetNextAssociatedNetwork(uint32& cookie, - BNetworkAddress& address); - status_t GetNextNetwork(uint32& cookie, - wireless_network& network); - - BObjectList& NameServers() { return fNameServers; } - - /* Grab and write interface settings directly from interface */ - void ReadConfiguration(); - void SetConfiguration(); - - /* Grab interface settings from interface configuration file */ - void LoadConfiguration(); - /* Generate settings file entry suitable for interfaces settings */ - BString GenerateConfiguration(); - -private: - status_t _DetectProtocols(); - - // Stored network addresses and paramaters - BoolMap fAutoConfigure; - AddressMap fAddress; - AddressMap fNetmask; - AddressMap fGateway; - - BString fName; - BString fDomain; - bool fDisabled; - BObjectList fNameServers; - BString fWirelessNetwork; - - protocols fProtocols[MAX_PROTOCOLS]; - - BNetworkInterface* fNetworkInterface; - BNetworkDevice* fNetworkDevice; - - InterfaceAddressMap fInterfaceAddressMap; -}; - - -#endif // SETTINGS_H diff --git a/src/preferences/network/InterfacesAddOn/StatusAddOn.cpp b/src/preferences/network/InterfacesAddOn/StatusAddOn.cpp deleted file mode 100644 index 925712c2c2..0000000000 --- a/src/preferences/network/InterfacesAddOn/StatusAddOn.cpp +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright 2004-2011 Haiku, Inc. All rights reserved. - * Distributed under the terms of the MIT License. - * - */ - - -#include "NetworkSetupAddOn.h" - -#include - - -class StatusAddOn : public NetworkSetupAddOn { - public: - const char * Name(); -}; - - -const char * StatusAddOn::Name() -{ - return "Status"; -} - - -NetworkSetupAddOn * get_addon() -{ - return new StatusAddOn(); -}