]> git.meshlink.io Git - meshlink/blobdiff - src/protocol.c
Clean up the request jump table.
[meshlink] / src / protocol.c
index e2842abe5c7665ab341b1af86213c3f4f981b04b..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) {
@@ -67,10 +80,14 @@ bool check_id(const char *id) {
    detection as well */
 
 bool send_request(meshlink_handle_t *mesh, connection_t *c, const submesh_t *s, const char *format, ...) {
-       assert(c);
        assert(format);
        assert(*format);
 
+       if(!c) {
+               logger(mesh, MESHLINK_ERROR, "Trying to send request to non-existing connection");
+               return false;
+       }
+
        va_list args;
        char request[MAXBUFSIZE];
        int len;
@@ -133,14 +150,14 @@ 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 {
                        logger(mesh, MESHLINK_DEBUG, "Got %s from %s: %s", request_name[reqno], c->name, request);
                }
 
-               if((c->allow_request != ALL) && (c->allow_request != reqno)) {
+               if((c->allow_request != ALL) && (c->allow_request != reqno) && (reqno != ERROR)) {
                        logger(mesh, MESHLINK_ERROR, "Unauthorized request from %s", c->name);
                        return false;
                }