aboutsummaryrefslogtreecommitdiffstats
path: root/src/protocol.c
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2016-05-16 02:54:33 -0400
committerJesse Luehrs <doy@tozt.net>2016-05-16 03:05:55 -0400
commiteabb10d017c312c5fac5446fbac5a1c23af94ae3 (patch)
treed0fbf14a2ef098979a5c79f9d7d54055434fe80c /src/protocol.c
parent3859c8d205581ee7238552af6bab05bda17f70e4 (diff)
downloadrunes-eabb10d017c312c5fac5446fbac5a1c23af94ae3.tar.gz
runes-eabb10d017c312c5fac5446fbac5a1c23af94ae3.zip
change the protocol up a bit to allow different message types
Diffstat (limited to 'src/protocol.c')
-rw-r--r--src/protocol.c41
1 files changed, 30 insertions, 11 deletions
diff --git a/src/protocol.c b/src/protocol.c
index 6451f63..1e9991f 100644
--- a/src/protocol.c
+++ b/src/protocol.c
@@ -20,8 +20,8 @@ static ssize_t runes_protocol_load_str(char *buf, size_t len, char **str);
static int runes_protocol_read_bytes(int sock, size_t len, char *outbuf);
static int runes_protocol_write_bytes(int sock, char *buf, size_t len);
-int runes_protocol_parse_message(
- char *buf, size_t len, struct runes_protocol_message *outmsg)
+int runes_protocol_parse_new_term_message(
+ char *buf, size_t len, struct runes_protocol_new_term_message *outmsg)
{
uint32_t version;
size_t offset = 0;
@@ -55,8 +55,9 @@ int runes_protocol_parse_message(
return 1;
}
-int runes_protocol_create_message(
- struct runes_protocol_message *msg, char **outbuf, size_t *outlen)
+int runes_protocol_create_new_term_message(
+ struct runes_protocol_new_term_message *msg,
+ char **outbuf, size_t *outlen)
{
GArray *buf;
@@ -88,7 +89,8 @@ int runes_protocol_create_message(
return 1;
}
-void runes_protocol_free_message(struct runes_protocol_message *msg)
+void runes_protocol_free_new_term_message(
+ struct runes_protocol_new_term_message *msg)
{
char **p;
@@ -109,16 +111,22 @@ void runes_protocol_free_message(struct runes_protocol_message *msg)
free(msg->cwd);
}
-int runes_protocol_read_packet(int sock, char **outbuf, size_t *outlen)
+int runes_protocol_read_packet(
+ int sock, int *outtype, char **outbuf, size_t *outlen)
{
- uint32_t len;
+ uint32_t len, type;
char *buf;
if (!runes_protocol_read_bytes(sock, sizeof(len), (char *)&len)) {
return 0;
}
-
len = ntohl(len);
+
+ if (!runes_protocol_read_bytes(sock, sizeof(type), (char *)&type)) {
+ return 0;
+ }
+ type = ntohl(type);
+
if (len > RUNES_MAX_PACKET_SIZE) {
runes_warn("packet of size %d is too big", len);
return 0;
@@ -132,26 +140,37 @@ int runes_protocol_read_packet(int sock, char **outbuf, size_t *outlen)
*outlen = len;
*outbuf = buf;
+ *outtype = type;
return 1;
}
-int runes_protocol_send_packet(int sock, char *buf, size_t len)
+int runes_protocol_send_packet(int sock, int type, char *buf, size_t len)
{
- uint32_t len32 = len;
+ uint32_t len32 = len, type32 = type;
if (len > RUNES_MAX_PACKET_SIZE) {
runes_warn("packet of size %d is too big", len);
return 0;
}
- len32 = htonl(len32);
+ if (type < 0 || type >= RUNES_PROTOCOL_NUM_MESSAGE_TYPES) {
+ runes_warn("unknown message type %d", type);
+ return 0;
+ }
+ len32 = htonl(len32);
if (!runes_protocol_write_bytes(sock, (char *)&len32, sizeof(len32))) {
runes_warn("failed to write packet to socket");
return 0;
}
+ type32 = htonl(type32);
+ if (!runes_protocol_write_bytes(sock, (char *)&type32, sizeof(type32))) {
+ runes_warn("failed to write packet to socket");
+ return 0;
+ }
+
if (!runes_protocol_write_bytes(sock, buf, len)) {
runes_warn("failed to write packet to socket");
return 0;