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 2001 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate /*
31*0Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
32*0Sstevel@tonic-gate  * The Regents of the University of California
33*0Sstevel@tonic-gate  * All Rights Reserved
34*0Sstevel@tonic-gate  *
35*0Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
36*0Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
37*0Sstevel@tonic-gate  * contributors.
38*0Sstevel@tonic-gate  */
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate /*
43*0Sstevel@tonic-gate  * code for netstat's -k option
44*0Sstevel@tonic-gate  *
45*0Sstevel@tonic-gate  * NOTES:
46*0Sstevel@tonic-gate  * 1. A comment "LINTED: (note 1)" appears before certain lines where
47*0Sstevel@tonic-gate  *    lint would have complained, "pointer cast may result in improper
48*0Sstevel@tonic-gate  *    alignment". These are lines where lint had suspected potential
49*0Sstevel@tonic-gate  *    improper alignment of a data structure; in each such situation
50*0Sstevel@tonic-gate  *    we have relied on the kernel guaranteeing proper alignment.
51*0Sstevel@tonic-gate  */
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate #include <stdio.h>
54*0Sstevel@tonic-gate #include <stdlib.h>
55*0Sstevel@tonic-gate #include <unistd.h>
56*0Sstevel@tonic-gate #include <strings.h>
57*0Sstevel@tonic-gate #include <string.h>
58*0Sstevel@tonic-gate #include <kstat.h>
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate #include <sys/types.h>
61*0Sstevel@tonic-gate #include <sys/socket.h>
62*0Sstevel@tonic-gate #include <sys/stream.h>
63*0Sstevel@tonic-gate #include <sys/tiuser.h>
64*0Sstevel@tonic-gate #include <sys/socketvar.h>
65*0Sstevel@tonic-gate #include <sys/sysmacros.h>
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate static char		*typetoname(t_scalar_t);
68*0Sstevel@tonic-gate static void		print_kn(kstat_t *);
69*0Sstevel@tonic-gate static char		*nextstr(char *);
70*0Sstevel@tonic-gate extern void		fail(int, char *, ...);
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate #define	NALEN	8			/* nulladdress string length	*/
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate /*
75*0Sstevel@tonic-gate  * Print a summary of connections related to a unix protocol.
76*0Sstevel@tonic-gate  */
77*0Sstevel@tonic-gate void
78*0Sstevel@tonic-gate unixpr(kstat_ctl_t 	*kc)
79*0Sstevel@tonic-gate {
80*0Sstevel@tonic-gate 	kstat_t		*ksp;
81*0Sstevel@tonic-gate 
82*0Sstevel@tonic-gate 	if (kc == NULL) {	/* sanity check.			*/
83*0Sstevel@tonic-gate 		fail(0, "unixpr: No kstat");
84*0Sstevel@tonic-gate 		exit(3);
85*0Sstevel@tonic-gate 	}
86*0Sstevel@tonic-gate 
87*0Sstevel@tonic-gate 	/* find the sockfs kstat:					*/
88*0Sstevel@tonic-gate 	if ((ksp = kstat_lookup(kc, "sockfs", 0, "sock_unix_list")) ==
89*0Sstevel@tonic-gate 	    (kstat_t *)NULL) {
90*0Sstevel@tonic-gate 		fail(0, "kstat_data_lookup failed\n");
91*0Sstevel@tonic-gate 	}
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate 	if (kstat_read(kc, ksp, NULL) == -1) {
94*0Sstevel@tonic-gate 		fail(0, "kstat_read failed for sock_unix_list\n");
95*0Sstevel@tonic-gate 	}
96*0Sstevel@tonic-gate 
97*0Sstevel@tonic-gate 	print_kn(ksp);
98*0Sstevel@tonic-gate }
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate static void
101*0Sstevel@tonic-gate print_kn(kstat_t *ksp)
102*0Sstevel@tonic-gate {
103*0Sstevel@tonic-gate 	int		i;
104*0Sstevel@tonic-gate 	struct sockinfo	*psi;		/* ptr to current sockinfo	*/
105*0Sstevel@tonic-gate 	char		*pas;		/* ptr to string-format addrs	*/
106*0Sstevel@tonic-gate 	char		*nullstr;	/* ptr to null string		*/
107*0Sstevel@tonic-gate 	char		*conn_vp;
108*0Sstevel@tonic-gate 	char		*local_vp;
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate 	if (ksp->ks_ndata == 0) {
111*0Sstevel@tonic-gate 		return;			/* no AF_UNIX sockets found	*/
112*0Sstevel@tonic-gate 	}
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate 	/*
115*0Sstevel@tonic-gate 	 * Having ks_data set with ks_data == NULL shouldn't happen;
116*0Sstevel@tonic-gate 	 * If it does, the sockfs kstat is seriously broken.
117*0Sstevel@tonic-gate 	 */
118*0Sstevel@tonic-gate 	if ((psi = ksp->ks_data) == NULL) {
119*0Sstevel@tonic-gate 		fail(0, "print_kn: no kstat data\n");
120*0Sstevel@tonic-gate 	}
121*0Sstevel@tonic-gate 
122*0Sstevel@tonic-gate 	/* set pas to the address strings which are after the sockinfo	*/
123*0Sstevel@tonic-gate 	pas = &((char *)psi)[sizeof (struct sockinfo)];
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate 	/* Create a string of NALEN "0"'s for NULL addresses.		*/
126*0Sstevel@tonic-gate 	if ((nullstr = calloc(1, NALEN)) == NULL) {
127*0Sstevel@tonic-gate 		fail(0, "print_kn: out of memory\n");
128*0Sstevel@tonic-gate 	}
129*0Sstevel@tonic-gate 	(void) memset((void *)nullstr, '0', NALEN);
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate 	(void) printf("\nActive UNIX domain sockets\n");
132*0Sstevel@tonic-gate 	(void) printf("%-8.8s %-10.10s %8.8s %8.8s  "
133*0Sstevel@tonic-gate 			"Local Addr      Remote Addr\n",
134*0Sstevel@tonic-gate 			"Address", "Type", "Vnode", "Conn");
135*0Sstevel@tonic-gate 
136*0Sstevel@tonic-gate 	/* for each sockinfo structure, display what we need:		*/
137*0Sstevel@tonic-gate 	for (i = 0; i < ksp->ks_ndata; i++) {
138*0Sstevel@tonic-gate 		/* display sonode's address. 1st string after sockinfo:	*/
139*0Sstevel@tonic-gate 		pas = &(((char *)psi)[sizeof (struct sockinfo)]);
140*0Sstevel@tonic-gate 		(void) printf("%s ", pas);
141*0Sstevel@tonic-gate 
142*0Sstevel@tonic-gate 		(void) printf("%-10.10s ", typetoname(psi->si_serv_type));
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate 		/* laddr.sou_vp: 2nd string after sockinfo:		*/
145*0Sstevel@tonic-gate 		pas = nextstr(pas);
146*0Sstevel@tonic-gate 
147*0Sstevel@tonic-gate 		local_vp = conn_vp = nullstr;
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate 		if ((psi->si_state & SS_ISBOUND) &&
150*0Sstevel@tonic-gate 		    (psi->si_ux_laddr_sou_magic == SOU_MAGIC_EXPLICIT)) {
151*0Sstevel@tonic-gate 			local_vp = pas;
152*0Sstevel@tonic-gate 		}
153*0Sstevel@tonic-gate 
154*0Sstevel@tonic-gate 		/* faddr.sou_vp: 3rd string after sockinfo:		*/
155*0Sstevel@tonic-gate 		pas = nextstr(pas);
156*0Sstevel@tonic-gate 		if ((psi->si_state & SS_ISCONNECTED) &&
157*0Sstevel@tonic-gate 		    (psi->si_ux_faddr_sou_magic == SOU_MAGIC_EXPLICIT)) {
158*0Sstevel@tonic-gate 			conn_vp = pas;
159*0Sstevel@tonic-gate 		}
160*0Sstevel@tonic-gate 
161*0Sstevel@tonic-gate 		(void) printf("%s %s ", local_vp, conn_vp);
162*0Sstevel@tonic-gate 
163*0Sstevel@tonic-gate 		/* laddr.soa_sa: 					*/
164*0Sstevel@tonic-gate 		if ((psi->si_state & SS_ISBOUND) &&
165*0Sstevel@tonic-gate 		    strlen(psi->si_laddr_sun_path) != 0 &&
166*0Sstevel@tonic-gate 		    psi->si_laddr_soa_len != 0) {
167*0Sstevel@tonic-gate 			if (psi->si_state & SS_FADDR_NOXLATE) {
168*0Sstevel@tonic-gate 				(void) printf(" (socketpair)  ");
169*0Sstevel@tonic-gate 			} else {
170*0Sstevel@tonic-gate 				if (psi->si_laddr_soa_len >
171*0Sstevel@tonic-gate 					sizeof (psi->si_laddr_family))
172*0Sstevel@tonic-gate 					(void) printf("%s ",
173*0Sstevel@tonic-gate 						psi->si_laddr_sun_path);
174*0Sstevel@tonic-gate 				else
175*0Sstevel@tonic-gate 					(void) printf("               ");
176*0Sstevel@tonic-gate 			}
177*0Sstevel@tonic-gate 		} else
178*0Sstevel@tonic-gate 			(void) printf("               ");
179*0Sstevel@tonic-gate 
180*0Sstevel@tonic-gate 		/* faddr.soa_sa:					*/
181*0Sstevel@tonic-gate 		if ((psi->si_state & SS_ISCONNECTED) &&
182*0Sstevel@tonic-gate 		    strlen(psi->si_faddr_sun_path) != 0 &&
183*0Sstevel@tonic-gate 		    psi->si_faddr_soa_len != 0) {
184*0Sstevel@tonic-gate 
185*0Sstevel@tonic-gate 			if (psi->si_state & SS_FADDR_NOXLATE) {
186*0Sstevel@tonic-gate 				(void) printf(" (socketpair)  ");
187*0Sstevel@tonic-gate 			} else {
188*0Sstevel@tonic-gate 				if (psi->si_faddr_soa_len >
189*0Sstevel@tonic-gate 					sizeof (psi->si_faddr_family))
190*0Sstevel@tonic-gate 					(void) printf("%s ",
191*0Sstevel@tonic-gate 							psi->si_faddr_sun_path);
192*0Sstevel@tonic-gate 				else
193*0Sstevel@tonic-gate 					(void) printf("               ");
194*0Sstevel@tonic-gate 			}
195*0Sstevel@tonic-gate 		} else
196*0Sstevel@tonic-gate 			(void) printf("               ");
197*0Sstevel@tonic-gate 
198*0Sstevel@tonic-gate 		(void) printf("\n");
199*0Sstevel@tonic-gate 
200*0Sstevel@tonic-gate 		/* if si_size didn't get filled in, then we're done	*/
201*0Sstevel@tonic-gate 		if (psi->si_size == 0 ||
202*0Sstevel@tonic-gate 		    !IS_P2ALIGNED(psi->si_size, sizeof (psi))) {
203*0Sstevel@tonic-gate 			break;
204*0Sstevel@tonic-gate 		}
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate 		/* LINTED: (note 1) */
207*0Sstevel@tonic-gate 		psi = (struct sockinfo *)&(((char *)psi)[psi->si_size]);
208*0Sstevel@tonic-gate 	}
209*0Sstevel@tonic-gate }
210*0Sstevel@tonic-gate 
211*0Sstevel@tonic-gate static char *
212*0Sstevel@tonic-gate typetoname(t_scalar_t type)
213*0Sstevel@tonic-gate {
214*0Sstevel@tonic-gate 	switch (type) {
215*0Sstevel@tonic-gate 	case T_CLTS:
216*0Sstevel@tonic-gate 		return ("dgram");
217*0Sstevel@tonic-gate 
218*0Sstevel@tonic-gate 	case T_COTS:
219*0Sstevel@tonic-gate 		return ("stream");
220*0Sstevel@tonic-gate 
221*0Sstevel@tonic-gate 	case T_COTS_ORD:
222*0Sstevel@tonic-gate 		return ("stream-ord");
223*0Sstevel@tonic-gate 
224*0Sstevel@tonic-gate 	default:
225*0Sstevel@tonic-gate 		return ("");
226*0Sstevel@tonic-gate 	}
227*0Sstevel@tonic-gate }
228*0Sstevel@tonic-gate 
229*0Sstevel@tonic-gate /*
230*0Sstevel@tonic-gate  * nextstr():	find the beginning of a next string.
231*0Sstevel@tonic-gate  *	The sockfs kstat left-justifies each address string, leaving
232*0Sstevel@tonic-gate  *	null's between the strings. Since we don't necessarily know
233*0Sstevel@tonic-gate  *	the sizes of pointers in the kernel, we need to skip over these
234*0Sstevel@tonic-gate  *	nulls in order to get to the start of the next string.
235*0Sstevel@tonic-gate  */
236*0Sstevel@tonic-gate static char *
237*0Sstevel@tonic-gate nextstr(char *pas)
238*0Sstevel@tonic-gate {
239*0Sstevel@tonic-gate 	char *next;
240*0Sstevel@tonic-gate 
241*0Sstevel@tonic-gate 	for (next = &pas[strlen(pas) + 1]; *next == NULL; ) {
242*0Sstevel@tonic-gate 		next++;
243*0Sstevel@tonic-gate 	}
244*0Sstevel@tonic-gate 
245*0Sstevel@tonic-gate 	return (next);
246*0Sstevel@tonic-gate }
247