]> git.cameronkatri.com Git - apple_cmds.git/blob - network_cmds/netstat.tproj/vsock.c
Improve create_tarballs.sh
[apple_cmds.git] / network_cmds / netstat.tproj / vsock.c
1 /*
2 * Copyright (c) 2020 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 /*
30 * Display protocol blocks in the vsock domain.
31 */
32 #include <sys/proc_info.h>
33 #include <sys/socketvar.h>
34 #include <sys/sysctl.h>
35 #include <sys/vsock.h>
36
37 #include <errno.h>
38 #include <err.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include "netstat.h"
42
43 #ifdef AF_VSOCK
44
45 static void vsockdomainpr __P((struct xvsockpcb *));
46
47 void
48 vsockpr(uint32_t proto,
49 char *name, int af)
50 {
51 char *buf, *next;
52 size_t len;
53 struct xvsockpgen *xvg, *oxvg;
54 struct xvsockpcb *xpcb;
55
56 const char* mibvar = "net.vsock.pcblist";
57 len = 0;
58 if (sysctlbyname(mibvar, 0, &len, 0, 0) < 0) {
59 if (errno != ENOENT)
60 warn("sysctl: %s", mibvar);
61 return;
62 }
63 if ((buf = malloc(len)) == 0) {
64 warn("malloc %lu bytes", (u_long)len);
65 return;
66 }
67 if (sysctlbyname(mibvar, buf, &len, 0, 0) < 0) {
68 warn("sysctl: %s", mibvar);
69 free(buf);
70 return;
71 }
72
73 /*
74 * Bail-out to avoid logic error in the loop below when
75 * there is in fact no more control block to process
76 */
77 if (len <= 2 * sizeof(struct xvsockpgen)) {
78 free(buf);
79 return;
80 }
81
82 oxvg = (struct xvsockpgen *)buf;
83
84 // Save room for the last xvsockpgen.
85 len -= oxvg->xvg_len;
86
87 for (next = buf + oxvg->xvg_len; next < buf + len; next += xpcb->xv_len) {
88 xpcb = (struct xvsockpcb *)next;
89
90 /* Ignore PCBs which were freed during copyout. */
91 if (xpcb->xvp_gencnt > oxvg->xvg_gen)
92 continue;
93 vsockdomainpr(xpcb);
94 }
95 xvg = (struct xvsockpgen *)next;
96 if (xvg != oxvg && xvg->xvg_gen != oxvg->xvg_gen) {
97 if (oxvg->xvg_count > xvg->xvg_count) {
98 printf("Some vsock sockets may have been deleted.\n");
99 } else if (oxvg->xvg_count < xvg->xvg_count) {
100 printf("Some vsock sockets may have been created.\n");
101 } else {
102 printf("Some vsock sockets may have been created or deleted.\n");
103 }
104 }
105 free(buf);
106 }
107
108 static void
109 vsock_print_addr(buf, cid, port)
110 char *buf;
111 uint32_t cid;
112 uint32_t port;
113 {
114 if (cid == VMADDR_CID_ANY && port == VMADDR_PORT_ANY) {
115 (void) sprintf(buf, "*:*");
116 } else if (cid == VMADDR_CID_ANY) {
117 (void) sprintf(buf, "*:%u", port);
118 } else if (port == VMADDR_PORT_ANY) {
119 (void) sprintf(buf, "%u:*", cid);
120 } else {
121 (void) sprintf(buf, "%u:%u", cid, port);
122 }
123 }
124
125 static void
126 vsockdomainpr(xpcb)
127 struct xvsockpcb *xpcb;
128 {
129 static int first = 1;
130
131 if (first) {
132 printf("Active VSock sockets\n");
133 printf("%-5.5s %-6.6s %-6.6s %-6.6s %-18.18s %-18.18s %-11.11s",
134 "Proto", "Type",
135 "Recv-Q", "Send-Q",
136 "Local Address", "Foreign Address",
137 "State");
138 if (vflag > 0)
139 printf(" %10.10s %10.10s %10.10s %10.10s %6.6s %6.6s %6.6s %6s %10s",
140 "rxcnt", "txcnt", "peer_rxcnt", "peer_rxhiwat",
141 "rxhiwat", "txhiwat", "pid", "state", "options");
142 printf("\n");
143 first = 0;
144 }
145
146 struct xsocket *so = &xpcb->xv_socket;
147
148 char srcAddr[50];
149 char dstAddr[50];
150
151 vsock_print_addr(srcAddr, xpcb->xvp_local_cid, xpcb->xvp_local_port);
152 vsock_print_addr(dstAddr, xpcb->xvp_remote_cid, xpcb->xvp_remote_port);
153
154 // Determine the vsock socket state.
155 char *state;
156 if (so->so_state & SOI_S_ISCONNECTING) {
157 state = "CONNECTING";
158 } else if (so->so_state & SOI_S_ISCONNECTED) {
159 state = "ESTABLISHED";
160 } else if (so->so_state & SOI_S_ISDISCONNECTING) {
161 state = "CLOSING";
162 } else if (so->so_options & SO_ACCEPTCONN) {
163 state = "LISTEN";
164 } else {
165 state = "CLOSED";
166 }
167
168 printf("%-5.5s %-6.6s %6u %6u %-18s %-18s %-11s",
169 "vsock", "stream",
170 so->so_rcv.sb_cc, so->so_snd.sb_cc,
171 srcAddr, dstAddr,
172 state);
173 if (vflag > 0)
174 printf(" %10u %10u %10u %10u %6u %6u %6u 0x%04x 0x%08x",
175 xpcb->xvp_rxcnt,
176 xpcb->xvp_txcnt,
177 xpcb->xvp_peer_rxcnt,
178 xpcb->xvp_peer_rxhiwat,
179 so->so_rcv.sb_hiwat,
180 so->so_snd.sb_hiwat,
181 xpcb->xvp_last_pid,
182 so->so_state,
183 so->so_options);
184 printf("\n");
185 }
186
187 #endif /* AF_VSOCK */