Debian Codegraph @

listen/listen.c

raw
1 #include <sys/types.h>
2 #include <sys/ioctl.h>
3 #include <netdb.h>
4
5 #include <unistd.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <time.h>
10 #include <sys/time.h>
11 #include <curses.h>
12 #include <signal.h>
13 #include <errno.h>
14
15 #include <sys/socket.h>
16 #include <net/if.h>
17 #ifdef __GLIBC__
18 #include <net/ethernet.h>
19 #else
20 #include <linux/if_ether.h>
21 #endif
22 #include <netax25/ax25.h>
23 #include <netax25/axconfig.h>
24
25 #include <config.h>
26 #include "listen.h"
27
28 static struct timeval t_recv;
29 static int tflag = 0;
30 static int32_t thiszone; /* seconds offset from gmt to local time */
31 static int sigint;
32 static int sock;
33
34 static void display_port(char *dev)
35 {
36 char *port;
37
38 port = ax25_config_get_name(dev);
39 if (port == NULL)
40 port = dev;
41
42 lprintf(T_PORT, "%s: ", port);
43 }
44
45 /* from tcpdump util.c */
46
47 /*
48 * Format the timestamp
49 */
50 static char * ts_format(unsigned int sec, unsigned int usec)
51 {
52 static char buf[sizeof("00:00:00.000000")];
53 unsigned int hours, minutes, seconds;
54
55 seconds = sec % 60;
56 sec = sec / 60;
57 minutes = sec % 60;
58 sec = sec / 60;
59 hours = sec % 24;
60
61 /*
62 * The real purpose of these checks is to let GCC figure out the
63 * value range of all variables thus avoid bogus warnings. For any
64 * halfway modern GCC the checks will be optimized away.
65 */
66 if (hours >= 24)
67 unreachable();
68 if (minutes >= 60)
69 unreachable();
70 if (seconds >= 60)
71 unreachable();
72 if (usec >= 1000000)
73 unreachable();
74
75 snprintf(buf, sizeof(buf), "%02d:%02d:%02d.%06u",
76 hours, minutes, seconds, usec);
77
78 return buf;
79 }
80
81 /*
82 * Print the timestamp
83 */
84 static void ts_print(const struct timeval *tvp)
85 {
86 int s;
87 struct tm *tm;
88 time_t Time;
89 static unsigned b_sec;
90 static unsigned b_usec;
91 int d_usec;
92 int d_sec;
93
94 switch (tflag) {
95
96 case 0: /* Default */
97 s = (tvp->tv_sec + thiszone) % 86400;
98 (void)lprintf(T_TIMESTAMP, "%s ", ts_format(s, tvp->tv_usec));
99 break;
100
101 case 1: /* No time stamp */
102 break;
103
104 case 2: /* Unix timeval style */
105 (void)lprintf(T_TIMESTAMP, "%u.%06u ",
106 (unsigned)tvp->tv_sec,
107 (unsigned)tvp->tv_usec);
108 break;
109
110 case 3: /* Microseconds since previous packet */
111 case 5: /* Microseconds since first packet */
112 if (b_sec == 0) {
113 /* init timestamp for first packet */
114 b_usec = tvp->tv_usec;
115 b_sec = tvp->tv_sec;
116 }
117
118 d_usec = tvp->tv_usec - b_usec;
119 d_sec = tvp->tv_sec - b_sec;
120
121 while (d_usec < 0) {
122 d_usec += 1000000;
123 d_sec--;
124 }
125
126 (void)lprintf(T_TIMESTAMP, "%s ", ts_format(d_sec, d_usec));
127
128 if (tflag == 3) { /* set timestamp for last packet */
129 b_sec = tvp->tv_sec;
130 b_usec = tvp->tv_usec;
131 }
132 break;
133
134 case 4: /* Default + Date*/
135 s = (tvp->tv_sec + thiszone) % 86400;
136 Time = (tvp->tv_sec + thiszone) - s;
137 tm = gmtime (&Time);
138 if (!tm)
139 lprintf(T_TIMESTAMP, "Date fail ");
140 else
141 lprintf(T_TIMESTAMP, "%04d-%02d-%02d %s ",
142 tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
143 ts_format(s, tvp->tv_usec));
144 break;
145 }
146 }
147
148 void display_timestamp(void)
149 {
150 ts_print(&t_recv);
151 }
152
153 /* from tcpdump gmtlocal.c */
154
155 static int32_t gmt2local(time_t t)
156 {
157 int dt, dir;
158 struct tm *gmt, *loc;
159 struct tm sgmt;
160
161 if (t == 0)
162 t = time(NULL);
163 gmt = &sgmt;
164 *gmt = *gmtime(&t);
165 loc = localtime(&t);
166 dt = (loc->tm_hour - gmt->tm_hour) * 60 * 60 +
167 (loc->tm_min - gmt->tm_min) * 60;
168
169 /*
170 * If the year or julian day is different, we span 00:00 GMT
171 * and must add or subtract a day. Check the year first to
172 * avoid problems when the julian day wraps.
173 */
174 dir = loc->tm_year - gmt->tm_year;
175 if (dir == 0)
176 dir = loc->tm_yday - gmt->tm_yday;
177 dt += dir * 24 * 60 * 60;
178
179 return dt;
180 }
181
182 static void handle_sigint(int signal)
183 {
184 sigint++;
185 close(sock); /* disturb blocking recvfrom */
186 sock = -1;
187 }
188
189 #define ASCII 0
190 #define HEX 1
191 #define READABLE 2
192
193 #define BUFSIZE 1500
194
195 int main(int argc, char **argv)
196 {
197 unsigned char buffer[BUFSIZE];
198 int dumpstyle = ASCII;
199 int size;
200 int s;
201 char *port = NULL, *dev = NULL;
202 struct sockaddr sa;
203 socklen_t asize = sizeof(sa);
204 struct ifreq ifr;
205 int proto = ETH_P_AX25;
206 int exit_code = EXIT_SUCCESS;
207
208 while ((s = getopt(argc, argv, "8achip:rtv")) != -1) {
209 switch (s) {
210 case '8':
211 sevenbit = 0;
212 break;
213 case 'a':
214 proto = ETH_P_ALL;
215 break;
216 case 'c':
217 color = 1;
218 break;
219 case 'h':
220 dumpstyle = HEX;
221 break;
222 case 'i':
223 ibmhack = 1;
224 break;
225 case 'p':
226 port = optarg;
227 break;
228 case 'r':
229 dumpstyle = READABLE;
230 break;
231 case 't':
232 tflag++;
233 break;
234 case 'v':
235 printf("listen: %s\n", VERSION);
236 return 0;
237 case ':':
238 fprintf(stderr,
239 "listen: option -p needs a port name\n");
240 return 1;
241 case '?':
242 fprintf(stderr,
243 "Usage: listen [-8] [-a] [-c] [-h] [-i] [-p port] [-r] [-t..] [-v]\n");
244 return 1;
245 }
246 }
247
248 switch (tflag) {
249 case 0: /* Default */
250 case 4: /* Default + Date*/
251 thiszone = gmt2local(0);
252 break;
253 case 1: /* No time stamp */
254 case 2: /* Unix timeval style */
255 case 3: /* Microseconds since previous packet */
256 case 5: /* Microseconds since first packet */
257 break;
258 default: /* Not supported */
259 fprintf(stderr, "listen: only -t, -tt, -ttt, -tttt and -ttttt are supported\n");
260 return 1;
261 }
262
263 if (ax25_config_load_ports() == 0)
264 fprintf(stderr, "listen: no AX.25 port data configured\n");
265
266 if (port != NULL) {
267 dev = ax25_config_get_dev(port);
268 if (dev == NULL) {
269 fprintf(stderr, "listen: invalid port name - %s\n",
270 port);
271 return 1;
272 }
273 }
274
275 sock = socket(PF_PACKET, SOCK_PACKET, htons(proto));
276 if (sock == -1) {
277 perror("socket");
278 return 1;
279 }
280
281 if (color) {
282 color = initcolor(); /* Initialize color support */
283 if (!color)
284 printf("Could not initialize color support.\n");
285 }
286
287 setservent(1);
288
289 while (!sigint) {
290 asize = sizeof(sa);
291
292 signal(SIGINT, handle_sigint);
293 signal(SIGTERM, handle_sigint);
294 size = recvfrom(sock, buffer, sizeof(buffer), 0, &sa, &asize);
295 if (size == -1) {
296 /*
297 * Signals are cared for by the handler, and we
298 * don't want to abort on SIGWINCH
299 */
300 if (errno == EINTR) {
301 refresh();
302 continue;
303 } else if (!(errno == EBADF && sigint)) {
304 perror("recv");
305 exit_code = errno;
306 }
307 break;
308 }
309 gettimeofday(&t_recv, NULL);
310 signal(SIGINT, SIG_DFL);
311 signal(SIGTERM, SIG_DFL);
312 if (sock == -1 || sigint)
313 break;
314
315 if (dev != NULL && strcmp(dev, sa.sa_data) != 0)
316 continue;
317
318 if (proto == ETH_P_ALL) {
319 strcpy(ifr.ifr_name, sa.sa_data);
320 signal(SIGINT, handle_sigint);
321 signal(SIGTERM, handle_sigint);
322 if (ioctl(sock, SIOCGIFHWADDR, &ifr) == -1) {
323 if (!(errno == EBADF && sigint)) {
324 perror("SIOCGIFHWADDR");
325 exit_code = errno;
326 break;
327 }
328 }
329 signal(SIGINT, SIG_DFL);
330 signal(SIGTERM, SIG_DFL);
331 if (sock == -1 || sigint)
332 break;
333 if (ifr.ifr_hwaddr.sa_family == AF_AX25) {
334 if (size > 2 && *buffer == 0xcc) {
335 /* IP packets from the ax25 de-segmenter
336 are seen on socket "PF_PACKET,
337 SOCK_PACKET, ETH_P_ALL" without
338 AX.25 header (just the IP-frame),
339 prefixed by 0xcc (AX25_P_IP).
340 It's unclear why in the kernel code
341 this happens (unsegmentet AX25 PID
342 AX25_P_IP have not this behavior).
343 We have already displayed all the
344 segments and like to ignore this
345 data.
346 AX.25 packets start with a kiss
347 byte (buffer[0]); ax25_dump()
348 looks for it.
349 There's no kiss command 0xcc
350 defined; kiss bytes are checked
351 against & 0xf (= 0x0c), which is
352 also not defined.
353 Kiss commands may have one argument.
354 => We can make safely make the
355 assumption for first byte == 0xcc
356 and length > 2, that we safeley can
357 detect those IP frames, and then
358 ignore it.
359 */
360 continue;
361 }
362 display_port(sa.sa_data);
363 ki_dump(buffer, size, dumpstyle);
364 /* lprintf(T_DATA, "\n"); */
365 }
366 } else {
367 display_port(sa.sa_data);
368 ki_dump(buffer, size, dumpstyle);
369 /* lprintf(T_DATA, "\n"); */
370 }
371 if (color)
372 refresh();
373 }
374 if (color)
375 endwin();
376
377 return exit_code;
378 }
379
380 static void ascii_dump(unsigned char *data, int length)
381 {
382 char c;
383 int i, j;
384 char buf[100];
385
386 for (i = 0; length > 0; i += 64) {
387 sprintf(buf, "%04X ", i);
388
389 for (j = 0; j < 64 && length > 0; j++) {
390 c = *data++;
391 length--;
392
393 if ((c != '\0') && (c != '\n'))
394 strncat(buf, &c, 1);
395 else
396 strcat(buf, ".");
397 }
398
399 lprintf(T_DATA, "%s\n", buf);
400 }
401 }
402
403 static void readable_dump(unsigned char *data, int length)
404 {
405 unsigned char c;
406 int i;
407 int cr = 1;
408 char buf[BUFSIZE];
409
410 for (i = 0; length > 0; i++) {
411
412 c = *data++;
413 length--;
414
415 switch (c) {
416 case 0x00:
417 buf[i] = ' ';
418 case 0x0A: /* hum... */
419 case 0x0D:
420 if (cr)
421 buf[i] = '\n';
422 else
423 i--;
424 break;
425 default:
426 buf[i] = c;
427 }
428 cr = (buf[i] != '\n');
429 }
430 if (cr)
431 buf[i++] = '\n';
432 buf[i] = '\0';
433 lprintf(T_DATA, "%s", buf);
434 }
435
436 static void hex_dump(unsigned char *data, int length)
437 {
438 unsigned char *data2;
439 int i, j, length2;
440 unsigned char c;
441
442 char buf[4], hexd[49], ascd[17];
443
444 length2 = length;
445 data2 = data;
446
447 for (i = 0; length > 0; i += 16) {
448
449 hexd[0] = '\0';
450 for (j = 0; j < 16; j++) {
451 c = *data2++;
452 length2--;
453
454 if (length2 >= 0)
455 sprintf(buf, "%2.2X ", c);
456 else
457 strcpy(buf, " ");
458 strcat(hexd, buf);
459 }
460
461 ascd[0] = '\0';
462 for (j = 0; j < 16 && length > 0; j++) {
463 c = *data++;
464 length--;
465
466 sprintf(buf, "%c",
467 ((c != '\0') && (c != '\n')) ? c : '.');
468 strcat(ascd, buf);
469 }
470
471 lprintf(T_DATA, "%04X %s | %s\n", i, hexd, ascd);
472 }
473 }
474
475 void data_dump(void *data, int length, int dumpstyle)
476 {
477 switch (dumpstyle) {
478
479 case READABLE:
480 readable_dump(data, length);
481 break;
482 case HEX:
483 hex_dump(data, length);
484 break;
485 default:
486 ascii_dump(data, length);
487 }
488 }
489
490 int get16(unsigned char *cp)
491 {
492 int x;
493
494 x = *cp++;
495 x <<= 8;
496 x |= *cp++;
497
498 return x;
499 }
500
501 int get32(unsigned char *cp)
502 {
503 int x;
504
505 x = *cp++;
506 x <<= 8;
507 x |= *cp++;
508 x <<= 8;
509 x |= *cp++;
510 x <<= 8;
511 x |= *cp;
512
513 return x;
514 }
515