From 42aad279a7467d9369fc88aff3d03e7e362308cc Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Fri, 31 Dec 2010 16:27:09 +0100 Subject: [PATCH] Cleaned up parsing of packets --- nwhod.c | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/nwhod.c b/nwhod.c index 4f348e6..af38e85 100644 --- a/nwhod.c +++ b/nwhod.c @@ -218,25 +218,23 @@ receive(void) if (!is_valid(sa.sin_addr.s_addr) || sa.sin_port != port) { - syslog(LOG_WARNING, "Received packet from invalid source %s.%d", inet_ntoa(sa.sin_addr), ntohs(sa.sin_port)); + syslog(LOG_WARNING, "Received packet from invalid source %s:%d", inet_ntoa(sa.sin_addr), ntohs(sa.sin_port)); return; } - if (r < n || r != n + ntohl(pkt.num_users)*sizeof(struct userinfo)) + if (r < n || + pkt.magic != htonl(NWHO_MAGIC) || + ntohl(pkt.num_users) > MAX_USERS || + r != n + ntohl(pkt.num_users)*sizeof(struct userinfo)) { syslog(LOG_WARNING, "Malformed packet from %s", inet_ntoa(sa.sin_addr)); return; } - if (pkt.magic != htonl(NWHO_MAGIC)) - { - syslog(LOG_WARNING, "Received ancient nwho packet from %s", inet_ntoa(sa.sin_addr)); - return; - } - for(e=first_host; e; e=e->next) if (e->addr == sa.sin_addr.s_addr) break; + if (!e) { e = malloc(sizeof(struct hostrec)); @@ -250,8 +248,8 @@ receive(void) h = gethostbyaddr((char *) &sa.sin_addr, sizeof(sa.sin_addr), AF_INET); if (h) { - sprintf(e->name, "%.30s", h->h_name); - for(c=e->name; *c; c++) + snprintf(e->name, sizeof(e->name), "%s", h->h_name); + for (c=e->name; *c; c++) if (*c == '.') { *c = 0; -- 2.39.2