chiark / gitweb /
Use system sendmail if available (rather than SMTP over TCP)
[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 */
20
05b75f8d 21#include "common.h"
6ce6b5a9
RK
22
23#include <getopt.h>
24#include <sys/types.h>
25#include <sys/socket.h>
26#include <netinet/in.h>
27#include <errno.h>
28#include <locale.h>
29#include <netdb.h>
30#include <unistd.h>
31#include <ctype.h>
32
33#include "configuration.h"
34#include "syscalls.h"
35#include "log.h"
36#include "addr.h"
37#include "defs.h"
38#include "mem.h"
39
40static const struct option options[] = {
41 { "help", no_argument, 0, 'h' },
42 { "version", no_argument, 0, 'V' },
43 { "output", required_argument, 0, 'o' },
44 { 0, 0, 0, 0 }
45};
46
47/* display usage message and terminate */
48static void help(void) {
49 xprintf("Usage:\n"
50 " disorder-udplog [OPTIONS] ADDRESS PORT\n"
51 "Options:\n"
52 " --output, -o PATH Output to PATH (default: stdout)\n"
53 " --help, -h Display usage message\n"
54 " --version, -V Display version number\n"
55 "\n"
56 "UDP packet receiver.\n");
57 xfclose(stdout);
58 exit(0);
59}
60
61/* display version number and terminate */
62static void version(void) {
a05e4467 63 xprintf("%s", disorder_version_string);
6ce6b5a9
RK
64 xfclose(stdout);
65 exit(0);
66}
67
68int main(int argc, char **argv) {
69 int n, fd, err, i, j;
70 struct addrinfo *ai;
71 struct stringlist a;
72 char *name, h[4096], s[4096];
73 uint8_t buffer[4096];
74 union {
75 struct sockaddr sa;
76 struct sockaddr_in sin;
77 struct sockaddr_in6 sin6;
78 } sa;
79 socklen_t len;
95ceb9ea
RK
80 fd_set fds;
81 struct timeval tv;
6ce6b5a9 82 static const struct addrinfo pref = {
66613034
RK
83 .ai_flags = 0,
84 .ai_family = AF_UNSPEC,
85 .ai_socktype = SOCK_DGRAM,
86 .ai_protocol = IPPROTO_UDP,
6ce6b5a9
RK
87 };
88
89 set_progname(argv);
90 mem_init();
91 if(!setlocale(LC_CTYPE, "")) fatal(errno, "error calling setlocale");
92 while((n = getopt_long(argc, argv, "hVo:", options, 0)) >= 0) {
93 switch(n) {
94 case 'h': help();
95 case 'V': version();
96 case 'o':
97 if(!freopen(optarg, "w", stdout))
98 fatal(errno, "%s", optarg);
99 break;
100 default: fatal(0, "invalid option");
101 }
102 }
103 if(optind + 2 != argc)
104 fatal(0, "missing arguments");
105 a.n = 2;
106 a.s = &argv[optind];
107 if(!(ai = get_address(&a, &pref, &name)))
108 exit(1);
109 fd = xsocket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
95ceb9ea 110 nonblock(fd);
6ce6b5a9
RK
111 if(bind(fd, ai->ai_addr, ai->ai_addrlen) < 0)
112 fatal(errno, "error binding to %s", name);
113 while(getppid() != 1) {
95ceb9ea
RK
114 /* Wait for something to happen. We don't just block forever in recvfrom()
115 * as otherwise we'd never die if the parent terminated uncontrolledly. */
116 FD_ZERO(&fds);
117 FD_SET(fd, &fds);
118 tv.tv_sec = 1;
119 tv.tv_usec = 0;
120 select(fd + 1, &fds, 0, 0, &tv);
6ce6b5a9
RK
121 len = sizeof sa;
122 n = recvfrom(fd, buffer, sizeof buffer, 0, &sa.sa, &len);
123 if(n < 0) {
124 if(errno == EINTR || errno == EAGAIN)
125 continue;
126 fatal(errno, "%s: recvfrom", name);
127 }
128 if((err = getnameinfo(&sa.sa, len, h, sizeof h, s, sizeof s,
129 NI_NUMERICHOST|NI_NUMERICSERV|NI_DGRAM)))
130 fatal(0, "getnameinfo: %s", gai_strerror(err));
131 xprintf("from host %s service %s: %d bytes\n", h, s, n);
132 for(i = 0; i < n; i += 16) {
133 for(j = i; j < n && j < i + 16; ++j)
134 xprintf(" %02x", buffer[j]);
135 for(; j < i + 16; ++j)
136 xprintf(" ");
137 xprintf(" ");
138 for(j = i; j < n && j < i + 16; ++j)
139 xprintf("%c", buffer[j] < 128 && isprint(buffer[j]) ? buffer[j] : '.');
140 xprintf("\n");
141 if(fflush(stdout) < 0)
142 fatal(errno, "stdout");
143 }
144 }
145 return 0;
146}
147
148/*
149Local Variables:
150c-basic-offset:2
151comment-column:40
152fill-column:79
153indent-tabs-mode:nil
154End:
155*/