Break the Settings class away from the network Preference applet so It can be re-written without risk of breaking the current network preference applet; the InterfacesAddon is now autonomous

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@40389 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Alexander von Gluck IV
2011-02-08 14:52:27 +00:00
parent 4ee7d007d6
commit 083fe63d0d
7 changed files with 302 additions and 17 deletions
@@ -16,7 +16,6 @@
#include "InterfacesAddOn.h"
#include "InterfacesListView.h"
#include "NetworkWindow.h"
#include <stdio.h>
@@ -137,8 +136,8 @@ InterfacesAddOn::MessageReceived(BMessage* msg)
if (!item)
break;
NetworkWindow* nw = new NetworkWindow(item->GetSettings());
nw->Show();
//NetworkWindow* nw = new NetworkWindow(item->GetSettings());
//nw->Show();
break;
}
@@ -32,7 +32,7 @@
#include <AutoDeleter.h>
#include "Settings.h"
#include "NetworkSettings.h"
// #pragma mark -
@@ -192,7 +192,6 @@ InterfaceListItem::DrawItem(BView* owner, BRect /*bounds*/, bool complete)
list->DrawString(v4str.String(), v4addrPt);
list->DrawString("IPv6: none (auto)", v6addrPt);
// TODO : where will we keep this?
}
owner->PopState();
@@ -202,11 +201,12 @@ InterfaceListItem::DrawItem(BView* owner, BRect /*bounds*/, bool complete)
void
InterfaceListItem::_Init()
{
fSettings = new Settings(Name());
fSettings = new NetworkSettings(Name());
const char* mediaTypeName = NULL;
int media = fInterface.Media();
printf("%s media = 0x%x\n", Name(), media);
switch (IFM_TYPE(media)) {
case IFM_ETHER:
@@ -24,7 +24,7 @@
#include <NetworkInterface.h>
#include <String.h>
#include "Settings.h"
#include "NetworkSettings.h"
/* GCC 3.4 and onward have a built-in "population count"
@@ -64,7 +64,7 @@ public:
return fSettings->IsDisabled();}
inline void SetDisabled(bool disable) {
fSettings->SetDisabled(disable);}
inline Settings* GetSettings() {return fSettings;}
inline NetworkSettings* GetSettings() {return fSettings;}
private:
void _Init();
@@ -75,8 +75,12 @@ private:
BBitmap* fIconPending;
BBitmap* fIconOnline;
NetworkSettings* fSettings;
// Interface configuration
BNetworkInterface fInterface;
Settings* fSettings;
// Hardware Interface
float fFirstlineOffset;
float fSecondlineOffset;
float fThirdlineOffset;
@@ -1,7 +1,6 @@
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 ;
@@ -17,11 +16,7 @@ AddResources Interfaces :
Addon Interfaces :
InterfacesAddOn.cpp
InterfacesListView.cpp
NetworkWindow.cpp
# from src/preferences/network
EthernetSettingsView.cpp
Settings.cpp
NetworkSettings.cpp
# from src/apps/networkstatus
RadioView.cpp
@@ -0,0 +1,221 @@
/*
* Copyright 2004-2010 Haiku Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Andre Alves Garzia, [email protected]
* Axel Dörfler, [email protected].
* Vegard Wærp, [email protected]
*/
#include "NetworkSettings.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 <driver_settings.h>
#include <File.h>
#include <FindDirectory.h>
#include <Path.h>
#include <String.h>
#include <AutoDeleter.h>
NetworkSettings::NetworkSettings(const char* name)
:
fAuto(true),
fDisabled(false),
fNameServers(5, true)
{
fSocket = socket(AF_INET, SOCK_DGRAM, 0);
fName = name;
ReadConfiguration();
}
NetworkSettings::~NetworkSettings()
{
close(fSocket);
}
bool
NetworkSettings::_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
NetworkSettings::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) != 0) {
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 | IFF_CONFIGURING)) != 0;
fDisabled = (flags & IFF_UP) == 0;
// Read wireless network from interfaces
fWirelessNetwork.SetTo(NULL);
BPath path;
find_directory(B_COMMON_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];
}
}
@@ -0,0 +1,66 @@
/*
* Copyright 2004-2010 Haiku Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Andre Alves Garzia, [email protected]
* Vegard Wærp, [email protected]
*/
#ifndef SETTINGS_H
#define SETTINGS_H
#include <ObjectList.h>
#include <String.h>
class NetworkSettings {
public:
NetworkSettings(const char* name);
virtual ~NetworkSettings();
void SetIP(const BString& ip)
{ fIP = ip; }
void SetGateway(const BString& ip)
{ fGateway = ip; }
void SetNetmask(const BString& ip)
{ fNetmask = ip; }
void SetDomain(const BString& domain)
{ fDomain = domain; }
void SetAutoConfigure(bool autoConfigure)
{ fAuto = autoConfigure; }
void SetDisabled(bool disabled)
{ fDisabled = disabled; }
void SetWirelessNetwork(const char* name)
{ fWirelessNetwork.SetTo(name); }
const char* IP() { return fIP.String(); }
const char* Gateway() { return fGateway.String(); }
const char* Netmask() { return fNetmask.String(); }
const char* Name() { return fName.String(); }
const char* Domain() { return fDomain.String(); }
bool AutoConfigure() { return fAuto; }
bool IsDisabled() { return fDisabled; }
const BString& WirelessNetwork() { return fWirelessNetwork; }
BObjectList<BString>& NameServers() { return fNameServers; }
void ReadConfiguration();
private:
bool _PrepareRequest(struct ifreq& request);
BString fIP;
BString fGateway;
BString fNetmask;
BString fName;
BString fDomain;
int fSocket;
bool fAuto;
bool fDisabled;
BObjectList<BString> fNameServers;
BString fWirelessNetwork;
};
#endif /* SETTINGS_H */
@@ -13,10 +13,10 @@
#include <GroupLayout.h>
#include "EthernetSettingsView.h"
#include "Settings.h"
#include "NetworkSettings.h"
NetworkWindow::NetworkWindow(Settings* settings)
NetworkWindow::NetworkWindow(NetworkSettings* 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)