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