Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configuration cleanup #1893

Merged
merged 16 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions common/rfb/CSecurityTLS.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,9 @@ using namespace rfb;
static const char* configdirfn(const char* fn);

StringParameter CSecurityTLS::X509CA("X509CA", "X509 CA certificate",
configdirfn("x509_ca.pem"),
ConfViewer);
configdirfn("x509_ca.pem"));
StringParameter CSecurityTLS::X509CRL("X509CRL", "X509 CRL file",
configdirfn("x509_crl.pem"),
ConfViewer);
configdirfn("x509_crl.pem"));

static LogWriter vlog("TLS");

Expand Down
134 changes: 46 additions & 88 deletions common/rfb/Configuration.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <ctype.h>
#include <string.h>

#include <algorithm>
#include <stdexcept>

#include <os/Mutex.h>
Expand All @@ -48,29 +49,15 @@ using namespace rfb;
static LogWriter vlog("Config");


// -=- The Global/server/viewer Configuration objects
// -=- The Global Configuration object
Configuration* Configuration::global_ = nullptr;
Configuration* Configuration::server_ = nullptr;
Configuration* Configuration::viewer_ = nullptr;

Configuration* Configuration::global() {
if (!global_)
global_ = new Configuration("Global");
global_ = new Configuration();
return global_;
}

Configuration* Configuration::server() {
if (!server_)
server_ = new Configuration("Server");
return server_;
}

Configuration* Configuration::viewer() {
if (!viewer_)
viewer_ = new Configuration("Viewer");
return viewer_;
}

// -=- Configuration implementation

