make protocol selection more generic and easy to configure; this adds complexity but allows us to configure ipv4/ipv6 support from a single point.. now just have to figure out how to check for the presence of the IPv4 vs IPv6 network add-ons

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@40584 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Alexander von Gluck IV
2011-02-20 16:19:43 +00:00
parent f38b8016a8
commit 450a2dc7e4
6 changed files with 90 additions and 44 deletions
@@ -15,8 +15,8 @@
#include <StringView.h>
InterfaceAddressView::InterfaceAddressView(BRect frame, const char* name,
int family, NetworkSettings* settings)
InterfaceAddressView::InterfaceAddressView(BRect frame, int family,
NetworkSettings* settings)
:
BGroupView(B_VERTICAL),
fSettings(settings),
@@ -28,8 +28,7 @@ enum {
class InterfaceAddressView : public BGroupView {
public:
InterfaceAddressView(BRect frame,
const char* name, int family,
NetworkSettings* settings);
int family, NetworkSettings* settings);
virtual ~InterfaceAddressView();
virtual void MessageReceived(BMessage* message);
virtual void AttachedToWindow();
@@ -60,16 +60,25 @@ InterfaceWindow::~InterfaceWindow()
void
InterfaceWindow::MessageReceived(BMessage* message)
{
int* supportedFamilies = fNetworkSettings->ProtocolVersions();
unsigned int index;
switch (message->what) {
case MSG_IP_REVERT:
// RFC : we could check fTabView for Selection
// here and only revert the selected tab.
fIPv4TabView->RevertFields();
fIPv6TabView->RevertFields();
for (index = 0; index < sizeof(supportedFamilies); index++)
{
int protocol = supportedFamilies[index];
if (protocol > 0)
fTabIPView[protocol]->RevertFields();
}
break;
case MSG_IP_SAVE:
fIPv4TabView->SaveFields();
fIPv6TabView->SaveFields();
for (index = 0; index < sizeof(supportedFamilies); index++)
{
int protocol = supportedFamilies[index];
if (protocol > 0)
fTabIPView[protocol]->SaveFields();
}
this->Quit();
break;
default:
@@ -83,19 +92,22 @@ status_t
InterfaceWindow::_PopulateTabs()
{
BRect frame = fTabView->Bounds();
fIPv4TabView = new InterfaceAddressView(frame, "net_settings_ipv4",
AF_INET, fNetworkSettings);
fIPv6TabView = new InterfaceAddressView(frame, "net_settings_ipv6",
AF_INET6, fNetworkSettings);
int* supportedFamilies = fNetworkSettings->ProtocolVersions();
BTab* tab4 = new BTab;
BTab* tab6 = new BTab;
fTabView->AddTab(fIPv4TabView, tab4);
tab4->SetLabel("IPv4");
fTabView->AddTab(fIPv6TabView, tab6);
tab6->SetLabel("IPv6");
unsigned int index;
for (index = 0; index < sizeof(supportedFamilies); index++)
{
int protocol = supportedFamilies[index];
if (protocol > 0)
{
fTabIPView[protocol] = new InterfaceAddressView(frame,
protocol, fNetworkSettings);
BTab* tab = new BTab;
fTabView->AddTab(fTabIPView[protocol], tab);
tab->SetLabel((protocol == AF_INET) ? "IPv4" : "IPv6");
// TODO : find a better way
}
}
return B_OK;
}
@@ -19,6 +19,8 @@
#include <TabView.h>
#include <Window.h>
#include <map>
enum {
MSG_IP_SAVE = 'ipap',
@@ -26,6 +28,9 @@ enum {
};
typedef std::map<int, InterfaceAddressView*> IPViewMap;
class InterfaceWindow : public BWindow {
public:
InterfaceWindow(NetworkSettings* settings);
@@ -41,8 +46,7 @@ private:
BButton* fRevertButton;
BTabView* fTabView;
InterfaceAddressView* fIPv4TabView;
InterfaceAddressView* fIPv6TabView;
IPViewMap fTabIPView;
};
@@ -38,19 +38,35 @@ NetworkSettings::NetworkSettings(const char* name)
fDisabled(false),
fNameServers(5, true)
{
fSocket[AF_INET] = socket(AF_INET, SOCK_DGRAM, 0);
fSocket[AF_INET6] = socket(AF_INET6, SOCK_DGRAM, 0);
// TODO : Detect supported IP protocol versions
memset(fProtocolVersions, 0, sizeof(fProtocolVersions));
fProtocolVersions[0] = AF_INET;
fProtocolVersions[1] = AF_INET6;
unsigned int index;
for (index = 0; index < sizeof(fProtocolVersions); index++)
{
int protocol = fProtocolVersions[index];
if (protocol > 0)
fSocket[protocol] = socket(protocol, SOCK_DGRAM, 0);
}
fName = name;
ReadConfiguration();
}
NetworkSettings::~NetworkSettings()
{
close(fSocket[AF_INET]);
close(fSocket[AF_INET6]);
unsigned int index;
for (index = 0; index < sizeof(fProtocolVersions); index++)
{
int protocol = fProtocolVersions[index];
if (protocol > 0)
close(fSocket[protocol]);
}
}
@@ -62,23 +78,22 @@ NetworkSettings::ReadConfiguration()
{
BNetworkInterface fNetworkInterface(fName);
// Obtain possible IPv4 and IPv6 addresses
int32 zeroAddrV4 = fNetworkInterface.FindFirstAddress(AF_INET);
int32 zeroAddrV6 = fNetworkInterface.FindFirstAddress(AF_INET6);
unsigned int index;
for (index = 0; index < sizeof(fProtocolVersions); index++)
{
int protocol = fProtocolVersions[index];
BNetworkInterfaceAddress netIntAddr4;
BNetworkInterfaceAddress netIntAddr6;
if (zeroAddrV4 >= 0) {
fNetworkInterface.GetAddressAt(zeroAddrV4, netIntAddr4);
fAddress[AF_INET] = netIntAddr4.Address();
fNetmask[AF_INET] = netIntAddr4.Mask();
}
if (zeroAddrV6 >= 0) {
fNetworkInterface.GetAddressAt(zeroAddrV6, netIntAddr6);
fAddress[AF_INET6] = netIntAddr6.Address();
fNetmask[AF_INET6] = netIntAddr6.Mask();
if (protocol > 0) {
int32 zeroAddr = fNetworkInterface.FindFirstAddress(protocol);
if (zeroAddr >= 0) {
fNetworkInterface.GetAddressAt(zeroAddr,
fInterfaceAddressMap[protocol]);
fAddress[protocol]
= fInterfaceAddressMap[protocol].Address();
fNetmask[protocol]
= fInterfaceAddressMap[protocol].Mask();
}
}
}
// Obtain gateway
@@ -206,3 +221,10 @@ NetworkSettings::ReadConfiguration()
}
void
NetworkSettings::WriteConfiguration()
{
}
@@ -20,6 +20,7 @@
typedef std::map<int, BNetworkAddress> AddressMap;
typedef std::map<int, BNetworkInterfaceAddress> InterfaceAddressMap;
typedef std::map<int, bool> BoolMap;
typedef std::map<int, int> SocketMap;
@@ -29,6 +30,9 @@ public:
NetworkSettings(const char* name);
virtual ~NetworkSettings();
int* ProtocolVersions()
{ return fProtocolVersions; }
void SetIP(int family, const char* ip)
{ fAddress[family].SetTo(ip); }
void SetNetmask(int family, const char* mask)
@@ -69,10 +73,15 @@ public:
BObjectList<BString>& NameServers() { return fNameServers; }
void ReadConfiguration();
void WriteConfiguration();
private:
SocketMap fSocket;
BNetworkInterface fNetworkInterface;
InterfaceAddressMap fInterfaceAddressMap;
int fProtocolVersions[3];
// OS Supported protocol versions
// Stored network addresses and paramaters
BoolMap fAutoConfigure;