|
@@ -0,0 +1,121 @@
|
|
1
|
+/*
|
|
2
|
+ * general purpose UDP dumper
|
|
3
|
+ *
|
|
4
|
+ * made by tTh, around 2019...
|
|
5
|
+ */
|
|
6
|
+#include <stdio.h>
|
|
7
|
+#include <stdlib.h>
|
|
8
|
+#include <string.h>
|
|
9
|
+#include <unistd.h>
|
|
10
|
+#include <ctype.h>
|
|
11
|
+#include <sys/time.h>
|
|
12
|
+#include <arpa/inet.h>
|
|
13
|
+#include <sys/types.h>
|
|
14
|
+#include <sys/socket.h>
|
|
15
|
+
|
|
16
|
+#define PORT 5002
|
|
17
|
+#define BUFLEN 1024
|
|
18
|
+
|
|
19
|
+int verbosity = 0;
|
|
20
|
+
|
|
21
|
+/* ----------------------------------------------------------------- */
|
|
22
|
+int dumpln(unsigned char *ptr)
|
|
23
|
+{
|
|
24
|
+int foo;
|
|
25
|
+
|
|
26
|
+for (foo=0; foo<16; foo++)
|
|
27
|
+ printf("%02x ", ptr[foo]);
|
|
28
|
+
|
|
29
|
+printf("| ");
|
|
30
|
+for (foo=0; foo<16; foo++)
|
|
31
|
+ if (isprint(ptr[foo])) putchar(ptr[foo]);
|
|
32
|
+ else putchar('.');
|
|
33
|
+
|
|
34
|
+printf(" |\n");
|
|
35
|
+return 0;
|
|
36
|
+}
|
|
37
|
+/* ----------------------------------------------------------------- */
|
|
38
|
+void udp_dumper(uint16_t port)
|
|
39
|
+{
|
|
40
|
+unsigned char buffer[BUFLEN];
|
|
41
|
+struct sockaddr_in si_me, si_other;
|
|
42
|
+int sock, foo, bar, flag_exit;
|
|
43
|
+unsigned int slen=sizeof(si_other);
|
|
44
|
+long serial;
|
|
45
|
+double starttime, curtime;
|
|
46
|
+struct timeval tp;
|
|
47
|
+
|
|
48
|
+if (-1==(sock=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))) {
|
|
49
|
+ perror("socket fail ");
|
|
50
|
+ exit(1);
|
|
51
|
+ }
|
|
52
|
+
|
|
53
|
+#if DEBUG_LEVEL
|
|
54
|
+fprintf(stderr, "port %d / sock -> %d\n", port, sock);
|
|
55
|
+#endif
|
|
56
|
+
|
|
57
|
+memset((char *) &si_me, 0, sizeof(si_me));
|
|
58
|
+si_me.sin_family = AF_INET;
|
|
59
|
+si_me.sin_port = htons(port);
|
|
60
|
+si_me.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
61
|
+if (bind(sock, (struct sockaddr *)&si_me, sizeof(si_me))==-1) {
|
|
62
|
+ perror("'bind' failure ");
|
|
63
|
+ exit(1);
|
|
64
|
+ }
|
|
65
|
+
|
|
66
|
+flag_exit = 0; serial = 0L;
|
|
67
|
+
|
|
68
|
+gettimeofday(&tp, NULL);
|
|
69
|
+starttime = tp.tv_sec + tp.tv_usec / 1e6;
|
|
70
|
+
|
|
71
|
+do {
|
|
72
|
+ memset(buffer, 0, BUFLEN);
|
|
73
|
+ slen = sizeof(si_other);
|
|
74
|
+ foo = recvfrom(sock, buffer, BUFLEN, 0,
|
|
75
|
+ (struct sockaddr *)&si_other, &slen);
|
|
76
|
+
|
|
77
|
+ if (verbosity) {
|
|
78
|
+ gettimeofday(&tp, NULL);
|
|
79
|
+ curtime = tp.tv_sec + tp.tv_usec / 1e6;
|
|
80
|
+ printf("----+ %5d b ts: %.3f\n", foo, curtime - starttime);
|
|
81
|
+ }
|
|
82
|
+
|
|
83
|
+ for (bar=0; bar<=foo; bar+=16) {
|
|
84
|
+ printf("%3ld | ", serial);
|
|
85
|
+ dumpln(buffer+bar);
|
|
86
|
+ }
|
|
87
|
+ serial++;
|
|
88
|
+ } while ( !flag_exit);
|
|
89
|
+}
|
|
90
|
+/* ----------------------------------------------------------------- */
|
|
91
|
+void help(int foo)
|
|
92
|
+{
|
|
93
|
+puts("\t-h\tthis help text");
|
|
94
|
+puts("\t-p NNNN\treceiving port number");
|
|
95
|
+puts("\t-v\tincrease verbosity");
|
|
96
|
+exit(0);
|
|
97
|
+return ;
|
|
98
|
+}
|
|
99
|
+/* ----------------------------------------------------------------- */
|
|
100
|
+int main(int argc, char *argv[])
|
|
101
|
+{
|
|
102
|
+int opt, port;
|
|
103
|
+
|
|
104
|
+port = PORT;
|
|
105
|
+/* parsing command line options */
|
|
106
|
+while ((opt = getopt(argc, argv, "hp:v")) != -1) {
|
|
107
|
+ switch (opt) {
|
|
108
|
+ case 'h': help(0); break;
|
|
109
|
+ case 'p': port = atoi(optarg); break;
|
|
110
|
+ case 'v': verbosity++; break;
|
|
111
|
+ default: exit(1);
|
|
112
|
+ }
|
|
113
|
+ }
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+udp_dumper(port);
|
|
117
|
+
|
|
118
|
+return 0;
|
|
119
|
+}
|
|
120
|
+/* ----------------------------------------------------------------- */
|
|
121
|
+
|