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