summaryrefslogtreecommitdiff
path: root/shared
diff options
context:
space:
mode:
authorDavid Härdeman <david@hardeman.nu>2020-07-01 21:29:06 +0200
committerDavid Härdeman <david@hardeman.nu>2020-07-01 21:29:06 +0200
commit7e728d971b95f115c7e9c2def21815034d9bde54 (patch)
treeef719c2cc2a42ff765b43e888fddc2f5f54ed9d7 /shared
parent2ca520b06a1a80497dd39c98533ae21c165752c2 (diff)
Reduce some rcon duplication by using a common function
Diffstat (limited to 'shared')
-rw-r--r--shared/rcon-protocol.c29
-rw-r--r--shared/rcon-protocol.h16
2 files changed, 39 insertions, 6 deletions
diff --git a/shared/rcon-protocol.c b/shared/rcon-protocol.c
index 0cf73fc..1941662 100644
--- a/shared/rcon-protocol.c
+++ b/shared/rcon-protocol.c
@@ -75,6 +75,35 @@ static void write_end(char **pos, size_t *len)
*len += RCON_END_LEN;
}
+bool rcon_protocol_verify_response(int32_t id_sent, int32_t id_recv,
+ enum rcon_packet_type t_sent,
+ enum rcon_packet_type t_recv,
+ const char **error)
+{
+ if (t_sent == RCON_PACKET_LOGIN) {
+ if (t_recv != RCON_PACKET_LOGIN_RESPONSE) {
+ *error = "invalid reply id";
+ return false;
+ } else if (id_recv == RCON_PACKET_LOGIN_FAIL_ID) {
+ *error = "login failure";
+ return false;
+ } else if (id_recv != id_sent) {
+ *error = "invalid reply id";
+ return false;
+ }
+ } else {
+ if (t_recv != RCON_PACKET_RESPONSE) {
+ *error = "invalid reply type";
+ return false;
+ } else if (id_recv != id_sent) {
+ *error = "invalid reply id";
+ return false;
+ }
+ }
+
+ return true;
+}
+
bool rcon_protocol_create_packet(char *buf, size_t len, size_t *rlen,
int32_t reqid, enum rcon_packet_type type,
const char *msg)
diff --git a/shared/rcon-protocol.h b/shared/rcon-protocol.h
index b3aea98..593a43f 100644
--- a/shared/rcon-protocol.h
+++ b/shared/rcon-protocol.h
@@ -4,14 +4,13 @@
#include <stdbool.h>
#include <stdint.h>
-/* FIXME: FAIL is an id, not type, LOGIN_OK should be LOGIN_RESPONSE */
/* clang-format off */
+#define RCON_PACKET_LOGIN_FAIL_ID -1
enum rcon_packet_type {
- RCON_PACKET_LOGIN = 3,
- RCON_PACKET_LOGIN_OK = 2,
- RCON_PACKET_LOGIN_FAIL = -1,
- RCON_PACKET_COMMAND = 2,
- RCON_PACKET_RESPONSE = 0,
+ RCON_PACKET_LOGIN = 3,
+ RCON_PACKET_LOGIN_RESPONSE = 2,
+ RCON_PACKET_COMMAND = 2,
+ RCON_PACKET_RESPONSE = 0,
};
/* clang-format on */
@@ -30,6 +29,11 @@ static inline size_t rcon_protocol_packet_len(size_t msglen)
return (RCON_PKT_MIN_LEN + msglen);
}
+bool rcon_protocol_verify_response(int32_t id_sent, int32_t id_recv,
+ enum rcon_packet_type t_sent,
+ enum rcon_packet_type t_recv,
+ const char **error);
+
bool rcon_protocol_create_packet(char *buf, size_t len, size_t *rlen,
int32_t reqid, enum rcon_packet_type type,
const char *msg);