From: Andre Noll Date: Mon, 28 Sep 2015 17:43:39 +0000 (+0000) Subject: Avoid duplication of sender subcommands. X-Git-Tag: v0.5.6~56^2 X-Git-Url: http://git.tuebingen.mpg.de/?p=paraslash.git;a=commitdiff_plain;h=c241ffeb4c05234e6e8215bb8f55566f9f9383bb Avoid duplication of sender subcommands. The list of sender subcommands (add, delete, allow, deny, on, off) is defined as an enumeration in send.h. This list is duplicated in check_sender_args() of command.c which contains the six subcommands as C-strings to be matched against the first word of the sender command line. For the code to work properly it is essential that the two subcommand lists are identical, which is a bad design that is quite error prone. Fortunately it is easy to avoid the duplication with a little preprocessor fu. Since the subcommands are spelled in lower case and there is no toupper function in CPP, we need to change the subcommand part of the enumeration constants to lower case. The bulk of the patch consists in trivial changes of all the users of these constants. --- diff --git a/command.c b/command.c index e8f69484..3ab34daa 100644 --- a/command.c +++ b/command.c @@ -167,9 +167,8 @@ static unsigned get_status(struct misc_meta_data *nmmd, int parser_friendly, static int check_sender_args(int argc, char * const * argv, struct sender_command_data *scd) { int i; - /* this has to match sender.h */ - const char *subcmds[] = {"add", "delete", "allow", "deny", "on", "off", NULL}; + const char *subcmds[] = {SENDER_SUBCOMMANDS NULL}; scd->sender_num = -1; if (argc < 3) return -E_COMMAND_SYNTAX; @@ -189,19 +188,19 @@ static int check_sender_args(int argc, char * const * argv, struct sender_comman if (!senders[scd->sender_num].client_cmds[scd->cmd_num]) return -E_SENDER_CMD; switch (scd->cmd_num) { - case SENDER_ON: - case SENDER_OFF: + case SENDER_on: + case SENDER_off: if (argc != 3) return -E_COMMAND_SYNTAX; break; - case SENDER_DENY: - case SENDER_ALLOW: + case SENDER_deny: + case SENDER_allow: if (argc != 4 || parse_cidr(argv[3], scd->host, sizeof(scd->host), &scd->netmask) == NULL) return -E_COMMAND_SYNTAX; break; - case SENDER_ADD: - case SENDER_DELETE: + case SENDER_add: + case SENDER_delete: if (argc != 4) return -E_COMMAND_SYNTAX; return parse_fec_url(argv[3], scd); @@ -364,8 +363,8 @@ static int com_sender(struct command_context *cc) } switch (scd.cmd_num) { - case SENDER_ADD: - case SENDER_DELETE: + case SENDER_add: + case SENDER_delete: assert(senders[scd.sender_num].resolve_target); ret = senders[scd.sender_num].resolve_target(cc->argv[3], &scd); if (ret < 0) diff --git a/dccp_send.c b/dccp_send.c index b038fc01..92dd9333 100644 --- a/dccp_send.c +++ b/dccp_send.c @@ -225,12 +225,12 @@ void dccp_send_init(struct sender *s) s->shutdown_clients = dccp_shutdown_clients; s->resolve_target = NULL; s->help = generic_sender_help; - s->client_cmds[SENDER_ON] = dccp_com_on; - s->client_cmds[SENDER_OFF] = dccp_com_off; - s->client_cmds[SENDER_DENY] = dccp_com_deny; - s->client_cmds[SENDER_ALLOW] = dccp_com_allow; - s->client_cmds[SENDER_ADD] = NULL; - s->client_cmds[SENDER_DELETE] = NULL; + s->client_cmds[SENDER_on] = dccp_com_on; + s->client_cmds[SENDER_off] = dccp_com_off; + s->client_cmds[SENDER_deny] = dccp_com_deny; + s->client_cmds[SENDER_allow] = dccp_com_allow; + s->client_cmds[SENDER_add] = NULL; + s->client_cmds[SENDER_delete] = NULL; k = conf.dccp_data_slices_per_group_arg; n = conf.dccp_slices_per_group_arg; diff --git a/http_send.c b/http_send.c index daf39e73..9d0f49ae 100644 --- a/http_send.c +++ b/http_send.c @@ -256,12 +256,12 @@ void http_send_init(struct sender *s) s->shutdown_clients = http_shutdown_clients; s->resolve_target = NULL; s->help = generic_sender_help; - s->client_cmds[SENDER_ON] = http_com_on; - s->client_cmds[SENDER_OFF] = http_com_off; - s->client_cmds[SENDER_DENY] = http_com_deny; - s->client_cmds[SENDER_ALLOW] = http_com_allow; - s->client_cmds[SENDER_ADD] = NULL; - s->client_cmds[SENDER_DELETE] = NULL; + s->client_cmds[SENDER_on] = http_com_on; + s->client_cmds[SENDER_off] = http_com_off; + s->client_cmds[SENDER_deny] = http_com_deny; + s->client_cmds[SENDER_allow] = http_com_allow; + s->client_cmds[SENDER_add] = NULL; + s->client_cmds[SENDER_delete] = NULL; init_sender_status(hss, conf.http_access_arg, conf.http_access_given, conf.http_port_arg, conf.http_max_clients_arg, diff --git a/send.h b/send.h index 09eb78ba..0c74f0ea 100644 --- a/send.h +++ b/send.h @@ -6,16 +6,21 @@ /** \file send.h Sender-related defines and structures. */ -/** The sender subcommands. */ +#define SENDER_SUBCOMMANDS \ + SENDER_SUBCOMMAND(add) /**< Add a target (udp only). */ \ + SENDER_SUBCOMMAND(delete) /**< Delete a target (udp only). */ \ + SENDER_SUBCOMMAND(allow) /**< Allow connections from given IP address(es). */ \ + SENDER_SUBCOMMAND(deny) /**< Deny connections from given IP address(es). */ \ + SENDER_SUBCOMMAND(on) /**< Activate the sender. */ \ + SENDER_SUBCOMMAND(off) /**< Deactivate the sender. */ \ + +#define SENDER_SUBCOMMAND(_name) SENDER_ ## _name, enum sender_subcommand { - SENDER_ADD, /**< Add a target (udp only). */ - SENDER_DELETE, /**< Delete a target (udp only). */ - SENDER_ALLOW, /**< Allow connections from given IP address(es). */ - SENDER_DENY, /**< Deny connections from given IP address(es). */ - SENDER_ON, /**< Activate the sender. */ - SENDER_OFF, /**< Deactivate the sender. */ + SENDER_SUBCOMMANDS NUM_SENDER_CMDS /**< Used as array size in struct \ref sender. */ }; +#undef SENDER_SUBCOMMAND +#define SENDER_SUBCOMMAND(_name) #_name, /** * Describes one supported sender of para_server. diff --git a/udp_send.c b/udp_send.c index 3379f98d..9d9ddfa5 100644 --- a/udp_send.c +++ b/udp_send.c @@ -192,14 +192,14 @@ static int udp_resolve_target(const char *url, struct sender_command_data *scd) static int udp_com_on(__a_unused struct sender_command_data *scd) { - sender_status = SENDER_ON; + sender_status = SENDER_on; return 1; } static int udp_com_off(__a_unused struct sender_command_data *scd) { udp_shutdown_targets(); - sender_status = SENDER_OFF; + sender_status = SENDER_off; return 1; } @@ -282,7 +282,7 @@ static void udp_send_fec(struct sender_client *sc, char *buf, size_t len) { int ret; - if (sender_status == SENDER_OFF) + if (sender_status == SENDER_off) return; if (len == 0) return; @@ -371,7 +371,7 @@ static char *udp_status(void) "status: %s\n" "port: %s\n" "targets: %s\n", - (sender_status == SENDER_ON)? "on" : "off", + (sender_status == SENDER_on)? "on" : "off", stringify_port(conf.udp_default_port_arg, "udp"), tgts? tgts : "(none)" ); @@ -430,15 +430,15 @@ void udp_send_init(struct sender *s) s->post_select = NULL; s->shutdown_clients = udp_shutdown_targets; s->resolve_target = udp_resolve_target; - s->client_cmds[SENDER_ON] = udp_com_on; - s->client_cmds[SENDER_OFF] = udp_com_off; - s->client_cmds[SENDER_DENY] = NULL; - s->client_cmds[SENDER_ALLOW] = NULL; - s->client_cmds[SENDER_ADD] = udp_com_add; - s->client_cmds[SENDER_DELETE] = udp_com_delete; - sender_status = SENDER_OFF; + s->client_cmds[SENDER_on] = udp_com_on; + s->client_cmds[SENDER_off] = udp_com_off; + s->client_cmds[SENDER_deny] = NULL; + s->client_cmds[SENDER_allow] = NULL; + s->client_cmds[SENDER_add] = udp_com_add; + s->client_cmds[SENDER_delete] = udp_com_delete; + sender_status = SENDER_off; udp_init_target_list(); if (!conf.udp_no_autostart_given) - sender_status = SENDER_ON; + sender_status = SENDER_on; PARA_DEBUG_LOG("udp sender init complete\n"); }