Switched to new BNetwork* classes.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@40239 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Philippe Houdoin 2011-01-17 11:21:12 +00:00
parent 137c5fe2ba
commit 28b7d1d410
10 changed files with 132 additions and 910 deletions

View File

@ -1,461 +0,0 @@
/*
* Copyright 2004-2009, Haiku, Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Andre Alves Garzia, andre@andregarzia.com
* Stephan Assmuß
* Axel Dörfler
* Fredrik Modéen
* Hugo Santos
* Philippe Saint-Pierre
*/
#include "EthernetSettingsView.h"
#include "Setting.h"
#include <InterfaceKit.h>
#include <GridView.h>
#include <GroupView.h>
#include <LayoutItem.h>
#include <SpaceLayoutItem.h>
#include <File.h>
#include <Path.h>
#include <Directory.h>
#include <FindDirectory.h>
#include <errno.h>
#include <stdio.h>
#include <NetServer.h>
#include <support/Beep.h>
static const uint32 kMsgApply = 'aply';
static const uint32 kMsgRevert = 'rvrt';
static const uint32 kMsgClose = 'clse';
static const uint32 kMsgField = 'fild';
static const uint32 kMsgMode = 'mode';
static const uint32 kMsgChange = 'chng';
static void
SetupTextControl(BTextControl *control)
{
// TODO: Disallow characters, etc.
// Would be nice to have a real
// formatted input control
control->SetModificationMessage(new BMessage(kMsgChange));
}
// #pragma mark -
EthernetSettingsView::EthernetSettingsView(Setting* setting)
: BView("EthernetSettingsView", 0, NULL),
fCurrentSettings(setting)
{
SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
// build the GUI
BGroupLayout* rootLayout = new BGroupLayout(B_VERTICAL);
SetLayout(rootLayout);
BGridView* controlsGroup = new BGridView();
BGridLayout* layout = controlsGroup->GridLayout();
// insets
float inset = ceilf(be_plain_font->Size() * 0.7);
rootLayout->SetInsets(inset, inset, inset, inset);
rootLayout->SetSpacing(inset);
layout->SetSpacing(inset, inset);
BPopUpMenu* modeMenu = new BPopUpMenu("modes");
modeMenu->AddItem(new BMenuItem("Static", new BMessage(kMsgMode)));
modeMenu->AddItem(new BMenuItem("DHCP", new BMessage(kMsgMode)));
fTypeMenuField = new BMenuField("Mode:", modeMenu);
layout->AddItem(fTypeMenuField->CreateLabelLayoutItem(), 0, 1);
layout->AddItem(fTypeMenuField->CreateMenuBarLayoutItem(), 1, 1);
fIPTextControl = new BTextControl("IP Address:", "", NULL);
SetupTextControl(fIPTextControl);
BLayoutItem* layoutItem = fIPTextControl->CreateTextViewLayoutItem();
layoutItem->SetExplicitMinSize(BSize(
fIPTextControl->StringWidth("XXX.XXX.XXX.XXX") + inset,
B_SIZE_UNSET));
layout->AddItem(fIPTextControl->CreateLabelLayoutItem(), 0, 2);
layout->AddItem(layoutItem, 1, 2);
fNetMaskTextControl = new BTextControl("Netmask:", "", NULL);
SetupTextControl(fNetMaskTextControl);
layout->AddItem(fNetMaskTextControl->CreateLabelLayoutItem(), 0, 3);
layout->AddItem(fNetMaskTextControl->CreateTextViewLayoutItem(), 1, 3);
fGatewayTextControl = new BTextControl("Gateway:", "", NULL);
SetupTextControl(fGatewayTextControl);
layout->AddItem(fGatewayTextControl->CreateLabelLayoutItem(), 0, 4);
layout->AddItem(fGatewayTextControl->CreateTextViewLayoutItem(), 1, 4);
// TODO: Replace the DNS text controls by a BListView with add/remove
// functionality and so on...
fPrimaryDNSTextControl = new BTextControl("DNS #1:", "", NULL);
SetupTextControl(fPrimaryDNSTextControl);
layout->AddItem(fPrimaryDNSTextControl->CreateLabelLayoutItem(), 0, 5);
layout->AddItem(fPrimaryDNSTextControl->CreateTextViewLayoutItem(), 1, 5);
fSecondaryDNSTextControl = new BTextControl("DNS #2:", "", NULL);
SetupTextControl(fSecondaryDNSTextControl);
layout->AddItem(fSecondaryDNSTextControl->CreateLabelLayoutItem(), 0, 6);
layout->AddItem(fSecondaryDNSTextControl->CreateTextViewLayoutItem(), 1, 6);
fErrorMessage = new BStringView("error", "");
fErrorMessage->SetAlignment(B_ALIGN_LEFT);
fErrorMessage->SetFont(be_bold_font);
fErrorMessage->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET));
layout->AddView(fErrorMessage, 1, 7);
// button group (TODO: move to window, but take care of
// enabling/disabling)
BGroupView* buttonGroup = new BGroupView(B_HORIZONTAL);
fRevertButton = new BButton("Revert", new BMessage(kMsgRevert));
fRevertButton->SetEnabled(false);
buttonGroup->GroupLayout()->AddView(fRevertButton);
buttonGroup->GroupLayout()->AddItem(BSpaceLayoutItem::CreateGlue());
fApplyButton = new BButton("Apply", new BMessage(kMsgApply));
buttonGroup->GroupLayout()->AddView(fApplyButton);
rootLayout->AddView(controlsGroup);
rootLayout->AddView(buttonGroup);
}
EthernetSettingsView::~EthernetSettingsView()
{
close(fSocket);
}
bool
EthernetSettingsView::_PrepareRequest(struct ifreq& request, const char* name)
{
// This function is used for talking direct to the stack.
// It's used by _ShowConfiguration.
if (strlen(name) > IF_NAMESIZE)
return false;
strcpy(request.ifr_name, name);
return true;
}
void
EthernetSettingsView::AttachedToWindow()
{
fApplyButton->SetTarget(this);
fRevertButton->SetTarget(this);
fIPTextControl->SetTarget(this);
fNetMaskTextControl->SetTarget(this);
fGatewayTextControl->SetTarget(this);
fPrimaryDNSTextControl->SetTarget(this);
fSecondaryDNSTextControl->SetTarget(this);
fTypeMenuField->Menu()->SetTargetForItems(this);
// display settigs of first adapter on startup
_ShowConfiguration(fCurrentSettings);
}
void
EthernetSettingsView::DetachedFromWindow()
{
}
void
EthernetSettingsView::_ShowConfiguration(Setting* setting)
{
// Clear the inputs.
fIPTextControl->SetText("");
fGatewayTextControl->SetText("");
fNetMaskTextControl->SetText("");
fPrimaryDNSTextControl->SetText("");
fSecondaryDNSTextControl->SetText("");
bool enableControls = false;
fTypeMenuField->SetEnabled(setting != NULL);
if (setting) {
fIPTextControl->SetText(setting->IP());
fGatewayTextControl->SetText(setting->Gateway());
fNetMaskTextControl->SetText(setting->Netmask());
BMenuItem* item;
if (setting->AutoConfigured() == true)
item = fTypeMenuField->Menu()->FindItem("DHCP");
else
item = fTypeMenuField->Menu()->FindItem("Static");
if (item)
item->SetMarked(true);
enableControls = setting->AutoConfigured() == false;
if (setting->fNameservers.CountItems() >= 2) {
fSecondaryDNSTextControl->SetText(
setting->fNameservers.ItemAt(1)->String());
}
if (setting->fNameservers.CountItems() >= 1) {
fPrimaryDNSTextControl->SetText(
setting->fNameservers.ItemAt(0)->String());
}
}
//We don't want to enable loop
if (strcmp(fCurrentSettings->Name(), "loop") == 0) {
_EnableTextControls(false);
fTypeMenuField->SetEnabled(false);
} else
_EnableTextControls(enableControls);
}
void
EthernetSettingsView::_EnableTextControls(bool enable)
{
fIPTextControl->SetEnabled(enable);
fGatewayTextControl->SetEnabled(enable);
fNetMaskTextControl->SetEnabled(enable);
fPrimaryDNSTextControl->SetEnabled(enable);
fSecondaryDNSTextControl->SetEnabled(enable);
}
void
EthernetSettingsView::_ApplyControlsToConfiguration()
{
if (!fCurrentSettings)
return;
fCurrentSettings->SetIP(fIPTextControl->Text());
fCurrentSettings->SetNetmask(fNetMaskTextControl->Text());
fCurrentSettings->SetGateway(fGatewayTextControl->Text());
fCurrentSettings->SetAutoConfigure(
strcmp(fTypeMenuField->Menu()->FindMarked()->Label(), "DHCP") == 0);
fCurrentSettings->fNameservers.MakeEmpty();
fCurrentSettings->fNameservers.AddItem(new BString(
fPrimaryDNSTextControl->Text()));
fCurrentSettings->fNameservers.AddItem(new BString(
fSecondaryDNSTextControl->Text()));
fApplyButton->SetEnabled(false);
fRevertButton->SetEnabled(true);
}
void
EthernetSettingsView::_SaveConfiguration()
{
_ApplyControlsToConfiguration();
_SaveDNSConfiguration();
_SaveAdaptersConfiguration();
if (fCurrentSettings->AutoConfigured())
_TriggerAutoConfig(fCurrentSettings->Name());
}
void
EthernetSettingsView::_SaveDNSConfiguration()
{
BPath path;
if (find_directory(B_COMMON_SETTINGS_DIRECTORY, &path) != B_OK)
return;
path.Append("network/resolv.conf");
BFile file(path.Path(), B_CREATE_FILE | B_ERASE_FILE | B_WRITE_ONLY);
if (file.InitCheck() != B_OK) {
fprintf(stderr, "failed to open %s for writing: %s\n", path.Path(),
strerror(file.InitCheck()));
return;
}
BString content("# Generated by Network Preflet\n");
for (int j = 0; j < fCurrentSettings->fNameservers.CountItems(); j++) {
if (fCurrentSettings->fNameservers.ItemAt(j)->Length() > 0) {
content << "nameserver\t"
<< fCurrentSettings->fNameservers.ItemAt(j)->String() << "\n";
}
}
file.Write(content.String(), content.Length());
}
void
EthernetSettingsView::_SaveAdaptersConfiguration()
{
BPath path;
status_t status = _GetPath("interfaces", path);
printf("Path = %s\n", path.Path());
if (status < B_OK)
return;
FILE* fp = NULL;
if (fCurrentSettings->AutoConfigured())
return;
if (fp == NULL) {
fp = fopen(path.Path(), "w");
if (fp == NULL) {
fprintf(stderr, "failed to open file %s to write "
"configuration: %s\n", path.Path(), strerror(errno));
return;
}
}
fprintf(fp, "interface %s {\n\t\taddress {\n",fCurrentSettings->Name());
fprintf(fp, "\t\t\tfamily\tinet\n");
fprintf(fp, "\t\t\taddress\t%s\n",fCurrentSettings->IP());
fprintf(fp, "\t\t\tgateway\t%s\n",fCurrentSettings->Gateway());
fprintf(fp, "\t\t\tmask\t%s\n",fCurrentSettings->Netmask());
fprintf(fp, "\t\t}\n}\n\n");
if (fp) {
printf("%s saved.\n", path.Path());
fclose(fp);
} else {
// all configuration is DHCP, so delete interfaces file.
remove(path.Path());
}
}
status_t
EthernetSettingsView::_TriggerAutoConfig(const char* device)
{
BMessenger networkServer(kNetServerSignature);
if (!networkServer.IsValid()) {
(new BAlert("error", "The net_server needs to run for the auto "
"configuration!", "OK"))->Go();
return B_ERROR;
}
BMessage message(kMsgConfigureInterface);
message.AddString("device", device);
BMessage address;
address.AddString("family", "inet");
address.AddBool("auto_config", true);
message.AddMessage("address", &address);
BMessage reply;
status_t status = networkServer.SendMessage(&message, &reply);
if (status != B_OK) {
BString errorMessage("Sending auto-config message failed: ");
errorMessage << strerror(status);
(new BAlert("error", errorMessage.String(), "OK"))->Go();
return status;
} else if (reply.FindInt32("status", &status) == B_OK
&& status != B_OK) {
BString errorMessage("Auto-configuring failed: ");
errorMessage << strerror(status);
(new BAlert("error", errorMessage.String(), "OK"))->Go();
return status;
}
return B_OK;
}
status_t
EthernetSettingsView::_GetPath(const char* name, BPath& path)
{
if (find_directory(B_COMMON_SETTINGS_DIRECTORY, &path, true) != B_OK)
return B_ERROR;
path.Append("network");
create_directory(path.Path(), 0755);
if (name != NULL)
path.Append(name);
return B_OK;
}
bool
MatchPattern(const char* string, const char* pattern)
{
regex_t compiled;
bool result = regcomp(&compiled, pattern, REG_NOSUB | REG_EXTENDED) == 0
&& regexec(&compiled, string, 0, NULL, 0) == 0;
regfree(&compiled);
return result;
}
bool
EthernetSettingsView::_ValidateControl(BTextControl* control)
{
static const char* pattern = "^(25[0-5]|2[0-4][0-9]|[01][0-9]{2}|[0-9]"
"{1,2})(\\.(25[0-5]|2[0-4][0-9]|[01][0-9]{2}|[0-9]{1,2})){3}$";
if (control->IsEnabled() && !MatchPattern(control->Text(), pattern)) {
control->MakeFocus();
BString errorMessage;
errorMessage << control->Label();
errorMessage.RemoveLast(":");
errorMessage << " is invalid";
fErrorMessage->SetText(errorMessage.String());
beep();
return false;
}
return true;
}
void
EthernetSettingsView::MessageReceived(BMessage* message)
{
switch (message->what) {
case kMsgMode:
if (BMenuItem* item = fTypeMenuField->Menu()->FindMarked())
_EnableTextControls(strcmp(item->Label(), "DHCP") != 0);
fApplyButton->SetEnabled(true);
fRevertButton->SetEnabled(true);
break;
case kMsgRevert:
_ShowConfiguration(fCurrentSettings);
fRevertButton->SetEnabled(false);
break;
case kMsgApply:
if (_ValidateControl(fIPTextControl)
&& _ValidateControl(fNetMaskTextControl)
&& (strlen(fGatewayTextControl->Text()) == 0
|| _ValidateControl(fGatewayTextControl))
&& (strlen(fPrimaryDNSTextControl->Text()) == 0
|| _ValidateControl(fPrimaryDNSTextControl))
&& (strlen(fSecondaryDNSTextControl->Text()) == 0
|| _ValidateControl(fSecondaryDNSTextControl)))
_SaveConfiguration();
break;
case kMsgChange:
fErrorMessage->SetText("");
//We don't want to enable loop
if (strcmp(fCurrentSettings->Name(), "loop") == 0)
fApplyButton->SetEnabled(false);
else
fApplyButton->SetEnabled(true);
break;
default:
BView::MessageReceived(message);
}
}

