chiark / gitweb /
configure.ac, debian/: Set up correct dependencies for GStreamer.
[disorder] / tests / udplog.c
index 4d52efc066cb59286b7eaac8e1d5c94b8686f929..6c817ced7f3a1eb0e7927ffd72eab6616d660fe0 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * This file is part of DisOrder
- * Copyright (C) 2007 Richard Kettlewell
+ * Copyright (C) 2007-2009 Richard Kettlewell
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  * USA
  */
-
-#include <config.h>
-#include "types.h"
+/** @file tests/udplog.c
+ * @brief UDP logging utility
+ *
+ * Intended for low-level debugging.
+ */
+#include "common.h"
 
 #include <getopt.h>
 #include <sys/types.h>
@@ -61,7 +64,7 @@ static void help(void) {
 
 /* display version number and terminate */
 static void version(void) {
-  xprintf("disorder-udplog version %s\n", disorder_version_string);
+  xprintf("%s", disorder_version_string);
   xfclose(stdout);
   exit(0);
 }
@@ -78,63 +81,70 @@ int main(int argc, char **argv) {
     struct sockaddr_in6 sin6;
   } sa;
   socklen_t len;
+  fd_set fds;
+  struct timeval tv;
   static const struct addrinfo pref = {
-    0,                         /* ai_flags */
-    AF_UNSPEC,                 /* ai_family */
-    SOCK_DGRAM,                        /* ai_socktype */
-    IPPROTO_UDP,               /* ai_protocol */
-    0,
-    0,
-    0,
-    0
+    .ai_flags = 0,
+    .ai_family = AF_UNSPEC,
+    .ai_socktype = SOCK_DGRAM,
+    .ai_protocol = IPPROTO_UDP,
   };
   
   set_progname(argv);
   mem_init();
-  if(!setlocale(LC_CTYPE, "")) fatal(errno, "error calling setlocale");
+  if(!setlocale(LC_CTYPE, "")) disorder_fatal(errno, "error calling setlocale");
   while((n = getopt_long(argc, argv, "hVo:", options, 0)) >= 0) {
     switch(n) {
     case 'h': help();
     case 'V': version();
     case 'o':
       if(!freopen(optarg, "w", stdout))
-       fatal(errno, "%s", optarg);
+       disorder_fatal(errno, "%s", optarg);
       break;
-    default: fatal(0, "invalid option");
+    default: disorder_fatal(0, "invalid option");
     }
   }
   if(optind + 2 != argc)
-    fatal(0, "missing arguments");
+    disorder_fatal(0, "missing arguments");
   a.n = 2;
   a.s = &argv[optind];
   if(!(ai = get_address(&a, &pref, &name)))
     exit(1);
   fd = xsocket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
+  nonblock(fd);
   if(bind(fd, ai->ai_addr, ai->ai_addrlen) < 0)
-    fatal(errno, "error binding to %s", name);
+    disorder_fatal(errno, "error binding to %s", name);
   while(getppid() != 1) {
+    /* Wait for something to happen.  We don't just block forever in recvfrom()
+     * as otherwise we'd never die if the parent terminated uncontrolledly. */
+    FD_ZERO(&fds);
+    FD_SET(fd, &fds);
+    tv.tv_sec = 1;
+    tv.tv_usec = 0;
+    select(fd + 1, &fds, 0, 0, &tv);
     len = sizeof sa;
     n = recvfrom(fd, buffer, sizeof buffer, 0, &sa.sa, &len);
     if(n < 0) {
       if(errno == EINTR || errno == EAGAIN)
        continue;
-      fatal(errno, "%s: recvfrom", name);
+      disorder_fatal(errno, "%s: recvfrom", name);
     }
     if((err = getnameinfo(&sa.sa, len, h, sizeof h, s, sizeof s,
                          NI_NUMERICHOST|NI_NUMERICSERV|NI_DGRAM)))
-      fatal(0, "getnameinfo: %s", gai_strerror(err));
+      disorder_fatal(0, "getnameinfo: %s", gai_strerror(err));
     xprintf("from host %s service %s: %d bytes\n", h, s, n);
     for(i = 0; i < n; i += 16) {
-      for(j = i; j < n && j < i + 16; ++j)
+      const int limit = n > i + 16 ? i + 16 : n;
+      for(j = i; j < limit; ++j)
        xprintf(" %02x", buffer[j]);
       for(; j < i + 16; ++j)
        xprintf("   ");
       xprintf("  ");
-      for(j = i; j < n && j < i + 16; ++j)
+      for(j = i; j < limit; ++j)
        xprintf("%c", buffer[j] < 128 && isprint(buffer[j]) ? buffer[j] : '.');
       xprintf("\n");
       if(fflush(stdout) < 0)
-       fatal(errno, "stdout");
+       disorder_fatal(errno, "stdout");
     }
   }
   return 0;