📄 config.c
字号:
while (i < argc) {
if (!strcmp(argv[i],"-f") ||
!strcmp(argv[i],"--hash-password")) {
i += 2; /* command-line option with argument. ignore them. */
continue;
} else if (!strcmp(argv[i],"--list-fingerprint") ||
!strcmp(argv[i],"--verify-config") ||
!strcmp(argv[i],"--ignore-missing-torrc") ||
!strcmp(argv[i],"--quiet") ||
!strcmp(argv[i],"--hush")) {
i += 1; /* command-line option. ignore it. */
continue;
} else if (!strcmp(argv[i],"--nt-service") ||
!strcmp(argv[i],"-nt-service")) {
i += 1;
continue;
}
if (i == argc-1) {
log_warn(LD_CONFIG,"Command-line option '%s' with no value. Failing.",
argv[i]);
config_free_lines(front);
return -1;
}
*new = tor_malloc_zero(sizeof(config_line_t));
s = argv[i];
while (*s == '-')
s++;
(*new)->key = tor_strdup(expand_abbrev(&options_format, s, 1, 1));
(*new)->value = tor_strdup(argv[i+1]);
(*new)->next = NULL;
log(LOG_DEBUG, LD_CONFIG, "Commandline: parsed keyword '%s', value '%s'",
(*new)->key, (*new)->value);
new = &((*new)->next);
i += 2;
}
*result = front;
return 0;
}
/** Helper: allocate a new configuration option mapping 'key' to 'val',
* append it to *<b>lst</b>. */
static void
config_line_append(config_line_t **lst,
const char *key,
const char *val)
{
config_line_t *newline;
newline = tor_malloc(sizeof(config_line_t));
newline->key = tor_strdup(key);
newline->value = tor_strdup(val);
newline->next = NULL;
while (*lst)
lst = &((*lst)->next);
(*lst) = newline;
}
/** Helper: parse the config string and strdup into key/value
* strings. Set *result to the list, or NULL if parsing the string
* failed. Return 0 on success, -1 on failure. Warn and ignore any
* misformatted lines. */
int
config_get_lines(const char *string, config_line_t **result)
{
config_line_t *list = NULL, **next;
char *k, *v;
next = &list;
do {
string = parse_config_line_from_str(string, &k, &v);
if (!string) {
config_free_lines(list);
return -1;
}
if (k && v) {
/* This list can get long, so we keep a pointer to the end of it
* rather than using config_line_append over and over and getting
* n^2 performance. */
*next = tor_malloc(sizeof(config_line_t));
(*next)->key = k;
(*next)->value = v;
(*next)->next = NULL;
next = &((*next)->next);
} else {
tor_free(k);
tor_free(v);
}
} while (*string);
*result = list;
return 0;
}
/**
* Free all the configuration lines on the linked list <b>front</b>.
*/
void
config_free_lines(config_line_t *front)
{
config_line_t *tmp;
while (front) {
tmp = front;
front = tmp->next;
tor_free(tmp->key);
tor_free(tmp->value);
tor_free(tmp);
}
}
/** Return the description for a given configuration variable, or NULL if no
* description exists. */
static const char *
config_find_description(config_format_t *fmt, const char *name)
{
int i;
for (i=0; fmt->descriptions[i].name; ++i) {
if (!strcasecmp(name, fmt->descriptions[i].name))
return fmt->descriptions[i].description;
}
return NULL;
}
/** If <b>key</b> is a configuration option, return the corresponding
* config_var_t. Otherwise, if <b>key</b> is a non-standard abbreviation,
* warn, and return the corresponding config_var_t. Otherwise return NULL.
*/
static config_var_t *
config_find_option(config_format_t *fmt, const char *key)
{
int i;
size_t keylen = strlen(key);
if (!keylen)
return NULL; /* if they say "--" on the commandline, it's not an option */
/* First, check for an exact (case-insensitive) match */
for (i=0; fmt->vars[i].name; ++i) {
if (!strcasecmp(key, fmt->vars[i].name)) {
return &fmt->vars[i];
}
}
/* If none, check for an abbreviated match */
for (i=0; fmt->vars[i].name; ++i) {
if (!strncasecmp(key, fmt->vars[i].name, keylen)) {
log_warn(LD_CONFIG, "The abbreviation '%s' is deprecated. "
"Please use '%s' instead",
key, fmt->vars[i].name);
return &fmt->vars[i];
}
}
/* Okay, unrecognized option */
return NULL;
}
/*
* Functions to assign config options.
*/
/** <b>c</b>-\>key is known to be a real key. Update <b>options</b>
* with <b>c</b>-\>value and return 0, or return -1 if bad value.
*
* Called from config_assign_line() and option_reset().
*/
static int
config_assign_value(config_format_t *fmt, or_options_t *options,
config_line_t *c, char **msg)
{
int i, r, ok;
char buf[1024];
config_var_t *var;
void *lvalue;
CHECK(fmt, options);
var = config_find_option(fmt, c->key);
tor_assert(var);
lvalue = STRUCT_VAR_P(options, var->var_offset);
switch (var->type) {
case CONFIG_TYPE_UINT:
i = (int)tor_parse_long(c->value, 10, 0, INT_MAX, &ok, NULL);
if (!ok) {
r = tor_snprintf(buf, sizeof(buf),
"Int keyword '%s %s' is malformed or out of bounds.",
c->key, c->value);
*msg = tor_strdup(r >= 0 ? buf : "internal error");
return -1;
}
*(int *)lvalue = i;
break;
case CONFIG_TYPE_INTERVAL: {
i = config_parse_interval(c->value, &ok);
if (!ok) {
r = tor_snprintf(buf, sizeof(buf),
"Interval '%s %s' is malformed or out of bounds.",
c->key, c->value);
*msg = tor_strdup(r >= 0 ? buf : "internal error");
return -1;
}
*(int *)lvalue = i;
break;
}
case CONFIG_TYPE_MEMUNIT: {
uint64_t u64 = config_parse_memunit(c->value, &ok);
if (!ok) {
r = tor_snprintf(buf, sizeof(buf),
"Value '%s %s' is malformed or out of bounds.",
c->key, c->value);
*msg = tor_strdup(r >= 0 ? buf : "internal error");
return -1;
}
*(uint64_t *)lvalue = u64;
break;
}
case CONFIG_TYPE_BOOL:
i = (int)tor_parse_long(c->value, 10, 0, 1, &ok, NULL);
if (!ok) {
r = tor_snprintf(buf, sizeof(buf),
"Boolean '%s %s' expects 0 or 1.",
c->key, c->value);
*msg = tor_strdup(r >= 0 ? buf : "internal error");
return -1;
}
*(int *)lvalue = i;
break;
case CONFIG_TYPE_STRING:
tor_free(*(char **)lvalue);
*(char **)lvalue = tor_strdup(c->value);
break;
case CONFIG_TYPE_DOUBLE:
*(double *)lvalue = atof(c->value);
break;
case CONFIG_TYPE_ISOTIME:
if (parse_iso_time(c->value, (time_t *)lvalue)) {
r = tor_snprintf(buf, sizeof(buf),
"Invalid time '%s' for keyword '%s'", c->value, c->key);
*msg = tor_strdup(r >= 0 ? buf : "internal error");
return -1;
}
break;
case CONFIG_TYPE_CSV:
if (*(smartlist_t**)lvalue) {
SMARTLIST_FOREACH(*(smartlist_t**)lvalue, char *, cp, tor_free(cp));
smartlist_clear(*(smartlist_t**)lvalue);
} else {
*(smartlist_t**)lvalue = smartlist_create();
}
smartlist_split_string(*(smartlist_t**)lvalue, c->value, ",",
SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
break;
case CONFIG_TYPE_LINELIST:
case CONFIG_TYPE_LINELIST_S:
config_line_append((config_line_t**)lvalue, c->key, c->value);
break;
case CONFIG_TYPE_OBSOLETE:
log_warn(LD_CONFIG, "Skipping obsolete configuration option '%s'", c->key);
break;
case CONFIG_TYPE_LINELIST_V:
r = tor_snprintf(buf, sizeof(buf),
"You may not provide a value for virtual option '%s'", c->key);
*msg = tor_strdup(r >= 0 ? buf : "internal error");
return -1;
default:
tor_assert(0);
break;
}
return 0;
}
/** If <b>c</b> is a syntactically valid configuration line, update
* <b>options</b> with its value and return 0. Otherwise return -1 for bad
* key, -2 for bad value.
*
* If <b>clear_first</b> is set, clear the value first. Then if
* <b>use_defaults</b> is set, set the value to the default.
*
* Called from config_assign().
*/
static int
config_assign_line(config_format_t *fmt, or_options_t *options,
config_line_t *c, int use_defaults,
int clear_first, char **msg)
{
config_var_t *var;
CHECK(fmt, options);
var = config_find_option(fmt, c->key);
if (!var) {
if (fmt->extra) {
void *lvalue = STRUCT_VAR_P(options, fmt->extra->var_offset);
log_info(LD_CONFIG,
"Found unrecognized option '%s'; saving it.", c->key);
config_line_append((config_line_t**)lvalue, c->key, c->value);
return 0;
} else {
char buf[1024];
int tmp = tor_snprintf(buf, sizeof(buf),
"Unknown option '%s'. Failing.", c->key);
*msg = tor_strdup(tmp >= 0 ? buf : "internal error");
return -1;
}
}
/* Put keyword into canonical case. */
if (strcmp(var->name, c->key)) {
tor_free(c->key);
c->key = tor_strdup(var->name);
}
if (!strlen(c->value)) {
/* reset or clear it, then return */
if (!clear_first) {
if (var->type == CONFIG_TYPE_LINELIST ||
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -