BNetworkSettings: made service stuff writable.
* And added the ability to disable a service without losing its configuration.
This commit is contained in:
@@ -22,6 +22,7 @@ namespace BNetworkKit {
|
|||||||
|
|
||||||
|
|
||||||
class BNetworkInterfaceSettings;
|
class BNetworkInterfaceSettings;
|
||||||
|
class BNetworkServiceSettings;
|
||||||
|
|
||||||
|
|
||||||
class BNetworkSettings {
|
class BNetworkSettings {
|
||||||
@@ -57,6 +58,8 @@ public:
|
|||||||
status_t GetService(const char* name, BMessage& service);
|
status_t GetService(const char* name, BMessage& service);
|
||||||
status_t AddService(const BMessage& service);
|
status_t AddService(const BMessage& service);
|
||||||
status_t RemoveService(const char* name);
|
status_t RemoveService(const char* name);
|
||||||
|
BNetworkServiceSettings
|
||||||
|
Service(const char* name);
|
||||||
|
|
||||||
status_t StartMonitoring(const BMessenger& target);
|
status_t StartMonitoring(const BMessenger& target);
|
||||||
status_t StopMonitoring(const BMessenger& target);
|
status_t StopMonitoring(const BMessenger& target);
|
||||||
@@ -231,17 +234,43 @@ public:
|
|||||||
status_t InitCheck() const;
|
status_t InitCheck() const;
|
||||||
|
|
||||||
const char* Name() const;
|
const char* Name() const;
|
||||||
|
void SetName(const char* name);
|
||||||
bool IsStandAlone() const;
|
bool IsStandAlone() const;
|
||||||
|
void SetStandAlone(bool alone);
|
||||||
|
bool IsEnabled() const;
|
||||||
|
void SetEnabled(bool enable);
|
||||||
|
|
||||||
|
int Family() const;
|
||||||
|
void SetFamily(int family);
|
||||||
|
int Protocol() const;
|
||||||
|
void SetProtocol(int protocol);
|
||||||
|
int Type() const;
|
||||||
|
void SetType(int type);
|
||||||
|
int Port() const;
|
||||||
|
void SetPort(int port);
|
||||||
|
|
||||||
int32 CountArguments() const;
|
int32 CountArguments() const;
|
||||||
const char* ArgumentAt(int32 index) const;
|
const char* ArgumentAt(int32 index) const;
|
||||||
|
void AddArgument(const char* argument);
|
||||||
|
void RemoveArgument(int32 index);
|
||||||
|
|
||||||
int32 CountAddresses() const;
|
int32 CountAddresses() const;
|
||||||
const BNetworkServiceAddressSettings&
|
const BNetworkServiceAddressSettings&
|
||||||
AddressAt(int32 index) const;
|
AddressAt(int32 index) const;
|
||||||
|
void AddAddress(const
|
||||||
|
BNetworkServiceAddressSettings& address);
|
||||||
|
void RemoveAddress(int32 index);
|
||||||
|
|
||||||
|
status_t GetMessage(BMessage& data) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const BMessage& fData;
|
BString fName;
|
||||||
|
int32 fFamily;
|
||||||
|
int32 fType;
|
||||||
|
int32 fProtocol;
|
||||||
|
int32 fPort;
|
||||||
|
bool fEnabled;
|
||||||
|
bool fStandAlone;
|
||||||
BStringList fArguments;
|
BStringList fArguments;
|
||||||
std::vector<BNetworkServiceAddressSettings>
|
std::vector<BNetworkServiceAddressSettings>
|
||||||
fAddresses;
|
fAddresses;
|
||||||
|
|||||||
@@ -120,6 +120,7 @@ const static settings_template kServiceAddressTemplate[] = {
|
|||||||
|
|
||||||
const static settings_template kServiceTemplate[] = {
|
const static settings_template kServiceTemplate[] = {
|
||||||
{B_STRING_TYPE, "name", NULL, true},
|
{B_STRING_TYPE, "name", NULL, true},
|
||||||
|
{B_BOOL_TYPE, "disabled", NULL},
|
||||||
{B_MESSAGE_TYPE, "address", kServiceAddressTemplate},
|
{B_MESSAGE_TYPE, "address", kServiceAddressTemplate},
|
||||||
{B_STRING_TYPE, "user", NULL},
|
{B_STRING_TYPE, "user", NULL},
|
||||||
{B_STRING_TYPE, "group", NULL},
|
{B_STRING_TYPE, "group", NULL},
|
||||||
@@ -466,6 +467,15 @@ BNetworkSettings::RemoveService(const char* name)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
BNetworkServiceSettings
|
||||||
|
BNetworkSettings::Service(const char* name)
|
||||||
|
{
|
||||||
|
BMessage service;
|
||||||
|
GetService(name, service);
|
||||||
|
return BNetworkServiceSettings(service);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
status_t
|
status_t
|
||||||
BNetworkSettings::StartMonitoring(const BMessenger& target)
|
BNetworkSettings::StartMonitoring(const BMessenger& target)
|
||||||
{
|
{
|
||||||
@@ -1393,10 +1403,16 @@ BNetworkServiceAddressSettings::operator==(
|
|||||||
|
|
||||||
BNetworkServiceSettings::BNetworkServiceSettings(const BMessage& message)
|
BNetworkServiceSettings::BNetworkServiceSettings(const BMessage& message)
|
||||||
:
|
:
|
||||||
fData(message)
|
fType(-1),
|
||||||
|
fProtocol(-1),
|
||||||
|
fPort(-1),
|
||||||
|
fEnabled(true),
|
||||||
|
fStandAlone(false)
|
||||||
{
|
{
|
||||||
// TODO: user/group is currently ignored!
|
// TODO: user/group is currently ignored!
|
||||||
|
|
||||||
|
fName = message.GetString("name");
|
||||||
|
|
||||||
// Default family/port/protocol/type for all addresses
|
// Default family/port/protocol/type for all addresses
|
||||||
|
|
||||||
// we default to inet/tcp/port-from-service-name if nothing is specified
|
// we default to inet/tcp/port-from-service-name if nothing is specified
|
||||||
@@ -1404,35 +1420,33 @@ BNetworkServiceSettings::BNetworkServiceSettings(const BMessage& message)
|
|||||||
if (message.FindString("family", &string) != B_OK)
|
if (message.FindString("family", &string) != B_OK)
|
||||||
string = "inet";
|
string = "inet";
|
||||||
|
|
||||||
int32 serviceFamily = get_address_family(string);
|
fFamily = get_address_family(string);
|
||||||
if (serviceFamily == AF_UNSPEC)
|
if (fFamily == AF_UNSPEC)
|
||||||
serviceFamily = AF_INET;
|
fFamily = AF_INET;
|
||||||
|
|
||||||
int32 serviceProtocol;
|
|
||||||
if (message.FindString("protocol", &string) == B_OK)
|
if (message.FindString("protocol", &string) == B_OK)
|
||||||
serviceProtocol = parse_protocol(string);
|
fProtocol = parse_protocol(string);
|
||||||
else {
|
else {
|
||||||
string = "tcp";
|
string = "tcp";
|
||||||
// we set 'string' here for an eventual call to getservbyname()
|
// we set 'string' here for an eventual call to getservbyname()
|
||||||
// below
|
// below
|
||||||
serviceProtocol = IPPROTO_TCP;
|
fProtocol = IPPROTO_TCP;
|
||||||
}
|
}
|
||||||
|
|
||||||
int32 servicePort;
|
if (message.FindInt32("port", &fPort) != B_OK) {
|
||||||
if (message.FindInt32("port", &servicePort) != B_OK) {
|
|
||||||
struct servent* servent = getservbyname(Name(), string);
|
struct servent* servent = getservbyname(Name(), string);
|
||||||
if (servent != NULL)
|
if (servent != NULL)
|
||||||
servicePort = ntohs(servent->s_port);
|
fPort = ntohs(servent->s_port);
|
||||||
else
|
else
|
||||||
servicePort = -1;
|
fPort = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int32 serviceType = -1;
|
if (message.FindString("type", &string) == B_OK)
|
||||||
if (message.FindString("type", &string) == B_OK) {
|
fType = parse_type(string);
|
||||||
serviceType = parse_type(string);
|
else
|
||||||
} else {
|
fType = type_for_protocol(fProtocol);
|
||||||
serviceType = type_for_protocol(serviceProtocol);
|
|
||||||
}
|
fStandAlone = message.GetBool("stand_alone");
|
||||||
|
|
||||||
const char* argument;
|
const char* argument;
|
||||||
for (int i = 0; message.FindString("launch", i, &argument) == B_OK; i++) {
|
for (int i = 0; message.FindString("launch", i, &argument) == B_OK; i++) {
|
||||||
@@ -1442,12 +1456,12 @@ BNetworkServiceSettings::BNetworkServiceSettings(const BMessage& message)
|
|||||||
BMessage addressData;
|
BMessage addressData;
|
||||||
int32 i = 0;
|
int32 i = 0;
|
||||||
for (; message.FindMessage("address", i, &addressData) == B_OK; i++) {
|
for (; message.FindMessage("address", i, &addressData) == B_OK; i++) {
|
||||||
BNetworkServiceAddressSettings address(addressData, serviceFamily,
|
BNetworkServiceAddressSettings address(addressData, fFamily,
|
||||||
serviceType, serviceProtocol, servicePort);
|
fType, fProtocol, fPort);
|
||||||
fAddresses.push_back(address);
|
fAddresses.push_back(address);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (i == 0 && (serviceFamily < 0 || servicePort < 0)) {
|
if (i == 0 && (fFamily < 0 || fPort < 0)) {
|
||||||
// no address specified
|
// no address specified
|
||||||
printf("service %s has no address specified\n", Name());
|
printf("service %s has no address specified\n", Name());
|
||||||
return;
|
return;
|
||||||
@@ -1456,10 +1470,10 @@ BNetworkServiceSettings::BNetworkServiceSettings(const BMessage& message)
|
|||||||
if (i == 0) {
|
if (i == 0) {
|
||||||
// no address specified, but family/port were given; add empty address
|
// no address specified, but family/port were given; add empty address
|
||||||
BNetworkServiceAddressSettings address;
|
BNetworkServiceAddressSettings address;
|
||||||
address.SetFamily(serviceFamily);
|
address.SetFamily(fFamily);
|
||||||
address.SetType(serviceType);
|
address.SetType(fType);
|
||||||
address.SetProtocol(serviceProtocol);
|
address.SetProtocol(fProtocol);
|
||||||
address.Address().SetToWildcard(serviceFamily, servicePort);
|
address.Address().SetToWildcard(fFamily, fPort);
|
||||||
|
|
||||||
fAddresses.push_back(address);
|
fAddresses.push_back(address);
|
||||||
}
|
}
|
||||||
@@ -1474,8 +1488,7 @@ BNetworkServiceSettings::~BNetworkServiceSettings()
|
|||||||
status_t
|
status_t
|
||||||
BNetworkServiceSettings::InitCheck() const
|
BNetworkServiceSettings::InitCheck() const
|
||||||
{
|
{
|
||||||
if (fData.HasString("name") && fData.HasString("launch")
|
if (!fName.IsEmpty() && !fArguments.IsEmpty() && CountAddresses() > 0)
|
||||||
&& CountAddresses() > 0)
|
|
||||||
return B_OK;
|
return B_OK;
|
||||||
|
|
||||||
return B_BAD_VALUE;
|
return B_BAD_VALUE;
|
||||||
@@ -1485,14 +1498,98 @@ BNetworkServiceSettings::InitCheck() const
|
|||||||
const char*
|
const char*
|
||||||
BNetworkServiceSettings::Name() const
|
BNetworkServiceSettings::Name() const
|
||||||
{
|
{
|
||||||
return fData.GetString("name");
|
return fName.String();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
BNetworkServiceSettings::SetName(const char* name)
|
||||||
|
{
|
||||||
|
fName = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool
|
bool
|
||||||
BNetworkServiceSettings::IsStandAlone() const
|
BNetworkServiceSettings::IsStandAlone() const
|
||||||
{
|
{
|
||||||
return fData.GetBool("stand_alone");
|
return fStandAlone;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
BNetworkServiceSettings::SetStandAlone(bool alone)
|
||||||
|
{
|
||||||
|
fStandAlone = alone;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool
|
||||||
|
BNetworkServiceSettings::IsEnabled() const
|
||||||
|
{
|
||||||
|
return InitCheck() == B_OK && !fEnabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
BNetworkServiceSettings::SetEnabled(bool enable)
|
||||||
|
{
|
||||||
|
fEnabled = enable;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int
|
||||||
|
BNetworkServiceSettings::Family() const
|
||||||
|
{
|
||||||
|
return fFamily;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
BNetworkServiceSettings::SetFamily(int family)
|
||||||
|
{
|
||||||
|
fFamily = family;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int
|
||||||
|
BNetworkServiceSettings::Protocol() const
|
||||||
|
{
|
||||||
|
return fProtocol;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
BNetworkServiceSettings::SetProtocol(int protocol)
|
||||||
|
{
|
||||||
|
fProtocol = protocol;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int
|
||||||
|
BNetworkServiceSettings::Type() const
|
||||||
|
{
|
||||||
|
return fType;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
BNetworkServiceSettings::SetType(int type)
|
||||||
|
{
|
||||||
|
fType = type;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int
|
||||||
|
BNetworkServiceSettings::Port() const
|
||||||
|
{
|
||||||
|
return fPort;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
BNetworkServiceSettings::SetPort(int port)
|
||||||
|
{
|
||||||
|
fPort = port;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -1522,3 +1619,55 @@ BNetworkServiceSettings::AddressAt(int32 index) const
|
|||||||
{
|
{
|
||||||
return fAddresses[index];
|
return fAddresses[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
BNetworkServiceSettings::AddAddress(
|
||||||
|
const BNetworkServiceAddressSettings& address)
|
||||||
|
{
|
||||||
|
fAddresses.push_back(address);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
BNetworkServiceSettings::RemoveAddress(int32 index)
|
||||||
|
{
|
||||||
|
fAddresses.erase(fAddresses.begin() + index);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
status_t
|
||||||
|
BNetworkServiceSettings::GetMessage(BMessage& data) const
|
||||||
|
{
|
||||||
|
status_t status = data.SetString("name", fName);
|
||||||
|
if (status == B_OK && !fEnabled)
|
||||||
|
status = data.SetBool("disabled", true);
|
||||||
|
if (status == B_OK && fStandAlone)
|
||||||
|
status = data.SetBool("stand_alone", true);
|
||||||
|
|
||||||
|
if (fFamily != AF_UNSPEC)
|
||||||
|
status = data.SetInt32("family", fFamily);
|
||||||
|
if (fType != -1)
|
||||||
|
status = data.SetInt32("type", fType);
|
||||||
|
if (fProtocol != -1)
|
||||||
|
status = data.SetInt32("protocol", fProtocol);
|
||||||
|
if (fPort != -1)
|
||||||
|
status = data.SetInt32("port", fPort);
|
||||||
|
|
||||||
|
for (int32 i = 0; i < fArguments.CountStrings(); i++) {
|
||||||
|
if (status == B_OK)
|
||||||
|
status = data.AddString("launch", fArguments.StringAt(i));
|
||||||
|
if (status != B_OK)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int32 i = 0; i < CountAddresses(); i++) {
|
||||||
|
BMessage address;
|
||||||
|
status = AddressAt(i).GetMessage(address);
|
||||||
|
if (status == B_OK)
|
||||||
|
status = data.AddMessage("address", &address);
|
||||||
|
if (status != B_OK)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|||||||
@@ -326,6 +326,8 @@ Services::_ToService(const BMessage& message, struct service*& service)
|
|||||||
status_t status = settings.InitCheck();
|
status_t status = settings.InitCheck();
|
||||||
if (status != B_OK)
|
if (status != B_OK)
|
||||||
return status;
|
return status;
|
||||||
|
if (!settings.IsEnabled())
|
||||||
|
return B_NAME_NOT_FOUND;
|
||||||
|
|
||||||
service = new (std::nothrow) ::service;
|
service = new (std::nothrow) ::service;
|
||||||
if (service == NULL)
|
if (service == NULL)
|
||||||
|
|||||||
Reference in New Issue
Block a user