patch by Andre Garzia:

* it gets all the configuration and it saves it back but for some reason,
  the net_server is not noticing the changes yet


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@22007 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Stephan Aßmus 2007-08-17 16:36:13 +00:00
parent ae3a7f4dac
commit b60056cdb3
4 changed files with 188 additions and 32 deletions

View File

@ -33,6 +33,11 @@
#include <SupportDefs.h>
#include <Directory.h>
#include <FindDirectory.h>
#include <fs_interface.h>
#include <Path.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <net/if_dl.h>
@ -113,6 +118,13 @@ EthernetSettingsView::AttachedToWindow()
fOKButton->SetTarget(this);
fApplyButton->SetTarget(this);
fDeviceMenuField->Menu()->SetTargetForItems(this);
// Display first adapter by default.
BMessage* info = new BMessage(kMsgInfo);
info->AddString("interface", fSettings.ItemAt(0)->GetName());
_ShowConfiguration(info);
}
@ -151,8 +163,10 @@ EthernetSettingsView::EthernetSettingsView(BRect rect)
BPopUpMenu* modemenu = new BPopUpMenu("modes");
BMenuItem* modeitem = new BMenuItem("dummy", NULL);
modemenu->AddItem(modeitem);
BMenuItem* staticitem = new BMenuItem("Static", NULL);
modemenu->AddItem(staticitem);
BMenuItem* dhcpitem = new BMenuItem("DHCP", NULL);
modemenu->AddItem(dhcpitem);
fDeviceMenuField = new BMenuField(frame, "networkcards", "Adapter:", devmenu);
fDeviceMenuField->SetDivider(
@ -242,11 +256,23 @@ EthernetSettingsView::_ShowConfiguration(BMessage* message)
int i;
for (i=0; i<fSettings.CountItems();i++) {
if (strcmp(fSettings.ItemAt(i)->GetName(), name) == 0) {
fDeviceMenuField->Menu()->FindItem(name)->SetMarked(true);
fIPTextControl->SetText(fSettings.ItemAt(i)->GetIP());
fGatewayTextControl->SetText(fSettings.ItemAt(i)->GetGateway());
fNetMaskTextControl->SetText(fSettings.ItemAt(i)->GetNetmask());
if (fSettings.ItemAt(i)->GetAutoConfigure() == true) {
fTypeMenuField->Menu()->FindItem("DHCP")->SetMarked(true);
fTypeMenuField->Menu()->FindItem("Static")->SetMarked(false);
} else {
fTypeMenuField->Menu()->FindItem("Static")->SetMarked(true);
fTypeMenuField->Menu()->FindItem("DHCP")->SetMarked(false);
}
fTypeMenuField->Menu()->SetLabelFromMarked(true);
fDeviceMenuField->Menu()->SetLabelFromMarked(true);
if (fSettings.ItemAt(i)->fNameservers.CountItems() == 2) {
fSecondaryDNSTextControl->SetText(
@ -263,7 +289,6 @@ EthernetSettingsView::_ShowConfiguration(BMessage* message)
}
// Obtain DNS
@ -271,6 +296,118 @@ EthernetSettingsView::_ShowConfiguration(BMessage* message)
}
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)->
SetNetmask(fNetMaskTextControl->Text());
fSettings.ItemAt(i)->
SetGateway(fGatewayTextControl->Text());
fSettings.ItemAt(i)->SetAutoConfigure(false);
fSettings.ItemAt(i)->fNameservers.MakeEmpty();
fSettings.ItemAt(i)->fNameservers.AddItem(new BString(
fPrimaryDNSTextControl->Text()));
fSettings.ItemAt(i)->fNameservers.AddItem(new BString(
fSecondaryDNSTextControl->Text()));
}
}
}
}
void
EthernetSettingsView::_SaveConfiguration()
{
_ApplyControlsToConfiguration();
_SaveDNSConfiguration();
_SaveAdaptersConfiguration();
}
void
EthernetSettingsView::_SaveDNSConfiguration()
{
FILE* fp;
if ((fp = fopen("/etc/resolv.conf", "w")) != NULL) {
fprintf(fp, "# Generated by Network Preflet\n");
int i;
for (i = 0; i < fSettings.CountItems(); i++) {
// 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());
}
}
fclose(fp);
}
}
void
EthernetSettingsView::_SaveAdaptersConfiguration()
{
BPath path;
status_t status = _GetPath("interfaces", path);
if (status < B_OK)
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;
}
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);
}
}
}
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;
}
void
@ -280,6 +417,9 @@ EthernetSettingsView::MessageReceived(BMessage* message)
case kMsgInfo:
_ShowConfiguration(message);
break;
case kMsgApply:
_SaveConfiguration();
break;
default:
BView::MessageReceived(message);
}

View File

@ -34,6 +34,8 @@ class EthernetSettingsView : public BView {
virtual void AttachedToWindow();
virtual void DetachedFromWindow();
void SaveProfile(BString profileName);
void LoadProfile(BString profileName);
private:
BButton* fCloseButton;
@ -54,6 +56,12 @@ class EthernetSettingsView : public BView {
void _GatherInterfaces();
bool _PrepareRequest(struct ifreq& request, const char* name);
void _ShowConfiguration(BMessage* message);
void _SaveConfiguration();
void _SaveDNSConfiguration();
void _SaveAdaptersConfiguration();
void _ApplyControlsToConfiguration();
status_t _GetPath(const char* name, BPath& path);
};

View File

@ -154,7 +154,16 @@ Settings::ReadConfiguration()
bufferDeleter.Detach();
free(buffer);
// Now that silly DNS problem...
uint32 flags = 0;
if (ioctl(fSocket, SIOCGIFFLAGS, &request, sizeof(struct ifreq)) == 0)
flags = request.ifr_flags;
if ((flags & IFF_AUTO_CONFIGURED) != 0)
fAuto = true;
else
fAuto = false;
// read resolv.conf for the dns.
fNameservers.MakeEmpty();
#define MATCH(line, name) \
@ -173,25 +182,27 @@ Settings::ReadConfiguration()
if ((fp = fopen("/etc/resolv.conf", "r")) != NULL) {
/* 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')) {
fNameservers.AddItem(new BString(cp));
nserv++;
}
}
/* 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')) {
fNameservers.AddItem(new BString(cp));
nserv++;
}
}
fclose(fp);
continue;
}
fclose(fp);
}
}

View File

@ -17,22 +17,19 @@ class Settings {
virtual ~Settings();
void SetName(BString name);
void SetIP(BString ip);
void SetGateway(BString ip);
void SetNetmask(BString ip);
void SetAutoConfigure(bool t);
void SetIP(BString ip) {fIP = ip; }
void SetGateway(BString ip) {fGateway = ip; }
void SetNetmask(BString ip) {fNetmask = ip; }
void SetAutoConfigure(bool t) {fAuto = t; }
const char* GetIP() {return fIP.String(); }
const char* GetGateway() {return fGateway.String(); }
const char* GetNetmask() {return fNetmask.String(); }
const char* GetName() {return fName.String(); }
bool GetAutoConfigure() const {return fAuto; }
bool GetAutoConfigure() {return fAuto; }
BObjectList<BString> fNameservers;
void SaveProfile(BString profileName);
void LoadProfile(BString profileName);
void SaveConfiguration();
void ReadConfiguration();
private:
bool _PrepareRequest(struct ifreq& request);