diff options
-rw-r--r-- | cfgdir.c | 11 | ||||
-rw-r--r-- | config.c | 67 | ||||
-rw-r--r-- | config.h | 7 |
3 files changed, 44 insertions, 41 deletions
@@ -100,7 +100,7 @@ scfg_parse(struct cfg *cfg, struct server *scfg) { char *pos = &scfg->tbuf.buf[0]; - if (!config_parse_header(cfg, "server", &pos)) + if (!config_parse_header(cfg, scfg->name, "server", &pos)) return; while (true) { @@ -108,7 +108,8 @@ scfg_parse(struct cfg *cfg, struct server *scfg) const char *keyname; union cfg_value value; - if (!config_parse_line(cfg, &pos, scfg_key_map, &key, &keyname, &value)) + if (!config_parse_line(cfg, scfg->name, &pos, scfg_key_map, + &key, &keyname, &value)) break; if (key == SCFG_KEY_INVALID) @@ -141,7 +142,7 @@ scfg_parse(struct cfg *cfg, struct server *scfg) case SCFG_KEY_LOCAL: { struct sockaddr_in46 *addr, *tmp; - list_for_each_entry_safe(addr, tmp, &value.addr_list, list) { + list_for_each_entry_safe(addr, tmp, &value.addrs, list) { list_del(&addr->list); server_add_local(cfg, scfg, addr); } @@ -151,7 +152,7 @@ scfg_parse(struct cfg *cfg, struct server *scfg) case SCFG_KEY_REMOTE: { struct sockaddr_in46 *addr, *tmp; - list_for_each_entry_safe(addr, tmp, &value.addr_list, list) { + list_for_each_entry_safe(addr, tmp, &value.addrs, list) { list_del(&addr->list); server_add_remote(cfg, scfg, addr); } @@ -199,7 +200,7 @@ scfg_parse(struct cfg *cfg, struct server *scfg) case SCFG_KEY_RCON: { struct sockaddr_in46 *addr, *tmp; - list_for_each_entry_safe(addr, tmp, &value.addr_list, list) { + list_for_each_entry_safe(addr, tmp, &value.addrs, list) { list_del(&addr->list); server_add_rcon(cfg, scfg, addr); } @@ -75,7 +75,8 @@ strtosockaddrs(const char *str, struct list_head *list) if (*str == '[') { /* IPv6, [a:b:c...h]:p or [*]:p */ - fprintf(stderr, "Attempting to parse an IPv6 addr\n"); + debug(DBG_CFG, "attempting to parse IPv6 addr (%s)\n", str); + str++; tmp = strchr(str, ']'); if (!tmp) @@ -106,7 +107,8 @@ strtosockaddrs(const char *str, struct list_head *list) } else if (*str == '*') { /* IPv4, *:p */ - fprintf(stderr, "Attempting to parse an IPv4 wildcard\n"); + debug(DBG_CFG, "attempting to parse IPv4 addr (%s)\n", str); + str++; if (*str != ':') goto out; @@ -127,7 +129,8 @@ strtosockaddrs(const char *str, struct list_head *list) } else if ((tmp = strchr(str, ':'))) { /* IPv4, a.b.c.d:p or IPv4/6 hostname:p */ - fprintf(stderr, "Got an IPv4:port or hostname:port\n"); + debug(DBG_CFG, "attempting to parse IPv4 addr or hostname (%s)\n", str); + *tmp = '\0'; tmp++; if (strtou16_strict(tmp, &port) < 0) @@ -138,7 +141,7 @@ strtosockaddrs(const char *str, struct list_head *list) goto out; if (inet_pton(AF_INET, str, &addr->in4.sin_addr) > 0) { - fprintf(stderr, "...Got an IPv4:port (0x%p, list 0x%p)\n", addr, &addr->list); + debug(DBG_CFG, "got an IPv4:port (%s)\n", str); addr->in4.sin_family = AF_INET; addr->in4.sin_port = htons(port); addr->addrlen = sizeof(addr->in4); @@ -159,14 +162,12 @@ strtosockaddrs(const char *str, struct list_head *list) /* FIXME: This is completely synchronous but getaddrinfo_a is not very ergonomic */ r = getaddrinfo(str, tmp, &hints, &results); if (r != 0) { - fprintf(stderr, "gettaddrinfo(%s): %s\n", str, gai_strerror(r)); + error("getaddrinfo(%s): %s\n", str, gai_strerror(r)); goto out; } - fprintf(stderr, "...Got an hostname:port\n"); + debug(DBG_CFG, "got a hostname:port (%s)\n", str); for (ai = results; ai; ai = ai->ai_next) { - fprintf(stderr, "Got a result from getaddrinfo\n"); - addr = zmalloc(sizeof(*addr)); if (!addr) { freeaddrinfo(results); @@ -195,7 +196,8 @@ strtosockaddrs(const char *str, struct list_head *list) break; } default: - fprintf(stderr, " Fam: Unknown (%i)\n", ai->ai_family); + error("getaddrinfo(%s): unknown address family (%i)\n", + str, ai->ai_family); xfree(addr); continue; } @@ -205,8 +207,8 @@ strtosockaddrs(const char *str, struct list_head *list) } else if (strtou16_strict(tmp, &port) == 0) { /* Port */ + debug(DBG_CFG, "attempting to parse a port number (%s)\n", str); - fprintf(stderr, "Attempting to parse a port\n"); addr = zmalloc(sizeof(*addr)); if (!addr) goto out; @@ -225,12 +227,14 @@ strtosockaddrs(const char *str, struct list_head *list) addr->addrlen = sizeof(addr->in4); list_add(&addr->list, list); } else { + /* Unknown */ + error("unable to parse address: %s\n", str); goto out; } success: if (list_empty(list)) { - fprintf(stderr, "Success but empty list!?\n"); + error("empty address list!?\n"); return false; } else { int i = 0; @@ -238,16 +242,16 @@ success: list_for_each_entry(addr, list, list) i++; - fprintf(stderr, "Success, %i entries\n", i); + debug(DBG_CFG, "parsed to %i addresses\n", i); } return true; out: if (!list_empty(list)) { - struct sockaddr_in46 *tmpaddr; + struct sockaddr_in46 *tmp; - list_for_each_entry_safe(addr, tmpaddr, list, list) { + list_for_each_entry_safe(addr, tmp, list, list) { list_del(&addr->list); xfree(addr); } @@ -257,21 +261,22 @@ out: /* Returns true if theres data left to parse in buf */ bool -config_parse_line(struct cfg *cfg, char **buf, struct cfg_key_value_map *kvmap, - int *rkey, const char **rkeyname, union cfg_value *rvalue) +config_parse_line(struct cfg *cfg, const char *filename, char **buf, + struct cfg_key_value_map *kvmap, int *rkey, + const char **rkeyname, union cfg_value *rvalue) { char *line, *tmp, *key; int i; - if (!cfg || !buf || !*buf || !kvmap || !rkey || !rvalue) - die("%s: called with invalid parameters\n", __func__); + if (!cfg || !buf || !*buf || !kvmap || !rkey || !rkeyname || !rvalue) + die("%s: invalid parameters", filename); eat_whitespace_and_comments(buf); line = get_line(buf); if (!line) return false; - printf("%s: examining line: %s\n", __func__, line); + debug(DBG_CFG, "%s: parsing config line: %s\n", filename, line); tmp = line; while (isspace(*tmp)) @@ -323,18 +328,11 @@ config_parse_line(struct cfg *cfg, char **buf, struct cfg_key_value_map *kvmap, } case CFG_VAL_TYPE_ADDRS: { - if (!strtosockaddrs(tmp, &rvalue->addr_list)) + if (!strtosockaddrs(tmp, &rvalue->addrs)) goto out; - if (list_empty(&rvalue->addr_list)) { - fprintf(stderr, "CFG_VAL_TYPE_ADDRS with zero list!?\n"); + if (list_empty(&rvalue->addrs)) { + error("empty address list\n"); goto out; - } else { - int i = 0; - struct list_head *pos; - - list_for_each(pos, &rvalue->addr_list) - i++; - fprintf(stderr, "CFG_VAL_TYPE_ADDRS with list %i entries\n", i); } break; } @@ -351,14 +349,16 @@ config_parse_line(struct cfg *cfg, char **buf, struct cfg_key_value_map *kvmap, } out: - fprintf(stderr, "Invalid line\n"); + /* FIXME: the line is already mangled here, a line number would be nice */ + error("%s: invalid config line: %s\n", filename, line); *rkey = 0; *rkeyname = NULL; return true; } bool -config_parse_header(struct cfg *cfg, const char *title, char **buf) +config_parse_header(struct cfg *cfg, const char *filename, const char *title, + char **buf) { char *line; @@ -369,14 +369,15 @@ config_parse_header(struct cfg *cfg, const char *title, char **buf) line = get_line(buf); if (!line) { - printf("Cfg: premature EOF\n"); + error("%s: missing header in configuration file\n", filename); return false; } else { char titlehdr[strlen(title) + 3]; sprintf(titlehdr, "[%s]", title); if (strcmp(line, titlehdr)) { - printf("Invalid header: %s\n", line); + printf("%s: incorrect header in configuration file\n", + filename); return false; } } @@ -17,14 +17,15 @@ struct cfg_key_value_map { union cfg_value { const char *str; uint16_t uint16; - struct list_head addr_list; /* FIXME: addrs */ + struct list_head addrs; }; -bool config_parse_line(struct cfg *cfg, char **buf, +bool config_parse_line(struct cfg *cfg, const char *filename, char **buf, struct cfg_key_value_map *kvmap, int *rkey, const char **rkeyname, union cfg_value *rvalue); -bool config_parse_header(struct cfg *cfg, const char *title, char **buf); +bool config_parse_header(struct cfg *cfg, const char *filename, + const char *title, char **buf); #endif |