xref: /netbsd-src/external/bsd/tcpdump/dist/print-beep.c (revision 627f7eb200a4419d89b531d55fccd2ee3ffdcde0)
1 /*
2  * Copyright (C) 2000, Richard Sharpe
3  *
4  * This software may be distributed either under the terms of the
5  * BSD-style license that accompanies tcpdump or under the GNU GPL
6  * version 2 or later.
7  *
8  * print-beep.c
9  *
10  */
11 
12 #include <sys/cdefs.h>
13 #ifndef lint
14 __RCSID("$NetBSD: print-beep.c,v 1.7 2017/09/08 14:01:12 christos Exp $");
15 #endif
16 
17 /* \summary: Blocks Extensible Exchange Protocol (BEEP) printer */
18 
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22 
23 #include <netdissect-stdinc.h>
24 
25 #include <string.h>
26 
27 #include "netdissect.h"
28 
29 /* Check for a string but not go beyond length
30  * Return TRUE on match, FALSE otherwise
31  *
32  * Looks at the first few chars up to tl1 ...
33  */
34 
35 static int
36 l_strnstart(netdissect_options *ndo, const char *tstr1, u_int tl1,
37     const char *str2, u_int l2)
38 {
39 	if (!ND_TTEST2(*str2, tl1)) {
40 		/*
41 		 * We don't have tl1 bytes worth of captured data
42 		 * for the string, so we can't check for this
43 		 * string.
44 		 */
45 		return 0;
46 	}
47 	if (tl1 > l2)
48 		return 0;
49 
50 	return (strncmp(tstr1, str2, tl1) == 0 ? 1 : 0);
51 }
52 
53 void
54 beep_print(netdissect_options *ndo, const u_char *bp, u_int length)
55 {
56 
57 	if (l_strnstart(ndo, "MSG", 4, (const char *)bp, length)) /* A REQuest */
58 		ND_PRINT((ndo, " BEEP MSG"));
59 	else if (l_strnstart(ndo, "RPY ", 4, (const char *)bp, length))
60 		ND_PRINT((ndo, " BEEP RPY"));
61 	else if (l_strnstart(ndo, "ERR ", 4, (const char *)bp, length))
62 		ND_PRINT((ndo, " BEEP ERR"));
63 	else if (l_strnstart(ndo, "ANS ", 4, (const char *)bp, length))
64 		ND_PRINT((ndo, " BEEP ANS"));
65 	else if (l_strnstart(ndo, "NUL ", 4, (const char *)bp, length))
66 		ND_PRINT((ndo, " BEEP NUL"));
67 	else if (l_strnstart(ndo, "SEQ ", 4, (const char *)bp, length))
68 		ND_PRINT((ndo, " BEEP SEQ"));
69 	else if (l_strnstart(ndo, "END", 4, (const char *)bp, length))
70 		ND_PRINT((ndo, " BEEP END"));
71 	else
72 		ND_PRINT((ndo, " BEEP (payload or undecoded)"));
73 }
74