1 /*
2  * Copyright (c) 1995 Danny Gasparovski.
3  * Portions copyright (c) 2000 Kelly Price.
4  *
5  * Please read the file COPYRIGHT for the
6  * terms and conditions of the copyright.
7  */
8 
9 #include "slirp.h"
10 
11 FILE *dfd = NULL;
12 #ifdef SLIRP_DEBUG
13 int dostats = 1;
14 #else
15 int dostats = 0;
16 #endif
17 int slirp_debug = 0;
18 
19 extern char *strerror _P((int));
20 
21 /* Carry over one item from main.c so that the tty's restored.
22  * Only done when the tty being used is /dev/tty --RedWolf */
23 extern struct termios slirp_tty_settings;
24 extern int slirp_tty_restore;
25 
26 
27 void
debug_init(file,dbg)28 debug_init(file, dbg)
29 	char *file;
30 	int dbg;
31 {
32 	/* Close the old debugging file */
33 	if (dfd)
34 	   fclose(dfd);
35 
36 	dfd = fopen(file,"w");
37 	if (dfd != NULL) {
38 #if 1
39 		fprintf(dfd,"Slirp %s - Debugging Started.\n", SLIRP_VERSION);
40 #endif
41 		fprintf(dfd,"Debugging Started level %i.\r\n",dbg);
42 		fflush(dfd);
43 		slirp_debug = dbg;
44 	} else {
45 		lprint("Error: Debugging file \"%s\" could not be opened: %s\r\n",
46 			file, strerror(errno));
47 	}
48 }
49 
50 /*
51  * Dump a packet in the same format as tcpdump -x
52  */
53 #ifdef SLIRP_DEBUG
54 void
dump_packet(dat,n)55 dump_packet(dat, n)
56 	void *dat;
57 	int n;
58 {
59 	u_char *pptr = (u_char *)dat;
60 	int j,k;
61 
62 	n /= 16;
63 	n++;
64 	DEBUG_MISC((dfd, "PACKET DUMPED: \n"));
65 	for(j = 0; j < n; j++) {
66 		for(k = 0; k < 6; k++)
67 			DEBUG_MISC((dfd, "%02x ", *pptr++));
68 		DEBUG_MISC((dfd, "\n"));
69 		fflush(dfd);
70 	}
71 }
72 #endif
73 
74 #if 0
75 /*
76  * Statistic routines
77  *
78  * These will print statistics to the screen, the debug file (dfd), or
79  * a buffer, depending on "type", so that the stats can be sent over
80  * the link as well.
81  */
82 
83 void
84 ttystats(ttyp)
85 	struct ttys *ttyp;
86 {
87 	struct slirp_ifstats *is = &ttyp->ifstats;
88 	char buff[512];
89 
90 	lprint(" \r\n");
91 
92 	if (if_comp & IF_COMPRESS)
93 	   strcpy(buff, "on");
94 	else if (if_comp & IF_NOCOMPRESS)
95 	   strcpy(buff, "off");
96 	else
97 	   strcpy(buff, "off (for now)");
98 	lprint("Unit %d:\r\n", ttyp->unit);
99 	lprint("  using %s encapsulation (VJ compression is %s)\r\n", (
100 #ifdef USE_PPP
101 	       ttyp->proto==PROTO_PPP?"PPP":
102 #endif
103 	       "SLIP"), buff);
104 	lprint("  %d baudrate\r\n", ttyp->baud);
105 	lprint("  interface is %s\r\n", ttyp->up?"up":"down");
106 	lprint("  using fd %d, guardian pid is %d\r\n", ttyp->fd, ttyp->pid);
107 #ifndef FULL_BOLT
108 	lprint("  towrite is %d bytes\r\n", ttyp->towrite);
109 #endif
110 	if (ttyp->zeros)
111 	   lprint("  %d zeros have been typed\r\n", ttyp->zeros);
112 	else if (ttyp->ones)
113 	   lprint("  %d ones have been typed\r\n", ttyp->ones);
114 	lprint("Interface stats:\r\n");
115 	lprint("  %6d output packets sent (%d bytes)\r\n", is->out_pkts, is->out_bytes);
116 	lprint("  %6d output packets dropped (%d bytes)\r\n", is->out_errpkts, is->out_errbytes);
117 	lprint("  %6d input packets received (%d bytes)\r\n", is->in_pkts, is->in_bytes);
118 	lprint("  %6d input packets dropped (%d bytes)\r\n", is->in_errpkts, is->in_errbytes);
119 	lprint("  %6d bad input packets\r\n", is->in_mbad);
120 }
121 
122 void
123 allttystats()
124 {
125 	struct ttys *ttyp;
126 
127 	for (ttyp = ttys; ttyp; ttyp = ttyp->next)
128 	   ttystats(ttyp);
129 }
130 #endif
131 
132 void
ipstats()133 ipstats()
134 {
135 	lprint(" \r\n");
136 
137 	lprint("IP stats:\r\n");
138 	lprint("  %6d total packets received (%d were unaligned)\r\n",
139 			ipstat.ips_total, ipstat.ips_unaligned);
140 	lprint("  %6d with incorrect version\r\n", ipstat.ips_badvers);
141 	lprint("  %6d with bad header checksum\r\n", ipstat.ips_badsum);
142 	lprint("  %6d with length too short (len < sizeof(iphdr))\r\n", ipstat.ips_tooshort);
143 	lprint("  %6d with length too small (len < ip->len)\r\n", ipstat.ips_toosmall);
144 	lprint("  %6d with bad header length\r\n", ipstat.ips_badhlen);
145 	lprint("  %6d with bad packet length\r\n", ipstat.ips_badlen);
146 	lprint("  %6d fragments received\r\n", ipstat.ips_fragments);
147 	lprint("  %6d fragments dropped\r\n", ipstat.ips_fragdropped);
148 	lprint("  %6d fragments timed out\r\n", ipstat.ips_fragtimeout);
149 	lprint("  %6d packets reassembled ok\r\n", ipstat.ips_reassembled);
150 	lprint("  %6d outgoing packets fragmented\r\n", ipstat.ips_fragmented);
151 	lprint("  %6d total outgoing fragments\r\n", ipstat.ips_ofragments);
152 	lprint("  %6d with bad protocol field\r\n", ipstat.ips_noproto);
153 	lprint("  %6d total packets delivered\r\n", ipstat.ips_delivered);
154 }
155 
156 #if 0
157 void
158 vjstats()
159 {
160 	lprint(" \r\n");
161 
162 	lprint("VJ compression stats:\r\n");
163 
164 	lprint("  %6d outbound packets (%d compressed)\r\n",
165 	       comp_s.sls_packets, comp_s.sls_compressed);
166 	lprint("  %6d searches for connection stats (%d misses)\r\n",
167 	       comp_s.sls_searches, comp_s.sls_misses);
168 	lprint("  %6d inbound uncompressed packets\r\n", comp_s.sls_uncompressedin);
169 	lprint("  %6d inbound compressed packets\r\n", comp_s.sls_compressedin);
170 	lprint("  %6d inbound unknown type packets\r\n", comp_s.sls_errorin);
171 	lprint("  %6d inbound packets tossed due to error\r\n", comp_s.sls_tossed);
172 }
173 #endif
174 
175 void
tcpstats()176 tcpstats()
177 {
178 	lprint(" \r\n");
179 
180 	lprint("TCP stats:\r\n");
181 
182 	lprint("  %6d packets sent\r\n", tcpstat.tcps_sndtotal);
183 	lprint("          %6d data packets (%d bytes)\r\n",
184 			tcpstat.tcps_sndpack, tcpstat.tcps_sndbyte);
185 	lprint("          %6d data packets retransmitted (%d bytes)\r\n",
186 			tcpstat.tcps_sndrexmitpack, tcpstat.tcps_sndrexmitbyte);
187 	lprint("          %6d ack-only packets (%d delayed)\r\n",
188 			tcpstat.tcps_sndacks, tcpstat.tcps_delack);
189 	lprint("          %6d URG only packets\r\n", tcpstat.tcps_sndurg);
190 	lprint("          %6d window probe packets\r\n", tcpstat.tcps_sndprobe);
191 	lprint("          %6d window update packets\r\n", tcpstat.tcps_sndwinup);
192 	lprint("          %6d control (SYN/FIN/RST) packets\r\n", tcpstat.tcps_sndctrl);
193 	lprint("          %6d times tcp_output did nothing\r\n", tcpstat.tcps_didnuttin);
194 
195 	lprint("  %6d packets received\r\n", tcpstat.tcps_rcvtotal);
196 	lprint("          %6d acks (for %d bytes)\r\n",
197 			tcpstat.tcps_rcvackpack, tcpstat.tcps_rcvackbyte);
198 	lprint("          %6d duplicate acks\r\n", tcpstat.tcps_rcvdupack);
199 	lprint("          %6d acks for unsent data\r\n", tcpstat.tcps_rcvacktoomuch);
200 	lprint("          %6d packets received in sequence (%d bytes)\r\n",
201 			tcpstat.tcps_rcvpack, tcpstat.tcps_rcvbyte);
202         lprint("          %6d completely duplicate packets (%d bytes)\r\n",
203 			tcpstat.tcps_rcvduppack, tcpstat.tcps_rcvdupbyte);
204 
205 	lprint("          %6d packets with some duplicate data (%d bytes duped)\r\n",
206 			tcpstat.tcps_rcvpartduppack, tcpstat.tcps_rcvpartdupbyte);
207 	lprint("          %6d out-of-order packets (%d bytes)\r\n",
208 			tcpstat.tcps_rcvoopack, tcpstat.tcps_rcvoobyte);
209 	lprint("          %6d packets of data after window (%d bytes)\r\n",
210 			tcpstat.tcps_rcvpackafterwin, tcpstat.tcps_rcvbyteafterwin);
211 	lprint("          %6d window probes\r\n", tcpstat.tcps_rcvwinprobe);
212 	lprint("          %6d window update packets\r\n", tcpstat.tcps_rcvwinupd);
213 	lprint("          %6d packets received after close\r\n", tcpstat.tcps_rcvafterclose);
214 	lprint("          %6d discarded for bad checksums\r\n", tcpstat.tcps_rcvbadsum);
215 	lprint("          %6d discarded for bad header offset fields\r\n",
216 			tcpstat.tcps_rcvbadoff);
217 
218 	lprint("  %6d connection requests\r\n", tcpstat.tcps_connattempt);
219 	lprint("  %6d connection accepts\r\n", tcpstat.tcps_accepts);
220 	lprint("  %6d connections established (including accepts)\r\n", tcpstat.tcps_connects);
221 	lprint("  %6d connections closed (including %d drop)\r\n",
222 			tcpstat.tcps_closed, tcpstat.tcps_drops);
223 	lprint("  %6d embryonic connections dropped\r\n", tcpstat.tcps_conndrops);
224 	lprint("  %6d segments we tried to get rtt (%d succeeded)\r\n",
225 			tcpstat.tcps_segstimed, tcpstat.tcps_rttupdated);
226 	lprint("  %6d retransmit timeouts\r\n", tcpstat.tcps_rexmttimeo);
227 	lprint("          %6d connections dropped by rxmt timeout\r\n",
228 			tcpstat.tcps_timeoutdrop);
229 	lprint("  %6d persist timeouts\r\n", tcpstat.tcps_persisttimeo);
230 	lprint("  %6d keepalive timeouts\r\n", tcpstat.tcps_keeptimeo);
231 	lprint("          %6d keepalive probes sent\r\n", tcpstat.tcps_keepprobe);
232 	lprint("          %6d connections dropped by keepalive\r\n", tcpstat.tcps_keepdrops);
233 	lprint("  %6d correct ACK header predictions\r\n", tcpstat.tcps_predack);
234 	lprint("  %6d correct data packet header predictions\n", tcpstat.tcps_preddat);
235 	lprint("  %6d TCP cache misses\r\n", tcpstat.tcps_socachemiss);
236 
237 
238 /*	lprint("    Packets received too short:		%d\r\n", tcpstat.tcps_rcvshort); */
239 /*	lprint("    Segments dropped due to PAWS:	%d\r\n", tcpstat.tcps_pawsdrop); */
240 
241 }
242 
243 void
udpstats()244 udpstats()
245 {
246         lprint(" \r\n");
247 
248 	lprint("UDP stats:\r\n");
249 	lprint("  %6d datagrams received\r\n", udpstat.udps_ipackets);
250 	lprint("  %6d with packets shorter than header\r\n", udpstat.udps_hdrops);
251 	lprint("  %6d with bad checksums\r\n", udpstat.udps_badsum);
252 	lprint("  %6d with data length larger than packet\r\n", udpstat.udps_badlen);
253 	lprint("  %6d UDP socket cache misses\r\n", udpstat.udpps_pcbcachemiss);
254 	lprint("  %6d datagrams sent\r\n", udpstat.udps_opackets);
255 }
256 
257 void
icmpstats()258 icmpstats()
259 {
260 	lprint(" \r\n");
261 	lprint("ICMP stats:\r\n");
262 	lprint("  %6d ICMP packets received\r\n", icmpstat.icps_received);
263 	lprint("  %6d were too short\r\n", icmpstat.icps_tooshort);
264 	lprint("  %6d with bad checksums\r\n", icmpstat.icps_checksum);
265 	lprint("  %6d with type not supported\r\n", icmpstat.icps_notsupp);
266 	lprint("  %6d with bad type feilds\r\n", icmpstat.icps_badtype);
267 	lprint("  %6d ICMP packets sent in reply\r\n", icmpstat.icps_reflect);
268 }
269 
270 void
mbufstats()271 mbufstats()
272 {
273 	struct SLIRPmbuf *m;
274 	int i;
275 
276         lprint(" \r\n");
277 
278 	lprint("Mbuf stats:\r\n");
279 
280 	lprint("  %6d mbufs allocated (%d max)\r\n", mbuf_alloced, mbuf_max);
281 
282 	i = 0;
283 	for (m = m_freelist.m_next; m != &m_freelist; m = m->m_next)
284 		i++;
285 	lprint("  %6d mbufs on free list\r\n",  i);
286 
287 	i = 0;
288 	for (m = m_usedlist.m_next; m != &m_usedlist; m = m->m_next)
289 		i++;
290 	lprint("  %6d mbufs on used list\r\n",  i);
291         lprint("  %6d mbufs queued as packets\r\n\r\n", if_queued);
292 }
293 
294 
sockstats(void)295 void sockstats(void)
296 {
297 	char buff[256];
298 	int n;
299 	struct SLIRPsocket *so;
300 
301         lprint(" \r\n");
302 
303 	lprint(
304 	   "Proto[state]     Sock     Local Address, Port  Remote Address, Port RecvQ SendQ\r\n");
305 
306 	for (so = tcb.so_next; so != &tcb; so = so->so_next) {
307 
308 		n = sprintf(buff, "tcp[%s]", so->so_tcpcb?tcpstates[so->so_tcpcb->t_state]:"NONE");
309 		while (n < 17)
310 		   buff[n++] = ' ';
311 		buff[17] = 0;
312 		lprint("%s %3d   %15s %5d ",
313 				buff, so->s,
314 				inet_ntoa(so->so_laddr), ntohs(so->so_lport));
315 		lprint("%15s %5d %5d %5d\r\n",
316 				inet_ntoa(so->so_faddr), ntohs(so->so_fport),
317 				so->so_rcv.sb_cc, so->so_snd.sb_cc);
318 
319 	}
320 
321 	for (so = udb.so_next; so != &udb; so = so->so_next) {
322 
323 		n = sprintf(buff, "udp[%d sec]", (so->so_expire - curtime) / 1000);
324 		while (n < 17)
325 		   buff[n++] = ' ';
326 		buff[17] = 0;
327 		lprint("%s %3d  %15s %5d  ",
328 				buff, so->s,
329 				inet_ntoa(so->so_laddr), ntohs(so->so_lport));
330 		lprint("%15s %5d %5d %5d\r\n",
331 				inet_ntoa(so->so_faddr), ntohs(so->so_fport),
332 				so->so_rcv.sb_cc, so->so_snd.sb_cc);
333 	}
334 }
335 
336 
337 
printf_sockstats(void)338 void printf_sockstats(void)
339 {
340 	char buff[256];
341 	int n;
342 	struct SLIRPsocket *so;
343 
344         printf(" \r\n");
345 
346 	printf(
347 	   "Proto[state]     Sock     Local Address, Port  Remote Address, Port RecvQ SendQ\r\n");
348 
349 	for (so = tcb.so_next; so != &tcb; so = so->so_next) {
350 
351 		n = sprintf(buff, "tcp[%s]", so->so_tcpcb?tcpstates[so->so_tcpcb->t_state]:"NONE");
352 		while (n < 17)
353 		   buff[n++] = ' ';
354 		buff[17] = 0;
355 		printf("%s %3d   %15s %5d ",
356 				buff, so->s,
357 				inet_ntoa(so->so_laddr), ntohs(so->so_lport));
358 		printf("%15s %5d %5d %5d\r\n",
359 				inet_ntoa(so->so_faddr), ntohs(so->so_fport),
360 				so->so_rcv.sb_cc, so->so_snd.sb_cc);
361 
362 	}
363 
364 	for (so = udb.so_next; so != &udb; so = so->so_next) {
365 
366 		n = sprintf(buff, "udp[%d sec]", (so->so_expire - curtime) / 1000);
367 		while (n < 17)
368 		   buff[n++] = ' ';
369 		buff[17] = 0;
370 		printf("%s %3d  %15s %5d  ",
371 				buff, so->s,
372 				inet_ntoa(so->so_laddr), ntohs(so->so_lport));
373 		printf("%15s %5d %5d %5d\r\n",
374 				inet_ntoa(so->so_faddr), ntohs(so->so_fport),
375 				so->so_rcv.sb_cc, so->so_snd.sb_cc);
376 	}
377 printf("\n\n");
378 }
379 
380 //Simple code to purge and close open sockets.
381 //This way we can open/close/open/close..
purgesocks(void)382 void purgesocks(void)
383 {
384 	struct SLIRPsocket *so;
385 
386 	for (so = tcb.so_next; so != &tcb; so = so->so_next) {
387 
388 	closesocket(so->s);	//close the socket
389 	}
390 }
391 
392 #if 1
393 void
slirp_exit(exit_status)394 slirp_exit(exit_status)
395 	int exit_status;
396 {
397 //	struct ttys *ttyp;
398 
399 	DEBUG_CALL("slirp_exit");
400 	DEBUG_ARG("exit_status = %d", exit_status);
401 
402 	if (dostats) {
403 		lprint_print = (int (*) _P((void *, const char *, va_list)))vfprintf;
404 		if (!dfd)
405 		   debug_init("slirp_stats", 0xf);
406 		lprint_arg = (char **)&dfd;
407 
408 		ipstats();
409 		tcpstats();
410 		udpstats();
411 		icmpstats();
412 		mbufstats();
413 		sockstats();
414 		fclose(dfd);
415 //		allttystats();
416 //		vjstats();
417 	}
418 
419 //	for (ttyp = ttys; ttyp; ttyp = ttyp->next)
420 //	   tty_detached(ttyp, 1);
421 
422 //	if (slirp_forked) {
423 //		/* Menendez time */
424 //		if (kill(getppid(), SIGQUIT) < 0)
425 //			lprint("Couldn't kill parent process %ld!\n",
426 //			    (long) getppid());
427 //  	}
428 
429 	/* Restore the terminal if we gotta */
430 //	if(slirp_tty_restore)
431 //	  tcsetattr(0,TCSANOW, &slirp_tty_settings);  /* NOW DAMMIT! */
432 //	exit(exit_status);
433 
434 	//This will iterate though the sockets, and close them all (think redirects)
435 	//PCem will have SLiRP open, close several times, which trips up SLiRP
436 	//So for now I go through the sockets and close them
437 	purgesocks();
438 
439 }
440 #endif
441