Skip to content

Commit

Permalink
make libconfig optionnal again
Browse files Browse the repository at this point in the history
  • Loading branch information
yrutschle committed Jul 24, 2020
1 parent 3315a72 commit 2c93a01
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 40 deletions.
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Configuration

VERSION=$(shell ./genver.sh -r)

# Configuration -- you probably need to `make clean` if you
# change any of these
ENABLE_REGEX=1 # Enable regex probes
USELIBCONFIG=1 # Use libconfig? (necessary to use configuration files)
USELIBPCRE=1 # Use libpcre? (needed for regex on musl)
Expand Down
2 changes: 1 addition & 1 deletion doc/INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ For Fedora, you'll need packages `libconfig` and
If you want to rebuild `sslh-conf.c` (after a `make
distclean` for example), you will also need to add
[conf2struct](https://www.rutschle.net/tech/conf2struct/README.html)
(v1.3) to your path.
(v1.4) to your path.

Compilation
-----------
Expand Down
133 changes: 100 additions & 33 deletions sslh-conf.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* Generated by conf2struct (https://www.rutschle.net/tech/conf2struct/README)
* on Sun Jul 19 16:59:51 2020.
* on Fri Jul 24 16:29:39 2020.
# conf2struct: generate libconf parsers that read to structs
# Copyright (C) 2018-2019 Yves Rutschle
Expand Down Expand Up @@ -30,9 +30,12 @@

#define _GNU_SOURCE
#include <string.h>
#include <libconfig.h>
#ifdef LIBCONFIG
# include <libconfig.h>
#endif
#include <stdlib.h>
#include <stddef.h>
#include <stdio.h>
#include "sslh-conf.h"
#include "argtable3.h"
#ifdef LIBPCRE
Expand All @@ -56,18 +59,7 @@ typedef enum {
CFG_ARRAY,
CFG_LIST,
} config_type;

typedef int (*lookup_fn)(const config_setting_t*, const char*, void*);
lookup_fn lookup_fns[] = {
(lookup_fn)config_setting_lookup_bool,
(lookup_fn)config_setting_lookup_int,
(lookup_fn)config_setting_lookup_int64,
(lookup_fn)config_setting_lookup_float,
(lookup_fn)config_setting_lookup_string,
NULL, /* CFG_GROUP */
NULL, /* CFG_ARRAY */
NULL, /* CFG_LIST */
};
/* /config_type */

const char* type2str[] = {
"boolean",
Expand All @@ -80,8 +72,6 @@ const char* type2str[] = {
"list",
};

/* /config_type */

typedef union {
int def_bool;
int def_int;
Expand All @@ -90,6 +80,97 @@ typedef union {
char* def_string;
} any_val;

struct config_desc {
const char* name;
int type;
struct config_desc * sub_group; /* Table for compound types (list and group) */
void* arg_cl; /* command-line argument for this setting */
void* base_addr; /* Base of the structure (filled at runtime). Probably not useable for list elements */
size_t offset; /* Offset of setting in the structure */
size_t offset_len; /* Offset of *_len field, for arrays and lists */
size_t offset_present; /* offset of *_is_present field, for optional settings */
size_t size; /* Size of element, or size of group for groups and lists */
int array_type; /* type of array elements, when type == CFG_ARRAY */
int mandatory;
int optional;
any_val default_val;
};

#ifndef LIBCONFIG
/* Stubs in case you don't want libconfig */

typedef void config_setting_t;
typedef int config_t;
#define CONFIG_TRUE 1
#define CONFIG_FALSE 0

#define make_config_setting_lookup(type) \
int config_setting_lookup_##type(const config_setting_t* a, const char* b, void* c) { \
return 0; \
}

#define make_config_setting_get(type, ret_type) \
ret_type config_setting_get_##type(const config_setting_t* a) { \
return 0; \
}

make_config_setting_lookup(bool);
make_config_setting_lookup(int);
make_config_setting_lookup(int64);
make_config_setting_lookup(float);
make_config_setting_lookup(string);

make_config_setting_get(bool, int);
make_config_setting_get(int, int);
make_config_setting_get(int64, int);
make_config_setting_get(float, double);
make_config_setting_get(string, char*);

config_setting_t* config_lookup(config_t* c, const char* b) {
return NULL;
}

void config_init(config_t* c) {
return;
}

config_setting_t* config_setting_lookup(config_setting_t* a, char* b) {
return NULL;
}

int config_setting_length(config_setting_t* a) {
return 0;
}

config_setting_t* config_setting_get_elem(config_setting_t* a, int i) {
return NULL;
}

int config_read_file(config_t* a, const char* b) {
return CONFIG_TRUE;
}

int config_error_line(config_t* c) {
return 0;
}

char* config_error_text(config_t* c) {
return NULL;
}
#endif

typedef int (*lookup_fn)(const config_setting_t*, const char*, void*);
lookup_fn lookup_fns[] = {
(lookup_fn)config_setting_lookup_bool,
(lookup_fn)config_setting_lookup_int,
(lookup_fn)config_setting_lookup_int64,
(lookup_fn)config_setting_lookup_float,
(lookup_fn)config_setting_lookup_string,
NULL, /* CFG_GROUP */
NULL, /* CFG_ARRAY */
NULL, /* CFG_LIST */
};

/* Copy an any_val to arbitrary memory location */
/* 0: success
* <0: error */
Expand Down Expand Up @@ -253,22 +334,6 @@ static int stringcpy(config_type type, void* target, char* from)
}


struct config_desc {
const char* name;
int type;
struct config_desc * sub_group; /* Table for compound types (list and group) */
void* arg_cl; /* command-line argument for this setting */
void* base_addr; /* Base of the structure (filled at runtime). Probably not useable for list elements */
size_t offset; /* Offset of setting in the structure */
size_t offset_len; /* Offset of *_len field, for arrays and lists */
size_t offset_present; /* offset of *_is_present field, for optional settings */
size_t size; /* Size of element, or size of group for groups and lists */
int array_type; /* type of array elements, when type == CFG_ARRAY */
int mandatory;
int optional;
any_val default_val;
};

/* Element to describe the target of a compound element
* element: which config entry is being changed
* match: if >0, index in pmatch to set
Expand Down Expand Up @@ -1488,7 +1553,9 @@ int sslhcfg_cl_parse(int argc, char* argv[], struct sslhcfg_item* cfg)
char* errmsg;
config_setting_t* s;
void* argtable[] = {
sslhcfg_conffile = arg_filen("F", "config", "<file>", 0, 1, "Specify configuration file"),
#ifdef LIBCONFIG
sslhcfg_conffile = arg_filen("F", "config", "<file>", 0, 1, "Specify configuration file"),
#endif
sslhcfg_verbose = arg_intn("v", "verbose", "<n>", 0, 1, ""),
sslhcfg_foreground = arg_litn("f", "foreground", 0, 1, "Run in foreground instead of as a daemon"),
sslhcfg_inetd = arg_litn("i", "inetd", 0, 1, "Run in inetd mode: use stdin/stdout instead of network listen"),
Expand Down
6 changes: 4 additions & 2 deletions sslh-conf.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* Generated by conf2struct (https://www.rutschle.net/tech/conf2struct/README)
* on Sun Jul 19 16:59:51 2020.
* on Fri Jul 24 16:29:39 2020.
# conf2struct: generate libconf parsers that read to structs
# Copyright (C) 2018-2019 Yves Rutschle
Expand Down Expand Up @@ -30,7 +30,9 @@

#ifndef C2S_SSLHCFG_H
#define C2S_SSLHCFG_H
#include <libconfig.h>
#ifdef LIBCONFIG
# include <libconfig.h>
#endif


#include "probe.h"
Expand Down
8 changes: 5 additions & 3 deletions sslh-main.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ void cmd_ssl_to_tls(int argc, char* argv[])
/* Extract configuration on addresses and ports on which to listen.
* out: newly allocated list of addrinfo to listen to
*/
#ifdef LIBCONFIG
static int config_resolve_listen(struct addrinfo **listen)
{
int i, res;
Expand All @@ -164,12 +163,11 @@ static int config_resolve_listen(struct addrinfo **listen)
}
return 0;
}
#endif



#ifdef LIBCONFIG
static void setup_regex_probe(struct sslhcfg_protocols_item *p)
#ifdef LIBCONFIG
{
int num_patterns, i, res;
regex_t** pattern_list;
Expand All @@ -196,6 +194,10 @@ static void setup_regex_probe(struct sslhcfg_protocols_item *p)
}
}
}
#else
{
return;
}
#endif

/* For each protocol in the configuration, resolve address and set up protocol
Expand Down

0 comments on commit 2c93a01

Please sign in to comment.