[28449] | 1 | /* $Id: debug.c 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
| 3 | * NAT - debug helpers.
|
---|
| 4 | */
|
---|
| 5 |
|
---|
[1] | 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
[28449] | 8 | *
|
---|
[96407] | 9 | * This file is part of VirtualBox base platform packages, as
|
---|
| 10 | * available from https://www.virtualbox.org.
|
---|
| 11 | *
|
---|
| 12 | * This program is free software; you can redistribute it and/or
|
---|
| 13 | * modify it under the terms of the GNU General Public License
|
---|
| 14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
| 15 | * License.
|
---|
| 16 | *
|
---|
| 17 | * This program is distributed in the hope that it will be useful, but
|
---|
| 18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 20 | * General Public License for more details.
|
---|
| 21 | *
|
---|
| 22 | * You should have received a copy of the GNU General Public License
|
---|
| 23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
| 24 | *
|
---|
| 25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
[28449] | 26 | */
|
---|
| 27 |
|
---|
| 28 |
|
---|
| 29 | /*
|
---|
| 30 | * This code is based on:
|
---|
| 31 | *
|
---|
[1] | 32 | * Copyright (c) 1995 Danny Gasparovski.
|
---|
| 33 | * Portions copyright (c) 2000 Kelly Price.
|
---|
[1033] | 34 | *
|
---|
| 35 | * Please read the file COPYRIGHT for the
|
---|
[1] | 36 | * terms and conditions of the copyright.
|
---|
| 37 | */
|
---|
| 38 |
|
---|
| 39 | #include <slirp.h>
|
---|
[14882] | 40 | #include <iprt/string.h>
|
---|
| 41 | #include <iprt/stream.h>
|
---|
[38492] | 42 | #include <iprt/critsect.h>
|
---|
| 43 | #include "zone.h"
|
---|
[1] | 44 |
|
---|
[14466] | 45 | #ifdef DEBUG
|
---|
| 46 | void dump_packet(void *, int);
|
---|
| 47 | #endif
|
---|
[1] | 48 |
|
---|
[37738] | 49 | #ifndef STRINGIFY
|
---|
| 50 | # define STRINGIFY(x) #x
|
---|
| 51 | #endif
|
---|
| 52 |
|
---|
| 53 | static char *g_apszTcpStates[TCP_NSTATES] =
|
---|
| 54 | {
|
---|
| 55 | STRINGIFY(TCPS_CLOSED),
|
---|
| 56 | STRINGIFY(TCPS_LISTEN),
|
---|
| 57 | STRINGIFY(TCPS_SYN_SENT),
|
---|
| 58 | STRINGIFY(TCPS_SYN_RECEIVED),
|
---|
| 59 | STRINGIFY(TCPS_ESTABLISHED),
|
---|
| 60 | STRINGIFY(TCPS_CLOSE_WAIT),
|
---|
| 61 | STRINGIFY(TCPS_FIN_WAIT_1),
|
---|
| 62 | STRINGIFY(TCPS_CLOSING),
|
---|
| 63 | STRINGIFY(TCPS_LAST_ACK),
|
---|
| 64 | STRINGIFY(TCPS_FIN_WAIT_2),
|
---|
| 65 | STRINGIFY(TCPS_TIME_WAIT)
|
---|
| 66 | };
|
---|
| 67 |
|
---|
[39286] | 68 | typedef struct DEBUGSTRSOCKETSTATE
|
---|
| 69 | {
|
---|
| 70 | uint32_t u32SocketState;
|
---|
| 71 | const char *pcszSocketStateName;
|
---|
| 72 | } DEBUGSTRSOCKETSTATE;
|
---|
| 73 |
|
---|
| 74 | #define DEBUGSTRSOCKETSTATE_HELPER(x) {(x), #x}
|
---|
| 75 |
|
---|
| 76 | static DEBUGSTRSOCKETSTATE g_apszSocketStates[8] =
|
---|
| 77 | {
|
---|
| 78 | DEBUGSTRSOCKETSTATE_HELPER(SS_NOFDREF),
|
---|
| 79 | DEBUGSTRSOCKETSTATE_HELPER(SS_ISFCONNECTING),
|
---|
| 80 | DEBUGSTRSOCKETSTATE_HELPER(SS_ISFCONNECTED),
|
---|
| 81 | DEBUGSTRSOCKETSTATE_HELPER(SS_FCANTRCVMORE),
|
---|
| 82 | DEBUGSTRSOCKETSTATE_HELPER(SS_FCANTSENDMORE),
|
---|
| 83 | DEBUGSTRSOCKETSTATE_HELPER(SS_FWDRAIN),
|
---|
| 84 | DEBUGSTRSOCKETSTATE_HELPER(SS_FACCEPTCONN),
|
---|
| 85 | DEBUGSTRSOCKETSTATE_HELPER(SS_FACCEPTONCE),
|
---|
| 86 | };
|
---|
| 87 |
|
---|
[41228] | 88 | static DEBUGSTRSOCKETSTATE g_aTcpFlags[] =
|
---|
| 89 | {
|
---|
| 90 | DEBUGSTRSOCKETSTATE_HELPER(TH_FIN),
|
---|
| 91 | DEBUGSTRSOCKETSTATE_HELPER(TH_SYN),
|
---|
| 92 | DEBUGSTRSOCKETSTATE_HELPER(TH_RST),
|
---|
| 93 | DEBUGSTRSOCKETSTATE_HELPER(TH_PUSH),
|
---|
| 94 | DEBUGSTRSOCKETSTATE_HELPER(TH_ACK),
|
---|
| 95 | DEBUGSTRSOCKETSTATE_HELPER(TH_URG),
|
---|
| 96 | };
|
---|
| 97 |
|
---|
[1] | 98 | /*
|
---|
| 99 | * Dump a packet in the same format as tcpdump -x
|
---|
| 100 | */
|
---|
| 101 | #ifdef DEBUG
|
---|
| 102 | void
|
---|
[5715] | 103 | dump_packet(void *dat, int n)
|
---|
[1] | 104 | {
|
---|
[26180] | 105 | Log(("nat: PACKET DUMPED:\n%.*Rhxd\n", n, dat));
|
---|
[1] | 106 | }
|
---|
| 107 | #endif
|
---|
| 108 |
|
---|
[15054] | 109 | #ifdef LOG_ENABLED
|
---|
| 110 | static void
|
---|
| 111 | lprint(const char *pszFormat, ...)
|
---|
| 112 | {
|
---|
| 113 | va_list args;
|
---|
| 114 | va_start(args, pszFormat);
|
---|
| 115 | RTLogPrintfV(pszFormat, args);
|
---|
| 116 | va_end(args);
|
---|
| 117 | }
|
---|
[1] | 118 |
|
---|
| 119 | void
|
---|
[1033] | 120 | ipstats(PNATState pData)
|
---|
[1] | 121 | {
|
---|
[34103] | 122 | lprint("\n");
|
---|
[1] | 123 |
|
---|
[15054] | 124 | lprint("IP stats:\n");
|
---|
| 125 | lprint(" %6d total packets received (%d were unaligned)\n",
|
---|
| 126 | ipstat.ips_total, ipstat.ips_unaligned);
|
---|
| 127 | lprint(" %6d with incorrect version\n", ipstat.ips_badvers);
|
---|
| 128 | lprint(" %6d with bad header checksum\n", ipstat.ips_badsum);
|
---|
| 129 | lprint(" %6d with length too short (len < sizeof(iphdr))\n", ipstat.ips_tooshort);
|
---|
| 130 | lprint(" %6d with length too small (len < ip->len)\n", ipstat.ips_toosmall);
|
---|
| 131 | lprint(" %6d with bad header length\n", ipstat.ips_badhlen);
|
---|
| 132 | lprint(" %6d with bad packet length\n", ipstat.ips_badlen);
|
---|
| 133 | lprint(" %6d fragments received\n", ipstat.ips_fragments);
|
---|
| 134 | lprint(" %6d fragments dropped\n", ipstat.ips_fragdropped);
|
---|
| 135 | lprint(" %6d fragments timed out\n", ipstat.ips_fragtimeout);
|
---|
| 136 | lprint(" %6d packets reassembled ok\n", ipstat.ips_reassembled);
|
---|
| 137 | lprint(" %6d outgoing packets fragmented\n", ipstat.ips_fragmented);
|
---|
| 138 | lprint(" %6d total outgoing fragments\n", ipstat.ips_ofragments);
|
---|
| 139 | lprint(" %6d with bad protocol field\n", ipstat.ips_noproto);
|
---|
| 140 | lprint(" %6d total packets delivered\n", ipstat.ips_delivered);
|
---|
[1] | 141 | }
|
---|
| 142 |
|
---|
| 143 | void
|
---|
[1033] | 144 | tcpstats(PNATState pData)
|
---|
[1] | 145 | {
|
---|
[34103] | 146 | lprint("\n");
|
---|
[1] | 147 |
|
---|
[15054] | 148 | lprint("TCP stats:\n");
|
---|
[1033] | 149 |
|
---|
[15054] | 150 | lprint(" %6d packets sent\n", tcpstat.tcps_sndtotal);
|
---|
| 151 | lprint(" %6d data packets (%d bytes)\n",
|
---|
| 152 | tcpstat.tcps_sndpack, tcpstat.tcps_sndbyte);
|
---|
| 153 | lprint(" %6d data packets retransmitted (%d bytes)\n",
|
---|
| 154 | tcpstat.tcps_sndrexmitpack, tcpstat.tcps_sndrexmitbyte);
|
---|
| 155 | lprint(" %6d ack-only packets (%d delayed)\n",
|
---|
| 156 | tcpstat.tcps_sndacks, tcpstat.tcps_delack);
|
---|
| 157 | lprint(" %6d URG only packets\n", tcpstat.tcps_sndurg);
|
---|
| 158 | lprint(" %6d window probe packets\n", tcpstat.tcps_sndprobe);
|
---|
| 159 | lprint(" %6d window update packets\n", tcpstat.tcps_sndwinup);
|
---|
| 160 | lprint(" %6d control (SYN/FIN/RST) packets\n", tcpstat.tcps_sndctrl);
|
---|
| 161 | lprint(" %6d times tcp_output did nothing\n", tcpstat.tcps_didnuttin);
|
---|
[1033] | 162 |
|
---|
[15054] | 163 | lprint(" %6d packets received\n", tcpstat.tcps_rcvtotal);
|
---|
| 164 | lprint(" %6d acks (for %d bytes)\n",
|
---|
| 165 | tcpstat.tcps_rcvackpack, tcpstat.tcps_rcvackbyte);
|
---|
| 166 | lprint(" %6d duplicate acks\n", tcpstat.tcps_rcvdupack);
|
---|
| 167 | lprint(" %6d acks for unsent data\n", tcpstat.tcps_rcvacktoomuch);
|
---|
| 168 | lprint(" %6d packets received in sequence (%d bytes)\n",
|
---|
| 169 | tcpstat.tcps_rcvpack, tcpstat.tcps_rcvbyte);
|
---|
| 170 | lprint(" %6d completely duplicate packets (%d bytes)\n",
|
---|
| 171 | tcpstat.tcps_rcvduppack, tcpstat.tcps_rcvdupbyte);
|
---|
[1033] | 172 |
|
---|
[15054] | 173 | lprint(" %6d packets with some duplicate data (%d bytes duped)\n",
|
---|
| 174 | tcpstat.tcps_rcvpartduppack, tcpstat.tcps_rcvpartdupbyte);
|
---|
| 175 | lprint(" %6d out-of-order packets (%d bytes)\n",
|
---|
| 176 | tcpstat.tcps_rcvoopack, tcpstat.tcps_rcvoobyte);
|
---|
| 177 | lprint(" %6d packets of data after window (%d bytes)\n",
|
---|
| 178 | tcpstat.tcps_rcvpackafterwin, tcpstat.tcps_rcvbyteafterwin);
|
---|
| 179 | lprint(" %6d window probes\n", tcpstat.tcps_rcvwinprobe);
|
---|
| 180 | lprint(" %6d window update packets\n", tcpstat.tcps_rcvwinupd);
|
---|
| 181 | lprint(" %6d packets received after close\n", tcpstat.tcps_rcvafterclose);
|
---|
| 182 | lprint(" %6d discarded for bad checksums\n", tcpstat.tcps_rcvbadsum);
|
---|
| 183 | lprint(" %6d discarded for bad header offset fields\n",
|
---|
| 184 | tcpstat.tcps_rcvbadoff);
|
---|
[1033] | 185 |
|
---|
[15054] | 186 | lprint(" %6d connection requests\n", tcpstat.tcps_connattempt);
|
---|
| 187 | lprint(" %6d connection accepts\n", tcpstat.tcps_accepts);
|
---|
| 188 | lprint(" %6d connections established (including accepts)\n", tcpstat.tcps_connects);
|
---|
| 189 | lprint(" %6d connections closed (including %d drop)\n",
|
---|
| 190 | tcpstat.tcps_closed, tcpstat.tcps_drops);
|
---|
| 191 | lprint(" %6d embryonic connections dropped\n", tcpstat.tcps_conndrops);
|
---|
| 192 | lprint(" %6d segments we tried to get rtt (%d succeeded)\n",
|
---|
| 193 | tcpstat.tcps_segstimed, tcpstat.tcps_rttupdated);
|
---|
| 194 | lprint(" %6d retransmit timeouts\n", tcpstat.tcps_rexmttimeo);
|
---|
| 195 | lprint(" %6d connections dropped by rxmt timeout\n",
|
---|
| 196 | tcpstat.tcps_timeoutdrop);
|
---|
| 197 | lprint(" %6d persist timeouts\n", tcpstat.tcps_persisttimeo);
|
---|
| 198 | lprint(" %6d keepalive timeouts\n", tcpstat.tcps_keeptimeo);
|
---|
| 199 | lprint(" %6d keepalive probes sent\n", tcpstat.tcps_keepprobe);
|
---|
| 200 | lprint(" %6d connections dropped by keepalive\n", tcpstat.tcps_keepdrops);
|
---|
| 201 | lprint(" %6d correct ACK header predictions\n", tcpstat.tcps_predack);
|
---|
| 202 | lprint(" %6d correct data packet header predictions\n", tcpstat.tcps_preddat);
|
---|
| 203 | lprint(" %6d TCP cache misses\n", tcpstat.tcps_socachemiss);
|
---|
[1033] | 204 |
|
---|
[15054] | 205 | /* lprint(" Packets received too short: %d\n", tcpstat.tcps_rcvshort); */
|
---|
| 206 | /* lprint(" Segments dropped due to PAWS: %d\n", tcpstat.tcps_pawsdrop); */
|
---|
[1033] | 207 |
|
---|
[1] | 208 | }
|
---|
| 209 |
|
---|
| 210 | void
|
---|
[1033] | 211 | udpstats(PNATState pData)
|
---|
[1] | 212 | {
|
---|
[34103] | 213 | lprint("\n");
|
---|
[1] | 214 |
|
---|
[15054] | 215 | lprint("UDP stats:\n");
|
---|
| 216 | lprint(" %6d datagrams received\n", udpstat.udps_ipackets);
|
---|
| 217 | lprint(" %6d with packets shorter than header\n", udpstat.udps_hdrops);
|
---|
| 218 | lprint(" %6d with bad checksums\n", udpstat.udps_badsum);
|
---|
| 219 | lprint(" %6d with data length larger than packet\n", udpstat.udps_badlen);
|
---|
| 220 | lprint(" %6d UDP socket cache misses\n", udpstat.udpps_pcbcachemiss);
|
---|
| 221 | lprint(" %6d datagrams sent\n", udpstat.udps_opackets);
|
---|
[1] | 222 | }
|
---|
| 223 |
|
---|
| 224 | void
|
---|
[1033] | 225 | icmpstats(PNATState pData)
|
---|
[1] | 226 | {
|
---|
[34103] | 227 | lprint("\n");
|
---|
[15054] | 228 | lprint("ICMP stats:\n");
|
---|
| 229 | lprint(" %6d ICMP packets received\n", icmpstat.icps_received);
|
---|
| 230 | lprint(" %6d were too short\n", icmpstat.icps_tooshort);
|
---|
| 231 | lprint(" %6d with bad checksums\n", icmpstat.icps_checksum);
|
---|
| 232 | lprint(" %6d with type not supported\n", icmpstat.icps_notsupp);
|
---|
| 233 | lprint(" %6d with bad type feilds\n", icmpstat.icps_badtype);
|
---|
| 234 | lprint(" %6d ICMP packets sent in reply\n", icmpstat.icps_reflect);
|
---|
[1] | 235 | }
|
---|
| 236 |
|
---|
| 237 | void
|
---|
[1033] | 238 | mbufstats(PNATState pData)
|
---|
[1] | 239 | {
|
---|
[23466] | 240 | /*
|
---|
[26180] | 241 | * (vvl) this static code can't work with mbuf zone anymore
|
---|
[23466] | 242 | * @todo: make statistic correct
|
---|
| 243 | */
|
---|
[39085] | 244 | NOREF(pData);
|
---|
[1] | 245 | }
|
---|
| 246 |
|
---|
| 247 | void
|
---|
[1033] | 248 | sockstats(PNATState pData)
|
---|
[1] | 249 | {
|
---|
[15054] | 250 | char buff[256];
|
---|
[28440] | 251 | size_t n;
|
---|
[16443] | 252 | struct socket *so, *so_next;
|
---|
[1] | 253 |
|
---|
[34103] | 254 | lprint("\n");
|
---|
[1033] | 255 |
|
---|
[15054] | 256 | lprint(
|
---|
| 257 | "Proto[state] Sock Local Address, Port Remote Address, Port RecvQ SendQ\n");
|
---|
[1033] | 258 |
|
---|
[16443] | 259 | QSOCKET_FOREACH(so, so_next, tcp)
|
---|
| 260 | /* { */
|
---|
[22873] | 261 | n = RTStrPrintf(buff, sizeof(buff), "tcp[%s]", so->so_tcpcb?tcpstates[so->so_tcpcb->t_state]:"NONE");
|
---|
[15054] | 262 | while (n < 17)
|
---|
| 263 | buff[n++] = ' ';
|
---|
| 264 | buff[17] = 0;
|
---|
| 265 | lprint("%s %3d %15s %5d ",
|
---|
[25822] | 266 | buff, so->s, inet_ntoa(so->so_laddr), RT_N2H_U16(so->so_lport));
|
---|
[15054] | 267 | lprint("%15s %5d %5d %5d\n",
|
---|
[25822] | 268 | inet_ntoa(so->so_faddr), RT_N2H_U16(so->so_fport),
|
---|
[30045] | 269 | SBUF_LEN(&so->so_rcv), SBUF_LEN(&so->so_snd));
|
---|
[16443] | 270 | LOOP_LABEL(tcp, so, so_next);
|
---|
[15054] | 271 | }
|
---|
[1033] | 272 |
|
---|
[16443] | 273 | QSOCKET_FOREACH(so, so_next, udp)
|
---|
| 274 | /* { */
|
---|
[22873] | 275 | n = RTStrPrintf(buff, sizeof(buff), "udp[%d sec]", (so->so_expire - curtime) / 1000);
|
---|
[15054] | 276 | while (n < 17)
|
---|
| 277 | buff[n++] = ' ';
|
---|
| 278 | buff[17] = 0;
|
---|
| 279 | lprint("%s %3d %15s %5d ",
|
---|
[25822] | 280 | buff, so->s, inet_ntoa(so->so_laddr), RT_N2H_U16(so->so_lport));
|
---|
[15054] | 281 | lprint("%15s %5d %5d %5d\n",
|
---|
[25822] | 282 | inet_ntoa(so->so_faddr), RT_N2H_U16(so->so_fport),
|
---|
[30045] | 283 | SBUF_LEN(&so->so_rcv), SBUF_LEN(&so->so_snd));
|
---|
[16443] | 284 | LOOP_LABEL(udp, so, so_next);
|
---|
[15054] | 285 | }
|
---|
| 286 | }
|
---|
| 287 | #endif
|
---|
[1033] | 288 |
|
---|
[15090] | 289 | static DECLCALLBACK(size_t)
|
---|
[39286] | 290 | printSocket(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
|
---|
[15238] | 291 | const char *pszType, void const *pvValue,
|
---|
| 292 | int cchWidth, int cchPrecision, unsigned fFlags,
|
---|
| 293 | void *pvUser)
|
---|
| 294 | {
|
---|
| 295 | struct socket *so = (struct socket*)pvValue;
|
---|
[39360] | 296 | PNATState pData = (PNATState)pvUser;
|
---|
[51329] | 297 | size_t cb = 0;
|
---|
| 298 |
|
---|
[39085] | 299 | NOREF(cchWidth);
|
---|
| 300 | NOREF(cchPrecision);
|
---|
| 301 | NOREF(fFlags);
|
---|
[39360] | 302 | Assert(pData);
|
---|
[15238] | 303 |
|
---|
| 304 | AssertReturn(strcmp(pszType, "natsock") == 0, 0);
|
---|
[51329] | 305 |
|
---|
[23369] | 306 | if (so == NULL)
|
---|
| 307 | return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
|
---|
[19802] | 308 | "socket is null");
|
---|
[39285] | 309 | if (so->s == -1)
|
---|
[23369] | 310 | return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
|
---|
[39285] | 311 | "socket(%d)", so->s);
|
---|
[34209] | 312 |
|
---|
[51329] | 313 | cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
|
---|
[57783] | 314 | "socket %d", so->s);
|
---|
| 315 |
|
---|
| 316 | if (so->so_type == IPPROTO_TCP)
|
---|
| 317 | cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
|
---|
| 318 | " (tcp)");
|
---|
| 319 | else if (so->so_type == IPPROTO_UDP)
|
---|
| 320 | cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
|
---|
| 321 | " (udp)");
|
---|
| 322 | else
|
---|
| 323 | cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
|
---|
| 324 | " (proto %u)", so->so_type);
|
---|
| 325 |
|
---|
| 326 | cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
|
---|
| 327 | " exp. in %d"
|
---|
[51329] | 328 | " state=%R[natsockstate]"
|
---|
[55855] | 329 | "%s" /* fUnderPolling */
|
---|
| 330 | "%s" /* fShouldBeRemoved */
|
---|
[51329] | 331 | " f_(addr:port)=%RTnaipv4:%d"
|
---|
| 332 | " l_(addr:port)=%RTnaipv4:%d",
|
---|
[39363] | 333 | so->so_expire ? so->so_expire - curtime : 0,
|
---|
[39360] | 334 | so->so_state,
|
---|
[55855] | 335 | so->fUnderPolling ? " fUnderPolling" : "",
|
---|
| 336 | so->fShouldBeRemoved ? " fShouldBeRemoved" : "",
|
---|
[37741] | 337 | so->so_faddr.s_addr,
|
---|
[25822] | 338 | RT_N2H_U16(so->so_fport),
|
---|
[37741] | 339 | so->so_laddr.s_addr,
|
---|
[51329] | 340 | RT_N2H_U16(so->so_lport));
|
---|
| 341 |
|
---|
| 342 | if (so->s != -1)
|
---|
| 343 | {
|
---|
| 344 | struct sockaddr addr;
|
---|
| 345 | socklen_t socklen;
|
---|
| 346 | int status;
|
---|
| 347 |
|
---|
| 348 | socklen = sizeof(addr);
|
---|
| 349 | status = getsockname(so->s, &addr, &socklen);
|
---|
| 350 |
|
---|
| 351 | if (status != 0)
|
---|
| 352 | {
|
---|
| 353 | cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
|
---|
| 354 | " (getsockname failed)");
|
---|
| 355 | }
|
---|
| 356 | else if (addr.sa_family != AF_INET)
|
---|
| 357 | {
|
---|
| 358 | cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
|
---|
| 359 | " (unexpected address family %d)",
|
---|
| 360 | addr.sa_family);
|
---|
| 361 | }
|
---|
| 362 | else
|
---|
| 363 | {
|
---|
| 364 | struct sockaddr_in *in_addr = (struct sockaddr_in *)&addr;
|
---|
| 365 | cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
|
---|
| 366 | " name=%RTnaipv4:%d",
|
---|
| 367 | in_addr->sin_addr.s_addr,
|
---|
| 368 | RT_N2H_U16(in_addr->sin_port));
|
---|
| 369 | }
|
---|
| 370 | }
|
---|
| 371 | return cb;
|
---|
[15238] | 372 | }
|
---|
| 373 |
|
---|
[39286] | 374 | static DECLCALLBACK(size_t)
|
---|
| 375 | printNATSocketState(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
|
---|
| 376 | const char *pszType, void const *pvValue,
|
---|
| 377 | int cchWidth, int cchPrecision, unsigned fFlags,
|
---|
| 378 | void *pvUser)
|
---|
| 379 | {
|
---|
| 380 | uint32_t u32SocketState = (uint32_t)(uintptr_t)pvValue;
|
---|
| 381 | int idxNATState = 0;
|
---|
[39430] | 382 | bool fFirst = true;
|
---|
| 383 | size_t cbReturn = 0;
|
---|
[39286] | 384 | NOREF(cchWidth);
|
---|
| 385 | NOREF(cchPrecision);
|
---|
| 386 | NOREF(fFlags);
|
---|
| 387 | NOREF(pvUser);
|
---|
| 388 | AssertReturn(strcmp(pszType, "natsockstate") == 0, 0);
|
---|
| 389 |
|
---|
| 390 | for (idxNATState = 0; idxNATState < RT_ELEMENTS(g_apszSocketStates); ++idxNATState)
|
---|
| 391 | {
|
---|
[39430] | 392 | if (u32SocketState & g_apszSocketStates[idxNATState].u32SocketState)
|
---|
| 393 | {
|
---|
| 394 | if (fFirst)
|
---|
| 395 | {
|
---|
| 396 | cbReturn += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, g_apszSocketStates[idxNATState].pcszSocketStateName);
|
---|
| 397 | fFirst = false;
|
---|
| 398 | }
|
---|
| 399 | else
|
---|
| 400 | cbReturn += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "|%s", g_apszSocketStates[idxNATState].pcszSocketStateName);
|
---|
| 401 | }
|
---|
[39286] | 402 | }
|
---|
[39430] | 403 |
|
---|
| 404 | if (!cbReturn)
|
---|
| 405 | return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "[unknown state %RX32]", u32SocketState);
|
---|
| 406 |
|
---|
| 407 | return cbReturn;
|
---|
[39286] | 408 | }
|
---|
| 409 |
|
---|
[34305] | 410 | /**
|
---|
| 411 | * Print callback dumping TCP Control Block in terms of RFC 793.
|
---|
| 412 | */
|
---|
[15244] | 413 | static DECLCALLBACK(size_t)
|
---|
[35351] | 414 | printTcpcbRfc793(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
|
---|
[34305] | 415 | const char *pszType, void const *pvValue,
|
---|
| 416 | int cchWidth, int cchPrecision, unsigned fFlags,
|
---|
| 417 | void *pvUser)
|
---|
| 418 | {
|
---|
| 419 | size_t cb = 0;
|
---|
[35351] | 420 | const struct tcpcb *tp = (const struct tcpcb *)pvValue;
|
---|
[39101] | 421 | NOREF(cchWidth);
|
---|
| 422 | NOREF(cchPrecision);
|
---|
| 423 | NOREF(fFlags);
|
---|
| 424 | NOREF(pvUser);
|
---|
[37938] | 425 | AssertReturn(RTStrCmp(pszType, "tcpcb793") == 0, 0);
|
---|
[37937] | 426 | if (tp)
|
---|
| 427 | {
|
---|
| 428 | cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "TCB793[ state:%R[tcpstate] SND(UNA: %x, NXT: %x, UP: %x, WND: %x, WL1:%x, WL2:%x, ISS:%x), ",
|
---|
| 429 | tp->t_state, tp->snd_una, tp->snd_nxt, tp->snd_up, tp->snd_wnd, tp->snd_wl1, tp->snd_wl2, tp->iss);
|
---|
| 430 | cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "RCV(WND: %x, NXT: %x, UP: %x, IRS:%x)]", tp->rcv_wnd, tp->rcv_nxt, tp->rcv_up, tp->irs);
|
---|
| 431 | }
|
---|
| 432 | else
|
---|
| 433 | {
|
---|
| 434 | cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "TCB793[ NULL ]");
|
---|
| 435 | }
|
---|
[34305] | 436 | return cb;
|
---|
| 437 | }
|
---|
| 438 | /*
|
---|
| 439 | * Prints TCP segment in terms of RFC 793.
|
---|
| 440 | */
|
---|
| 441 | static DECLCALLBACK(size_t)
|
---|
[35351] | 442 | printTcpSegmentRfc793(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
|
---|
[34305] | 443 | const char *pszType, void const *pvValue,
|
---|
| 444 | int cchWidth, int cchPrecision, unsigned fFlags,
|
---|
| 445 | void *pvUser)
|
---|
| 446 | {
|
---|
| 447 | size_t cb = 0;
|
---|
[35351] | 448 | const struct tcpiphdr *ti = (const struct tcpiphdr *)pvValue;
|
---|
[39101] | 449 | NOREF(cchWidth);
|
---|
| 450 | NOREF(cchPrecision);
|
---|
| 451 | NOREF(fFlags);
|
---|
| 452 | NOREF(pvUser);
|
---|
[34305] | 453 | AssertReturn(RTStrCmp(pszType, "tcpseg793") == 0 && ti, 0);
|
---|
[35351] | 454 | cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "SEG[ACK: %x, SEQ: %x, LEN: %x, WND: %x, UP: %x]",
|
---|
[34305] | 455 | ti->ti_ack, ti->ti_seq, ti->ti_len, ti->ti_win, ti->ti_urp);
|
---|
| 456 | return cb;
|
---|
| 457 | }
|
---|
| 458 |
|
---|
[37738] | 459 | /*
|
---|
| 460 | * Prints TCP state
|
---|
| 461 | */
|
---|
[34305] | 462 | static DECLCALLBACK(size_t)
|
---|
[37738] | 463 | printTcpState(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
|
---|
| 464 | const char *pszType, void const *pvValue,
|
---|
| 465 | int cchWidth, int cchPrecision, unsigned fFlags,
|
---|
| 466 | void *pvUser)
|
---|
| 467 | {
|
---|
| 468 | size_t cb = 0;
|
---|
[37742] | 469 | const int idxTcpState = (int)(uintptr_t)pvValue;
|
---|
[37738] | 470 | char *pszTcpStateName = (idxTcpState >= 0 && idxTcpState < TCP_NSTATES) ? g_apszTcpStates[idxTcpState] : "TCPS_INVALIDE_STATE";
|
---|
[39101] | 471 | NOREF(cchWidth);
|
---|
| 472 | NOREF(cchPrecision);
|
---|
| 473 | NOREF(fFlags);
|
---|
| 474 | NOREF(pvUser);
|
---|
[37738] | 475 | AssertReturn(RTStrCmp(pszType, "tcpstate") == 0, 0);
|
---|
| 476 | cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%s", pszTcpStateName);
|
---|
| 477 | return cb;
|
---|
| 478 | }
|
---|
| 479 |
|
---|
[37744] | 480 | /*
|
---|
[41228] | 481 | * Prints TCP flags
|
---|
| 482 | */
|
---|
| 483 | static DECLCALLBACK(size_t)
|
---|
| 484 | printTcpFlags(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
|
---|
| 485 | const char *pszType, void const *pvValue,
|
---|
| 486 | int cchWidth, int cchPrecision, unsigned fFlags,
|
---|
| 487 | void *pvUser)
|
---|
| 488 | {
|
---|
| 489 | size_t cbPrint = 0;
|
---|
| 490 | uint32_t u32TcpFlags = (uint32_t)(uintptr_t)pvValue;
|
---|
| 491 | bool fSingleValue = true;
|
---|
| 492 | int idxTcpFlags = 0;
|
---|
| 493 | NOREF(cchWidth);
|
---|
| 494 | NOREF(cchPrecision);
|
---|
| 495 | NOREF(fFlags);
|
---|
| 496 | NOREF(pvUser);
|
---|
| 497 | AssertReturn(RTStrCmp(pszType, "tcpflags") == 0, 0);
|
---|
| 498 | cbPrint += RTStrFormat(pfnOutput,
|
---|
| 499 | pvArgOutput,
|
---|
| 500 | NULL,
|
---|
| 501 | 0,
|
---|
| 502 | "tcpflags: %RX8 [", (uint8_t)u32TcpFlags);
|
---|
| 503 | for (idxTcpFlags = 0; idxTcpFlags < RT_ELEMENTS(g_aTcpFlags); ++idxTcpFlags)
|
---|
| 504 | {
|
---|
| 505 | if (u32TcpFlags & g_aTcpFlags[idxTcpFlags].u32SocketState)
|
---|
| 506 | {
|
---|
| 507 | cbPrint += RTStrFormat(pfnOutput,
|
---|
| 508 | pvArgOutput,
|
---|
| 509 | NULL,
|
---|
| 510 | 0,
|
---|
| 511 | fSingleValue ? "%s(%RX8)" : "|%s(%RX8)",
|
---|
| 512 | g_aTcpFlags[idxTcpFlags].pcszSocketStateName,
|
---|
| 513 | (uint8_t)g_aTcpFlags[idxTcpFlags].u32SocketState);
|
---|
| 514 | fSingleValue = false;
|
---|
| 515 | }
|
---|
| 516 | }
|
---|
| 517 | cbPrint += RTStrFormat(pfnOutput,
|
---|
| 518 | pvArgOutput,
|
---|
| 519 | NULL,
|
---|
| 520 | 0,
|
---|
| 521 | "]");
|
---|
| 522 | return cbPrint;
|
---|
| 523 | }
|
---|
| 524 |
|
---|
| 525 | /*
|
---|
[37744] | 526 | * Prints sbuf state
|
---|
| 527 | */
|
---|
[37738] | 528 | static DECLCALLBACK(size_t)
|
---|
[37744] | 529 | printSbuf(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
|
---|
| 530 | const char *pszType, void const *pvValue,
|
---|
| 531 | int cchWidth, int cchPrecision, unsigned fFlags,
|
---|
| 532 | void *pvUser)
|
---|
| 533 | {
|
---|
| 534 | size_t cb = 0;
|
---|
| 535 | const struct sbuf *sb = (struct sbuf *)pvValue;
|
---|
[39101] | 536 | NOREF(cchWidth);
|
---|
| 537 | NOREF(cchPrecision);
|
---|
| 538 | NOREF(fFlags);
|
---|
| 539 | NOREF(pvUser);
|
---|
[37744] | 540 | AssertReturn(RTStrCmp(pszType, "sbuf") == 0, 0);
|
---|
| 541 | cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "[sbuf:%p cc:%d, datalen:%d, wprt:%p, rptr:%p data:%p]",
|
---|
| 542 | sb, sb->sb_cc, sb->sb_datalen, sb->sb_wptr, sb->sb_rptr, sb->sb_data);
|
---|
| 543 | return cb;
|
---|
| 544 | }
|
---|
| 545 |
|
---|
[38492] | 546 | /*
|
---|
| 547 | * Prints zone state
|
---|
| 548 | */
|
---|
[37744] | 549 | static DECLCALLBACK(size_t)
|
---|
[38492] | 550 | printMbufZone(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
|
---|
| 551 | const char *pszType, void const *pvValue,
|
---|
| 552 | int cchWidth, int cchPrecision, unsigned fFlags,
|
---|
| 553 | void *pvUser)
|
---|
| 554 | {
|
---|
| 555 | size_t cb = 0;
|
---|
| 556 | const uma_zone_t zone = (const uma_zone_t)pvValue;
|
---|
[39101] | 557 | NOREF(cchWidth);
|
---|
| 558 | NOREF(cchPrecision);
|
---|
| 559 | NOREF(fFlags);
|
---|
| 560 | NOREF(pvUser);
|
---|
[38492] | 561 | AssertReturn(RTStrCmp(pszType, "mzone") == 0, 0);
|
---|
| 562 | if (!zone)
|
---|
| 563 | cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "[zone:NULL]");
|
---|
| 564 | else
|
---|
| 565 | cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "[zone:%p name:%s, master_zone:%R[mzone]]",
|
---|
| 566 | zone, zone->name, zone->master_zone);
|
---|
| 567 | return cb;
|
---|
| 568 | }
|
---|
| 569 |
|
---|
| 570 | /*
|
---|
| 571 | * Prints zone's item state
|
---|
| 572 | */
|
---|
| 573 | static DECLCALLBACK(size_t)
|
---|
| 574 | printMbufZoneItem(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
|
---|
| 575 | const char *pszType, void const *pvValue,
|
---|
| 576 | int cchWidth, int cchPrecision, unsigned fFlags,
|
---|
| 577 | void *pvUser)
|
---|
| 578 | {
|
---|
| 579 | size_t cb = 0;
|
---|
| 580 | const struct item *it = (const struct item *)pvValue;
|
---|
[39101] | 581 | NOREF(cchWidth);
|
---|
| 582 | NOREF(cchPrecision);
|
---|
| 583 | NOREF(fFlags);
|
---|
| 584 | NOREF(pvUser);
|
---|
[38492] | 585 | AssertReturn(RTStrCmp(pszType, "mzoneitem") == 0, 0);
|
---|
| 586 | if (!it)
|
---|
| 587 | cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "[item:NULL]");
|
---|
| 588 | else
|
---|
| 589 | cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "[iptem:%p ref_count:%d, zone:%R[mzone]]",
|
---|
| 590 | it, it->ref_count, it->zone);
|
---|
| 591 | return cb;
|
---|
| 592 | }
|
---|
| 593 |
|
---|
| 594 | static DECLCALLBACK(size_t)
|
---|
[15244] | 595 | print_networkevents(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
|
---|
| 596 | const char *pszType, void const *pvValue,
|
---|
| 597 | int cchWidth, int cchPrecision, unsigned fFlags,
|
---|
| 598 | void *pvUser)
|
---|
| 599 | {
|
---|
[15245] | 600 | size_t cb = 0;
|
---|
[39102] | 601 | #ifdef RT_OS_WINDOWS
|
---|
| 602 | WSANETWORKEVENTS *pNetworkEvents = (WSANETWORKEVENTS*)pvValue;
|
---|
| 603 | bool fDelim = false;
|
---|
| 604 | #endif
|
---|
| 605 |
|
---|
[39101] | 606 | NOREF(cchWidth);
|
---|
| 607 | NOREF(cchPrecision);
|
---|
| 608 | NOREF(fFlags);
|
---|
| 609 | NOREF(pvUser);
|
---|
[39102] | 610 |
|
---|
[15245] | 611 | #ifdef RT_OS_WINDOWS
|
---|
[15244] | 612 | AssertReturn(strcmp(pszType, "natwinnetevents") == 0, 0);
|
---|
| 613 |
|
---|
[23369] | 614 | cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "events=%02x (",
|
---|
[19802] | 615 | pNetworkEvents->lNetworkEvents);
|
---|
[15245] | 616 | # define DO_BIT(bit) \
|
---|
[19802] | 617 | if (pNetworkEvents->lNetworkEvents & FD_ ## bit) \
|
---|
| 618 | { \
|
---|
| 619 | cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, \
|
---|
| 620 | "%s" #bit "(%d)", fDelim ? "," : "", \
|
---|
| 621 | pNetworkEvents->iErrorCode[FD_ ## bit ## _BIT]); \
|
---|
| 622 | fDelim = true; \
|
---|
[15244] | 623 | }
|
---|
| 624 | DO_BIT(READ);
|
---|
| 625 | DO_BIT(WRITE);
|
---|
| 626 | DO_BIT(OOB);
|
---|
| 627 | DO_BIT(ACCEPT);
|
---|
| 628 | DO_BIT(CONNECT);
|
---|
| 629 | DO_BIT(CLOSE);
|
---|
| 630 | DO_BIT(QOS);
|
---|
[15245] | 631 | # undef DO_BIT
|
---|
[15244] | 632 | cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, ")");
|
---|
[39101] | 633 | #else
|
---|
| 634 | NOREF(pfnOutput);
|
---|
| 635 | NOREF(pvArgOutput);
|
---|
| 636 | NOREF(pszType);
|
---|
| 637 | NOREF(pvValue);
|
---|
[15245] | 638 | #endif
|
---|
[15244] | 639 | return cb;
|
---|
| 640 | }
|
---|
| 641 |
|
---|
| 642 | #if 0
|
---|
| 643 | /*
|
---|
| 644 | * Debugging
|
---|
| 645 | */
|
---|
| 646 | int errno_func(const char *file, int line)
|
---|
| 647 | {
|
---|
| 648 | int err = WSAGetLastError();
|
---|
| 649 | LogRel(("errno=%d (%s:%d)\n", err, file, line));
|
---|
| 650 | return err;
|
---|
| 651 | }
|
---|
| 652 | #endif
|
---|
| 653 |
|
---|
[15287] | 654 | int
|
---|
[39360] | 655 | debug_init(PNATState pData)
|
---|
[14882] | 656 | {
|
---|
[15054] | 657 | int rc = VINF_SUCCESS;
|
---|
| 658 |
|
---|
[15116] | 659 | static int g_fFormatRegistered;
|
---|
[15054] | 660 |
|
---|
[15116] | 661 | if (!g_fFormatRegistered)
|
---|
| 662 | {
|
---|
[36410] | 663 |
|
---|
[39360] | 664 | rc = RTStrFormatTypeRegister("natsock", printSocket, pData); AssertRC(rc);
|
---|
[39286] | 665 | rc = RTStrFormatTypeRegister("natsockstate", printNATSocketState, NULL); AssertRC(rc);
|
---|
[23369] | 666 | rc = RTStrFormatTypeRegister("natwinnetevents",
|
---|
[38492] | 667 | print_networkevents, NULL); AssertRC(rc);
|
---|
| 668 | rc = RTStrFormatTypeRegister("tcpcb793", printTcpcbRfc793, NULL); AssertRC(rc);
|
---|
| 669 | rc = RTStrFormatTypeRegister("tcpseg793", printTcpSegmentRfc793, NULL); AssertRC(rc);
|
---|
| 670 | rc = RTStrFormatTypeRegister("tcpstate", printTcpState, NULL); AssertRC(rc);
|
---|
[41228] | 671 | rc = RTStrFormatTypeRegister("tcpflags", printTcpFlags, NULL); AssertRC(rc);
|
---|
[38492] | 672 | rc = RTStrFormatTypeRegister("sbuf", printSbuf, NULL); AssertRC(rc);
|
---|
| 673 | rc = RTStrFormatTypeRegister("mzone", printMbufZone, NULL); AssertRC(rc);
|
---|
| 674 | rc = RTStrFormatTypeRegister("mzoneitem", printMbufZoneItem, NULL); AssertRC(rc);
|
---|
[15117] | 675 | g_fFormatRegistered = 1;
|
---|
[15116] | 676 | }
|
---|
| 677 |
|
---|
[15054] | 678 | return rc;
|
---|
[14882] | 679 | }
|
---|