xref: /titanic_52/usr/src/cmd/cmd-inet/usr.sbin/snoop/snoop_socks.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1998-1999,2001 by Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate  * All rights reserved.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <stdio.h>
30*7c478bd9Sstevel@tonic-gate #include <sys/socket.h>
31*7c478bd9Sstevel@tonic-gate #include <netinet/in.h>
32*7c478bd9Sstevel@tonic-gate #include <string.h>
33*7c478bd9Sstevel@tonic-gate #include "snoop.h"
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate static void put_method(char *cp, int method);
36*7c478bd9Sstevel@tonic-gate static void put_socks5_addr(char *cp, const unsigned char *buf, int fraglen);
37*7c478bd9Sstevel@tonic-gate static void put_socks4_res(char *cp, int code);
38*7c478bd9Sstevel@tonic-gate static void put_socks5_res(char *cp, int code);
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate int
41*7c478bd9Sstevel@tonic-gate interpret_socks_call(flags, line, fraglen)
42*7c478bd9Sstevel@tonic-gate 	int flags;
43*7c478bd9Sstevel@tonic-gate 	char *line;
44*7c478bd9Sstevel@tonic-gate 	int fraglen;
45*7c478bd9Sstevel@tonic-gate {
46*7c478bd9Sstevel@tonic-gate 	unsigned char	*buf = (unsigned char *)line;
47*7c478bd9Sstevel@tonic-gate 	char		*cp;
48*7c478bd9Sstevel@tonic-gate 	struct in_addr	ipaddr;
49*7c478bd9Sstevel@tonic-gate 	int		i, n;
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate 	if (flags & F_SUM) {
52*7c478bd9Sstevel@tonic-gate 	cp = get_sum_line();
53*7c478bd9Sstevel@tonic-gate 	if (fraglen >= 2) {
54*7c478bd9Sstevel@tonic-gate 		switch (buf[0]) {
55*7c478bd9Sstevel@tonic-gate 		case 4:		/* SOCKS4 */
56*7c478bd9Sstevel@tonic-gate 			n = buf[1];
57*7c478bd9Sstevel@tonic-gate 			switch (n) {
58*7c478bd9Sstevel@tonic-gate 			case 1:
59*7c478bd9Sstevel@tonic-gate 			case 2:
60*7c478bd9Sstevel@tonic-gate 				if (fraglen >= 8) {
61*7c478bd9Sstevel@tonic-gate 					(void) memcpy(&ipaddr, &buf[4],
62*7c478bd9Sstevel@tonic-gate 					    sizeof (ipaddr));
63*7c478bd9Sstevel@tonic-gate 					(void) sprintf(cp,
64*7c478bd9Sstevel@tonic-gate 					    "SOCKS4 %s %s:%u",
65*7c478bd9Sstevel@tonic-gate 					    addrtoname(AF_INET, &ipaddr),
66*7c478bd9Sstevel@tonic-gate 					    (n == 1)? "CONNECT": "BIND",
67*7c478bd9Sstevel@tonic-gate 					    (buf[2] << 8) | buf[3]);
68*7c478bd9Sstevel@tonic-gate 					cp += strlen(cp);
69*7c478bd9Sstevel@tonic-gate 					if (fraglen > 8) {
70*7c478bd9Sstevel@tonic-gate 						(void) sprintf(cp, " User=");
71*7c478bd9Sstevel@tonic-gate 						cp += strlen(cp);
72*7c478bd9Sstevel@tonic-gate 						for (i = 8;
73*7c478bd9Sstevel@tonic-gate 							i < 40 && i < fraglen;
74*7c478bd9Sstevel@tonic-gate 							++i) {
75*7c478bd9Sstevel@tonic-gate 							if (buf[i] == '\0')
76*7c478bd9Sstevel@tonic-gate 								break;
77*7c478bd9Sstevel@tonic-gate 							*cp++ = buf[i];
78*7c478bd9Sstevel@tonic-gate 						}
79*7c478bd9Sstevel@tonic-gate 						if (i == 40) {
80*7c478bd9Sstevel@tonic-gate 							*cp++ = '.';
81*7c478bd9Sstevel@tonic-gate 							*cp++ = '.';
82*7c478bd9Sstevel@tonic-gate 							*cp++ = '.';
83*7c478bd9Sstevel@tonic-gate 						}
84*7c478bd9Sstevel@tonic-gate 						*cp = '\0';
85*7c478bd9Sstevel@tonic-gate 					}
86*7c478bd9Sstevel@tonic-gate 				}
87*7c478bd9Sstevel@tonic-gate 				break;
88*7c478bd9Sstevel@tonic-gate 			default:
89*7c478bd9Sstevel@tonic-gate 				(void) sprintf(cp, "SOCKS4 OP=%u", n);
90*7c478bd9Sstevel@tonic-gate 			}
91*7c478bd9Sstevel@tonic-gate 			break;
92*7c478bd9Sstevel@tonic-gate 		case 5:		/* SOCKS5 */
93*7c478bd9Sstevel@tonic-gate 			n = buf[1];
94*7c478bd9Sstevel@tonic-gate 			if (2 + n == fraglen) {
95*7c478bd9Sstevel@tonic-gate 				(void) sprintf(cp,
96*7c478bd9Sstevel@tonic-gate 					"SOCKS5 CONTACT NMETHODS=%d:", n);
97*7c478bd9Sstevel@tonic-gate 				cp += strlen(cp);
98*7c478bd9Sstevel@tonic-gate 				for (i = 0; i < n && 2 + i < fraglen; ++i) {
99*7c478bd9Sstevel@tonic-gate 					put_method(cp, buf[2 + i]);
100*7c478bd9Sstevel@tonic-gate 					cp += strlen(cp);
101*7c478bd9Sstevel@tonic-gate 				}
102*7c478bd9Sstevel@tonic-gate 			} else if (fraglen >= 6 && buf[2] == 0) {
103*7c478bd9Sstevel@tonic-gate 				const char	*cmd;
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate 				if (n < 1 || n > 3) {
106*7c478bd9Sstevel@tonic-gate 					(void) sprintf(cp,
107*7c478bd9Sstevel@tonic-gate 						"SOCKS (send data): %s",
108*7c478bd9Sstevel@tonic-gate 						show_string(line, fraglen, 20));
109*7c478bd9Sstevel@tonic-gate 				} else {
110*7c478bd9Sstevel@tonic-gate 					switch (n) {
111*7c478bd9Sstevel@tonic-gate 					case 1:
112*7c478bd9Sstevel@tonic-gate 						cmd = "CONNECT";
113*7c478bd9Sstevel@tonic-gate 						break;
114*7c478bd9Sstevel@tonic-gate 					case 2:
115*7c478bd9Sstevel@tonic-gate 						cmd = "BIND";
116*7c478bd9Sstevel@tonic-gate 						break;
117*7c478bd9Sstevel@tonic-gate 					case 3:
118*7c478bd9Sstevel@tonic-gate 						cmd = "ASSOCIATE_UDP";
119*7c478bd9Sstevel@tonic-gate 						break;
120*7c478bd9Sstevel@tonic-gate 					}
121*7c478bd9Sstevel@tonic-gate 					(void) sprintf(cp, "SOCKS5 %s ", cmd);
122*7c478bd9Sstevel@tonic-gate 					cp += strlen(cp);
123*7c478bd9Sstevel@tonic-gate 					put_socks5_addr(cp, &buf[3],
124*7c478bd9Sstevel@tonic-gate 						fraglen - 3);
125*7c478bd9Sstevel@tonic-gate 				}
126*7c478bd9Sstevel@tonic-gate 			} else {
127*7c478bd9Sstevel@tonic-gate 				(void) sprintf(cp, "SOCKS (send data): %s",
128*7c478bd9Sstevel@tonic-gate 					show_string(line, fraglen, 20));
129*7c478bd9Sstevel@tonic-gate 			}
130*7c478bd9Sstevel@tonic-gate 			break;
131*7c478bd9Sstevel@tonic-gate 		default:
132*7c478bd9Sstevel@tonic-gate 			(void) sprintf(cp, "SOCKS (send data): %s",
133*7c478bd9Sstevel@tonic-gate 				show_string(line, fraglen, 20));
134*7c478bd9Sstevel@tonic-gate 		}
135*7c478bd9Sstevel@tonic-gate 	} else {
136*7c478bd9Sstevel@tonic-gate 		(void) sprintf(cp, "SOCKS (send data): %s",
137*7c478bd9Sstevel@tonic-gate 			show_string(line, fraglen, 20));
138*7c478bd9Sstevel@tonic-gate 	}
139*7c478bd9Sstevel@tonic-gate 
140*7c478bd9Sstevel@tonic-gate 	} /* if (flags & F_SUM) */
141*7c478bd9Sstevel@tonic-gate 
142*7c478bd9Sstevel@tonic-gate 	if (flags & F_DTAIL) {
143*7c478bd9Sstevel@tonic-gate 		show_header("SOCKS: ", "SOCKS Header", fraglen);
144*7c478bd9Sstevel@tonic-gate 		show_space();
145*7c478bd9Sstevel@tonic-gate 		cp = get_line(0, 0);
146*7c478bd9Sstevel@tonic-gate 		if (fraglen >= 2) {
147*7c478bd9Sstevel@tonic-gate 			switch (buf[0]) {
148*7c478bd9Sstevel@tonic-gate 			case 4:
149*7c478bd9Sstevel@tonic-gate 				(void) sprintf(cp, "Version = 4");
150*7c478bd9Sstevel@tonic-gate 				n = buf[1];
151*7c478bd9Sstevel@tonic-gate 				switch (n) {
152*7c478bd9Sstevel@tonic-gate 				case 1:
153*7c478bd9Sstevel@tonic-gate 				case 2:
154*7c478bd9Sstevel@tonic-gate 					(void) sprintf(get_line(0, 0),
155*7c478bd9Sstevel@tonic-gate 					    "Operation = %s",
156*7c478bd9Sstevel@tonic-gate 					    (n == 1)? "CONNECT": "BIND");
157*7c478bd9Sstevel@tonic-gate 					if (fraglen >= 8) {
158*7c478bd9Sstevel@tonic-gate 						(void) memcpy(&ipaddr, &buf[4],
159*7c478bd9Sstevel@tonic-gate 						    sizeof (ipaddr));
160*7c478bd9Sstevel@tonic-gate 						(void) sprintf(get_line(0, 0),
161*7c478bd9Sstevel@tonic-gate 						    "Destination = %s:%u",
162*7c478bd9Sstevel@tonic-gate 						    addrtoname(AF_INET,
163*7c478bd9Sstevel@tonic-gate 						    &ipaddr),
164*7c478bd9Sstevel@tonic-gate 						    (buf[2] << 8) | buf[3]);
165*7c478bd9Sstevel@tonic-gate 						if (fraglen > 8) {
166*7c478bd9Sstevel@tonic-gate 							cp = get_line(0, 0);
167*7c478bd9Sstevel@tonic-gate 							(void) sprintf(cp,
168*7c478bd9Sstevel@tonic-gate 							    "User = ");
169*7c478bd9Sstevel@tonic-gate 							cp += strlen(cp);
170*7c478bd9Sstevel@tonic-gate 							for (i = 8;
171*7c478bd9Sstevel@tonic-gate 								i < 40; ++i) {
172*7c478bd9Sstevel@tonic-gate 								if
173*7c478bd9Sstevel@tonic-gate 								(buf[i] == '\0')
174*7c478bd9Sstevel@tonic-gate 									break;
175*7c478bd9Sstevel@tonic-gate 								*cp++ = buf[i];
176*7c478bd9Sstevel@tonic-gate 							}
177*7c478bd9Sstevel@tonic-gate 							if (i == 40) {
178*7c478bd9Sstevel@tonic-gate 								*cp++ = '.';
179*7c478bd9Sstevel@tonic-gate 								*cp++ = '.';
180*7c478bd9Sstevel@tonic-gate 								*cp++ = '.';
181*7c478bd9Sstevel@tonic-gate 							}
182*7c478bd9Sstevel@tonic-gate 							*cp = '\0';
183*7c478bd9Sstevel@tonic-gate 						}
184*7c478bd9Sstevel@tonic-gate 					}
185*7c478bd9Sstevel@tonic-gate 					break;
186*7c478bd9Sstevel@tonic-gate 				default:
187*7c478bd9Sstevel@tonic-gate 					(void) sprintf(get_line(0, 0),
188*7c478bd9Sstevel@tonic-gate 					    "Operation = %u (unknown)", n);
189*7c478bd9Sstevel@tonic-gate 				}
190*7c478bd9Sstevel@tonic-gate 				break;
191*7c478bd9Sstevel@tonic-gate 			case 5:		/* SOCKS5 */
192*7c478bd9Sstevel@tonic-gate 				(void) sprintf(cp, "Version = 5");
193*7c478bd9Sstevel@tonic-gate 				n = buf[1];
194*7c478bd9Sstevel@tonic-gate 				if (2 + n == fraglen) {
195*7c478bd9Sstevel@tonic-gate 					(void) sprintf(get_line(0, 0),
196*7c478bd9Sstevel@tonic-gate 					    "Number of methods = %u", n);
197*7c478bd9Sstevel@tonic-gate 					for (i = 0;
198*7c478bd9Sstevel@tonic-gate 						i < n && 2 + i < fraglen; ++i) {
199*7c478bd9Sstevel@tonic-gate 						cp = get_line(0, 0);
200*7c478bd9Sstevel@tonic-gate 						(void) sprintf(cp,
201*7c478bd9Sstevel@tonic-gate 							"Method %3u =", i);
202*7c478bd9Sstevel@tonic-gate 						cp += strlen(cp);
203*7c478bd9Sstevel@tonic-gate 						put_method(cp, buf[2 + i]);
204*7c478bd9Sstevel@tonic-gate 					}
205*7c478bd9Sstevel@tonic-gate 				} else if (fraglen >= 6 && buf[2] == 0) {
206*7c478bd9Sstevel@tonic-gate 					const char	*cmd;
207*7c478bd9Sstevel@tonic-gate 					if (n < 1 || n > 3) {
208*7c478bd9Sstevel@tonic-gate 						(void) sprintf(cp,
209*7c478bd9Sstevel@tonic-gate 							"SOCKS (send data): %s",
210*7c478bd9Sstevel@tonic-gate 							show_string(line,
211*7c478bd9Sstevel@tonic-gate 							fraglen, 20));
212*7c478bd9Sstevel@tonic-gate 					} else {
213*7c478bd9Sstevel@tonic-gate 						switch (n) {
214*7c478bd9Sstevel@tonic-gate 						case 1:
215*7c478bd9Sstevel@tonic-gate 							cmd = "CONNECT";
216*7c478bd9Sstevel@tonic-gate 							break;
217*7c478bd9Sstevel@tonic-gate 						case 2:
218*7c478bd9Sstevel@tonic-gate 							cmd = "BIND";
219*7c478bd9Sstevel@tonic-gate 							break;
220*7c478bd9Sstevel@tonic-gate 						case 3:
221*7c478bd9Sstevel@tonic-gate 							cmd = "ASSOCIATE_UDP";
222*7c478bd9Sstevel@tonic-gate 							break;
223*7c478bd9Sstevel@tonic-gate 						}
224*7c478bd9Sstevel@tonic-gate 						(void) sprintf(get_line(0, 0),
225*7c478bd9Sstevel@tonic-gate 						    "Operation = %s ", cmd);
226*7c478bd9Sstevel@tonic-gate 						put_socks5_addr(get_line(0, 0),
227*7c478bd9Sstevel@tonic-gate 						    &buf[3], fraglen - 3);
228*7c478bd9Sstevel@tonic-gate 						break;
229*7c478bd9Sstevel@tonic-gate 					}
230*7c478bd9Sstevel@tonic-gate 				} else
231*7c478bd9Sstevel@tonic-gate 					(void) sprintf(cp,
232*7c478bd9Sstevel@tonic-gate 						" SOCKS (send data): %s",
233*7c478bd9Sstevel@tonic-gate 						show_string(line, fraglen,
234*7c478bd9Sstevel@tonic-gate 						20));
235*7c478bd9Sstevel@tonic-gate 				break;
236*7c478bd9Sstevel@tonic-gate 			default:
237*7c478bd9Sstevel@tonic-gate 				(void) sprintf(cp,
238*7c478bd9Sstevel@tonic-gate 					"SOCKS (send data): %s",
239*7c478bd9Sstevel@tonic-gate 					show_string(line, fraglen, 20));
240*7c478bd9Sstevel@tonic-gate 			}
241*7c478bd9Sstevel@tonic-gate 			show_space();
242*7c478bd9Sstevel@tonic-gate 		} else
243*7c478bd9Sstevel@tonic-gate 			(void) sprintf(cp,
244*7c478bd9Sstevel@tonic-gate 				"SOCKS (send data): %s",
245*7c478bd9Sstevel@tonic-gate 				show_string(line, fraglen, 20));
246*7c478bd9Sstevel@tonic-gate 	}
247*7c478bd9Sstevel@tonic-gate 
248*7c478bd9Sstevel@tonic-gate out:
249*7c478bd9Sstevel@tonic-gate 	return (fraglen);
250*7c478bd9Sstevel@tonic-gate }
251*7c478bd9Sstevel@tonic-gate 
252*7c478bd9Sstevel@tonic-gate int
253*7c478bd9Sstevel@tonic-gate interpret_socks_reply(flags, line, fraglen)
254*7c478bd9Sstevel@tonic-gate 	int flags;
255*7c478bd9Sstevel@tonic-gate 	char *line;
256*7c478bd9Sstevel@tonic-gate 	int fraglen;
257*7c478bd9Sstevel@tonic-gate {
258*7c478bd9Sstevel@tonic-gate 	unsigned char	*buf = (unsigned char *)line;
259*7c478bd9Sstevel@tonic-gate 	char		*cp;
260*7c478bd9Sstevel@tonic-gate 	struct in_addr	ipaddr;
261*7c478bd9Sstevel@tonic-gate 
262*7c478bd9Sstevel@tonic-gate 	if (flags & F_SUM) {
263*7c478bd9Sstevel@tonic-gate 		cp = get_sum_line();
264*7c478bd9Sstevel@tonic-gate 		if (fraglen >= 2) {
265*7c478bd9Sstevel@tonic-gate 			switch (buf[0]) {
266*7c478bd9Sstevel@tonic-gate 			case 0:
267*7c478bd9Sstevel@tonic-gate 				(void) sprintf(cp, "SOCKS4 ");
268*7c478bd9Sstevel@tonic-gate 				cp += strlen(cp);
269*7c478bd9Sstevel@tonic-gate 				if (fraglen >= 8) {
270*7c478bd9Sstevel@tonic-gate 					(void) memcpy(&ipaddr, &buf[4],
271*7c478bd9Sstevel@tonic-gate 					    sizeof (ipaddr));
272*7c478bd9Sstevel@tonic-gate 					(void) sprintf(cp, "%s:%u ",
273*7c478bd9Sstevel@tonic-gate 					    addrtoname(AF_INET, &ipaddr),
274*7c478bd9Sstevel@tonic-gate 					    (buf[2] << 8) | buf[3]);
275*7c478bd9Sstevel@tonic-gate 					cp += strlen(cp);
276*7c478bd9Sstevel@tonic-gate 				}
277*7c478bd9Sstevel@tonic-gate 				/* reply version, no SOCKS version in v4 */
278*7c478bd9Sstevel@tonic-gate 				put_socks4_res(cp, buf[1]);
279*7c478bd9Sstevel@tonic-gate 				break;
280*7c478bd9Sstevel@tonic-gate 			case 5:
281*7c478bd9Sstevel@tonic-gate 				(void) sprintf(cp, "SOCKS5 method accepted:");
282*7c478bd9Sstevel@tonic-gate 				cp += strlen(cp);
283*7c478bd9Sstevel@tonic-gate 				put_method(cp, buf[1]);
284*7c478bd9Sstevel@tonic-gate 				break;
285*7c478bd9Sstevel@tonic-gate 			default:
286*7c478bd9Sstevel@tonic-gate 				(void) sprintf(cp, "SOCKS (recv data)");
287*7c478bd9Sstevel@tonic-gate 			}
288*7c478bd9Sstevel@tonic-gate 		} else
289*7c478bd9Sstevel@tonic-gate 			(void) sprintf(cp, "SOCKS (recv data)");
290*7c478bd9Sstevel@tonic-gate 	}
291*7c478bd9Sstevel@tonic-gate 
292*7c478bd9Sstevel@tonic-gate 	if (flags & F_DTAIL) {
293*7c478bd9Sstevel@tonic-gate 		show_header("SOCKS: ", "SOCKS Header", fraglen);
294*7c478bd9Sstevel@tonic-gate 		show_space();
295*7c478bd9Sstevel@tonic-gate 		cp = get_line(0, 0);
296*7c478bd9Sstevel@tonic-gate 		if (fraglen >= 2) {
297*7c478bd9Sstevel@tonic-gate 			switch (buf[0]) {
298*7c478bd9Sstevel@tonic-gate 			case 0:
299*7c478bd9Sstevel@tonic-gate 				/* reply version, no SOCKS version in v4 */
300*7c478bd9Sstevel@tonic-gate 				(void) sprintf(cp,
301*7c478bd9Sstevel@tonic-gate 				    "Reply version = 0 (SOCKS version 4)");
302*7c478bd9Sstevel@tonic-gate 				if (fraglen >= 8) {
303*7c478bd9Sstevel@tonic-gate 					(void) memcpy(&ipaddr, &buf[4],
304*7c478bd9Sstevel@tonic-gate 					    sizeof (ipaddr));
305*7c478bd9Sstevel@tonic-gate 					(void) sprintf(get_line(0, 0),
306*7c478bd9Sstevel@tonic-gate 					    "Destination %s:%u ",
307*7c478bd9Sstevel@tonic-gate 					    addrtoname(AF_INET, &ipaddr),
308*7c478bd9Sstevel@tonic-gate 					    (buf[2] << 8) | buf[3]);
309*7c478bd9Sstevel@tonic-gate 				}
310*7c478bd9Sstevel@tonic-gate 				cp = get_line(0, 0);
311*7c478bd9Sstevel@tonic-gate 				(void) sprintf(cp, "Result code = %u ", buf[1]);
312*7c478bd9Sstevel@tonic-gate 				cp += strlen(cp);
313*7c478bd9Sstevel@tonic-gate 				put_socks4_res(cp, buf[1]);
314*7c478bd9Sstevel@tonic-gate 				break;
315*7c478bd9Sstevel@tonic-gate 			case 5:
316*7c478bd9Sstevel@tonic-gate 				(void) sprintf(cp, "Reply version = 5");
317*7c478bd9Sstevel@tonic-gate 				if (fraglen == 2) {
318*7c478bd9Sstevel@tonic-gate 					cp = get_line(0, 0);
319*7c478bd9Sstevel@tonic-gate 					(void) sprintf(cp, "Method accepted =");
320*7c478bd9Sstevel@tonic-gate 					cp += strlen(cp);
321*7c478bd9Sstevel@tonic-gate 					put_method(cp, buf[1]);
322*7c478bd9Sstevel@tonic-gate 				} else if (fraglen >= 6 && buf[2] == 0x00) {
323*7c478bd9Sstevel@tonic-gate 					cp = get_line(0, 0);
324*7c478bd9Sstevel@tonic-gate 					(void) sprintf(cp, "Status = ");
325*7c478bd9Sstevel@tonic-gate 					cp += strlen(cp);
326*7c478bd9Sstevel@tonic-gate 					put_socks5_res(cp, buf[1]);
327*7c478bd9Sstevel@tonic-gate 					put_socks5_addr(get_line(0, 0),
328*7c478bd9Sstevel@tonic-gate 					    &buf[3], fraglen - 3);
329*7c478bd9Sstevel@tonic-gate 				}
330*7c478bd9Sstevel@tonic-gate 				break;
331*7c478bd9Sstevel@tonic-gate 			default:
332*7c478bd9Sstevel@tonic-gate 				(void) sprintf(cp, "(recv data)");
333*7c478bd9Sstevel@tonic-gate 			}
334*7c478bd9Sstevel@tonic-gate 		} else
335*7c478bd9Sstevel@tonic-gate 			(void) sprintf(cp, "(recv data)");
336*7c478bd9Sstevel@tonic-gate 		show_space();
337*7c478bd9Sstevel@tonic-gate 	}
338*7c478bd9Sstevel@tonic-gate 
339*7c478bd9Sstevel@tonic-gate out:
340*7c478bd9Sstevel@tonic-gate 	return (fraglen);
341*7c478bd9Sstevel@tonic-gate }
342*7c478bd9Sstevel@tonic-gate 
343*7c478bd9Sstevel@tonic-gate static void
344*7c478bd9Sstevel@tonic-gate put_method(char *cp, int method)
345*7c478bd9Sstevel@tonic-gate {
346*7c478bd9Sstevel@tonic-gate 	switch (method) {
347*7c478bd9Sstevel@tonic-gate 	case 0:
348*7c478bd9Sstevel@tonic-gate 		(void) sprintf(cp, " NOAUTH");
349*7c478bd9Sstevel@tonic-gate 		break;
350*7c478bd9Sstevel@tonic-gate 	case 1:
351*7c478bd9Sstevel@tonic-gate 		(void) sprintf(cp, " GSSAPI");
352*7c478bd9Sstevel@tonic-gate 		break;
353*7c478bd9Sstevel@tonic-gate 	case 2:
354*7c478bd9Sstevel@tonic-gate 		(void) sprintf(cp, " USERNAME/PASSWD");
355*7c478bd9Sstevel@tonic-gate 		break;
356*7c478bd9Sstevel@tonic-gate 	case 255:
357*7c478bd9Sstevel@tonic-gate 		(void) sprintf(cp, " NONE");
358*7c478bd9Sstevel@tonic-gate 		break;
359*7c478bd9Sstevel@tonic-gate 	default:
360*7c478bd9Sstevel@tonic-gate 		(void) sprintf(cp, " 0x%02x (unknown)", method);
361*7c478bd9Sstevel@tonic-gate 	}
362*7c478bd9Sstevel@tonic-gate }
363*7c478bd9Sstevel@tonic-gate 
364*7c478bd9Sstevel@tonic-gate static void
365*7c478bd9Sstevel@tonic-gate put_socks5_addr(char *cp, const unsigned char *buf, int fraglen)
366*7c478bd9Sstevel@tonic-gate {
367*7c478bd9Sstevel@tonic-gate 	struct in_addr	ipaddr;
368*7c478bd9Sstevel@tonic-gate 	int		i;
369*7c478bd9Sstevel@tonic-gate 
370*7c478bd9Sstevel@tonic-gate 	switch (buf[0]) {
371*7c478bd9Sstevel@tonic-gate 	case 1:
372*7c478bd9Sstevel@tonic-gate 		/* IPv4 */
373*7c478bd9Sstevel@tonic-gate 		(void) sprintf(cp, "Address = ");
374*7c478bd9Sstevel@tonic-gate 		cp += strlen(cp);
375*7c478bd9Sstevel@tonic-gate 		if (1 + 4 + 2 <= fraglen) {
376*7c478bd9Sstevel@tonic-gate 			(void) memcpy(&ipaddr, &buf[1], sizeof (ipaddr));
377*7c478bd9Sstevel@tonic-gate 			(void) sprintf(cp, "%s:%u",
378*7c478bd9Sstevel@tonic-gate 			    addrtoname(AF_INET, &ipaddr),
379*7c478bd9Sstevel@tonic-gate 			    (buf[5] << 8) | buf[5 + 1]);
380*7c478bd9Sstevel@tonic-gate 		} else
381*7c478bd9Sstevel@tonic-gate 			(void) strcat(cp, "(IPv4)");
382*7c478bd9Sstevel@tonic-gate 		break;
383*7c478bd9Sstevel@tonic-gate 	case 3:
384*7c478bd9Sstevel@tonic-gate 		/* domain name */
385*7c478bd9Sstevel@tonic-gate 		(void) sprintf(cp, "Domain name = ");
386*7c478bd9Sstevel@tonic-gate 		cp += strlen(cp);
387*7c478bd9Sstevel@tonic-gate 		for (i = 0; i <= buf[1] && 1 + i < fraglen; ++i)
388*7c478bd9Sstevel@tonic-gate 			*cp++ = buf[1 + i];
389*7c478bd9Sstevel@tonic-gate 		if (1 + i + 2 <= fraglen)
390*7c478bd9Sstevel@tonic-gate 			(void) sprintf(cp, ":%u",
391*7c478bd9Sstevel@tonic-gate 			    (buf[1 + i] << 8) | buf[1 + i + 1]);
392*7c478bd9Sstevel@tonic-gate 		else
393*7c478bd9Sstevel@tonic-gate 			*cp = '\0';
394*7c478bd9Sstevel@tonic-gate 		break;
395*7c478bd9Sstevel@tonic-gate 	case 4:
396*7c478bd9Sstevel@tonic-gate 		/* IPv6 */
397*7c478bd9Sstevel@tonic-gate 		(void) sprintf(cp, "Address = ");
398*7c478bd9Sstevel@tonic-gate 		if (1 + 16 <= fraglen) {
399*7c478bd9Sstevel@tonic-gate 			for (i = 0; i < 16; ++i) {
400*7c478bd9Sstevel@tonic-gate 				if (i > 0)
401*7c478bd9Sstevel@tonic-gate 					*cp++ = '.';
402*7c478bd9Sstevel@tonic-gate 				(void) sprintf(cp, "%u", buf[1 + i]);
403*7c478bd9Sstevel@tonic-gate 				cp += strlen(cp);
404*7c478bd9Sstevel@tonic-gate 			}
405*7c478bd9Sstevel@tonic-gate 			if (1 + 16 + 2 <= fraglen) {
406*7c478bd9Sstevel@tonic-gate 				(void) sprintf(cp, ":%u",
407*7c478bd9Sstevel@tonic-gate 				    (buf[1 + 16] << 8) | buf[1 + 16 + 1]);
408*7c478bd9Sstevel@tonic-gate 			}
409*7c478bd9Sstevel@tonic-gate 		} else
410*7c478bd9Sstevel@tonic-gate 			(void) strcat(cp, "(IPv6)");
411*7c478bd9Sstevel@tonic-gate 		break;
412*7c478bd9Sstevel@tonic-gate 	default:
413*7c478bd9Sstevel@tonic-gate 		(void) sprintf(cp, "Address type = 0x%02x (unknown)", buf[0]);
414*7c478bd9Sstevel@tonic-gate 	}
415*7c478bd9Sstevel@tonic-gate }
416*7c478bd9Sstevel@tonic-gate 
417*7c478bd9Sstevel@tonic-gate static void
418*7c478bd9Sstevel@tonic-gate put_socks4_res(char *cp, int code)
419*7c478bd9Sstevel@tonic-gate {
420*7c478bd9Sstevel@tonic-gate 	switch (code) {
421*7c478bd9Sstevel@tonic-gate 	case 90:
422*7c478bd9Sstevel@tonic-gate 		(void) sprintf(cp, "request granted");
423*7c478bd9Sstevel@tonic-gate 		break;
424*7c478bd9Sstevel@tonic-gate 	case 91:
425*7c478bd9Sstevel@tonic-gate 		(void) sprintf(cp, "request rejected or failed");
426*7c478bd9Sstevel@tonic-gate 		break;
427*7c478bd9Sstevel@tonic-gate 	case 92:
428*7c478bd9Sstevel@tonic-gate 		(void) sprintf(cp, "socksd can't connect to client's identd");
429*7c478bd9Sstevel@tonic-gate 		break;
430*7c478bd9Sstevel@tonic-gate 	case 93:
431*7c478bd9Sstevel@tonic-gate 		(void) sprintf(cp, "identity mismatch");
432*7c478bd9Sstevel@tonic-gate 		break;
433*7c478bd9Sstevel@tonic-gate 	default:
434*7c478bd9Sstevel@tonic-gate 		(void) sprintf(cp, "0x%02x (unknown)", code);
435*7c478bd9Sstevel@tonic-gate 	}
436*7c478bd9Sstevel@tonic-gate }
437*7c478bd9Sstevel@tonic-gate 
438*7c478bd9Sstevel@tonic-gate static void
439*7c478bd9Sstevel@tonic-gate put_socks5_res(char *cp, int code)
440*7c478bd9Sstevel@tonic-gate {
441*7c478bd9Sstevel@tonic-gate 	switch (code) {
442*7c478bd9Sstevel@tonic-gate 	case 0:
443*7c478bd9Sstevel@tonic-gate 		(void) strcpy(cp, "succeeded");
444*7c478bd9Sstevel@tonic-gate 		break;
445*7c478bd9Sstevel@tonic-gate 	case 1:
446*7c478bd9Sstevel@tonic-gate 		(void) strcpy(cp, "general SOCKS server failure");
447*7c478bd9Sstevel@tonic-gate 		break;
448*7c478bd9Sstevel@tonic-gate 	case 2:
449*7c478bd9Sstevel@tonic-gate 		(void) strcpy(cp, "connection not allowed by ruleset");
450*7c478bd9Sstevel@tonic-gate 		break;
451*7c478bd9Sstevel@tonic-gate 	case 3:
452*7c478bd9Sstevel@tonic-gate 		(void) strcpy(cp, "network unreachable");
453*7c478bd9Sstevel@tonic-gate 		break;
454*7c478bd9Sstevel@tonic-gate 	case 4:
455*7c478bd9Sstevel@tonic-gate 		(void) strcpy(cp, "host unreachable");
456*7c478bd9Sstevel@tonic-gate 		break;
457*7c478bd9Sstevel@tonic-gate 	case 5:
458*7c478bd9Sstevel@tonic-gate 		(void) strcpy(cp, "connection refused");
459*7c478bd9Sstevel@tonic-gate 		break;
460*7c478bd9Sstevel@tonic-gate 	case 6:
461*7c478bd9Sstevel@tonic-gate 		(void) strcpy(cp, "TTL expired");
462*7c478bd9Sstevel@tonic-gate 		break;
463*7c478bd9Sstevel@tonic-gate 	case 7:
464*7c478bd9Sstevel@tonic-gate 		(void) strcpy(cp, "command not supported");
465*7c478bd9Sstevel@tonic-gate 		break;
466*7c478bd9Sstevel@tonic-gate 	case 8:
467*7c478bd9Sstevel@tonic-gate 		(void) strcpy(cp, "address type not supported");
468*7c478bd9Sstevel@tonic-gate 		break;
469*7c478bd9Sstevel@tonic-gate 	default:
470*7c478bd9Sstevel@tonic-gate 		(void) sprintf(cp, "code 0x%02x", code);
471*7c478bd9Sstevel@tonic-gate 	}
472*7c478bd9Sstevel@tonic-gate }
473