]> mj.ucw.cz Git - nwho.git/blobdiff - nwhod.c
Let die() be shared between nwho and nwhod
[nwho.git] / nwhod.c
diff --git a/nwhod.c b/nwhod.c
index 4f348e60d6886364613d79b5431f2d8de3ca0e99..878db6b3844247f7a0b24b8f9e2a64b5fe7ee8a5 100644 (file)
--- a/nwhod.c
+++ b/nwhod.c
@@ -7,7 +7,6 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <stdarg.h>
 #include <fcntl.h>
 #include <unistd.h>
 #include <sys/socket.h>
@@ -22,7 +21,7 @@
 #include <dirent.h>
 #include <errno.h>
 
-#include "net.h"
+#include "nwho.h"
 
 struct hostrec {
   struct hostrec *next;
@@ -34,20 +33,7 @@ struct hostrec {
 static int sock, port;
 static struct hostrec *first_host;
 static time_t now, last_local_scan;
-
-static void die(char *msg, ...) __attribute__((noreturn));
-
-static void
-die(char *msg, ...)
-{
-  va_list args;
-
-  va_start(args, msg);
-  fprintf(stderr, "nwhod: ");
-  vfprintf(stderr, msg, args);
-  fputc('\n', stderr);
-  exit(1);
-}
+static char hostname[64];
 
 static void
 net_init(char *name)
@@ -79,6 +65,9 @@ net_init(char *name)
       if (connect(sock, (struct sockaddr *) &sa, sizeof(sa)) < 0)
        die("Cannot connect socket: %m");
     }
+
+  if (gethostname(hostname, sizeof(hostname)) < 0)
+    die("Unable to get my own host name: %m");
 }
 
 static void
@@ -170,8 +159,9 @@ cleanup(void)
 }
 
 static void
-save_pkt(char *name, struct nwho_pkt *pkt, int len)
+save_pkt(char *name, struct nwho_pkt *pkt)
 {
+  int len = nwho_pkt_size(pkt);
   int fd = open(name, O_WRONLY | O_CREAT, 0666);
   if (fd < 0)
     {
@@ -218,25 +208,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 < nwho_pkt_size(&pkt))
     {
       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 +238,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;
@@ -269,19 +257,16 @@ receive(void)
     }
 
   e->last_rec = now;
-  save_pkt(e->name, &pkt, r);
+  save_pkt(e->name, &pkt);
 }
 
 static void
 local_scan(void)
 {
   struct nwho_pkt pkt;
-  static char hostname[64];
 
   make_pkt(&pkt);
-  if (!hostname[0] && gethostname(hostname, sizeof(hostname)) < 0)
-    die("gethostname: %m");
-  save_pkt(hostname, &pkt, sizeof(pkt) - (MAX_USERS - ntohl(pkt.num_users))*sizeof(struct userinfo));
+  save_pkt(hostname, &pkt);
 }
 
 static void
@@ -359,7 +344,7 @@ client(char *serv)
     {
       now = time(NULL);
       make_pkt(&pkt);
-      if (send(sock, &pkt, sizeof(pkt) - (MAX_USERS - ntohl(pkt.num_users))*sizeof(struct userinfo), 0) < 0)
+      if (send(sock, &pkt, nwho_pkt_size(&pkt), 0) < 0)
        syslog(LOG_ERR, "sendmsg: %m");
       sleep(DEFAULT_SEND_TIME);
     }
@@ -368,6 +353,12 @@ client(char *serv)
 int
 main(int argc, char **argv)
 {
+  if (argc == 2 && !strcmp(argv[1], "--version"))
+    {
+      printf("nwho " STRINGIFY(VERSION) "\n");
+      return 0;
+    }
+
   if (argc == 2)
     client(argv[1]);
   else if (argc == 1)