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