]> mj.ucw.cz Git - nwho.git/commitdiff
Cleaned up parsing of packets
authorMartin Mares <mj@ucw.cz>
Fri, 31 Dec 2010 15:27:09 +0000 (16:27 +0100)
committerMartin Mares <mj@ucw.cz>
Fri, 31 Dec 2010 15:27:09 +0000 (16:27 +0100)
nwhod.c

diff --git a/nwhod.c b/nwhod.c
index 4f348e60d6886364613d79b5431f2d8de3ca0e99..af38e85e93bc10953229e91f0f4e51ff4d2d26f3 100644 (file)
--- 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;