summaryrefslogtreecommitdiff
path: root/shared/mc-protocol.c
diff options
context:
space:
mode:
authorDavid Härdeman <david@hardeman.nu>2020-06-28 14:58:32 +0200
committerDavid Härdeman <david@hardeman.nu>2020-06-28 14:58:32 +0200
commitd3352b997ca59a336a40fe6660c6e5b7079864aa (patch)
treef365d6c0304e512057223e9a3cf37357da44a443 /shared/mc-protocol.c
parent99b2c70137fef05a5a18f439b9010ddba455f5cb (diff)
Run clang-format on source tree (excl igmp.c)
Diffstat (limited to 'shared/mc-protocol.c')
-rw-r--r--shared/mc-protocol.c71
1 files changed, 31 insertions, 40 deletions
diff --git a/shared/mc-protocol.c b/shared/mc-protocol.c
index afc9b38..03a2dcd 100644
--- a/shared/mc-protocol.c
+++ b/shared/mc-protocol.c
@@ -4,15 +4,14 @@
#include "utils.h"
-#define MC_HELO 0x00
-#define MC_NEXT_STATE_STATUS 0x01
-#define MC_GET_STATUS 0x00
-#define MC_VARINT_MAX_BYTES 5
-#define MC_STATUS_REPLY 0x00
-#define MC_UNDEFINED_VERSION -1
-
-static inline unsigned
-write_byte(char **pos, char byte)
+#define MC_HELO 0x00
+#define MC_NEXT_STATE_STATUS 0x01
+#define MC_GET_STATUS 0x00
+#define MC_VARINT_MAX_BYTES 5
+#define MC_STATUS_REPLY 0x00
+#define MC_UNDEFINED_VERSION -1
+
+static inline unsigned write_byte(char **pos, char byte)
{
if (pos && *pos) {
**pos = byte;
@@ -20,8 +19,7 @@ write_byte(char **pos, char byte)
}
return 1;
}
-static inline unsigned
-write_varint(char **pos, int32_t v)
+static inline unsigned write_varint(char **pos, int32_t v)
{
uint32_t u = (uint32_t)v;
unsigned rv = 0;
@@ -40,8 +38,7 @@ write_varint(char **pos, int32_t v)
return rv;
}
-static inline unsigned
-write_bytes(char **pos, const char *bytes, size_t n)
+static inline unsigned write_bytes(char **pos, const char *bytes, size_t n)
{
if (pos && *pos) {
memcpy(*pos, bytes, n);
@@ -51,8 +48,7 @@ write_bytes(char **pos, const char *bytes, size_t n)
return n;
}
-static inline unsigned
-write_str(char **pos, const char *str, size_t len)
+static inline unsigned write_str(char **pos, const char *str, size_t len)
{
unsigned rv;
@@ -68,8 +64,7 @@ write_str(char **pos, const char *str, size_t len)
* 0 = need more bytes
* < 0 = error
*/
-static inline int
-read_varint(const char **from, size_t *remain, int32_t *res)
+static inline int read_varint(const char **from, size_t *remain, int32_t *res)
{
unsigned consumed;
uint32_t val = 0;
@@ -103,8 +98,7 @@ read_varint(const char **from, size_t *remain, int32_t *res)
* 0 = need more bytes
* < 0 = error
*/
-int
-mc_is_handshake_complete(const char *buf, size_t len)
+int mc_is_handshake_complete(const char *buf, size_t len)
{
int32_t mclen;
int r;
@@ -123,21 +117,21 @@ mc_is_handshake_complete(const char *buf, size_t len)
}
if (mclen < len) {
- debug(DBG_IDLE, "short MC message - len: %" PRIi32 ", remain: %zu",
- mclen, len);
+ debug(DBG_IDLE,
+ "short MC message - len: %" PRIi32 ", remain: %zu", mclen,
+ len);
return 0;
- }
+ }
debug(DBG_IDLE, "Complete message");
return 1;
}
-#define PLAYERS_NEEDLE "\"players\""
-#define MAX_NEEDLE "\"max\""
-#define ONLINE_NEEDLE "\"online\""
-static bool
-mc_get_player_count(const char *buf, size_t remain,
- unsigned *ronline, unsigned *rmax)
+#define PLAYERS_NEEDLE "\"players\""
+#define MAX_NEEDLE "\"max\""
+#define ONLINE_NEEDLE "\"online\""
+static bool mc_get_player_count(const char *buf, size_t remain,
+ unsigned *ronline, unsigned *rmax)
{
/*
* Example JSON (line breaks added):
@@ -182,9 +176,8 @@ error:
return false;
}
-bool
-mc_protocol_parse_status_reply(const char *buf, size_t len,
- unsigned *online, unsigned *max)
+bool mc_protocol_parse_status_reply(const char *buf, size_t len,
+ unsigned *online, unsigned *max)
{
const char *from = buf;
size_t remain = len;
@@ -201,8 +194,8 @@ mc_protocol_parse_status_reply(const char *buf, size_t len,
return false;
}
- debug(DBG_IDLE, "MC message - len: %" PRIi32 ", remain: %zu",
- mclen, remain);
+ debug(DBG_IDLE, "MC message - len: %" PRIi32 ", remain: %zu", mclen,
+ remain);
if (*from != MC_STATUS_REPLY) {
error("unknown server reply (0x%02hhx)", *from);
@@ -218,8 +211,8 @@ mc_protocol_parse_status_reply(const char *buf, size_t len,
return false;
}
- debug(DBG_IDLE, "MC - json len: %" PRIi32 ", remain: %zu",
- jsonlen, remain);
+ debug(DBG_IDLE, "MC - json len: %" PRIi32 ", remain: %zu", jsonlen,
+ remain);
if (jsonlen < remain) {
error("invalid JSON length");
@@ -232,9 +225,8 @@ mc_protocol_parse_status_reply(const char *buf, size_t len,
return false;
}
-bool
-mc_protocol_create_status_request(char *buf, size_t len, size_t *rlen,
- struct saddr *saddr)
+bool mc_protocol_create_status_request(char *buf, size_t len, size_t *rlen,
+ struct saddr *saddr)
{
uint16_t port;
char hostname[INET6_ADDRSTRLEN];
@@ -248,7 +240,7 @@ mc_protocol_create_status_request(char *buf, size_t len, size_t *rlen,
hostlen = strlen(hostname);
/* First, a handshake, calculate body length... */
- bodylen = write_byte(NULL, MC_HELO);
+ bodylen = write_byte(NULL, MC_HELO);
bodylen += write_varint(NULL, MC_UNDEFINED_VERSION);
bodylen += write_str(NULL, hostname, hostlen);
bodylen += write_byte(NULL, (port >> 8) & 0xff);
@@ -279,4 +271,3 @@ mc_protocol_create_status_request(char *buf, size_t len, size_t *rlen,
*rlen = packetlen;
return true;
}
-