BNetworkSettings: added some convenience methods.
* From InterfaceAddressView: FindFirstAddress(), and IsAutoConfigure(). * Removed some debug leftovers in InterfaceAddressView, too.
This commit is contained in:
@@ -21,6 +21,9 @@
|
|||||||
namespace BNetworkKit {
|
namespace BNetworkKit {
|
||||||
|
|
||||||
|
|
||||||
|
class BNetworkInterfaceSettings;
|
||||||
|
|
||||||
|
|
||||||
class BNetworkSettings {
|
class BNetworkSettings {
|
||||||
public:
|
public:
|
||||||
static const uint32 kMsgInterfaceSettingsUpdated = 'SUif';
|
static const uint32 kMsgInterfaceSettingsUpdated = 'SUif';
|
||||||
@@ -37,6 +40,8 @@ public:
|
|||||||
BMessage& interface);
|
BMessage& interface);
|
||||||
status_t AddInterface(const BMessage& interface);
|
status_t AddInterface(const BMessage& interface);
|
||||||
status_t RemoveInterface(const char* name);
|
status_t RemoveInterface(const char* name);
|
||||||
|
BNetworkInterfaceSettings
|
||||||
|
Interface(const char* name);
|
||||||
|
|
||||||
int32 CountNetworks() const;
|
int32 CountNetworks() const;
|
||||||
status_t GetNextNetwork(uint32& cookie,
|
status_t GetNextNetwork(uint32& cookie,
|
||||||
@@ -105,7 +110,7 @@ public:
|
|||||||
|
|
||||||
int Family() const;
|
int Family() const;
|
||||||
void SetFamily(int family);
|
void SetFamily(int family);
|
||||||
bool AutoConfigure() const;
|
bool IsAutoConfigure() const;
|
||||||
void SetAutoConfigure(bool configure);
|
void SetAutoConfigure(bool configure);
|
||||||
|
|
||||||
const BNetworkAddress&
|
const BNetworkAddress&
|
||||||
@@ -164,10 +169,13 @@ public:
|
|||||||
AddressAt(int32 index) const;
|
AddressAt(int32 index) const;
|
||||||
BNetworkInterfaceAddressSettings&
|
BNetworkInterfaceAddressSettings&
|
||||||
AddressAt(int32 index);
|
AddressAt(int32 index);
|
||||||
|
int32 FindFirstAddress(int family) const;
|
||||||
void AddAddress(const
|
void AddAddress(const
|
||||||
BNetworkInterfaceAddressSettings& address);
|
BNetworkInterfaceAddressSettings& address);
|
||||||
void RemoveAddress(int32 index);
|
void RemoveAddress(int32 index);
|
||||||
|
|
||||||
|
bool IsAutoConfigure(int family) const;
|
||||||
|
|
||||||
status_t GetMessage(BMessage& data) const;
|
status_t GetMessage(BMessage& data) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
@@ -215,8 +215,11 @@ InterfaceAddressView::_EnableFields(bool enable)
|
|||||||
void
|
void
|
||||||
InterfaceAddressView::_UpdateFields()
|
InterfaceAddressView::_UpdateFields()
|
||||||
{
|
{
|
||||||
bool autoConfigure = (fInterface.Flags()
|
bool autoConfigure = fInterfaceSettings.IsEmpty();
|
||||||
& (IFF_AUTO_CONFIGURED | IFF_CONFIGURING)) != 0;
|
if (!autoConfigure) {
|
||||||
|
BNetworkInterfaceSettings settings(fInterfaceSettings);
|
||||||
|
autoConfigure = settings.IsAutoConfigure(fFamily);
|
||||||
|
}
|
||||||
|
|
||||||
BNetworkInterfaceAddress address;
|
BNetworkInterfaceAddress address;
|
||||||
status_t status = B_ERROR;
|
status_t status = B_ERROR;
|
||||||
@@ -226,13 +229,6 @@ InterfaceAddressView::_UpdateFields()
|
|||||||
status = fInterface.GetAddressAt(index, address);
|
status = fInterface.GetAddressAt(index, address);
|
||||||
if (index < 0 || status != B_OK
|
if (index < 0 || status != B_OK
|
||||||
|| address.Address().IsEmpty() && !autoConfigure) {
|
|| 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) {
|
if (!autoConfigure) {
|
||||||
_SetModeField(kModeDisabled);
|
_SetModeField(kModeDisabled);
|
||||||
return;
|
return;
|
||||||
@@ -286,12 +282,11 @@ InterfaceAddressView::_UpdateSettings()
|
|||||||
|
|
||||||
// Remove previous address for family
|
// Remove previous address for family
|
||||||
|
|
||||||
int32 index = _FindFirstAddress(settings, fFamily);
|
int32 index = settings.FindFirstAddress(fFamily);
|
||||||
if (index < 0)
|
if (index < 0)
|
||||||
index = _FindFirstAddress(settings, AF_UNSPEC);
|
index = settings.FindFirstAddress(AF_UNSPEC);
|
||||||
if (index >= 0 && index < settings.CountAddresses()) {
|
if (index >= 0 && index < settings.CountAddresses()) {
|
||||||
BNetworkInterfaceAddressSettings& address = settings.AddressAt(index);
|
BNetworkInterfaceAddressSettings& address = settings.AddressAt(index);
|
||||||
printf("family = %d", address.Family());
|
|
||||||
_ConfigureAddress(address);
|
_ConfigureAddress(address);
|
||||||
} else {
|
} else {
|
||||||
BNetworkInterfaceAddressSettings address;
|
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
|
void
|
||||||
InterfaceAddressView::_ConfigureAddress(
|
InterfaceAddressView::_ConfigureAddress(
|
||||||
BNetworkInterfaceAddressSettings& settings)
|
BNetworkInterfaceAddressSettings& settings)
|
||||||
{
|
{
|
||||||
uint32 mode = _Mode();
|
uint32 mode = _Mode();
|
||||||
|
|
||||||
printf("family: %d\n", (int)fFamily);
|
|
||||||
settings.SetFamily(fFamily);
|
settings.SetFamily(fFamily);
|
||||||
settings.SetAutoConfigure(mode == kModeAuto);
|
settings.SetAutoConfigure(mode == kModeAuto);
|
||||||
|
|
||||||
|
|||||||
@@ -13,6 +13,7 @@
|
|||||||
#include <GroupView.h>
|
#include <GroupView.h>
|
||||||
#include <NetworkInterface.h>
|
#include <NetworkInterface.h>
|
||||||
#include <NetworkSettings.h>
|
#include <NetworkSettings.h>
|
||||||
|
#include <NetworkSettingsAddOn.h>
|
||||||
|
|
||||||
|
|
||||||
class BButton;
|
class BButton;
|
||||||
@@ -41,16 +42,12 @@ public:
|
|||||||
|
|
||||||
void ConfigurationUpdated(const BMessage& message);
|
void ConfigurationUpdated(const BMessage& message);
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void _EnableFields(bool enable);
|
void _EnableFields(bool enable);
|
||||||
void _UpdateFields();
|
void _UpdateFields();
|
||||||
void _SetModeField(uint32 mode);
|
void _SetModeField(uint32 mode);
|
||||||
void _UpdateSettings();
|
void _UpdateSettings();
|
||||||
uint32 _Mode() const;
|
uint32 _Mode() const;
|
||||||
int32 _FindFirstAddress(
|
|
||||||
const BNetworkInterfaceSettings& settings,
|
|
||||||
int family);
|
|
||||||
|
|
||||||
void _ConfigureAddress(
|
void _ConfigureAddress(
|
||||||
BNetworkInterfaceAddressSettings& address);
|
BNetworkInterfaceAddressSettings& address);
|
||||||
|
|||||||
@@ -19,6 +19,7 @@
|
|||||||
#include <FindDirectory.h>
|
#include <FindDirectory.h>
|
||||||
#include <fs_interface.h>
|
#include <fs_interface.h>
|
||||||
#include <NetworkDevice.h>
|
#include <NetworkDevice.h>
|
||||||
|
#include <NetworkInterface.h>
|
||||||
#include <Path.h>
|
#include <Path.h>
|
||||||
#include <PathMonitor.h>
|
#include <PathMonitor.h>
|
||||||
#include <String.h>
|
#include <String.h>
|
||||||
@@ -348,6 +349,15 @@ BNetworkSettings::RemoveInterface(const char* name)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
BNetworkInterfaceSettings
|
||||||
|
BNetworkSettings::Interface(const char* name)
|
||||||
|
{
|
||||||
|
BMessage interface;
|
||||||
|
GetInterface(name, interface);
|
||||||
|
return BNetworkInterfaceSettings(interface);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
int32
|
int32
|
||||||
BNetworkSettings::CountNetworks() const
|
BNetworkSettings::CountNetworks() const
|
||||||
{
|
{
|
||||||
@@ -926,7 +936,7 @@ BNetworkInterfaceAddressSettings::SetFamily(int family)
|
|||||||
|
|
||||||
|
|
||||||
bool
|
bool
|
||||||
BNetworkInterfaceAddressSettings::AutoConfigure() const
|
BNetworkInterfaceAddressSettings::IsAutoConfigure() const
|
||||||
{
|
{
|
||||||
return fAutoConfigure;
|
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
|
void
|
||||||
BNetworkInterfaceSettings::AddAddress(
|
BNetworkInterfaceSettings::AddAddress(
|
||||||
const BNetworkInterfaceAddressSettings& address)
|
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
|
status_t
|
||||||
BNetworkInterfaceSettings::GetMessage(BMessage& data) const
|
BNetworkInterfaceSettings::GetMessage(BMessage& data) const
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -473,7 +473,7 @@ NetServer::_ConfigureInterface(BMessage& message)
|
|||||||
&addressMessage) == B_OK; index++) {
|
&addressMessage) == B_OK; index++) {
|
||||||
BNetworkInterfaceAddressSettings addressSettings(addressMessage);
|
BNetworkInterfaceAddressSettings addressSettings(addressMessage);
|
||||||
|
|
||||||
if (addressSettings.AutoConfigure()) {
|
if (addressSettings.IsAutoConfigure()) {
|
||||||
_QuitLooperForDevice(name);
|
_QuitLooperForDevice(name);
|
||||||
startAutoConfig = true;
|
startAutoConfig = true;
|
||||||
} else if (!addressSettings.Gateway().IsEmpty()) {
|
} else if (!addressSettings.Gateway().IsEmpty()) {
|
||||||
|
|||||||
Reference in New Issue
Block a user