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

View File

@ -1,38 +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
* 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;
}

View File

@ -115,7 +115,6 @@ EthernetSettingsView::_GatherInterfaces() {
void
EthernetSettingsView::AttachedToWindow()
{
fOKButton->SetTarget(this);
fApplyButton->SetTarget(this);
fDeviceMenuField->Menu()->SetTargetForItems(this);
@ -167,6 +166,8 @@ EthernetSettingsView::EthernetSettingsView(BRect rect)
modemenu->AddItem(staticitem);
BMenuItem* dhcpitem = new BMenuItem("DHCP", NULL);
modemenu->AddItem(dhcpitem);
BMenuItem* offitem = new BMenuItem("Disconnected", NULL);
modemenu->AddItem(offitem);
fDeviceMenuField = new BMenuField(frame, "networkcards", "Adapter:", devmenu);
fDeviceMenuField->SetDivider(
@ -233,10 +234,6 @@ EthernetSettingsView::EthernetSettingsView(BRect rect)
fSecondaryDNSTextControl->Frame().LeftBottom() + BPoint(0,10));
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)
{
// Clear the inputs.
fIPTextControl->SetText("");
fGatewayTextControl->SetText("");
fNetMaskTextControl->SetText("");
fPrimaryDNSTextControl->SetText("");
fSecondaryDNSTextControl->SetText("");
const char* name;
if (message->FindString("interface", &name) != B_OK)
return;
@ -283,33 +290,21 @@ EthernetSettingsView::_ShowConfiguration(BMessage* message)
fPrimaryDNSTextControl->SetText(
fSettings.ItemAt(i)->fNameservers.ItemAt(0)->String());
}
} else {
return;
}
}
}
}
}
void
EthernetSettingsView::_ApplyControlsToConfiguration()
{
if (strcmp(fTypeMenuField->Menu()->FindMarked()->Label(), "Static") == 0) {
printf("we´re in!\n");
int i;
for (i = 0; i < fSettings.CountItems(); i++) {
if (strcmp(fSettings.ItemAt(i)->
GetName(), fDeviceMenuField->Menu()->FindMarked()->Label()) == 0) {
printf("changing settings for %s", fSettings.ItemAt(i)->GetName());
fSettings.ItemAt(i)->
SetIP(fIPTextControl->Text());
fSettings.ItemAt(i)->
@ -348,8 +343,11 @@ EthernetSettingsView::_SaveDNSConfiguration()
// loop all the adapters.
int j;
for (j = 0; j < fSettings.ItemAt(i)->fNameservers.CountItems(); j++) {
fprintf(fp, "nameserver\t%s\n",
fSettings.ItemAt(i)->fNameservers.ItemAt(j)->String());
if (strcmp(
fSettings.ItemAt(i)->fNameservers.ItemAt(j)->String(), "") != 0) {
fprintf(fp, "nameserver\t%s\n",
fSettings.ItemAt(i)->fNameservers.ItemAt(j)->String());
}
}
}
fclose(fp);
@ -363,37 +361,39 @@ EthernetSettingsView::_SaveAdaptersConfiguration()
BPath path;
status_t status = _GetPath("interfaces", path);
if (status < B_OK)
return;
return;
FILE* fp;
bool isOpen = false;
int i;
for (i = 0; i < fSettings.CountItems(); i++) {
// loop all the adapters.
if (fSettings.ItemAt(i)->GetAutoConfigure() == false) {
if (isOpen == false) {
if ((fp = fopen(path.Path(), "w")) == NULL)
return;
isOpen = true;
FILE* fp = NULL;
// loop over all adapters. open the settings file only once,
// append the settins of each non-autoconfiguring adapter
for (int i = 0; i < fSettings.CountItems(); i++) {
if (fSettings.ItemAt(i)->GetAutoConfigure())
continue;
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,
"device %s {\n\t\taddress {\n", fSettings.ItemAt(i)->GetName());
fprintf(fp, "\t\t\tfamily\tinet\n");
fprintf(fp, "\t\t\taddress\t%s\n",
fSettings.ItemAt(i)->GetIP());
fprintf(fp, "\t\t\tgateway\t%s\n",
fSettings.ItemAt(i)->GetGateway());
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);
}
}
fprintf(fp, "interface %s {\n\t\taddress {\n",
fSettings.ItemAt(i)->GetName());
fprintf(fp, "\t\t\tfamily\tinet\n");
fprintf(fp, "\t\t\taddress\t%s\n",
fSettings.ItemAt(i)->GetIP());
fprintf(fp, "\t\t\tgateway\t%s\n",
fSettings.ItemAt(i)->GetGateway());
fprintf(fp, "\t\t\tmask\t%s\n",
fSettings.ItemAt(i)->GetNetmask());
fprintf(fp, "\t\t}\n}\n\n");
}
if (fp) {
printf("%s saved.\n", path.Path());
fclose(fp);
}
}
status_t

View File

@ -21,7 +21,6 @@ class BTextControl;
static const uint32 kMsgApply = 'aply';
static const uint32 kMsgClose = 'clse';
static const uint32 kMsgOK = 'okok';
static const uint32 kMsgField = 'fild';
static const uint32 kMsgInfo = 'info';
@ -40,7 +39,6 @@ class EthernetSettingsView : public BView {
BButton* fCloseButton;
BButton* fApplyButton;
BButton* fOKButton;
BMenuField* fDeviceMenuField;
BMenuField* fTypeMenuField;
BTextControl* fIPTextControl;

View File

@ -5,10 +5,10 @@ SetSubDirSupportedPlatformsBeOSCompatible ;
UsePrivateHeaders shared ;
Preference Network :
EthernetSettingsWindow.cpp
settings.cpp
NetworkApp.cpp
NetworkWindow.cpp
EthernetSettingsView.cpp
EthernetSettings.cpp
settings.cpp
: be root $(HAIKU_NETWORK_LIBS)
;

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, andre@andregarzia.com
* 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;
}

View File

@ -9,24 +9,24 @@
* Hugo Santos
*/
#ifndef ETHERNET_SETTINGS_H
#define ETHERNET_SETTINGS_H
#ifndef NETWORK_APP_H
#define NETWORK_APP_H
#include <stdlib.h>
#include <unistd.h>
#include <Application.h>
#include "EthernetSettingsWindow.h"
#include "NetworkWindow.h"
class EthernetSettings : public BApplication {
class NetworkApp : public BApplication {
public:
EthernetSettings();
virtual ~EthernetSettings();
NetworkApp();
virtual ~NetworkApp();
virtual void ReadyToRun();
private:
EthernetSettingsWindow *fEthWindow;
NetworkWindow *fEthWindow;
};
#endif /* ETHERNET_SETTINGS_H */
#endif /* NETWORK_APP_H */

View File

@ -11,12 +11,12 @@
#include <Application.h>
#include "EthernetSettingsWindow.h"
#include "NetworkWindow.h"
#include "EthernetSettingsView.h"
EthernetSettingsWindow::EthernetSettingsWindow()
: BWindow(BRect(50, 50, 269, 302), "Ethernet Settings",
NetworkWindow::NetworkWindow()
: BWindow(BRect(50, 50, 269, 302), "Network",
B_TITLED_WINDOW, B_NOT_RESIZABLE | B_ASYNCHRONOUS_CONTROLS
| B_NOT_ZOOMABLE)
{
@ -29,7 +29,7 @@ B_TITLED_WINDOW, B_NOT_RESIZABLE | B_ASYNCHRONOUS_CONTROLS
void
EthernetSettingsWindow::MessageReceived(BMessage* message)
NetworkWindow::MessageReceived(BMessage* message)
{
switch (message->what) {
case kMsgInfo:
@ -41,13 +41,13 @@ EthernetSettingsWindow::MessageReceived(BMessage* message)
}
EthernetSettingsWindow::~EthernetSettingsWindow()
NetworkWindow::~NetworkWindow()
{
}
bool
EthernetSettingsWindow::QuitRequested()
NetworkWindow::QuitRequested()
{
be_app->PostMessage(B_QUIT_REQUESTED);
return true;

View File

@ -8,16 +8,16 @@
* Axel Dorfler
* Hugo Santos
*/
#ifndef ETHERNET_SETTINGS_WINDOW_H
#define ETHERNET_SETTINGS_WINDOW_H
#ifndef NETWORK_WINDOW_H
#define NETWORK_WINDOW_H
#include <Window.h>
#include "EthernetSettingsView.h"
class EthernetSettingsWindow : public BWindow {
class NetworkWindow : public BWindow {
public:
EthernetSettingsWindow();
virtual ~EthernetSettingsWindow();
NetworkWindow();
virtual ~NetworkWindow();
virtual bool QuitRequested();
virtual void MessageReceived(BMessage* mesage);
@ -27,4 +27,4 @@ class EthernetSettingsWindow : public BWindow {
};
#endif /* ETHERNET_SETTINGS_WINDOW_H */
#endif /* NETWORK_WINDOW_H */

View File

@ -60,7 +60,7 @@ Settings::_PrepareRequest(struct ifreq& request)
void
Settings::ReadConfiguration()
Settings::ReadConfiguration()
{
ifreq request;
@ -172,37 +172,39 @@ Settings::ReadConfiguration()
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;
char buf[1024];
register FILE *fp;
register char *cp, **pp;
register int n;
register char *cp; //, **pp;
// register int n;
int MAXNS = 2;
if ((fp = fopen("/etc/resolv.conf", "r")) != NULL) {
/* read the config file */
while (fgets(buf, sizeof(buf), fp) != NULL) {
/* skip comments */
// read the config file
while (fgets(buf, sizeof(buf), fp) != NULL) {
// skip comments
if (*buf == ';' || *buf == '#')
continue;
/* read nameservers to query */
if (MATCH(buf, "nameserver") && nserv < MAXNS) {
char sbuf[2];
cp = buf + sizeof("nameserver") - 1;
while (*cp == ' ' || *cp == '\t')
cp++;
cp[strcspn(cp, ";# \t\n")] = '\0';
if ((*cp != '\0') && (*cp != '\n')) {
// read nameservers to query
if (MATCH(buf, "nameserver") && nserv < MAXNS) {
// char sbuf[2];
cp = buf + sizeof("nameserver") - 1;
while (*cp == ' ' || *cp == '\t')
cp++;
cp[strcspn(cp, ";# \t\n")] = '\0';
if ((*cp != '\0') && (*cp != '\n')) {
fNameservers.AddItem(new BString(cp));
nserv++;
}
nserv++;
}
}
continue;
}
fclose(fp);
continue;
}
fclose(fp);
}