xref: /aosp_15_r20/external/tcpdump/print-ssh.c (revision 05b00f6010a2396e3db2409989fc67270046269f)
1*05b00f60SXin Li /*
2*05b00f60SXin Li  * Redistribution and use in source and binary forms, with or without
3*05b00f60SXin Li  * modification, are permitted provided that: (1) source code
4*05b00f60SXin Li  * distributions retain the above copyright notice and this paragraph
5*05b00f60SXin Li  * in its entirety, and (2) distributions including binary code include
6*05b00f60SXin Li  * the above copyright notice and this paragraph in its entirety in
7*05b00f60SXin Li  * the documentation or other materials provided with the distribution.
8*05b00f60SXin Li  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND
9*05b00f60SXin Li  * WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT
10*05b00f60SXin Li  * LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
11*05b00f60SXin Li  * FOR A PARTICULAR PURPOSE.
12*05b00f60SXin Li  */
13*05b00f60SXin Li 
14*05b00f60SXin Li /* \summary: Secure Shell (SSH) printer */
15*05b00f60SXin Li 
16*05b00f60SXin Li #ifdef HAVE_CONFIG_H
17*05b00f60SXin Li #include <config.h>
18*05b00f60SXin Li #endif
19*05b00f60SXin Li 
20*05b00f60SXin Li #include "netdissect-stdinc.h"
21*05b00f60SXin Li #include "netdissect-ctype.h"
22*05b00f60SXin Li 
23*05b00f60SXin Li #include "netdissect.h"
24*05b00f60SXin Li #include "extract.h"
25*05b00f60SXin Li 
26*05b00f60SXin Li static int
ssh_print_version(netdissect_options * ndo,const u_char * pptr,u_int len)27*05b00f60SXin Li ssh_print_version(netdissect_options *ndo, const u_char *pptr, u_int len)
28*05b00f60SXin Li {
29*05b00f60SXin Li 	u_int idx = 0;
30*05b00f60SXin Li 
31*05b00f60SXin Li 	if ( GET_U_1(pptr+idx) != 'S' )
32*05b00f60SXin Li 		return 0;
33*05b00f60SXin Li 	idx++;
34*05b00f60SXin Li 	if ( GET_U_1(pptr+idx) != 'S' )
35*05b00f60SXin Li 		return 0;
36*05b00f60SXin Li 	idx++;
37*05b00f60SXin Li 	if ( GET_U_1(pptr+idx) != 'H' )
38*05b00f60SXin Li 		return 0;
39*05b00f60SXin Li 	idx++;
40*05b00f60SXin Li 	if ( GET_U_1(pptr+idx) != '-' )
41*05b00f60SXin Li 		return 0;
42*05b00f60SXin Li 	idx++;
43*05b00f60SXin Li 
44*05b00f60SXin Li 	while (idx < len) {
45*05b00f60SXin Li 		u_char c;
46*05b00f60SXin Li 
47*05b00f60SXin Li 		c = GET_U_1(pptr + idx);
48*05b00f60SXin Li 		if (c == '\n') {
49*05b00f60SXin Li 			/*
50*05b00f60SXin Li 			 * LF without CR; end of line.
51*05b00f60SXin Li 			 * Skip the LF and print the line, with the
52*05b00f60SXin Li 			 * exception of the LF.
53*05b00f60SXin Li 			 */
54*05b00f60SXin Li 			goto print;
55*05b00f60SXin Li 		} else if (c == '\r') {
56*05b00f60SXin Li 			/* CR - any LF? */
57*05b00f60SXin Li 			if ((idx+1) >= len) {
58*05b00f60SXin Li 				/* not in this packet */
59*05b00f60SXin Li 				goto trunc;
60*05b00f60SXin Li 			}
61*05b00f60SXin Li 			if (GET_U_1(pptr + idx + 1) == '\n') {
62*05b00f60SXin Li 				/*
63*05b00f60SXin Li 				 * CR-LF; end of line.
64*05b00f60SXin Li 				 * Skip the CR-LF and print the line, with
65*05b00f60SXin Li 				 * the exception of the CR-LF.
66*05b00f60SXin Li 				 */
67*05b00f60SXin Li 				goto print;
68*05b00f60SXin Li 			}
69*05b00f60SXin Li 
70*05b00f60SXin Li 			/*
71*05b00f60SXin Li 			 * CR followed by something else; treat this as
72*05b00f60SXin Li 			 * if it were binary data and don't print it.
73*05b00f60SXin Li 			 */
74*05b00f60SXin Li 			goto trunc;
75*05b00f60SXin Li 		} else if (!ND_ASCII_ISPRINT(c) ) {
76*05b00f60SXin Li 			/*
77*05b00f60SXin Li 			 * Not a printable ASCII character; treat this
78*05b00f60SXin Li 			 * as if it were binary data and don't print it.
79*05b00f60SXin Li 			 */
80*05b00f60SXin Li 			goto trunc;
81*05b00f60SXin Li 		}
82*05b00f60SXin Li 		idx++;
83*05b00f60SXin Li 	}
84*05b00f60SXin Li trunc:
85*05b00f60SXin Li 	return -1;
86*05b00f60SXin Li print:
87*05b00f60SXin Li 	ND_PRINT(": ");
88*05b00f60SXin Li 	nd_print_protocol_caps(ndo);
89*05b00f60SXin Li 	ND_PRINT(": %.*s", (int)idx, pptr);
90*05b00f60SXin Li 	return idx;
91*05b00f60SXin Li }
92*05b00f60SXin Li 
93*05b00f60SXin Li void
ssh_print(netdissect_options * ndo,const u_char * pptr,u_int len)94*05b00f60SXin Li ssh_print(netdissect_options *ndo, const u_char *pptr, u_int len)
95*05b00f60SXin Li {
96*05b00f60SXin Li 	ndo->ndo_protocol = "ssh";
97*05b00f60SXin Li 
98*05b00f60SXin Li 	ssh_print_version(ndo, pptr, len);
99*05b00f60SXin Li }
100