bool Configuration::set(const char* n, const char* v, bool immutable) {
Expand All @@ -80,8 +67,7 @@ bool Configuration::set(const char* n, const char* v, bool immutable) {
bool Configuration::set(const char* paramName, int len,
const char* val, bool immutable)
{
VoidParameter* current = head;
while (current) {
for (VoidParameter* current: params) {
if ((int)strlen(current->getName()) == len &&
strncasecmp(current->getName(), paramName, len) == 0)
{
Expand All @@ -90,9 +76,8 @@ bool Configuration::set(const char* paramName, int len,
current->setImmutable();
return b;
}
current = current->_next;
}
return _next ? _next->set(paramName, len, val, immutable) : false;
return false;
}

bool Configuration::set(const char* config, bool immutable) {
Expand All @@ -106,36 +91,29 @@ bool Configuration::set(const char* config, bool immutable) {
if (equal) {
return set(config, equal-config, equal+1, immutable);
} else if (hyphen) {
VoidParameter* current = head;
while (current) {
for (VoidParameter* current: params) {
if (strcasecmp(current->getName(), config) == 0) {
bool b = current->setParam();
if (b && immutable)
current->setImmutable();
return b;
}
current = current->_next;
}
}
return _next ? _next->set(config, immutable) : false;
return false;
}

VoidParameter* Configuration::get(const char* param)
{
VoidParameter* current = head;
while (current) {
for (VoidParameter* current: params) {
if (strcasecmp(current->getName(), param) == 0)
return current;
current = current->_next;
}
return _next ? _next->get(param) : nullptr;
return nullptr;
}

void Configuration::list(int width, int nameWidth) {
VoidParameter* current = head;

fprintf(stderr, "%s Parameters:\n", name.c_str());
while (current) {
for (VoidParameter* current: params) {
std::string def_str = current->getDefaultStr();
const char* desc = current->getDescription();
fprintf(stderr," %-*s -", nameWidth, current->getName());
Expand Down Expand Up @@ -165,55 +143,47 @@ void Configuration::list(int width, int nameWidth) {
} else {
fprintf(stderr,"\n");
}
current = current->_next;
}

if (_next)
_next->list(width, nameWidth);
}


bool Configuration::remove(const char* param) {
VoidParameter *current = head;
VoidParameter **prevnext = &head;
std::list<VoidParameter*>::iterator iter;

while (current) {
if (strcasecmp(current->getName(), param) == 0) {
*prevnext = current->_next;
return true;
}
prevnext = &current->_next;
current = current->_next;
}
iter = std::find_if(params.begin(), params.end(),
[param](VoidParameter* p) {
return strcasecmp(p->getName(), param) == 0;
});
if (iter != params.end())
return false;

return false;
params.erase(iter);
return true;
}


// -=- VoidParameter

VoidParameter::VoidParameter(const char* name_, const char* desc_,
ConfigurationObject co)
VoidParameter::VoidParameter(const char* name_, const char* desc_)
: immutable(false), name(name_), description(desc_)
{
Configuration *conf = nullptr;

switch (co) {
case ConfGlobal: conf = Configuration::global();
break;
case ConfServer: conf = Configuration::server();
break;
case ConfViewer: conf = Configuration::viewer();
break;
}
Configuration *conf;

_next = conf->head;
conf->head = this;
conf = Configuration::global();
conf->params.push_back(this);
conf->params.sort([](const VoidParameter* a, const VoidParameter* b) {
return strcasecmp(a->getName(), b->getName()) < 0;
});

mutex = new os::Mutex();
}

VoidParameter::~VoidParameter() {
Configuration *conf;

conf = Configuration::global();
conf->params.remove(this);

delete mutex;
}

Expand All @@ -231,8 +201,8 @@ bool VoidParameter::setParam() {
return false;
}

bool VoidParameter::isBool() const {
return false;
bool VoidParameter::isDefault() const {
return getDefaultStr() == getValueStr();
}

void
Expand All @@ -244,8 +214,8 @@ VoidParameter::setImmutable() {
// -=- AliasParameter

AliasParameter::AliasParameter(const char* name_, const char* desc_,
VoidParameter* param_, ConfigurationObject co)
: VoidParameter(name_, desc_, co), param(param_) {
VoidParameter* param_)
: VoidParameter(name_, desc_), param(param_) {
}

bool
Expand All @@ -265,10 +235,6 @@ std::string AliasParameter::getValueStr() const {
return param->getValueStr();
}

bool AliasParameter::isBool() const {
return param->isBool();
}

void
AliasParameter::setImmutable() {
vlog.debug("Set immutable %s (Alias)", getName());
Expand All @@ -278,9 +244,8 @@ AliasParameter::setImmutable() {

// -=- BoolParameter

BoolParameter::BoolParameter(const char* name_, const char* desc_, bool v,
ConfigurationObject co)
: VoidParameter(name_, desc_, co), value(v), def_value(v) {
BoolParameter::BoolParameter(const char* name_, const char* desc_, bool v)
: VoidParameter(name_, desc_), value(v), def_value(v) {
}

bool
Expand Down Expand Up @@ -309,19 +274,15 @@ bool BoolParameter::setParam() {
void BoolParameter::setParam(bool b) {
if (immutable) return;
value = b;
vlog.debug("Set %s(Bool) to %d", getName(), value);
vlog.debug("Set %s(Bool) to %s", getName(), getValueStr().c_str());
}

std::string BoolParameter::getDefaultStr() const {
return def_value ? "1" : "0";
return def_value ? "on" : "off";
}

std::string BoolParameter::getValueStr() const {
return value ? "1" : "0";
}

bool BoolParameter::isBool() const {
return true;
return value ? "on" : "off";
}

BoolParameter::operator bool() const {
Expand All @@ -331,8 +292,8 @@ BoolParameter::operator bool() const {
// -=- IntParameter

IntParameter::IntParameter(const char* name_, const char* desc_, int v,
int minValue_, int maxValue_, ConfigurationObject co)
: VoidParameter(name_, desc_, co), value(v), def_value(v),
int minValue_, int maxValue_)
: VoidParameter(name_, desc_), value(v), def_value(v),
minValue(minValue_), maxValue(maxValue_)
{
}
Expand Down Expand Up @@ -372,18 +333,15 @@ IntParameter::operator int() const {
// -=- StringParameter

StringParameter::StringParameter(const char* name_, const char* desc_,
const char* v, ConfigurationObject co)
: VoidParameter(name_, desc_, co), value(v), def_value(v)
const char* v)
: VoidParameter(name_, desc_), value(v), def_value(v)
{
if (!v) {
vlog.error("Default value <null> for %s not allowed",name_);
throw std::invalid_argument("Default value <null> not allowed");
}
}

StringParameter::~StringParameter() {
}

bool StringParameter::setParam(const char* v) {
LOCK_CONFIG;
if (immutable) return true;
Expand All @@ -410,8 +368,8 @@ StringParameter::operator const char *() const {
// -=- BinaryParameter

BinaryParameter::BinaryParameter(const char* name_, const char* desc_,
const uint8_t* v, size_t l, ConfigurationObject co)
: VoidParameter(name_, desc_, co),
const uint8_t* v, size_t l)
: VoidParameter(name_, desc_),
value(nullptr), length(0), def_value(nullptr), def_length(0) {
if (l) {
assert(v);
Expand Down
Loading