* Moved settings stuff into its own file.

* Minor cleanup.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@19083 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler 2006-10-19 11:24:57 +00:00
parent 919c897b98
commit 1a2ad4cb2c
4 changed files with 253 additions and 224 deletions

View File

@ -10,6 +10,7 @@ UsePrivateHeaders net ;
Server net_server :
NetServer.cpp
Settings.cpp
;
LinkAgainst net_server :

View File

@ -7,16 +7,13 @@
*/
#include "Settings.h"
#include <Alert.h>
#include <Application.h>
#include <Entry.h>
#include <File.h>
#include <FindDirectory.h>
#include <Path.h>
#include <TextView.h>
#include <driver_settings.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <net/if_dl.h>
@ -27,43 +24,10 @@
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
struct settings_template {
uint32 type;
const char* name;
const settings_template* sub_template;
};
class Settings {
public:
Settings();
~Settings();
status_t GetNextInterface(uint32& cookie, BMessage& interface);
private:
status_t _Load();
status_t _GetPath(const char* name, BPath& path);
const settings_template* _FindSettingsTemplate(const settings_template* settingsTemplate,
const char* name);
status_t _ConvertFromDriverParameter(const driver_parameter& parameter,
const settings_template* settingsTemplate, BMessage& message);
status_t _ConvertFromDriverSettings(const driver_settings& settings,
const settings_template* settingsTemplate, BMessage& message);
status_t _ConvertFromDriverSettings(const char* path,
const settings_template* settingsTemplate, BMessage& message);
BMessage fInterfaces;
bool fUpdated;
};
class NetServer : public BApplication {
public:
NetServer();
@ -95,33 +59,9 @@ static const address_family kFamilies[] = {
AF_INET,
"inet",
{"AF_INET", "inet", "ipv4", NULL},
inet_parse_address,
inet_print_address
inet_parse_address
},
{ -1, NULL, {NULL}, NULL, NULL }
};
const static settings_template kInterfaceAddressTemplate[] = {
{B_STRING_TYPE, "family", NULL},
{B_STRING_TYPE, "address", NULL},
{B_STRING_TYPE, "mask", NULL},
{B_STRING_TYPE, "peer", NULL},
{B_STRING_TYPE, "broadcast", NULL},
{0, NULL, NULL}
};
const static settings_template kInterfaceDeviceTemplate[] = {
{B_STRING_TYPE, "device", NULL},
{B_MESSAGE_TYPE, "address", kInterfaceAddressTemplate},
{B_INT32_TYPE, "flags", NULL},
{B_INT32_TYPE, "metric", NULL},
{B_INT32_TYPE, "mtu", NULL},
{0, NULL, NULL}
};
const static settings_template kInterfaceTemplate[] = {
{B_MESSAGE_TYPE, "interface", kInterfaceDeviceTemplate},
{0, NULL, NULL}
{ -1, NULL, {NULL}, NULL }
};
@ -179,166 +119,6 @@ parse_address(int32 familyIndex, const char* argument, struct sockaddr& address)
// #pragma mark -
Settings::Settings()
:
fUpdated(false)
{
_Load();
}
Settings::~Settings()
{
// only save the settings if something has changed
if (!fUpdated)
return;
#if 0
BFile file;
if (_Open(&file, B_CREATE_FILE | B_WRITE_ONLY) != B_OK)
return;
#endif
}
status_t
Settings::_GetPath(const char* name, BPath& path)
{
if (find_directory(B_COMMON_SETTINGS_DIRECTORY, &path) != B_OK)
return B_ERROR;
path.Append("network");
path.Append(name);
return B_OK;
}
const settings_template*
Settings::_FindSettingsTemplate(const settings_template* settingsTemplate,
const char* name)
{
while (settingsTemplate->name != NULL) {
if (!strcmp(name, settingsTemplate->name))
return settingsTemplate;
settingsTemplate++;
}
return NULL;
}
status_t
Settings::_ConvertFromDriverParameter(const driver_parameter& parameter,
const settings_template* settingsTemplate, BMessage& message)
{
settingsTemplate = _FindSettingsTemplate(settingsTemplate, parameter.name);
if (settingsTemplate == NULL) {
fprintf(stderr, "unknown parameter %s\n", parameter.name);
return B_BAD_VALUE;
}
for (int32 i = 0; i < parameter.value_count; i++) {
switch (settingsTemplate->type) {
case B_STRING_TYPE:
message.AddString(parameter.name, parameter.values[i]);
break;
case B_INT32_TYPE:
message.AddInt32(parameter.name, atoi(parameter.values[i]));
break;
case B_BOOL_TYPE:
if (!strcasecmp(parameter.values[i], "true")
|| !strcasecmp(parameter.values[i], "on")
|| !strcasecmp(parameter.values[i], "enabled")
|| !strcasecmp(parameter.values[i], "1"))
message.AddBool(parameter.name, true);
else
message.AddBool(parameter.name, false);
break;
}
}
if (settingsTemplate->type == B_MESSAGE_TYPE && parameter.parameter_count > 0) {
status_t status = B_OK;
BMessage subMessage;
for (int32 j = 0; j < parameter.parameter_count; j++) {
status = _ConvertFromDriverParameter(parameter.parameters[j],
settingsTemplate->sub_template, subMessage);
if (status < B_OK)
break;
}
if (status == B_OK)
message.AddMessage(parameter.name, &subMessage);
}
return B_OK;
}
status_t
Settings::_ConvertFromDriverSettings(const driver_settings& settings,
const settings_template* settingsTemplate, BMessage& message)
{
for (int32 i = 0; i < settings.parameter_count; i++) {
status_t status = _ConvertFromDriverParameter(settings.parameters[i],
settingsTemplate, message);
if (status == B_BAD_VALUE) {
// ignore unknown entries
continue;
}
if (status < B_OK)
return status;
}
return B_OK;
}
status_t
Settings::_ConvertFromDriverSettings(const char* name,
const settings_template* settingsTemplate, BMessage& message)
{
BPath path;
status_t status = _GetPath("interfaces", path);
if (status < B_OK)
return status;
void* handle = load_driver_settings(path.Path());
if (handle == NULL)
return B_ENTRY_NOT_FOUND;
const driver_settings* settings = get_driver_settings(handle);
if (settings != NULL)
status = _ConvertFromDriverSettings(*settings, settingsTemplate, message);
unload_driver_settings(handle);
return status;
}
status_t
Settings::_Load()
{
return _ConvertFromDriverSettings("interfaces", kInterfaceTemplate, fInterfaces);
}
status_t
Settings::GetNextInterface(uint32& cookie, BMessage& interface)
{
status_t status = fInterfaces.FindMessage("interface", cookie, &interface);
if (status < B_OK)
return status;
cookie++;
return B_OK;
}
// #pragma mark -
NetServer::NetServer()
: BApplication("application/x-vnd.haiku-net_server")
{

View File

@ -0,0 +1,199 @@
/*
* Copyright 2006, Haiku, Inc. All Rights Reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Axel Dörfler, axeld@pinc-software.de
*/
#include "Settings.h"
#include <FindDirectory.h>
#include <Path.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const static settings_template kInterfaceAddressTemplate[] = {
{B_STRING_TYPE, "family", NULL},
{B_STRING_TYPE, "address", NULL},
{B_STRING_TYPE, "mask", NULL},
{B_STRING_TYPE, "peer", NULL},
{B_STRING_TYPE, "broadcast", NULL},
{0, NULL, NULL}
};
const static settings_template kInterfaceDeviceTemplate[] = {
{B_STRING_TYPE, "device", NULL},
{B_MESSAGE_TYPE, "address", kInterfaceAddressTemplate},
{B_INT32_TYPE, "flags", NULL},
{B_INT32_TYPE, "metric", NULL},
{B_INT32_TYPE, "mtu", NULL},
{0, NULL, NULL}
};
const static settings_template kInterfaceTemplate[] = {
{B_MESSAGE_TYPE, "interface", kInterfaceDeviceTemplate},
{0, NULL, NULL}
};
Settings::Settings()
:
fUpdated(false)
{
_Load();
}
Settings::~Settings()
{
// only save the settings if something has changed
if (!fUpdated)
return;
#if 0
BFile file;
if (_Open(&file, B_CREATE_FILE | B_WRITE_ONLY) != B_OK)
return;
#endif
}
status_t
Settings::_GetPath(const char* name, BPath& path)
{
if (find_directory(B_COMMON_SETTINGS_DIRECTORY, &path) != B_OK)
return B_ERROR;
path.Append("network");
path.Append(name);
return B_OK;
}
const settings_template*
Settings::_FindSettingsTemplate(const settings_template* settingsTemplate,
const char* name)
{
while (settingsTemplate->name != NULL) {
if (!strcmp(name, settingsTemplate->name))
return settingsTemplate;
settingsTemplate++;
}
return NULL;
}
status_t
Settings::_ConvertFromDriverParameter(const driver_parameter& parameter,
const settings_template* settingsTemplate, BMessage& message)
{
settingsTemplate = _FindSettingsTemplate(settingsTemplate, parameter.name);
if (settingsTemplate == NULL) {
fprintf(stderr, "unknown parameter %s\n", parameter.name);
return B_BAD_VALUE;
}
for (int32 i = 0; i < parameter.value_count; i++) {
switch (settingsTemplate->type) {
case B_STRING_TYPE:
message.AddString(parameter.name, parameter.values[i]);
break;
case B_INT32_TYPE:
message.AddInt32(parameter.name, atoi(parameter.values[i]));
break;
case B_BOOL_TYPE:
if (!strcasecmp(parameter.values[i], "true")
|| !strcasecmp(parameter.values[i], "on")
|| !strcasecmp(parameter.values[i], "enabled")
|| !strcasecmp(parameter.values[i], "1"))
message.AddBool(parameter.name, true);
else
message.AddBool(parameter.name, false);
break;
}
}
if (settingsTemplate->type == B_MESSAGE_TYPE && parameter.parameter_count > 0) {
status_t status = B_OK;
BMessage subMessage;
for (int32 j = 0; j < parameter.parameter_count; j++) {
status = _ConvertFromDriverParameter(parameter.parameters[j],
settingsTemplate->sub_template, subMessage);
if (status < B_OK)
break;
}
if (status == B_OK)
message.AddMessage(parameter.name, &subMessage);
}
return B_OK;
}
status_t
Settings::_ConvertFromDriverSettings(const driver_settings& settings,
const settings_template* settingsTemplate, BMessage& message)
{
for (int32 i = 0; i < settings.parameter_count; i++) {
status_t status = _ConvertFromDriverParameter(settings.parameters[i],
settingsTemplate, message);
if (status == B_BAD_VALUE) {
// ignore unknown entries
continue;
}
if (status < B_OK)
return status;
}
return B_OK;
}
status_t
Settings::_ConvertFromDriverSettings(const char* name,
const settings_template* settingsTemplate, BMessage& message)
{
BPath path;
status_t status = _GetPath("interfaces", path);
if (status < B_OK)
return status;
void* handle = load_driver_settings(path.Path());
if (handle == NULL)
return B_ENTRY_NOT_FOUND;
const driver_settings* settings = get_driver_settings(handle);
if (settings != NULL)
status = _ConvertFromDriverSettings(*settings, settingsTemplate, message);
unload_driver_settings(handle);
return status;
}
status_t
Settings::_Load()
{
return _ConvertFromDriverSettings("interfaces", kInterfaceTemplate, fInterfaces);
}
status_t
Settings::GetNextInterface(uint32& cookie, BMessage& interface)
{
status_t status = fInterfaces.FindMessage("interface", cookie, &interface);
if (status < B_OK)
return status;
cookie++;
return B_OK;
}

View File

@ -0,0 +1,49 @@
/*
* Copyright 2006, Haiku, Inc. All Rights Reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Axel Dörfler, axeld@pinc-software.de
*/
#ifndef SETTINGS_H
#define SETTINGS_H
#include <driver_settings.h>
#include <Message.h>
class BPath;
struct settings_template {
uint32 type;
const char* name;
const settings_template* sub_template;
};
class Settings {
public:
Settings();
~Settings();
status_t GetNextInterface(uint32& cookie, BMessage& interface);
private:
status_t _Load();
status_t _GetPath(const char* name, BPath& path);
const settings_template* _FindSettingsTemplate(const settings_template* settingsTemplate,
const char* name);
status_t _ConvertFromDriverParameter(const driver_parameter& parameter,
const settings_template* settingsTemplate, BMessage& message);
status_t _ConvertFromDriverSettings(const driver_settings& settings,
const settings_template* settingsTemplate, BMessage& message);
status_t _ConvertFromDriverSettings(const char* path,
const settings_template* settingsTemplate, BMessage& message);
BMessage fInterfaces;
bool fUpdated;
};
#endif // SETTINGS_H