diff options
Diffstat (limited to 'switchctl.c')
-rw-r--r-- | switchctl.c | 66 |
1 files changed, 62 insertions, 4 deletions
diff --git a/switchctl.c b/switchctl.c index b293015..5d98b0b 100644 --- a/switchctl.c +++ b/switchctl.c @@ -78,11 +78,67 @@ int send_response(int fd, const char* response) int process_cmd_request(const char* cmd, cmd_id_t cmd_id, const char* param, int fd, cmd_t **cmd_q, client_t* client_lst, options_t* opt) { - char* tmp = key_value_storage_find(&opt->alias_table_, param); - int ret = cmd_push(cmd_q, fd, cmd_id, tmp != NULL ? tmp : param); + client_t* c = client_find(client_lst, fd); + if(!c) { + log_printf(WARNING, "ignoring request form unknown client"); + return 0; + } + + if(c->type == HEARTBEAT) { + log_printf(WARNING, "ignoring requests form heartbeat agent"); + return 0; + } + + char* cmd_param = NULL; + if(cmd_id == SWITCH) { + if((opt->mode_ == MODE_MASTER && c->type == STANDBY )|| + (opt->mode_ == MODE_STANDBY && c->type == MASTER )) + { + log_printf(INFO, "ignoring request from inactive system (%s)", c->type == MASTER ? "master" : "standby"); + return 0; + } + + if(!param) { + log_printf(INFO, "ignoring switch command without parameter"); + return 0; + } + + if(param[0] == '*') { + cmd_param = strdup(param); + } + else { + const char* ch_name = NULL; + cmd_param = strdup("*0Fxnn"); + if(!strncmp(param, "up ", 3)) { + cmd_param[3] = 'U'; + ch_name = &(param[3]); + } + else if(!strncmp(param, "down ", 5)) { + cmd_param[3] = 'D'; + ch_name = &(param[5]); + } + else { + log_printf(INFO, "ignoring invalid switch command: '%s'", param); + return 0; + } + char* ch_nr = key_value_storage_find(&opt->alias_table_, ch_name); + if(!ch_nr || ch_nr[0] == 0 || ch_nr[1] == 0 || ch_nr[2] != 0) { + log_printf(ERROR, "invalid channel name or number: %s", ch_name); + return 0; + } + + cmd_param[4] = ch_nr[0]; + cmd_param[5] = ch_nr[1]; + } + log_printf(DEBUG, "enqueing command to switch: '%s' (request was: '%s')", cmd_param, param); + } + + int ret = cmd_push(cmd_q, fd, cmd_id, cmd_param); + if(cmd_param) + free(cmd_param); if(ret) return ret; - + if(cmd_id == STATUS) { char buf[30]; snprintf(buf, 30, "Current Mode: %s", opt->mode_ == MODE_MASTER ? "Master" : "Standby"); @@ -96,7 +152,7 @@ int process_cmd_request(const char* cmd, cmd_id_t cmd_id, const char* param, int } log_printf(DEBUG, "sent status to %d additional listeners", listener_cnt); } - + log_printf(NOTICE, "command: %s", cmd); return 0; @@ -141,6 +197,8 @@ void process_cmd_mode(const char* param, options_t* opt) } else log_printf(ERROR, "unable to set mode: empty parameter"); + + log_printf(NOTICE, "new mode: %s", opt->mode_ == MODE_MASTER ? "master" : "standby"); } void process_cmd_listen(const char* param, int fd, client_t* client_lst) |