From ecdf32fbe225be2f90524536c49e507664199b37 Mon Sep 17 00:00:00 2001 From: Andrew Bettison Date: Fri, 7 Dec 2012 11:14:05 +1030 Subject: [PATCH] Rename cf_opt_port() to cf_opt_uint16_nonzero() Improves the usefulness of the 'config schema' output. --- conf.h | 3 ++- conf_schema.c | 25 +++++++++++++++++-------- conf_schema.h | 10 +++++----- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/conf.h b/conf.h index b2bdd545..854fdef2 100644 --- a/conf.h +++ b/conf.h @@ -540,12 +540,13 @@ int cf_opt_rhizome_peer_from_uri(struct config_rhizome_peer *, const char *uri); int cf_opt_str(char *str, size_t len, const char *text); int cf_opt_str_nonempty(char *str, size_t len, const char *text); int cf_opt_int(int *intp, const char *text); +int cf_opt_uint16(uint16_t *intp, const char *text); +int cf_opt_uint16_nonzero(uint16_t *intp, const char *text); int cf_opt_int32_nonneg(int32_t *intp, const char *text); int cf_opt_uint32_nonzero(uint32_t *intp, const char *text); int cf_opt_uint64_scaled(uint64_t *intp, const char *text); int cf_opt_protocol(char *str, size_t len, const char *text); int cf_opt_in_addr(struct in_addr *addrp, const char *text); -int cf_opt_port(unsigned short *portp, const char *text); int cf_opt_sid(sid_t *sidp, const char *text); int cf_opt_rhizome_bk(rhizome_bk_t *bkp, const char *text); int cf_opt_interface_type(short *typep, const char *text); diff --git a/conf_schema.c b/conf_schema.c index 5285ec0a..3a13943a 100644 --- a/conf_schema.c +++ b/conf_schema.c @@ -297,19 +297,28 @@ int cf_opt_in_addr(struct in_addr *addrp, const char *text) return CFOK; } -int cf_opt_port(unsigned short *portp, const char *text) +int cf_opt_uint16(uint16_t *intp, const char *text) { - unsigned short port = 0; + unsigned short ui = 0; const char *p; for (p = text; isdigit(*p); ++p) { - unsigned oport = port; - port = port * 10 + *p - '0'; - if (port / 10 != oport) + unsigned oui = ui; + ui = ui * 10 + *p - '0'; + if (ui / 10 != oui) break; } - if (*p || port == 0) + if (*p) return CFINVALID; - *portp = port; + *intp = ui; + return CFOK; +} + +int cf_opt_uint16_nonzero(uint16_t *intp, const char *text) +{ + uint16_t ui; + if (cf_opt_uint16_nonzero(&ui, text) != CFOK || ui == 0) + return CFINVALID; + *intp = ui; return CFOK; } @@ -463,7 +472,7 @@ static int cf_opt_network_interface_legacy(struct config_network_interface *nifp if (len) { char buf[len + 1]; strncpy(buf, port, len)[len] = '\0'; - int result = cf_opt_port(&nif.port, buf); + int result = cf_opt_uint16_nonzero(&nif.port, buf); switch (result) { case CFERROR: return CFERROR; case CFOK: break; diff --git a/conf_schema.h b/conf_schema.h index 9955c330..196ebedb 100644 --- a/conf_schema.h +++ b/conf_schema.h @@ -216,8 +216,8 @@ END_STRUCT STRUCT(olsr) ATOM(int, enable, 1, cf_opt_boolean,, "If true, OLSR is used for mesh routing") -ATOM(uint16_t, remote_port,4130, cf_opt_port,, "Remote port number") -ATOM(uint16_t, local_port, 4131, cf_opt_port,, "Local port number") +ATOM(uint16_t, remote_port,4130, cf_opt_uint16_nonzero,, "Remote port number") +ATOM(uint16_t, local_port, 4131, cf_opt_uint16_nonzero,, "Local port number") END_STRUCT ARRAY(argv, SORTED NO_DUPLICATES, vld_argv) @@ -237,7 +237,7 @@ END_STRUCT STRUCT(rhizome_peer) STRING(25, protocol, "http", cf_opt_protocol,, "Protocol name") STRING(256, host, "", cf_opt_str_nonempty, MANDATORY, "Host name or IP address") -ATOM(uint16_t, port, RHIZOME_HTTP_PORT, cf_opt_port,, "Port number") +ATOM(uint16_t, port, RHIZOME_HTTP_PORT, cf_opt_uint16_nonzero,, "Port number") END_STRUCT ARRAY(peerlist,) @@ -277,7 +277,7 @@ END_STRUCT STRUCT(host) STRING(INTERFACE_NAME_STRLEN, interface, "", cf_opt_str_nonempty, MANDATORY, "Interface name") ATOM(struct in_addr, address, (struct in_addr){htonl(INADDR_NONE)}, cf_opt_in_addr, MANDATORY, "Host IP address") -ATOM(uint16_t, port, PORT_DNA, cf_opt_port,, "Port number") +ATOM(uint16_t, port, PORT_DNA, cf_opt_uint16_nonzero,, "Port number") END_STRUCT ARRAY(host_list, NO_DUPLICATES) @@ -290,7 +290,7 @@ ATOM(int, exclude, 0, cf_opt_boolean,, "If true, do not use ATOM(struct pattern_list, match, PATTERN_LIST_EMPTY, cf_opt_pattern_list,, "Names that match network interface") STRING(256, dummy, "", cf_opt_str_nonempty,, "Path of dummy file, absolute or relative to server.dummy_interface_dir") ATOM(short, type, OVERLAY_INTERFACE_WIFI, cf_opt_interface_type,, "Type of network interface") -ATOM(uint16_t, port, RHIZOME_HTTP_PORT, cf_opt_port,, "Port number for network interface") +ATOM(uint16_t, port, RHIZOME_HTTP_PORT, cf_opt_uint16_nonzero,, "Port number for network interface") ATOM(uint64_t, speed, 1000000, cf_opt_uint64_scaled,, "Speed in bits per second") ATOM(int, mdp_tick_ms, -1, cf_opt_int32_nonneg,, "Override MDP tick interval for this interface") END_STRUCT