chiark / gitweb /
update copyright date
[disorder] / tests / udplog.c
CommitLineData
6ce6b5a9
RK
1/*
2 * This file is part of DisOrder
3 * Copyright (C) 2007 Richard Kettlewell
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18 * USA
19 */
132a5a4a
RK
20/** @file tests/udplog.c
21 * @brief UDP logging utility
22 *
23 * Intended for low-level debugging.
24 */
05b75f8d 25#include "common.h"
6ce6b5a9
RK
26
27#include <getopt.h>
28#include <sys/types.h>
29#include <sys/socket.h>
30#include <netinet/in.h>
31#include <errno.h>
32#include <locale.h>
33#include <netdb.h>
34#include <unistd.h>
35#include <ctype.h>
36
37#include "configuration.h"
38#include "syscalls.h"
39#include "log.h"
40#include "addr.h"
41#include "defs.h"
42#include "mem.h"
43
44static const struct option options[] = {
45 { "help", no_argument, 0, 'h' },
46 { "version", no_argument, 0, 'V' },
47 { "output", required_argument, 0, 'o' },
48 { 0, 0, 0, 0 }
49};
50
51/* display usage message and terminate */
52static void help(void) {
53 xprintf("Usage:\n"
54 " disorder-udplog [OPTIONS] ADDRESS PORT\n"
55 "Options:\n"
56 " --output, -o PATH Output to PATH (default: stdout)\n"
57 " --help, -h Display usage message\n"
58 " --version, -V Display version number\n"
59 "\n"
60 "UDP packet receiver.\n");
61 xfclose(stdout);
62 exit(0);
63}
64
65/* display version number and terminate */
66static void version(void) {
a05e4467 67 xprintf("%s", disorder_version_string);
6ce6b5a9
RK
68 xfclose(stdout);
69 exit(0);
70}
71
72int main(int argc, char **argv) {
73 int n, fd, err, i, j;
74 struct addrinfo *ai;
75 struct stringlist a;
76 char *name, h[4096], s[4096];
77 uint8_t buffer[4096];
78 union {
79 struct sockaddr sa;
80 struct sockaddr_in sin;
81 struct sockaddr_in6 sin6;
82 } sa;
83 socklen_t len;
95ceb9ea
RK
84 fd_set fds;
85 struct timeval tv;
6ce6b5a9 86 static const struct addrinfo pref = {
66613034
RK
87 .ai_flags = 0,
88 .ai_family = AF_UNSPEC,
89 .ai_socktype = SOCK_DGRAM,
90 .ai_protocol = IPPROTO_UDP,
6ce6b5a9
RK
91 };
92
93 set_progname(argv);
94 mem_init();
95 if(!setlocale(LC_CTYPE, "")) fatal(errno, "error calling setlocale");
96 while((n = getopt_long(argc, argv, "hVo:", options, 0)) >= 0) {
97 switch(n) {
98 case 'h': help();
99 case 'V': version();
100 case 'o':
101 if(!freopen(optarg, "w", stdout))
102 fatal(errno, "%s", optarg);
103 break;
104 default: fatal(0, "invalid option");
105 }
106 }
107 if(optind + 2 != argc)
108 fatal(0, "missing arguments");
109 a.n = 2;
110 a.s = &argv[optind];
111 if(!(ai = get_address(&a, &pref, &name)))
112 exit(1);
113 fd = xsocket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
95ceb9ea 114 nonblock(fd);
6ce6b5a9
RK
115 if(bind(fd, ai->ai_addr, ai->ai_addrlen) < 0)
116 fatal(errno, "error binding to %s", name);
117 while(getppid() != 1) {
95ceb9ea
RK
118 /* Wait for something to happen. We don't just block forever in recvfrom()
119 * as otherwise we'd never die if the parent terminated uncontrolledly. */
120 FD_ZERO(&fds);
121 FD_SET(fd, &fds);
122 tv.tv_sec = 1;
123 tv.tv_usec = 0;
124 select(fd + 1, &fds, 0, 0, &tv);
6ce6b5a9
RK
125 len = sizeof sa;
126 n = recvfrom(fd, buffer, sizeof buffer, 0, &sa.sa, &len);
127 if(n < 0) {
128 if(errno == EINTR || errno == EAGAIN)
129 continue;
130 fatal(errno, "%s: recvfrom", name);
131 }
132 if((err = getnameinfo(&sa.sa, len, h, sizeof h, s, sizeof s,
133 NI_NUMERICHOST|NI_NUMERICSERV|NI_DGRAM)))
134 fatal(0, "getnameinfo: %s", gai_strerror(err));
135 xprintf("from host %s service %s: %d bytes\n", h, s, n);
136 for(i = 0; i < n; i += 16) {
137 for(j = i; j < n && j < i + 16; ++j)
138 xprintf(" %02x", buffer[j]);
139 for(; j < i + 16; ++j)
140 xprintf(" ");
141 xprintf(" ");
142 for(j = i; j < n && j < i + 16; ++j)
143 xprintf("%c", buffer[j] < 128 && isprint(buffer[j]) ? buffer[j] : '.');
144 xprintf("\n");
145 if(fflush(stdout) < 0)
146 fatal(errno, "stdout");
147 }
148 }
149 return 0;
150}
151
152/*
153Local Variables:
154c-basic-offset:2
155comment-column:40
156fill-column:79
157indent-tabs-mode:nil
158End:
159*/