View File

@ -1,78 +0,0 @@
/*
* Copyright 2004-2007 Haiku Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*
* Author:
* Andre Alves Garzia, andre@andregarzia.com
* Axel Dörfler
* Fredrik Modéen
* Hugo Santos
*/
#ifndef ETHERNET_SETTINGS_VIEW_H
#define ETHERNET_SETTINGS_VIEW_H
#include "Setting.h"
#include <ObjectList.h>
#include <View.h>
#include <posix/regex.h>
class BButton;
class BMenuField;
class BPath;
class BTextControl;
class BStringView;
class EthernetSettingsView : public BView {
public:
EthernetSettingsView(Setting* setting);
virtual ~EthernetSettingsView();
virtual void MessageReceived(BMessage* message);
virtual void AttachedToWindow();
virtual void DetachedFromWindow();
void SaveProfile(BString profileName);
void LoadProfile(BString profileName);
private:
bool _PrepareRequest(struct ifreq& request,
const char* name);
void _ShowConfiguration(Setting* setting);
void _EnableTextControls(bool enable);
void _SaveConfiguration();
void _SaveDNSConfiguration();
void _SaveAdaptersConfiguration();
void _ApplyControlsToConfiguration();
status_t _GetPath(const char* name, BPath& path);
status_t _TriggerAutoConfig(const char* device);
bool _ValidateControl(BTextControl* control);
private:
BButton* fApplyButton;
BButton* fRevertButton;
BMenuField* fDeviceMenuField;
BMenuField* fTypeMenuField;
BTextControl* fIPTextControl;
BTextControl* fNetMaskTextControl;
BTextControl* fGatewayTextControl;
BTextControl* fPrimaryDNSTextControl;
BTextControl* fSecondaryDNSTextControl;
BStringView* fErrorMessage;
// TODO: DNS settings do not belong here, do they?
BObjectList<BString> fInterfaces;
Setting* fCurrentSettings;
int32 fStatus;
int fSocket;
};
#endif /* ETHERNET_SETTINGS_VIEW_H */

