From eabb10d017c312c5fac5446fbac5a1c23af94ae3 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Mon, 16 May 2016 02:54:33 -0400 Subject: change the protocol up a bit to allow different message types --- src/daemon.c | 49 +++++++++++++++++++++++++++++++++---------------- src/protocol.c | 41 ++++++++++++++++++++++++++++++----------- src/protocol.h | 24 ++++++++++++++++-------- src/runesc.c | 6 +++--- 4 files changed, 82 insertions(+), 38 deletions(-) diff --git a/src/daemon.c b/src/daemon.c index 44c3c11..c354d67 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -16,6 +16,8 @@ #include "term.h" static int runes_daemon_handle_request(void *t); +static void runes_daemon_handle_new_term_message( + RunesDaemon *daemon, char *buf, size_t len); RunesDaemon *runes_daemon_new(RunesLoop *loop, RunesWindowBackend *wb) { @@ -48,33 +50,48 @@ void runes_socket_delete(RunesDaemon *daemon) static int runes_daemon_handle_request(void *t) { RunesDaemon *daemon = (RunesDaemon *)t; - int client_sock; + int client_sock, type; char *buf; size_t len; - struct runes_protocol_message msg; - RunesTerm *new_term; client_sock = runes_socket_server_accept(daemon->sock); - if (!runes_protocol_read_packet(client_sock, &buf, &len)) { + if (runes_protocol_read_packet(client_sock, &type, &buf, &len)) { + switch (type) { + case RUNES_PROTOCOL_NEW_TERM: + runes_daemon_handle_new_term_message(daemon, buf, len); + break; + default: + runes_warn("unknown packet type: %d", type); + break; + } + + free(buf); + } + else { runes_warn("invalid packet received"); - return 1; } runes_socket_client_close(client_sock); - if (!runes_protocol_parse_message(buf, len, &msg)) { - runes_warn("couldn't parse message from client"); - free(buf); - return 1; - } + return 1; +} + +static void runes_daemon_handle_new_term_message( + RunesDaemon *daemon, char *buf, size_t len) +{ + struct runes_protocol_new_term_message msg; - new_term = runes_term_new( - msg.argc, msg.argv, msg.envp, msg.cwd, daemon->wb); - runes_term_register_with_loop(new_term, daemon->loop); + if (runes_protocol_parse_new_term_message(buf, len, &msg)) { + RunesTerm *new_term; - free(buf); - runes_protocol_free_message(&msg); + new_term = runes_term_new( + msg.argc, msg.argv, msg.envp, msg.cwd, daemon->wb); + runes_term_register_with_loop(new_term, daemon->loop); - return 1; + runes_protocol_free_new_term_message(&msg); + } + else { + runes_warn("couldn't parse message from client"); + } } 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; diff --git a/src/protocol.h b/src/protocol.h index fb3af96..67d6be0 100644 --- a/src/protocol.h +++ b/src/protocol.h @@ -5,19 +5,27 @@ #define RUNES_PROTOCOL_MESSAGE_VERSION 1 -struct runes_protocol_message { +struct runes_protocol_new_term_message { uint32_t argc; char **argv; char **envp; char *cwd; }; -int runes_protocol_parse_message( - char *buf, size_t len, struct runes_protocol_message *outmsg); -int runes_protocol_create_message( - struct runes_protocol_message *msg, char **outbuf, size_t *outlen); -void runes_protocol_free_message(struct runes_protocol_message *msg); -int runes_protocol_read_packet(int sock, char **outbuf, size_t *outlen); -int runes_protocol_send_packet(int s, char *buf, size_t len); +enum runes_protocol_message_type { + RUNES_PROTOCOL_NEW_TERM, + RUNES_PROTOCOL_NUM_MESSAGE_TYPES +}; + +int runes_protocol_parse_new_term_message( + char *buf, size_t len, struct runes_protocol_new_term_message *outmsg); +int runes_protocol_create_new_term_message( + struct runes_protocol_new_term_message *msg, + char **outbuf, size_t *outlen); +void runes_protocol_free_new_term_message( + struct runes_protocol_new_term_message *msg); +int runes_protocol_read_packet( + int sock, int *outtype, char **outbuf, size_t *outlen); +int runes_protocol_send_packet(int s, int type, char *buf, size_t len); #endif diff --git a/src/runesc.c b/src/runesc.c index 88adec3..7cbc456 100644 --- a/src/runesc.c +++ b/src/runesc.c @@ -13,7 +13,7 @@ int main (int argc, char *argv[]) char *name, *buf; int s; size_t len; - struct runes_protocol_message msg; + struct runes_protocol_new_term_message msg; msg.argc = argc; msg.argv = argv; @@ -22,11 +22,11 @@ int main (int argc, char *argv[]) name = runes_get_daemon_socket_name(); s = runes_socket_client_open(name); - if (!runes_protocol_create_message(&msg, &buf, &len)) { + if (!runes_protocol_create_new_term_message(&msg, &buf, &len)) { runes_warn("couldn't create message"); } else { - if (!runes_protocol_send_packet(s, buf, len)) { + if (!runes_protocol_send_packet(s, RUNES_PROTOCOL_NEW_TERM, buf, len)) { runes_warn("couldn't send packet"); free(buf); } -- cgit v1.2.3-54-g00ecf