]> git.meshlink.io Git - meshlink/commitdiff
Clean up the request jump table.
authorGuus Sliepen <guus@meshlink.io>
Wed, 30 Dec 2020 13:53:46 +0000 (14:53 +0100)
committerGuus Sliepen <guus@meshlink.io>
Wed, 30 Dec 2020 13:53:46 +0000 (14:53 +0100)
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
src/protocol.h

index 39f529a268045ef78a1956a1970380dc440c719e..5da89c7d4ea272549f97603f1dd3261d099045f5 100644 (file)
 
 /* 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 {
index 060d7dff8b201a4a923615652e6e1d50b708259c..f72d42d8999869c8f2baa27daf2dbad868a3660a 100644 (file)
@@ -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 {