2 * This file is part of DisOrder
3 * Copyright (C) 2007 Richard Kettlewell
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.
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.
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
25 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <netinet/in.h>
34 #include "configuration.h"
41 static const struct option options[] = {
42 { "help", no_argument, 0, 'h' },
43 { "version", no_argument, 0, 'V' },
44 { "output", required_argument, 0, 'o' },
48 /* display usage message and terminate */
49 static void help(void) {
51 " disorder-udplog [OPTIONS] ADDRESS PORT\n"
53 " --output, -o PATH Output to PATH (default: stdout)\n"
54 " --help, -h Display usage message\n"
55 " --version, -V Display version number\n"
57 "UDP packet receiver.\n");
62 /* display version number and terminate */
63 static void version(void) {
64 xprintf("%s", disorder_version_string);
69 int main(int argc, char **argv) {
73 char *name, h[4096], s[4096];
77 struct sockaddr_in sin;
78 struct sockaddr_in6 sin6;
81 static const struct addrinfo pref = {
83 AF_UNSPEC, /* ai_family */
84 SOCK_DGRAM, /* ai_socktype */
85 IPPROTO_UDP, /* ai_protocol */
94 if(!setlocale(LC_CTYPE, "")) fatal(errno, "error calling setlocale");
95 while((n = getopt_long(argc, argv, "hVo:", options, 0)) >= 0) {
100 if(!freopen(optarg, "w", stdout))
101 fatal(errno, "%s", optarg);
103 default: fatal(0, "invalid option");
106 if(optind + 2 != argc)
107 fatal(0, "missing arguments");
110 if(!(ai = get_address(&a, &pref, &name)))
112 fd = xsocket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
113 if(bind(fd, ai->ai_addr, ai->ai_addrlen) < 0)
114 fatal(errno, "error binding to %s", name);
115 while(getppid() != 1) {
117 n = recvfrom(fd, buffer, sizeof buffer, 0, &sa.sa, &len);
119 if(errno == EINTR || errno == EAGAIN)
121 fatal(errno, "%s: recvfrom", name);
123 if((err = getnameinfo(&sa.sa, len, h, sizeof h, s, sizeof s,
124 NI_NUMERICHOST|NI_NUMERICSERV|NI_DGRAM)))
125 fatal(0, "getnameinfo: %s", gai_strerror(err));
126 xprintf("from host %s service %s: %d bytes\n", h, s, n);
127 for(i = 0; i < n; i += 16) {
128 for(j = i; j < n && j < i + 16; ++j)
129 xprintf(" %02x", buffer[j]);
130 for(; j < i + 16; ++j)
133 for(j = i; j < n && j < i + 16; ++j)
134 xprintf("%c", buffer[j] < 128 && isprint(buffer[j]) ? buffer[j] : '.');
136 if(fflush(stdout) < 0)
137 fatal(errno, "stdout");