/[gxemul]/upstream/0.4.4/experiments/udp_snoop.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /upstream/0.4.4/experiments/udp_snoop.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 35 - (show annotations)
Mon Oct 8 16:21:26 2007 UTC (16 years, 6 months ago) by dpavlin
File MIME type: text/plain
File size: 2802 byte(s)
0.4.4
1 /*
2 * Copyright (C) 2006 Anders Gavare. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 *
28 * $Id: udp_snoop.c,v 1.1 2006/09/07 11:44:42 debug Exp $
29 *
30 * Dumps UDP packets in hex and ASCII that arrive at a specific port.
31 */
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <netinet/in.h>
38
39
40 void dump_buf(unsigned char *p, ssize_t len)
41 {
42 while (len > 0) {
43 ssize_t i, n = len>=16? 16 : len;
44 for (i=0; i<n; i++)
45 printf(" %02x", p[i]);
46 for (i=n; i<16; i++)
47 printf(" ");
48 printf(" ");
49 for (i=0; i<n; i++) {
50 if (p[i]>=32 && p[i]<127)
51 printf("%c", p[i]);
52 else
53 printf(".");
54 }
55 printf("\n");
56 p += 16;
57 len -= 16;
58 }
59 }
60
61
62 int main(int argc, char *argv[])
63 {
64 struct sockaddr_in si;
65 int s;
66
67 if (argc < 2) {
68 fprintf(stderr, "usage: %s udp_portnr\n", argv[0]);
69 exit(1);
70 }
71
72 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
73 perror("socket");
74 exit(1);
75 }
76
77 memset((char *)&si, sizeof(si), 0);
78 si.sin_family = AF_INET;
79 si.sin_port = htons(atoi(argv[1]));
80 si.sin_addr.s_addr = htonl(INADDR_ANY);
81 if (bind(s, (struct sockaddr *)&si, sizeof(si)) < 0) {
82 perror("bind");
83 exit(1);
84 }
85
86 for (;;) {
87 unsigned char buf[2048];
88 ssize_t r = recv(s, buf, sizeof(buf), 0);
89
90 if (r < 0) {
91 perror("recv");
92 exit(1);
93 }
94
95 dump_buf(buf, r);
96 printf("\n");
97 }
98
99 return 0;
100 }
101

  ViewVC Help
Powered by ViewVC 1.1.26