diff options
author | Jose M. Guisado <jguisado@soleta.eu> | 2023-06-05 18:15:58 +0200 |
---|---|---|
committer | Jose M. Guisado <jguisado@soleta.eu> | 2023-06-13 14:04:54 +0200 |
commit | d2c19ef13d73b92b94a29e2b226d76347d36374e (patch) | |
tree | 8b3a6fe6eb57c0f22f1a18f105b41ced35cfaef8 /src/core.c | |
parent | 92f83c0385281c1d1e7996fc76c071e782ea6939 (diff) |
core: add X-Sequence header supportv1.2.2
Add non-standard HTTP header "X-Sequence" to the header section of
requests (og_send_request) sent to a connected client.
Define a starting sequence number when creating a new instance of struct
og_client inside og_server_accept_cb. This sequence number is
incremented by one for each outgoing request from ogServer.
This sequence number is checked when receiving a response from a
connected client, if they do not match the connection is dropped.
Use sequence 0 for out-of-band commands (reboot, poweroff, stop). Any
client response with header "X-Sequence: 0" bypasses sequence check.
Diffstat (limited to 'src/core.c')
-rw-r--r-- | src/core.c | 15 |
1 files changed, 15 insertions, 0 deletions
@@ -160,6 +160,7 @@ enum og_agent_state { static int og_agent_state_recv_hdr_rest(struct og_client *cli) { + uint32_t seq; char *ptr; ptr = strstr(cli->buf, "\r\n\r\n"); @@ -176,6 +177,20 @@ static int og_agent_state_recv_hdr_rest(struct og_client *cli) cli->msg_len += cli->content_length; } + ptr = strstr(cli->buf, "X-Sequence: "); + if (ptr) { + if (sscanf(ptr, "X-Sequence: %i[^\r\n]", &seq) != 1) { + syslog(LOG_ERR, "Invalid sequence value from client %s. Payload:\n%s", + inet_ntoa(cli->addr.sin_addr), cli->buf); + return -1; + } + if (cli->seq != 0 && cli->seq != seq) { + syslog(LOG_ERR, "Unexpected sequence %u from client %s, expecting %u.", + seq, inet_ntoa(cli->addr.sin_addr), cli->seq); + return -1; + } + } + return 1; } |