diff options
author | Christian Pointner <equinox@spreadspace.org> | 2014-09-24 23:22:11 +0200 |
---|---|---|
committer | Christian Pointner <equinox@spreadspace.org> | 2014-09-24 23:22:11 +0200 |
commit | ffa474615098fd1bffecac8fc6d617223a4a1744 (patch) | |
tree | fef4eec32590fa7b371b245e4e38a4744d6bb310 /src/sydra.c | |
parent | removed useless parse hex string (diff) |
added simple UDP protocol
Diffstat (limited to 'src/sydra.c')
-rw-r--r-- | src/sydra.c | 24 |
1 files changed, 20 insertions, 4 deletions
diff --git a/src/sydra.c b/src/sydra.c index c7503b6..624ce2e 100644 --- a/src/sydra.c +++ b/src/sydra.c @@ -459,6 +459,13 @@ static void udp_remove_client(struct sockaddr_storage *addr, socklen_t addrlen, g_free(name); } +#define UDP_PROTO_MAGIC "SYDRA:" +#define UDP_PROTO_MAGIC_LEN (sizeof(UDP_PROTO_MAGIC)-1) +#define UDP_PROTO_CMD_ADD_CLIENT "add_client\n" +#define UDP_PROTO_CMD_ADD_CLIENT_LEN (sizeof(UDP_PROTO_CMD_ADD_CLIENT)-1) +#define UDP_PROTO_CMD_RM_CLIENT "remove_client\n" +#define UDP_PROTO_CMD_RM_CLIENT_LEN (sizeof(UDP_PROTO_CMD_RM_CLIENT)-1) + static gboolean on_udp_desc_ready(gint fd, GIOCondition cond, gpointer user_data) { struct udp_sink* sink = (struct udp_sink*)user_data; @@ -469,7 +476,7 @@ static gboolean on_udp_desc_ready(gint fd, GIOCondition cond, gpointer user_data struct sockaddr_storage addr; socklen_t addrlen = sizeof(addr); - char buf[2048]; + u_int8_t buf[2048]; ssize_t bytes = recvfrom(fd, buf, sizeof(buf), 0, (struct sockaddr *)&addr, &addrlen); if(bytes < 1) { if(errno == EINTR) @@ -479,11 +486,20 @@ static gboolean on_udp_desc_ready(gint fd, GIOCondition cond, gpointer user_data return FALSE; } - // TODO: parse real packet format - if(buf[0] == 'a') { + if(bytes < UDP_PROTO_MAGIC_LEN || memcmp(UDP_PROTO_MAGIC, buf, UDP_PROTO_MAGIC_LEN)) { + log_printf(DEBUG, "client discovery: ignoring invalid incoming packet"); + return TRUE; + } + + u_int8_t* cmd = &(buf[UDP_PROTO_MAGIC_LEN]); + if(bytes == (UDP_PROTO_MAGIC_LEN+UDP_PROTO_CMD_ADD_CLIENT_LEN) && + !memcmp(UDP_PROTO_CMD_ADD_CLIENT, cmd, UDP_PROTO_CMD_ADD_CLIENT_LEN)) { udp_add_client(&addr, addrlen, sink); - } else { + } else if(bytes == (UDP_PROTO_MAGIC_LEN+UDP_PROTO_CMD_RM_CLIENT_LEN) && + !memcmp(UDP_PROTO_CMD_RM_CLIENT, cmd, UDP_PROTO_CMD_RM_CLIENT_LEN)) { udp_remove_client(&addr, addrlen, sink); + } else { + log_printf(DEBUG, "client discovery: ignoring invalid command"); } return TRUE; } |