]>
git.cameronkatri.com Git - apple_cmds.git/blob - network_cmds/netstat.tproj/vsock.c
2 * Copyright (c) 2020 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
30 * Display protocol blocks in the vsock domain.
32 #include <sys/proc_info.h>
33 #include <sys/socketvar.h>
34 #include <sys/sysctl.h>
35 #include <sys/vsock.h>
45 static void vsockdomainpr
__P((struct xvsockpcb
*));
48 vsockpr(uint32_t proto
,
53 struct xvsockpgen
*xvg
, *oxvg
;
54 struct xvsockpcb
*xpcb
;
56 const char* mibvar
= "net.vsock.pcblist";
58 if (sysctlbyname(mibvar
, 0, &len
, 0, 0) < 0) {
60 warn("sysctl: %s", mibvar
);
63 if ((buf
= malloc(len
)) == 0) {
64 warn("malloc %lu bytes", (u_long
)len
);
67 if (sysctlbyname(mibvar
, buf
, &len
, 0, 0) < 0) {
68 warn("sysctl: %s", mibvar
);
74 * Bail-out to avoid logic error in the loop below when
75 * there is in fact no more control block to process
77 if (len
<= 2 * sizeof(struct xvsockpgen
)) {
82 oxvg
= (struct xvsockpgen
*)buf
;
84 // Save room for the last xvsockpgen.
87 for (next
= buf
+ oxvg
->xvg_len
; next
< buf
+ len
; next
+= xpcb
->xv_len
) {
88 xpcb
= (struct xvsockpcb
*)next
;
90 /* Ignore PCBs which were freed during copyout. */
91 if (xpcb
->xvp_gencnt
> oxvg
->xvg_gen
)
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");
102 printf("Some vsock sockets may have been created or deleted.\n");
109 vsock_print_addr(buf
, cid
, port
)
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
);
121 (void) sprintf(buf
, "%u:%u", cid
, port
);
127 struct xvsockpcb
*xpcb
;
129 static int first
= 1;
132 printf("Active VSock sockets\n");
133 printf("%-5.5s %-6.6s %-6.6s %-6.6s %-18.18s %-18.18s %-11.11s",
136 "Local Address", "Foreign Address",
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");
146 struct xsocket
*so
= &xpcb
->xv_socket
;
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
);
154 // Determine the vsock socket 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
) {
162 } else if (so
->so_options
& SO_ACCEPTCONN
) {
168 printf("%-5.5s %-6.6s %6u %6u %-18s %-18s %-11s",
170 so
->so_rcv
.sb_cc
, so
->so_snd
.sb_cc
,
174 printf(" %10u %10u %10u %10u %6u %6u %6u 0x%04x 0x%08x",
177 xpcb
->xvp_peer_rxcnt
,
178 xpcb
->xvp_peer_rxhiwat
,
187 #endif /* AF_VSOCK */