patch by Andre Garzia:

* reading/writing interface configuration data is now working properly
* at boot, net_server will overwrite the configuration with settings retrieved
  via DHCP, though
* refactoring/renaming

changes by myself:
* fixed warnings and cleaned up in Settings::ReadConfiguration(), when the
  nameservers are parsed
* fixed warning in EthernetSettingsView::_SaveAdaptersConfiguration(), changed
  the loop to do without "isOpen", I think it was broken in two ways before:
  - it would use the fp pointer after closing the file (isOpen was not
  maintained), and if that would have worked, it would have overwritten the
  interface configuration saved in an earlier loop iteration, now it should
  write all interfaces that are not set to autoconfigure


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@22018 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Stephan Aßmus
2007-08-20 17:40:48 +00:00
parent e8b3a30edb
commit e583bfc206
9 changed files with 137 additions and 137 deletions
@@ -1,38 +0,0 @@
/*
* Copyright 2004-2007 Haiku Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*
* Author:
* Andre Alves Garzia, [email protected]
* With code from:
* Axel Dorfler
* Hugo Santos
*/
#include "EthernetSettings.h"
#include "EthernetSettingsWindow.h"
EthernetSettings::EthernetSettings()
: BApplication("application/x-vnd.Haiku-EthernetSettings")
{
}
EthernetSettings::~EthernetSettings()
{
}
void
EthernetSettings::ReadyToRun()
{
fEthWindow = new EthernetSettingsWindow();
fEthWindow->Show();
}
int
main(int argc, char** argv)
{
EthernetSettings app;
app.Run();
return 0;
}
@@ -115,7 +115,6 @@ EthernetSettingsView::_GatherInterfaces() {
void void
EthernetSettingsView::AttachedToWindow() EthernetSettingsView::AttachedToWindow()
{ {
fOKButton->SetTarget(this);
fApplyButton->SetTarget(this); fApplyButton->SetTarget(this);
fDeviceMenuField->Menu()->SetTargetForItems(this); fDeviceMenuField->Menu()->SetTargetForItems(this);
@@ -167,6 +166,8 @@ EthernetSettingsView::EthernetSettingsView(BRect rect)
modemenu->AddItem(staticitem); modemenu->AddItem(staticitem);
BMenuItem* dhcpitem = new BMenuItem("DHCP", NULL); BMenuItem* dhcpitem = new BMenuItem("DHCP", NULL);
modemenu->AddItem(dhcpitem); modemenu->AddItem(dhcpitem);
BMenuItem* offitem = new BMenuItem("Disconnected", NULL);
modemenu->AddItem(offitem);
fDeviceMenuField = new BMenuField(frame, "networkcards", "Adapter:", devmenu); fDeviceMenuField = new BMenuField(frame, "networkcards", "Adapter:", devmenu);
fDeviceMenuField->SetDivider( fDeviceMenuField->SetDivider(
@@ -233,10 +234,6 @@ EthernetSettingsView::EthernetSettingsView(BRect rect)
fSecondaryDNSTextControl->Frame().LeftBottom() + BPoint(0,10)); fSecondaryDNSTextControl->Frame().LeftBottom() + BPoint(0,10));
AddChild(fApplyButton); AddChild(fApplyButton);
fOKButton = new BButton(frame, "ok", "OK", new BMessage(kMsgOK));
fOKButton->ResizeToPreferred();
fOKButton->MoveTo(fApplyButton->Frame().RightTop() + BPoint(10,0));
AddChild(fOKButton);
} }
@@ -249,6 +246,16 @@ void
EthernetSettingsView::_ShowConfiguration(BMessage* message) EthernetSettingsView::_ShowConfiguration(BMessage* message)
{ {
// Clear the inputs.
fIPTextControl->SetText("");
fGatewayTextControl->SetText("");
fNetMaskTextControl->SetText("");
fPrimaryDNSTextControl->SetText("");
fSecondaryDNSTextControl->SetText("");
const char* name; const char* name;
if (message->FindString("interface", &name) != B_OK) if (message->FindString("interface", &name) != B_OK)
return; return;
@@ -283,33 +290,21 @@ EthernetSettingsView::_ShowConfiguration(BMessage* message)
fPrimaryDNSTextControl->SetText( fPrimaryDNSTextControl->SetText(
fSettings.ItemAt(i)->fNameservers.ItemAt(0)->String()); fSettings.ItemAt(i)->fNameservers.ItemAt(0)->String());
} }
} else { }
return; }
}
}
} }
void void
EthernetSettingsView::_ApplyControlsToConfiguration() EthernetSettingsView::_ApplyControlsToConfiguration()
{ {
if (strcmp(fTypeMenuField->Menu()->FindMarked()->Label(), "Static") == 0) { if (strcmp(fTypeMenuField->Menu()->FindMarked()->Label(), "Static") == 0) {
printf("we´re in!\n");
int i; int i;
for (i = 0; i < fSettings.CountItems(); i++) { for (i = 0; i < fSettings.CountItems(); i++) {
if (strcmp(fSettings.ItemAt(i)-> if (strcmp(fSettings.ItemAt(i)->
GetName(), fDeviceMenuField->Menu()->FindMarked()->Label()) == 0) { GetName(), fDeviceMenuField->Menu()->FindMarked()->Label()) == 0) {
printf("changing settings for %s", fSettings.ItemAt(i)->GetName());
fSettings.ItemAt(i)-> fSettings.ItemAt(i)->
SetIP(fIPTextControl->Text()); SetIP(fIPTextControl->Text());
fSettings.ItemAt(i)-> fSettings.ItemAt(i)->
@@ -348,8 +343,11 @@ EthernetSettingsView::_SaveDNSConfiguration()
// loop all the adapters. // loop all the adapters.
int j; int j;
for (j = 0; j < fSettings.ItemAt(i)->fNameservers.CountItems(); j++) { for (j = 0; j < fSettings.ItemAt(i)->fNameservers.CountItems(); j++) {
fprintf(fp, "nameserver\t%s\n", if (strcmp(
fSettings.ItemAt(i)->fNameservers.ItemAt(j)->String()); fSettings.ItemAt(i)->fNameservers.ItemAt(j)->String(), "") != 0) {
fprintf(fp, "nameserver\t%s\n",
fSettings.ItemAt(i)->fNameservers.ItemAt(j)->String());
}
} }
} }
fclose(fp); fclose(fp);
@@ -363,37 +361,39 @@ EthernetSettingsView::_SaveAdaptersConfiguration()
BPath path; BPath path;
status_t status = _GetPath("interfaces", path); status_t status = _GetPath("interfaces", path);
if (status < B_OK) if (status < B_OK)
return; return;
FILE* fp; FILE* fp = NULL;
bool isOpen = false; // loop over all adapters. open the settings file only once,
int i; // append the settins of each non-autoconfiguring adapter
for (i = 0; i < fSettings.CountItems(); i++) { for (int i = 0; i < fSettings.CountItems(); i++) {
// loop all the adapters. if (fSettings.ItemAt(i)->GetAutoConfigure())
if (fSettings.ItemAt(i)->GetAutoConfigure() == false) { continue;
if (isOpen == false) {
if ((fp = fopen(path.Path(), "w")) == NULL) if (fp == NULL) {
return; fp = fopen(path.Path(), "w");
isOpen = true; if (fp == NULL) {
fprintf(stderr, "failed to open file %s to write "
"configuration: %s\n", path.Path(), strerror(errno));
return;
} }
fprintf(fp, }
"device %s {\n\t\taddress {\n", fSettings.ItemAt(i)->GetName());
fprintf(fp, "\t\t\tfamily\tinet\n"); fprintf(fp, "interface %s {\n\t\taddress {\n",
fprintf(fp, "\t\t\taddress\t%s\n", fSettings.ItemAt(i)->GetName());
fSettings.ItemAt(i)->GetIP()); fprintf(fp, "\t\t\tfamily\tinet\n");
fprintf(fp, "\t\t\tgateway\t%s\n", fprintf(fp, "\t\t\taddress\t%s\n",
fSettings.ItemAt(i)->GetGateway()); fSettings.ItemAt(i)->GetIP());
fprintf(fp, "\t\t\tmask\t%s\n", fprintf(fp, "\t\t\tgateway\t%s\n",
fSettings.ItemAt(i)->GetNetmask()); fSettings.ItemAt(i)->GetGateway());
fprintf(fp, "\t\t}\n}\n\n"); fprintf(fp, "\t\t\tmask\t%s\n",
fSettings.ItemAt(i)->GetNetmask());
fprintf(fp, "\t\t}\n}\n\n");
}
if (isOpen) {
printf("%s saved.\n", path.Path());
fclose(fp);
}
} }
if (fp) {
printf("%s saved.\n", path.Path());
fclose(fp);
}
} }
status_t status_t
@@ -21,7 +21,6 @@ class BTextControl;
static const uint32 kMsgApply = 'aply'; static const uint32 kMsgApply = 'aply';
static const uint32 kMsgClose = 'clse'; static const uint32 kMsgClose = 'clse';
static const uint32 kMsgOK = 'okok';
static const uint32 kMsgField = 'fild'; static const uint32 kMsgField = 'fild';
static const uint32 kMsgInfo = 'info'; static const uint32 kMsgInfo = 'info';
@@ -40,7 +39,6 @@ class EthernetSettingsView : public BView {
BButton* fCloseButton; BButton* fCloseButton;
BButton* fApplyButton; BButton* fApplyButton;
BButton* fOKButton;
BMenuField* fDeviceMenuField; BMenuField* fDeviceMenuField;
BMenuField* fTypeMenuField; BMenuField* fTypeMenuField;
BTextControl* fIPTextControl; BTextControl* fIPTextControl;
+3 -3
View File
@@ -5,10 +5,10 @@ SetSubDirSupportedPlatformsBeOSCompatible ;
UsePrivateHeaders shared ; UsePrivateHeaders shared ;
Preference Network : Preference Network :
EthernetSettingsWindow.cpp NetworkApp.cpp
settings.cpp NetworkWindow.cpp
EthernetSettingsView.cpp EthernetSettingsView.cpp
EthernetSettings.cpp settings.cpp
: be root $(HAIKU_NETWORK_LIBS) : be root $(HAIKU_NETWORK_LIBS)
; ;
+38
View File
@@ -0,0 +1,38 @@
/*
* Copyright 2004-2007 Haiku Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*
* Author:
* Andre Alves Garzia, [email protected]
* With code from:
* Axel Dörfler
* Hugo Santos
*/
#include "NetworkApp.h"
#include "NetworkWindow.h"
NetworkApp::NetworkApp()
: BApplication("application/x-vnd.Haiku-Network")
{
}
NetworkApp::~NetworkApp()
{
}
void
NetworkApp::ReadyToRun()
{
fEthWindow = new NetworkWindow();
fEthWindow->Show();
}
int
main(int argc, char** argv)
{
NetworkApp app;
app.Run();
return 0;
}
@@ -9,24 +9,24 @@
* Hugo Santos * Hugo Santos
*/ */
#ifndef ETHERNET_SETTINGS_H #ifndef NETWORK_APP_H
#define ETHERNET_SETTINGS_H #define NETWORK_APP_H
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <Application.h> #include <Application.h>
#include "EthernetSettingsWindow.h" #include "NetworkWindow.h"
class EthernetSettings : public BApplication { class NetworkApp : public BApplication {
public: public:
EthernetSettings(); NetworkApp();
virtual ~EthernetSettings(); virtual ~NetworkApp();
virtual void ReadyToRun(); virtual void ReadyToRun();
private: private:
EthernetSettingsWindow *fEthWindow; NetworkWindow *fEthWindow;
}; };
#endif /* ETHERNET_SETTINGS_H */ #endif /* NETWORK_APP_H */
@@ -11,12 +11,12 @@
#include <Application.h> #include <Application.h>
#include "EthernetSettingsWindow.h" #include "NetworkWindow.h"
#include "EthernetSettingsView.h" #include "EthernetSettingsView.h"
EthernetSettingsWindow::EthernetSettingsWindow() NetworkWindow::NetworkWindow()
: BWindow(BRect(50, 50, 269, 302), "Ethernet Settings", : BWindow(BRect(50, 50, 269, 302), "Network",
B_TITLED_WINDOW, B_NOT_RESIZABLE | B_ASYNCHRONOUS_CONTROLS B_TITLED_WINDOW, B_NOT_RESIZABLE | B_ASYNCHRONOUS_CONTROLS
| B_NOT_ZOOMABLE) | B_NOT_ZOOMABLE)
{ {
@@ -29,7 +29,7 @@ B_TITLED_WINDOW, B_NOT_RESIZABLE | B_ASYNCHRONOUS_CONTROLS
void void
EthernetSettingsWindow::MessageReceived(BMessage* message) NetworkWindow::MessageReceived(BMessage* message)
{ {
switch (message->what) { switch (message->what) {
case kMsgInfo: case kMsgInfo:
@@ -41,13 +41,13 @@ EthernetSettingsWindow::MessageReceived(BMessage* message)
} }
EthernetSettingsWindow::~EthernetSettingsWindow() NetworkWindow::~NetworkWindow()
{ {
} }
bool bool
EthernetSettingsWindow::QuitRequested() NetworkWindow::QuitRequested()
{ {
be_app->PostMessage(B_QUIT_REQUESTED); be_app->PostMessage(B_QUIT_REQUESTED);
return true; return true;
@@ -8,16 +8,16 @@
* Axel Dorfler * Axel Dorfler
* Hugo Santos * Hugo Santos
*/ */
#ifndef ETHERNET_SETTINGS_WINDOW_H #ifndef NETWORK_WINDOW_H
#define ETHERNET_SETTINGS_WINDOW_H #define NETWORK_WINDOW_H
#include <Window.h> #include <Window.h>
#include "EthernetSettingsView.h" #include "EthernetSettingsView.h"
class EthernetSettingsWindow : public BWindow { class NetworkWindow : public BWindow {
public: public:
EthernetSettingsWindow(); NetworkWindow();
virtual ~EthernetSettingsWindow(); virtual ~NetworkWindow();
virtual bool QuitRequested(); virtual bool QuitRequested();
virtual void MessageReceived(BMessage* mesage); virtual void MessageReceived(BMessage* mesage);
@@ -27,4 +27,4 @@ class EthernetSettingsWindow : public BWindow {
}; };
#endif /* ETHERNET_SETTINGS_WINDOW_H */ #endif /* NETWORK_WINDOW_H */
+26 -24
View File
@@ -60,7 +60,7 @@ Settings::_PrepareRequest(struct ifreq& request)
void void
Settings::ReadConfiguration() Settings::ReadConfiguration()
{ {
ifreq request; ifreq request;
@@ -172,37 +172,39 @@ Settings::ReadConfiguration()
line[sizeof(name) - 1] == '\t')) line[sizeof(name) - 1] == '\t'))
register FILE *fp = fopen("/etc/resolv.conf", "r");
if (fp == NULL) {
fprintf(stderr, "failed to open '/etc/resolv.conf' to "
"read nameservers: %s\n", strerror(errno));
return;
}
int nserv = 0; int nserv = 0;
char buf[1024]; char buf[1024];
register FILE *fp; register char *cp; //, **pp;
register char *cp, **pp; // register int n;
register int n;
int MAXNS = 2; int MAXNS = 2;
if ((fp = fopen("/etc/resolv.conf", "r")) != NULL) { // read the config file
/* read the config file */ while (fgets(buf, sizeof(buf), fp) != NULL) {
while (fgets(buf, sizeof(buf), fp) != NULL) { // skip comments
/* skip comments */
if (*buf == ';' || *buf == '#') if (*buf == ';' || *buf == '#')
continue; continue;
/* read nameservers to query */
if (MATCH(buf, "nameserver") && nserv < MAXNS) {
char sbuf[2];
cp = buf + sizeof("nameserver") - 1; // read nameservers to query
while (*cp == ' ' || *cp == '\t') if (MATCH(buf, "nameserver") && nserv < MAXNS) {
cp++; // char sbuf[2];
cp[strcspn(cp, ";# \t\n")] = '\0'; cp = buf + sizeof("nameserver") - 1;
if ((*cp != '\0') && (*cp != '\n')) { while (*cp == ' ' || *cp == '\t')
cp++;
cp[strcspn(cp, ";# \t\n")] = '\0';
if ((*cp != '\0') && (*cp != '\n')) {
fNameservers.AddItem(new BString(cp)); fNameservers.AddItem(new BString(cp));
nserv++; nserv++;
} }
} }
continue; continue;
}
fclose(fp);
} }
fclose(fp);
} }