View File

@ -61,14 +61,14 @@ InterfacesAddOn::CreateView(BRect *bounds)
{
float w, h;
BRect r = *bounds;
#define H_MARGIN 10
#define V_MARGIN 10
#define SMALL_MARGIN 3
if (r.Width() < 100 || r.Height() < 100)
r.Set(0, 0, 100, 100);
ResizeTo(r.Width(), r.Height());
BRect rlv = r;
@ -77,14 +77,14 @@ InterfacesAddOn::CreateView(BRect *bounds)
rlv.right -= B_V_SCROLL_BAR_WIDTH;
fListview = new InterfacesListView(rlv, "interfaces", B_FOLLOW_ALL_SIDES);
fListview->SetSelectionMessage(new BMessage(INTERFACE_SELECTED_MSG));
fListview->SetInvocationMessage(new BMessage(CONFIGURE_INTERFACE_MSG));
AddChild(new BScrollView(NULL, fListview, B_FOLLOW_ALL_SIDES, B_WILL_DRAW
fListview->SetInvocationMessage(new BMessage(CONFIGURE_INTERFACE_MSG));
AddChild(new BScrollView(NULL, fListview, B_FOLLOW_ALL_SIDES, B_WILL_DRAW
| B_FRAME_EVENTS, false, true));
r.top = r.bottom - 60;
fConfigure = new BButton(r, "configure", "Configure" B_UTF8_ELLIPSIS,
fConfigure = new BButton(r, "configure", "Configure" B_UTF8_ELLIPSIS,
new BMessage(CONFIGURE_INTERFACE_MSG), B_FOLLOW_BOTTOM | B_FOLLOW_LEFT);
fConfigure->GetPreferredSize(&w, &h);
fConfigure->ResizeToPreferred();
fConfigure->SetEnabled(false);
@ -99,7 +99,7 @@ InterfacesAddOn::CreateView(BRect *bounds)
fOnOff->Hide();
AddChild(fOnOff);
*bounds = Bounds();
*bounds = Bounds();
return this;
}
@ -119,7 +119,7 @@ InterfacesAddOn::MessageReceived(BMessage* msg)
{
int nr = fListview->CurrentSelection();
InterfaceListItem *item = NULL;
if(nr != -1) {
if(nr != -1) {
item = dynamic_cast<InterfaceListItem*>(fListview->ItemAt(nr));
}
@ -131,26 +131,26 @@ InterfacesAddOn::MessageReceived(BMessage* msg)
fOnOff->Hide();
break;
}
fOnOff->SetLabel(item->Enabled() ? "Disable" : "Enable");
fOnOff->Show();
fOnOff->SetLabel(item->IsDisabled() ? "Enable" : "Disable");
fOnOff->Show();
break;
}
case CONFIGURE_INTERFACE_MSG: {
if (!item)
break;
NetworkWindow* nw = new NetworkWindow(item->GetSetting());
NetworkWindow* nw = new NetworkWindow(item->GetSettings());
nw->Show();
break;
}
case ONOFF_INTERFACE_MSG:
if (!item)
break;
item->SetEnabled(!item->Enabled());
fOnOff->SetLabel(item->Enabled() ? "Disable" : "Enable");
item->SetDisabled(!item->IsDisabled());
fOnOff->SetLabel(item->IsDisabled() ? "Enable" : "Disable");
fListview->Invalidate();
break;

View File

@ -9,7 +9,6 @@
#include "InterfacesListView.h"
#include "Setting.h"
#include <stdio.h>
#include <stdlib.h>
@ -22,42 +21,17 @@
#include <sys/socket.h>
#include <sys/sockio.h>
#include <IconUtils.h>
#include <File.h>
#include <IconUtils.h>
#include <net_notifications.h>
#include <NetworkDevice.h>
#include <NetworkInterface.h>
#include <NetworkRoster.h>
#include <Resources.h>
#include <AutoDeleter.h>
#include <net_notifications.h>
class SocketOpener {
public:
SocketOpener()
{
fSocket = socket(AF_INET, SOCK_DGRAM, 0);
}
~SocketOpener()
{
close(fSocket);
}
status_t InitCheck()
{
return fSocket >= 0 ? B_OK : B_ERROR;
}
operator int() const
{
return fSocket;
}
private:
int fSocket;
};
#include "Settings.h"
// #pragma mark -
@ -77,25 +51,30 @@ our_image(image_info& image)
}
// #pragma mark -
InterfaceListItem::InterfaceListItem(const char* name)
:
:
BListItem(0, false),
fIcon(NULL),
fSettings(new Setting(name))
fIcon(NULL)
{
_InitIcon();
fInterface.SetTo(name);
_Init();
}
InterfaceListItem::~InterfaceListItem()
{
delete fIcon;
delete fSettings;
}
void InterfaceListItem::Update(BView* owner, const BFont* font)
{
BListItem::Update(owner,font);
void
InterfaceListItem::Update(BView* owner, const BFont* font)
{
BListItem::Update(owner,font);
font_height height;
font->GetHeight(&height);
@ -110,21 +89,21 @@ InterfaceListItem::DrawItem(BView* owner, BRect /*bounds*/, bool complete)
BListView* list = dynamic_cast<BListView*>(owner);
if (!list)
return;
font_height height;
BFont font;
owner->GetFont(&font);
font.GetHeight(&height);
float fntheight = height.ascent+height.descent+height.leading;
BRect bounds = list->ItemFrame(list->IndexOf(this));
BRect bounds = list->ItemFrame(list->IndexOf(this));
rgb_color oldviewcolor = owner->ViewColor();
rgb_color oldlowcolor = owner->LowColor();
rgb_color oldcolor = owner->HighColor();
rgb_color color = oldviewcolor;
if ( IsSelected() )
if ( IsSelected() )
color = tint_color(color, B_HIGHLIGHT_BACKGROUND_TINT);
owner->SetViewColor( color );
@ -140,35 +119,35 @@ InterfaceListItem::DrawItem(BView* owner, BRect /*bounds*/, bool complete)
BPoint namePt = iconPt + BPoint(32+8, fntheight);
BPoint driverPt = iconPt + BPoint(32+8, fntheight*2);
BPoint commentPt = iconPt + BPoint(32+8, fntheight*3);
drawing_mode mode = owner->DrawingMode();
if (fSettings->Enabled())
owner->SetDrawingMode(B_OP_OVER);
else {
if (fSettings->IsDisabled()) {
owner->SetDrawingMode(B_OP_ALPHA);
owner->SetBlendingMode(B_CONSTANT_ALPHA, B_ALPHA_OVERLAY);
owner->SetHighColor(0, 0, 0, 32);
}
} else
owner->SetDrawingMode(B_OP_OVER);
owner->DrawBitmapAsync(fIcon, iconPt);
if (!fSettings->Enabled())
if (fSettings->IsDisabled())
owner->SetHighColor(tint_color(oldcolor, B_LIGHTEN_1_TINT));
owner->SetFont(be_bold_font);
owner->DrawString(Name(), namePt);
owner->SetFont(be_plain_font);
if (fSettings->Enabled()) {
if (fSettings->IsDisabled())
owner->DrawString("Disabled.", driverPt);
else {
BString str("Enabled, IPv4 address: ");
str << fSettings->IP();
owner->DrawString(str.String(), driverPt);
if (fSettings->AutoConfigured())
if (fSettings->AutoConfigure())
owner->DrawString("DHCP enabled", commentPt);
else
owner->DrawString("DHCP disabled, use static IP address", commentPt);
} else
owner->DrawString("Disabled.", driverPt);
}
owner->SetHighColor(oldcolor);
owner->SetDrawingMode(mode);
@ -176,12 +155,16 @@ InterfaceListItem::DrawItem(BView* owner, BRect /*bounds*/, bool complete)
void
InterfaceListItem::_InitIcon()
InterfaceListItem::_Init()
{
fSettings = new Settings(Name());
// Init icon
BBitmap* icon = NULL;
const char* mediaTypeName = "";
int media = fSettings->Media();
const char* mediaTypeName = NULL;
int media = fInterface.Media();
printf("%s media = 0x%x\n", Name(), media);
switch (IFM_TYPE(media)) {
case IFM_ETHER:
@ -190,6 +173,14 @@ InterfaceListItem::_InitIcon()
case IFM_IEEE80211:
mediaTypeName = "wifi";
break;
default: {
BNetworkDevice device(Name());
if (device.IsWireless())
mediaTypeName = "wifi";
else if (device.IsEthernet())
mediaTypeName = "ether";
break;
}
}
image_info info;
@ -207,12 +198,12 @@ InterfaceListItem::_InitIcon()
size_t size;
// Try specific interface icon?
const uint8* rawIcon = (const uint8*)resources.LoadResource(B_VECTOR_ICON_TYPE, Name(), &size);
if (!rawIcon)
if (rawIcon == NULL && mediaTypeName != NULL)
// Not found, try interface media type?
rawIcon = (const uint8*)resources.LoadResource(B_VECTOR_ICON_TYPE, mediaTypeName, &size);
if (!rawIcon)
if (rawIcon == NULL)
// Not found, try default interface icon?
rawIcon = (const uint8*)resources.LoadResource(B_VECTOR_ICON_TYPE, "wifi", &size);
rawIcon = (const uint8*)resources.LoadResource(B_VECTOR_ICON_TYPE, "ether", &size);
if (rawIcon) {
// Now build the bitmap
@ -244,7 +235,7 @@ InterfacesListView::AttachedToWindow()
BListView::AttachedToWindow();
_InitList();
start_watching_network(
B_WATCH_NETWORK_INTERFACE_CHANGES | B_WATCH_NETWORK_LINK_CHANGES, this);
}
@ -298,42 +289,16 @@ InterfacesListView::FindItem(const char* name)
status_t
InterfacesListView::_InitList()
{
SocketOpener socket;
if (socket.InitCheck() != B_OK)
return B_ERROR;
BNetworkRoster& roster = BNetworkRoster::Default();
BNetworkInterface interface;
uint32 cookie = 0;
// iterate over all interfaces and retrieve minimal status
ifconf config;
config.ifc_len = sizeof(config.ifc_value);
if (ioctl(socket, SIOCGIFCOUNT, &config, sizeof(struct ifconf)) < 0)
return B_ERROR;
uint32 count = (uint32)config.ifc_value;
if (count == 0)
return B_ERROR;
void* buffer = malloc(count * sizeof(struct ifreq));
if (buffer == NULL)
return B_ERROR;
MemoryDeleter deleter(buffer);
config.ifc_len = count * sizeof(struct ifreq);
config.ifc_buf = buffer;
if (ioctl(socket, SIOCGIFCONF, &config, sizeof(struct ifconf)) < 0)
return B_ERROR;
ifreq* interface = (ifreq*)buffer;
MakeEmpty();
for (uint32 i = 0; i < count; i++) {
if (strcmp(interface->ifr_name, "loop") != 0) {
AddItem(new InterfaceListItem(interface->ifr_name));
// printf("Name = %s\n", interface->ifr_name);
while (roster.GetNextInterface(&cookie, interface) == B_OK) {
if (strncmp(interface.Name(), "loop", 4) && interface.Name()[0]) {
AddItem(new InterfaceListItem(interface.Name()));
}
interface = (ifreq*)((addr_t)interface + IF_NAMESIZE
+ interface->ifr_addr.sa_len);
}
}
return B_OK;
}
@ -345,17 +310,17 @@ InterfacesListView::_UpdateList()
return B_OK;
}
void
void
InterfacesListView::_HandleNetworkMessage(BMessage* message)
{
const char* name;
int32 opcode;
message->PrintToStream();
if (message->FindInt32("opcode", &opcode) != B_OK)
return;
if (message->FindString("interface", &name) != B_OK
&& message->FindString("device", &name) != B_OK)
return;
@ -370,14 +335,14 @@ InterfacesListView::_HandleNetworkMessage(BMessage* message)
if (item)
InvalidateItem(IndexOf(item));
break;
case B_NETWORK_INTERFACE_ADDED:
if (item)
InvalidateItem(IndexOf(item));
else
AddItem(new InterfaceListItem(name));
break;
case B_NETWORK_INTERFACE_REMOVED:
if (item) {
RemoveItem(item);

View File

@ -11,17 +11,19 @@
#ifndef INTERFACES_LIST_VIEW_H
#define INTERFACES_LIST_VIEW_H
#include <String.h>
#include <ListView.h>
#include <ListItem.h>
#include <Bitmap.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <net/if_media.h>
#include <net/if_types.h>
#include "Setting.h"
#include <Bitmap.h>
#include <ListView.h>
#include <ListItem.h>
#include <NetworkDevice.h>
#include <NetworkInterface.h>
#include <String.h>
#include "Settings.h"
class InterfaceListItem : public BListItem {
public:
@ -30,17 +32,18 @@ public:
void DrawItem(BView* owner, BRect bounds, bool complete);
void Update(BView* owner, const BFont* font);
inline const char* Name() { return fSettings->Name(); }
inline bool Enabled() { return fSettings->Enabled(); }
inline void SetEnabled(bool enable){ fSettings->Enable(enable); }
inline Setting* GetSetting() { return fSettings; }
inline const char* Name() { return fInterface.Name(); }
inline bool IsDisabled() { return fSettings->IsDisabled(); }
inline void SetDisabled(bool disable){ fSettings->SetDisabled(disable); }
inline Settings* GetSettings() { return fSettings; }
private:
void _InitIcon();
void _Init();
BBitmap* fIcon;
Setting* fSettings;
BNetworkInterface fInterface;
Settings* fSettings;
};

View File

@ -1,26 +1,37 @@
SubDir HAIKU_TOP src tests kits net preflet InterfacesAddOn ;
SEARCH_SOURCE += [ FDirName $(HAIKU_TOP) src apps networkstatus ] ;
SEARCH_SOURCE += [ FDirName $(HAIKU_TOP) src preferences network ] ;
UseHeaders [ FDirName $(HAIKU_TOP) src tests kits net preflet ] ;
UseHeaders [ FDirName $(HAIKU_TOP) src servers net ] : true ;
UsePrivateHeaders net shared ;
UseLibraryHeaders agg icon ;
AddResources Interfaces :
InterfacesIcons.rdef
AddResources Interfaces :
InterfacesIcons.rdef
;
Addon Interfaces :
Addon Interfaces :
InterfacesAddOn.cpp
InterfacesListView.cpp
Setting.cpp
NetworkWindow.cpp
# from src/preferences/network
EthernetSettingsView.cpp
:
be
<nogrist>NetworkSetup
$(TARGET_NETWORK_LIBS)
$(TARGET_LIBSUPC++)
Settings.cpp
# from src/apps/networkstatus
RadioView.cpp
WirelessNetworkMenuItem.cpp
:
be
<nogrist>NetworkSetup
$(TARGET_NETWORK_LIBS)
libbnetapi.so
$(TARGET_LIBSUPC++)
$(HAIKU_LOCALE_LIBS)
libicon.a libagg.a
;

View File

@ -4,7 +4,7 @@
*
* Author:
* Andre Alves Garzia, andre@andregarzia.com
* Fredrik Modéen
* Fredrik Modéen
*/
#include "NetworkWindow.h"
@ -13,18 +13,19 @@
#include <GroupLayout.h>
#include "EthernetSettingsView.h"
#include "Settings.h"
NetworkWindow::NetworkWindow(Setting* setting)
NetworkWindow::NetworkWindow(Settings* settings)
: BWindow(BRect(50, 50, 269, 302), "Network", B_TITLED_WINDOW,
B_NOT_RESIZABLE | B_ASYNCHRONOUS_CONTROLS | B_NOT_ZOOMABLE
| B_AUTO_UPDATE_SIZE_LIMITS)
{
SetLayout(new BGroupLayout(B_HORIZONTAL));
fEthernetView = new EthernetSettingsView(setting);
fEthernetView = new EthernetSettingsView(); // settings);
GetLayout()->AddView(fEthernetView);
SetTitle(setting->Name());
SetTitle(settings->Name());
}

View File

@ -4,7 +4,7 @@
*
* Author:
* Andre Alves Garzia, andre@andregarzia.com
* Fredrik Modéen
* Fredrik Modéen
*/
#ifndef NETWORK_WINDOW_H
#define NETWORK_WINDOW_H
@ -12,12 +12,13 @@
#include <Window.h>
#include "EthernetSettingsView.h"
class Settings;
class EthernetSettingsView;
class NetworkWindow : public BWindow {
public:
NetworkWindow(Setting* setting);
NetworkWindow(Settings* setting);
virtual ~NetworkWindow();
virtual bool QuitRequested();
virtual void MessageReceived(BMessage* mesage);

View File

@ -1,170 +0,0 @@
/*
* Copyright 2004-2007 Haiku Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*
* Author:
* Andre Alves Garzia, andre@andregarzia.com
*/
#include "Setting.h"
#include <arpa/inet.h>
#include <errno.h>
#include <net/if.h>
#include <netinet/in.h>
#include <resolv.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/sockio.h>
#include <unistd.h>
#include <net/if_dl.h>
#include <net/if_media.h>
#include <net/if_types.h>
#include <File.h>
#include <Path.h>
#include <String.h>
#include <AutoDeleter.h>
Setting::Setting(const char *name)
:
fAuto(true),
fEnabled(false)
{
fSocket = socket(AF_INET, SOCK_DGRAM, 0);
fName = name;
ReadConfiguration();
}
Setting::~Setting()
{
close(fSocket);
}
bool
Setting::_PrepareRequest(struct ifreq& request)
{
//This function is used for talking direct to the stack.
//It´s used by _ShowConfiguration.
const char* name = fName.String();
if (strlen(name) > IF_NAMESIZE)
return false;
strcpy(request.ifr_name, name);
return true;
}
void
Setting::ReadConfiguration()
{
ifreq request;
if (!_PrepareRequest(request))
return;
BString text = "dummy";
char address[32];
sockaddr_in* inetAddress = NULL;
// Obtain IP.
if (ioctl(fSocket, SIOCGIFADDR, &request, sizeof(request)) < 0)
return;
inetAddress = (sockaddr_in*)&request.ifr_addr;
if (inet_ntop(AF_INET, &inetAddress->sin_addr, address,
sizeof(address)) == NULL) {
return;
}
fIP = address;
// Obtain netmask.
if (ioctl(fSocket, SIOCGIFNETMASK, &request,
sizeof(request)) < 0) {
return;
}
inetAddress = (sockaddr_in*)&request.ifr_mask;
if (inet_ntop(AF_INET, &inetAddress->sin_addr, address,
sizeof(address)) == NULL) {
return;
}
fNetmask = address;
// Obtain gateway
ifconf config;
config.ifc_len = sizeof(config.ifc_value);
if (ioctl(fSocket, SIOCGRTSIZE, &config, sizeof(struct ifconf)) < 0)
return;
uint32 size = (uint32)config.ifc_value;
if (size == 0)
return;
void *buffer = malloc(size);
if (buffer == NULL)
return;
MemoryDeleter bufferDeleter(buffer);
config.ifc_len = size;
config.ifc_buf = buffer;
if (ioctl(fSocket, SIOCGRTTABLE, &config, sizeof(struct ifconf)) < 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) {
inetAddress = (sockaddr_in*)route.gateway;
fGateway = inet_ntoa(inetAddress->sin_addr);
}
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);
}
uint32 flags = 0;
if (ioctl(fSocket, SIOCGIFFLAGS, &request, sizeof(struct ifreq)) == 0)
flags = request.ifr_flags;
fAuto = (flags & IFF_AUTO_CONFIGURED) != 0;
fEnabled = (flags & IFF_UP) != 0;
if (ioctl(fSocket, SIOCGIFMEDIA, &request, sizeof(struct ifreq)) == 0)
fMedia = request.ifr_media;
// 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)));
}
}
}

View File

@ -1,50 +0,0 @@
/*
* Copyright 2004-2007 Haiku Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*
* Author:
* Andre Alves Garzia, andre@andregarzia.com
*/
#ifndef SETTING_H
#define SETTING_H
#include <ObjectList.h>
#include <String.h>
class Setting {
public:
Setting(const char* name);
virtual ~Setting();
void SetName(BString name);
void SetIP(BString ip) {fIP = ip; }
void SetGateway(BString ip) {fGateway = ip; }
void SetNetmask(BString ip) {fNetmask = ip; }
void SetAutoConfigure(bool t) {fAuto = t; }
void Enable(bool enable) { fEnabled = enable; }
const char* IP() {return fIP.String(); }
const char* Gateway() {return fGateway.String(); }
const char* Netmask() {return fNetmask.String(); }
const char* Name() {return fName.String(); }
bool AutoConfigured() {return fAuto; }
bool Enabled() { return fEnabled; }
int Media() { return fMedia; }
BObjectList<BString> fNameservers;
void ReadConfiguration();
private:
bool _PrepareRequest(struct ifreq& request);
int fSocket;
BString fIP;
BString fGateway;
BString fNetmask;
BString fName;
bool fAuto;
bool fEnabled;
int fMedia;
};
#endif /* SETTINGS_H */