From b2e91cc3e773565a058927f81882e596db546159 Mon Sep 17 00:00:00 2001 From: Adrien Destugues Date: Tue, 25 Nov 2014 14:47:32 +0100 Subject: [PATCH] Network prefs: save interfaces to file. Fixes #11496. --- .../InterfacesAddOn/InterfacesAddOn.cpp | 32 ++++++++- .../InterfacesAddOn/InterfacesListView.cpp | 4 +- .../InterfacesAddOn/InterfacesListView.h | 2 +- .../InterfacesAddOn/NetworkSettings.cpp | 71 +++++++++++++++++-- .../network/InterfacesAddOn/NetworkSettings.h | 6 +- 5 files changed, 104 insertions(+), 11 deletions(-) diff --git a/src/preferences/network/InterfacesAddOn/InterfacesAddOn.cpp b/src/preferences/network/InterfacesAddOn/InterfacesAddOn.cpp index d8a3ad09b6..886e5c354b 100644 --- a/src/preferences/network/InterfacesAddOn/InterfacesAddOn.cpp +++ b/src/preferences/network/InterfacesAddOn/InterfacesAddOn.cpp @@ -22,10 +22,13 @@ #include #include #include +#include +#include #include #include #include #include +#include #include @@ -99,7 +102,34 @@ InterfacesAddOn::Save() InterfaceView* settingsView = _SettingsView(); if (settingsView != NULL) settingsView->Apply(); - return fListView->SaveItems(); + + BString settingsData; + + status_t result = fListView->SaveItems(settingsData); + if (result != B_OK) + return result; + + if (settingsData.IsEmpty()) { + // Nothing to save, all interfaces are auto-configured + return B_OK; + } + + BPath path; + result = find_directory(B_SYSTEM_SETTINGS_DIRECTORY, &path, true); + if (result != B_OK) + return result; + + path.Append("network"); + create_directory(path.Path(), 0755); + + path.Append("interfaces"); + + BFile settingsFile(path.Path(), B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE); + result = settingsFile.Write(settingsData.String(),settingsData.Length()); + if (result != settingsData.Length()) + return result; + + return B_OK; } diff --git a/src/preferences/network/InterfacesAddOn/InterfacesListView.cpp b/src/preferences/network/InterfacesAddOn/InterfacesListView.cpp index be31545d2c..b0bf32ac44 100644 --- a/src/preferences/network/InterfacesAddOn/InterfacesListView.cpp +++ b/src/preferences/network/InterfacesAddOn/InterfacesListView.cpp @@ -492,7 +492,7 @@ InterfacesListView::FindItem(BPoint where) status_t -InterfacesListView::SaveItems() +InterfacesListView::SaveItems(BString& settingsData) { // Grab each fSettings instance and write it's settings for (int32 i = CountItems(); i-- > 0;) { @@ -504,8 +504,8 @@ InterfacesListView::SaveItems() continue; NetworkSettings* ns = item->GetSettings(); - // TODO : SetConfiguration doesn't use the interface settings file ns->SetConfiguration(); + settingsData << ns->GenerateConfiguration(); } return B_OK; diff --git a/src/preferences/network/InterfacesAddOn/InterfacesListView.h b/src/preferences/network/InterfacesAddOn/InterfacesListView.h index 6fe209b0ab..baceaded28 100644 --- a/src/preferences/network/InterfacesAddOn/InterfacesListView.h +++ b/src/preferences/network/InterfacesAddOn/InterfacesListView.h @@ -70,7 +70,7 @@ public: InterfaceListItem* FindItem(const char* name); InterfaceListItem* FindItem(BPoint where); - status_t SaveItems(); + status_t SaveItems(BString& settingsData); protected: virtual void AttachedToWindow(); diff --git a/src/preferences/network/InterfacesAddOn/NetworkSettings.cpp b/src/preferences/network/InterfacesAddOn/NetworkSettings.cpp index 4aa969d0de..5330062849 100644 --- a/src/preferences/network/InterfacesAddOn/NetworkSettings.cpp +++ b/src/preferences/network/InterfacesAddOn/NetworkSettings.cpp @@ -339,13 +339,76 @@ NetworkSettings::LoadConfiguration() } -/*! WriteConfiguration reads this classes settings and write them to - the current interface configuration file that NetServer watches for. +/*! GenerateConfiguration reads this classes settings and write them to + a BString. This can then be put in the interfaces settings file to make + the settings persistant. */ -void -NetworkSettings::WriteConfiguration() +BString +NetworkSettings::GenerateConfiguration() { + // TODO should use ProtocolVersions to know which protocols are there + bool autoConfigure = true; + if (!IsDisabled()) { + for (int i = 0; i < MAX_PROTOCOLS; i++) { + if (fProtocols[i].present && strlen(IP(fProtocols[i].inet_id)) != 0 + && !AutoConfigure(fProtocols[i].inet_id)) { + autoConfigure = false; + } + } + } + // If the interface is fully autoconfigured, don't include it in the + // settings file. + if (autoConfigure) + return BString(); + + BString result; + + result.SetToFormat("interface %s {\n", Name()); + + if (IsDisabled()) + result << "\tdisabled\ttrue\n"; + else { + for (int i = 0; i < MAX_PROTOCOLS; i++) { + if (fProtocols[i].present && strlen(IP(fProtocols[i].inet_id)) != 0 + && !AutoConfigure(fProtocols[i].inet_id)) { + result << "\taddress {\n"; + + result << "\t\tfamily\t"; + if (fProtocols[i].inet_id == AF_INET) + result << "inet\n"; + else if (fProtocols[i].inet_id == AF_INET6) + result << "inet6\n"; + else { + // FIXME the struct protocols should know the name to use. + debugger("Unknown protocol found!"); + } + + result << "\t\taddress\t" << IP(fProtocols[i].inet_id) << "\n"; + result << "\t\tgateway\t" << Gateway(fProtocols[i].inet_id) + << "\n"; + result << "\t\tmask\t" << Netmask(fProtocols[i].inet_id) + << "\n"; + result << "\t}\n"; + } + } + } + +#if 0 + if (!networks.empty()) { + for (size_t index = 0; index < networks.size(); index++) { + wireless_network& network = networks[index]; + fprintf(fp, "\tnetwork %s {\n", network.name); + fprintf(fp, "\t\tmac\t%s\n", + network.address.ToString().String()); + fprintf(fp, "\t}\n"); + } + } +#endif + + result << "}\n\n"; + + return result; } diff --git a/src/preferences/network/InterfacesAddOn/NetworkSettings.h b/src/preferences/network/InterfacesAddOn/NetworkSettings.h index e2ed80477b..4ba77cf2d0 100644 --- a/src/preferences/network/InterfacesAddOn/NetworkSettings.h +++ b/src/preferences/network/InterfacesAddOn/NetworkSettings.h @@ -109,10 +109,10 @@ public: void ReadConfiguration(); void SetConfiguration(); - /* Grab and write interface settings from interface configuration - file and let NetServer sort it out */ + /* Grab interface settings from interface configuration file */ void LoadConfiguration(); - void WriteConfiguration(); + /* Generate settings file entry suitable for interfaces settings */ + BString GenerateConfiguration(); private: status_t _DetectProtocols();