From b00dcbcad2226979bc585c84dc50dcc02b0c0f9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Thu, 5 Mar 2015 12:54:35 +0000 Subject: [PATCH] BNetworkSettings: added some convenience methods. * From InterfaceAddressView: FindFirstAddress(), and IsAutoConfigure(). * Removed some debug leftovers in InterfaceAddressView, too. --- .../network_settings/NetworkSettings.h | 10 ++- .../ipv4/InterfaceAddressView.cpp | 34 ++-------- .../ipv4/InterfaceAddressView.h | 5 +- .../network/libnetapi/NetworkSettings.cpp | 65 ++++++++++++++++++- src/servers/net/NetServer.cpp | 2 +- 5 files changed, 82 insertions(+), 34 deletions(-) diff --git a/headers/os/add-ons/network_settings/NetworkSettings.h b/headers/os/add-ons/network_settings/NetworkSettings.h index fb36741beb..38f7464402 100644 --- a/headers/os/add-ons/network_settings/NetworkSettings.h +++ b/headers/os/add-ons/network_settings/NetworkSettings.h @@ -21,6 +21,9 @@ namespace BNetworkKit { +class BNetworkInterfaceSettings; + + class BNetworkSettings { public: static const uint32 kMsgInterfaceSettingsUpdated = 'SUif'; @@ -37,6 +40,8 @@ public: BMessage& interface); status_t AddInterface(const BMessage& interface); status_t RemoveInterface(const char* name); + BNetworkInterfaceSettings + Interface(const char* name); int32 CountNetworks() const; status_t GetNextNetwork(uint32& cookie, @@ -105,7 +110,7 @@ public: int Family() const; void SetFamily(int family); - bool AutoConfigure() const; + bool IsAutoConfigure() const; void SetAutoConfigure(bool configure); const BNetworkAddress& @@ -164,10 +169,13 @@ public: AddressAt(int32 index) const; BNetworkInterfaceAddressSettings& AddressAt(int32 index); + int32 FindFirstAddress(int family) const; void AddAddress(const BNetworkInterfaceAddressSettings& address); void RemoveAddress(int32 index); + bool IsAutoConfigure(int family) const; + status_t GetMessage(BMessage& data) const; private: diff --git a/src/add-ons/network_settings/ipv4/InterfaceAddressView.cpp b/src/add-ons/network_settings/ipv4/InterfaceAddressView.cpp index 9317cb717b..e0c7d5ddb8 100644 --- a/src/add-ons/network_settings/ipv4/InterfaceAddressView.cpp +++ b/src/add-ons/network_settings/ipv4/InterfaceAddressView.cpp @@ -215,8 +215,11 @@ InterfaceAddressView::_EnableFields(bool enable) void InterfaceAddressView::_UpdateFields() { - bool autoConfigure = (fInterface.Flags() - & (IFF_AUTO_CONFIGURED | IFF_CONFIGURING)) != 0; + bool autoConfigure = fInterfaceSettings.IsEmpty(); + if (!autoConfigure) { + BNetworkInterfaceSettings settings(fInterfaceSettings); + autoConfigure = settings.IsAutoConfigure(fFamily); + } BNetworkInterfaceAddress address; status_t status = B_ERROR; @@ -226,13 +229,6 @@ InterfaceAddressView::_UpdateFields() status = fInterface.GetAddressAt(index, address); if (index < 0 || status != B_OK || address.Address().IsEmpty() && !autoConfigure) { - if (status == B_OK) { - // Check persistent settings for the mode -- the address - // can also be empty if the automatic configuration hasn't - // started yet. - autoConfigure = fInterfaceSettings.IsEmpty() - || fInterfaceSettings.GetBool("auto_config", false); - } if (!autoConfigure) { _SetModeField(kModeDisabled); return; @@ -286,12 +282,11 @@ InterfaceAddressView::_UpdateSettings() // Remove previous address for family - int32 index = _FindFirstAddress(settings, fFamily); + int32 index = settings.FindFirstAddress(fFamily); if (index < 0) - index = _FindFirstAddress(settings, AF_UNSPEC); + index = settings.FindFirstAddress(AF_UNSPEC); if (index >= 0 && index < settings.CountAddresses()) { BNetworkInterfaceAddressSettings& address = settings.AddressAt(index); - printf("family = %d", address.Family()); _ConfigureAddress(address); } else { BNetworkInterfaceAddressSettings address; @@ -322,27 +317,12 @@ InterfaceAddressView::_Mode() const } -int32 -InterfaceAddressView::_FindFirstAddress( - const BNetworkInterfaceSettings& settings, int family) -{ - for (int32 index = 0; index < settings.CountAddresses(); index++) { - const BNetworkInterfaceAddressSettings address - = settings.AddressAt(index); - if (address.Family() == family) - return index; - } - return -1; -} - - void InterfaceAddressView::_ConfigureAddress( BNetworkInterfaceAddressSettings& settings) { uint32 mode = _Mode(); -printf("family: %d\n", (int)fFamily); settings.SetFamily(fFamily); settings.SetAutoConfigure(mode == kModeAuto); diff --git a/src/add-ons/network_settings/ipv4/InterfaceAddressView.h b/src/add-ons/network_settings/ipv4/InterfaceAddressView.h index d026d93aef..a8b8f99002 100644 --- a/src/add-ons/network_settings/ipv4/InterfaceAddressView.h +++ b/src/add-ons/network_settings/ipv4/InterfaceAddressView.h @@ -13,6 +13,7 @@ #include #include #include +#include class BButton; @@ -41,16 +42,12 @@ public: void ConfigurationUpdated(const BMessage& message); - private: void _EnableFields(bool enable); void _UpdateFields(); void _SetModeField(uint32 mode); void _UpdateSettings(); uint32 _Mode() const; - int32 _FindFirstAddress( - const BNetworkInterfaceSettings& settings, - int family); void _ConfigureAddress( BNetworkInterfaceAddressSettings& address); diff --git a/src/kits/network/libnetapi/NetworkSettings.cpp b/src/kits/network/libnetapi/NetworkSettings.cpp index ec61c55084..292e01e9f4 100644 --- a/src/kits/network/libnetapi/NetworkSettings.cpp +++ b/src/kits/network/libnetapi/NetworkSettings.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -348,6 +349,15 @@ BNetworkSettings::RemoveInterface(const char* name) } +BNetworkInterfaceSettings +BNetworkSettings::Interface(const char* name) +{ + BMessage interface; + GetInterface(name, interface); + return BNetworkInterfaceSettings(interface); +} + + int32 BNetworkSettings::CountNetworks() const { @@ -926,7 +936,7 @@ BNetworkInterfaceAddressSettings::SetFamily(int family) bool -BNetworkInterfaceAddressSettings::AutoConfigure() const +BNetworkInterfaceAddressSettings::IsAutoConfigure() const { return fAutoConfigure; } @@ -1160,6 +1170,18 @@ BNetworkInterfaceSettings::AddressAt(int32 index) } +int32 +BNetworkInterfaceSettings::FindFirstAddress(int family) const +{ + for (int32 index = 0; index < CountAddresses(); index++) { + const BNetworkInterfaceAddressSettings address = AddressAt(index); + if (address.Family() == family) + return index; + } + return -1; +} + + void BNetworkInterfaceSettings::AddAddress( const BNetworkInterfaceAddressSettings& address) @@ -1175,6 +1197,47 @@ BNetworkInterfaceSettings::RemoveAddress(int32 index) } +/*! This is a convenience method that returns the current state of the + interface, not just the one configured. + + This means, even if the settings say: auto configured, this method + may still return false, if the configuration has been manually tempered + with. +*/ +bool +BNetworkInterfaceSettings::IsAutoConfigure(int family) const +{ + BNetworkInterface interface(fName); + // TODO: this needs to happen at protocol level + if ((interface.Flags() & (IFF_AUTO_CONFIGURED | IFF_CONFIGURING)) != 0) + return true; + + BNetworkInterfaceAddress address; + status_t status = B_ERROR; + + int32 index = interface.FindFirstAddress(family); + if (index >= 0) + status = interface.GetAddressAt(index, address); + if (index < 0 || status != B_OK || address.Address().IsEmpty()) { + if (status == B_OK) { + // Check persistent settings for the mode -- the address + // can also be empty if the automatic configuration hasn't + // started yet (for example, because there is no link). + int32 index = FindFirstAddress(family); + if (index < 0) + index = FindFirstAddress(AF_UNSPEC); + if (index >= 0) { + const BNetworkInterfaceAddressSettings& address + = AddressAt(index); + return address.IsAutoConfigure(); + } + } + } + + return false; +} + + status_t BNetworkInterfaceSettings::GetMessage(BMessage& data) const { diff --git a/src/servers/net/NetServer.cpp b/src/servers/net/NetServer.cpp index 06ed2ad4b3..b0b89eea03 100644 --- a/src/servers/net/NetServer.cpp +++ b/src/servers/net/NetServer.cpp @@ -473,7 +473,7 @@ NetServer::_ConfigureInterface(BMessage& message) &addressMessage) == B_OK; index++) { BNetworkInterfaceAddressSettings addressSettings(addressMessage); - if (addressSettings.AutoConfigure()) { + if (addressSettings.IsAutoConfigure()) { _QuitLooperForDevice(name); startAutoConfig = true; } else if (!addressSettings.Gateway().IsEmpty()) {