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