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