xref: /netbsd-src/external/bsd/tcpdump/dist/print-beep.c (revision ccd9df534e375a4366c5b55f23782053c7a98d82)
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.8 2023/08/17 20:19:40 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_TTEST_LEN(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 	ndo->ndo_protocol = "beep";
58 	if (l_strnstart(ndo, "MSG", 4, (const char *)bp, length)) /* A REQuest */
59 		ND_PRINT(" BEEP MSG");
60 	else if (l_strnstart(ndo, "RPY ", 4, (const char *)bp, length))
61 		ND_PRINT(" BEEP RPY");
62 	else if (l_strnstart(ndo, "ERR ", 4, (const char *)bp, length))
63 		ND_PRINT(" BEEP ERR");
64 	else if (l_strnstart(ndo, "ANS ", 4, (const char *)bp, length))
65 		ND_PRINT(" BEEP ANS");
66 	else if (l_strnstart(ndo, "NUL ", 4, (const char *)bp, length))
67 		ND_PRINT(" BEEP NUL");
68 	else if (l_strnstart(ndo, "SEQ ", 4, (const char *)bp, length))
69 		ND_PRINT(" BEEP SEQ");
70 	else if (l_strnstart(ndo, "END", 4, (const char *)bp, length))
71 		ND_PRINT(" BEEP END");
72 	else
73 		ND_PRINT(" BEEP (payload or undecoded)");
74 }
75