From 9ae205157e4ba739dccab09cdbbc583c162b675a Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Wed, 30 Dec 2020 14:53:46 +0100 Subject: [PATCH] Clean up the request jump table. Use C99 designated array initializers to ensure the jump table and list of request names is kept in sync with the enums. --- src/protocol.c | 41 +++++++++++++++++++++++++++-------------- src/protocol.h | 6 +++--- 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/src/protocol.c b/src/protocol.c index 39f529a2..5da89c7d 100644 --- a/src/protocol.c +++ b/src/protocol.c @@ -31,23 +31,36 @@ /* Jumptable for the request handlers */ -static bool (*request_handlers[])(meshlink_handle_t *, connection_t *, const char *) = { - id_h, NULL, NULL, NULL /* metakey_h, challenge_h, chal_reply_h */, ack_h, - status_h, error_h, termreq_h, - ping_h, pong_h, - NULL, NULL, //add_subnet_h, del_subnet_h, - add_edge_h, del_edge_h, - key_changed_h, req_key_h, ans_key_h, tcppacket_h, NULL, //control_h, +static bool (*request_handlers[NUM_REQUESTS])(meshlink_handle_t *, connection_t *, const char *) = { + [ID] = id_h, + [ACK] = ack_h, + [STATUS] = status_h, + [ERROR] = error_h, + [TERMREQ] = termreq_h, + [PING] = ping_h, + [PONG] = pong_h, + [ADD_EDGE] = add_edge_h, + [DEL_EDGE] = del_edge_h, + [KEY_CHANGED] = key_changed_h, + [REQ_KEY] = req_key_h, + [ANS_KEY] = ans_key_h, }; /* Request names */ -static const char *request_name[] = { - "ID", "METAKEY", "CHALLENGE", "CHAL_REPLY", "ACK", - "STATUS", "ERROR", "TERMREQ", - "PING", "PONG", - "ADD_SUBNET", "DEL_SUBNET", - "ADD_EDGE", "DEL_EDGE", "KEY_CHANGED", "REQ_KEY", "ANS_KEY", "PACKET", "CONTROL", +static const char *request_name[NUM_REQUESTS] = { + [ID] = "ID", + [ACK] = "ACK", + [STATUS] = "STATUS", + [ERROR] = "ERROR", + [TERMREQ] = "TERMREQ", + [PING] = "PING", + [PONG] = "PONG", + [ADD_EDGE] = "ADD_EDGE", + [DEL_EDGE] = "DEL_EDGE", + [KEY_CHANGED] = "KEY_CHANGED", + [REQ_KEY] = "REQ_KEY", + [ANS_KEY] = "ANS_KEY", }; bool check_id(const char *id) { @@ -137,7 +150,7 @@ bool receive_request(meshlink_handle_t *mesh, connection_t *c, const char *reque int reqno = atoi(request); if(reqno || *request == '0') { - if((reqno < 0) || (reqno >= LAST) || !request_handlers[reqno]) { + if((reqno < 0) || (reqno >= NUM_REQUESTS) || !request_handlers[reqno]) { logger(mesh, MESHLINK_DEBUG, "Unknown request from %s: %s", c->name, request); return false; } else { diff --git a/src/protocol.h b/src/protocol.h index 060d7dff..f72d42d8 100644 --- a/src/protocol.h +++ b/src/protocol.h @@ -36,7 +36,7 @@ /* Request numbers */ typedef enum request_t { - ALL = -1, /* Guardian for allow_request */ + ALL = -1, /* Guardian for allow_request */ ID = 0, METAKEY, CHALLENGE, CHAL_REPLY, ACK, STATUS, ERROR, TERMREQ, PING, PONG, @@ -44,11 +44,11 @@ typedef enum request_t { ADD_EDGE, DEL_EDGE, KEY_CHANGED, REQ_KEY, ANS_KEY, PACKET, - /* Tinc 1.1 requests */ + /* Extended requests */ CONTROL, REQ_PUBKEY, ANS_PUBKEY, REQ_SPTPS, - LAST /* Guardian for the highest request number */ + NUM_REQUESTS } request_t; typedef enum request_error_t { -- 2.39.5