1 | /* $Id: debug.c 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * NAT - debug helpers.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
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
|
---|
26 | */
|
---|
27 |
|
---|
28 |
|
---|
29 | /*
|
---|
30 | * This code is based on:
|
---|
31 | *
|
---|
32 | * Copyright (c) 1995 Danny Gasparovski.
|
---|
33 | * Portions copyright (c) 2000 Kelly Price.
|
---|
34 | *
|
---|
35 | * Please read the file COPYRIGHT for the
|
---|
36 | * terms and conditions of the copyright.
|
---|
37 | */
|
---|
38 |
|
---|
39 | #include <slirp.h>
|
---|
40 | #include <iprt/string.h>
|
---|
41 | #include <iprt/stream.h>
|
---|
42 | #include <iprt/critsect.h>
|
---|
43 | #include "zone.h"
|
---|
44 |
|
---|
45 | #ifdef DEBUG
|
---|
46 | void dump_packet(void *, int);
|
---|
47 | #endif
|
---|
48 |
|
---|
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 |
|
---|
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 |
|
---|
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 |
|
---|
98 | /*
|
---|
99 | * Dump a packet in the same format as tcpdump -x
|
---|
100 | */
|
---|
101 | #ifdef DEBUG
|
---|
102 | void
|
---|
103 | dump_packet(void *dat, int n)
|
---|
104 | {
|
---|
105 | Log(("nat: PACKET DUMPED:\n%.*Rhxd\n", n, dat));
|
---|
106 | }
|
---|
107 | #endif
|
---|
108 |
|
---|
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 | }
|
---|
118 |
|
---|
119 | void
|
---|
120 | ipstats(PNATState pData)
|
---|
121 | {
|
---|
122 | lprint("\n");
|
---|
123 |
|
---|
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);
|
---|
141 | }
|
---|
142 |
|
---|
143 | void
|
---|
144 | tcpstats(PNATState pData)
|
---|
145 | {
|
---|
146 | lprint("\n");
|
---|
147 |
|
---|
148 | lprint("TCP stats:\n");
|
---|
149 |
|
---|
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);
|
---|
162 |
|
---|
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);
|
---|
172 |
|
---|
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);
|
---|
185 |
|
---|
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);
|
---|
204 |
|
---|
205 | /* lprint(" Packets received too short: %d\n", tcpstat.tcps_rcvshort); */
|
---|
206 | /* lprint(" Segments dropped due to PAWS: %d\n", tcpstat.tcps_pawsdrop); */
|
---|
207 |
|
---|
208 | }
|
---|
209 |
|
---|
210 | void
|
---|
211 | udpstats(PNATState pData)
|
---|
212 | {
|
---|
213 | lprint("\n");
|
---|
214 |
|
---|
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);
|
---|
222 | }
|
---|
223 |
|
---|
224 | void
|
---|
225 | icmpstats(PNATState pData)
|
---|
226 | {
|
---|
227 | lprint("\n");
|
---|
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);
|
---|
235 | }
|
---|
236 |
|
---|
237 | void
|
---|
238 | mbufstats(PNATState pData)
|
---|
239 | {
|
---|
240 | /*
|
---|
241 | * (vvl) this static code can't work with mbuf zone anymore
|
---|
242 | * @todo: make statistic correct
|
---|
243 | */
|
---|
244 | NOREF(pData);
|
---|
245 | }
|
---|
246 |
|
---|
247 | void
|
---|
248 | sockstats(PNATState pData)
|
---|
249 | {
|
---|
250 | char buff[256];
|
---|
251 | size_t n;
|
---|
252 | struct socket *so, *so_next;
|
---|
253 |
|
---|
254 | lprint("\n");
|
---|
255 |
|
---|
256 | lprint(
|
---|
257 | "Proto[state] Sock Local Address, Port Remote Address, Port RecvQ SendQ\n");
|
---|
258 |
|
---|
259 | QSOCKET_FOREACH(so, so_next, tcp)
|
---|
260 | /* { */
|
---|
261 | n = RTStrPrintf(buff, sizeof(buff), "tcp[%s]", so->so_tcpcb?tcpstates[so->so_tcpcb->t_state]:"NONE");
|
---|
262 | while (n < 17)
|
---|
263 | buff[n++] = ' ';
|
---|
264 | buff[17] = 0;
|
---|
265 | lprint("%s %3d %15s %5d ",
|
---|
266 | buff, so->s, inet_ntoa(so->so_laddr), RT_N2H_U16(so->so_lport));
|
---|
267 | lprint("%15s %5d %5d %5d\n",
|
---|
268 | inet_ntoa(so->so_faddr), RT_N2H_U16(so->so_fport),
|
---|
269 | SBUF_LEN(&so->so_rcv), SBUF_LEN(&so->so_snd));
|
---|
270 | LOOP_LABEL(tcp, so, so_next);
|
---|
271 | }
|
---|
272 |
|
---|
273 | QSOCKET_FOREACH(so, so_next, udp)
|
---|
274 | /* { */
|
---|
275 | n = RTStrPrintf(buff, sizeof(buff), "udp[%d sec]", (so->so_expire - curtime) / 1000);
|
---|
276 | while (n < 17)
|
---|
277 | buff[n++] = ' ';
|
---|
278 | buff[17] = 0;
|
---|
279 | lprint("%s %3d %15s %5d ",
|
---|
280 | buff, so->s, inet_ntoa(so->so_laddr), RT_N2H_U16(so->so_lport));
|
---|
281 | lprint("%15s %5d %5d %5d\n",
|
---|
282 | inet_ntoa(so->so_faddr), RT_N2H_U16(so->so_fport),
|
---|
283 | SBUF_LEN(&so->so_rcv), SBUF_LEN(&so->so_snd));
|
---|
284 | LOOP_LABEL(udp, so, so_next);
|
---|
285 | }
|
---|
286 | }
|
---|
287 | #endif
|
---|
288 |
|
---|
289 | static DECLCALLBACK(size_t)
|
---|
290 | printSocket(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
|
---|
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;
|
---|
296 | PNATState pData = (PNATState)pvUser;
|
---|
297 | size_t cb = 0;
|
---|
298 |
|
---|
299 | NOREF(cchWidth);
|
---|
300 | NOREF(cchPrecision);
|
---|
301 | NOREF(fFlags);
|
---|
302 | Assert(pData);
|
---|
303 |
|
---|
304 | AssertReturn(strcmp(pszType, "natsock") == 0, 0);
|
---|
305 |
|
---|
306 | if (so == NULL)
|
---|
307 | return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
|
---|
308 | "socket is null");
|
---|
309 | if (so->s == -1)
|
---|
310 | return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
|
---|
311 | "socket(%d)", so->s);
|
---|
312 |
|
---|
313 | cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
|
---|
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"
|
---|
328 | " state=%R[natsockstate]"
|
---|
329 | "%s" /* fUnderPolling */
|
---|
330 | "%s" /* fShouldBeRemoved */
|
---|
331 | " f_(addr:port)=%RTnaipv4:%d"
|
---|
332 | " l_(addr:port)=%RTnaipv4:%d",
|
---|
333 | so->so_expire ? so->so_expire - curtime : 0,
|
---|
334 | so->so_state,
|
---|
335 | so->fUnderPolling ? " fUnderPolling" : "",
|
---|
336 | so->fShouldBeRemoved ? " fShouldBeRemoved" : "",
|
---|
337 | so->so_faddr.s_addr,
|
---|
338 | RT_N2H_U16(so->so_fport),
|
---|
339 | so->so_laddr.s_addr,
|
---|
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;
|
---|
372 | }
|
---|
373 |
|
---|
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;
|
---|
382 | bool fFirst = true;
|
---|
383 | size_t cbReturn = 0;
|
---|
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 | {
|
---|
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 | }
|
---|
402 | }
|
---|
403 |
|
---|
404 | if (!cbReturn)
|
---|
405 | return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "[unknown state %RX32]", u32SocketState);
|
---|
406 |
|
---|
407 | return cbReturn;
|
---|
408 | }
|
---|
409 |
|
---|
410 | /**
|
---|
411 | * Print callback dumping TCP Control Block in terms of RFC 793.
|
---|
412 | */
|
---|
413 | static DECLCALLBACK(size_t)
|
---|
414 | printTcpcbRfc793(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
|
---|
415 | const char *pszType, void const *pvValue,
|
---|
416 | int cchWidth, int cchPrecision, unsigned fFlags,
|
---|
417 | void *pvUser)
|
---|
418 | {
|
---|
419 | size_t cb = 0;
|
---|
420 | const struct tcpcb *tp = (const struct tcpcb *)pvValue;
|
---|
421 | NOREF(cchWidth);
|
---|
422 | NOREF(cchPrecision);
|
---|
423 | NOREF(fFlags);
|
---|
424 | NOREF(pvUser);
|
---|
425 | AssertReturn(RTStrCmp(pszType, "tcpcb793") == 0, 0);
|
---|
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 | }
|
---|
436 | return cb;
|
---|
437 | }
|
---|
438 | /*
|
---|
439 | * Prints TCP segment in terms of RFC 793.
|
---|
440 | */
|
---|
441 | static DECLCALLBACK(size_t)
|
---|
442 | printTcpSegmentRfc793(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
|
---|
443 | const char *pszType, void const *pvValue,
|
---|
444 | int cchWidth, int cchPrecision, unsigned fFlags,
|
---|
445 | void *pvUser)
|
---|
446 | {
|
---|
447 | size_t cb = 0;
|
---|
448 | const struct tcpiphdr *ti = (const struct tcpiphdr *)pvValue;
|
---|
449 | NOREF(cchWidth);
|
---|
450 | NOREF(cchPrecision);
|
---|
451 | NOREF(fFlags);
|
---|
452 | NOREF(pvUser);
|
---|
453 | AssertReturn(RTStrCmp(pszType, "tcpseg793") == 0 && ti, 0);
|
---|
454 | cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "SEG[ACK: %x, SEQ: %x, LEN: %x, WND: %x, UP: %x]",
|
---|
455 | ti->ti_ack, ti->ti_seq, ti->ti_len, ti->ti_win, ti->ti_urp);
|
---|
456 | return cb;
|
---|
457 | }
|
---|
458 |
|
---|
459 | /*
|
---|
460 | * Prints TCP state
|
---|
461 | */
|
---|
462 | static DECLCALLBACK(size_t)
|
---|
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;
|
---|
469 | const int idxTcpState = (int)(uintptr_t)pvValue;
|
---|
470 | char *pszTcpStateName = (idxTcpState >= 0 && idxTcpState < TCP_NSTATES) ? g_apszTcpStates[idxTcpState] : "TCPS_INVALIDE_STATE";
|
---|
471 | NOREF(cchWidth);
|
---|
472 | NOREF(cchPrecision);
|
---|
473 | NOREF(fFlags);
|
---|
474 | NOREF(pvUser);
|
---|
475 | AssertReturn(RTStrCmp(pszType, "tcpstate") == 0, 0);
|
---|
476 | cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%s", pszTcpStateName);
|
---|
477 | return cb;
|
---|
478 | }
|
---|
479 |
|
---|
480 | /*
|
---|
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 | /*
|
---|
526 | * Prints sbuf state
|
---|
527 | */
|
---|
528 | static DECLCALLBACK(size_t)
|
---|
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;
|
---|
536 | NOREF(cchWidth);
|
---|
537 | NOREF(cchPrecision);
|
---|
538 | NOREF(fFlags);
|
---|
539 | NOREF(pvUser);
|
---|
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 |
|
---|
546 | /*
|
---|
547 | * Prints zone state
|
---|
548 | */
|
---|
549 | static DECLCALLBACK(size_t)
|
---|
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;
|
---|
557 | NOREF(cchWidth);
|
---|
558 | NOREF(cchPrecision);
|
---|
559 | NOREF(fFlags);
|
---|
560 | NOREF(pvUser);
|
---|
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;
|
---|
581 | NOREF(cchWidth);
|
---|
582 | NOREF(cchPrecision);
|
---|
583 | NOREF(fFlags);
|
---|
584 | NOREF(pvUser);
|
---|
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)
|
---|
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 | {
|
---|
600 | size_t cb = 0;
|
---|
601 | #ifdef RT_OS_WINDOWS
|
---|
602 | WSANETWORKEVENTS *pNetworkEvents = (WSANETWORKEVENTS*)pvValue;
|
---|
603 | bool fDelim = false;
|
---|
604 | #endif
|
---|
605 |
|
---|
606 | NOREF(cchWidth);
|
---|
607 | NOREF(cchPrecision);
|
---|
608 | NOREF(fFlags);
|
---|
609 | NOREF(pvUser);
|
---|
610 |
|
---|
611 | #ifdef RT_OS_WINDOWS
|
---|
612 | AssertReturn(strcmp(pszType, "natwinnetevents") == 0, 0);
|
---|
613 |
|
---|
614 | cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "events=%02x (",
|
---|
615 | pNetworkEvents->lNetworkEvents);
|
---|
616 | # define DO_BIT(bit) \
|
---|
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; \
|
---|
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);
|
---|
631 | # undef DO_BIT
|
---|
632 | cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, ")");
|
---|
633 | #else
|
---|
634 | NOREF(pfnOutput);
|
---|
635 | NOREF(pvArgOutput);
|
---|
636 | NOREF(pszType);
|
---|
637 | NOREF(pvValue);
|
---|
638 | #endif
|
---|
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 |
|
---|
654 | int
|
---|
655 | debug_init(PNATState pData)
|
---|
656 | {
|
---|
657 | int rc = VINF_SUCCESS;
|
---|
658 |
|
---|
659 | static int g_fFormatRegistered;
|
---|
660 |
|
---|
661 | if (!g_fFormatRegistered)
|
---|
662 | {
|
---|
663 |
|
---|
664 | rc = RTStrFormatTypeRegister("natsock", printSocket, pData); AssertRC(rc);
|
---|
665 | rc = RTStrFormatTypeRegister("natsockstate", printNATSocketState, NULL); AssertRC(rc);
|
---|
666 | rc = RTStrFormatTypeRegister("natwinnetevents",
|
---|
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);
|
---|
671 | rc = RTStrFormatTypeRegister("tcpflags", printTcpFlags, NULL); AssertRC(rc);
|
---|
672 | rc = RTStrFormatTypeRegister("sbuf", printSbuf, NULL); AssertRC(rc);
|
---|
673 | rc = RTStrFormatTypeRegister("mzone", printMbufZone, NULL); AssertRC(rc);
|
---|
674 | rc = RTStrFormatTypeRegister("mzoneitem", printMbufZoneItem, NULL); AssertRC(rc);
|
---|
675 | g_fFormatRegistered = 1;
|
---|
676 | }
|
---|
677 |
|
---|
678 | return rc;
|
---|
679 | }
|
---|