xref: /original-bsd/sys/netccitt/pk_debug.c (revision 5000927f)
1 /*
2  * Copyright (c) University of British Columbia, 1984
3  * Copyright (c) 1990, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * the Laboratory for Computation Vision and the Computer Science Department
8  * of the University of British Columbia.
9  *
10  * %sccs.include.redist.c%
11  *
12  *	@(#)pk_debug.c	8.1 (Berkeley) 06/10/93
13  */
14 
15 #include <sys/param.h>
16 #include <sys/systm.h>
17 #include <sys/mbuf.h>
18 #include <sys/socket.h>
19 #include <sys/protosw.h>
20 #include <sys/socketvar.h>
21 #include <sys/errno.h>
22 
23 #include <net/if.h>
24 
25 #include <netccitt/x25.h>
26 #include <netccitt/pk.h>
27 #include <netccitt/pk_var.h>
28 
29 char	*pk_state[] = {
30 	"Listen",	"Ready",	"Received-Call",
31 	"Sent-Call",	"Data-Transfer","Received-Clear",
32 	"Sent-Clear",
33 };
34 
35 char   *pk_name[] = {
36 	"Call",		"Call-Conf",	"Clear",
37 	"Clear-Conf",	"Data",		"Intr",		"Intr-Conf",
38 	"Rr",		"Rnr",		"Reset",	"Reset-Conf",
39 	"Restart",	"Restart-Conf",	"Reject",	"Diagnostic",
40 	"Invalid"
41 };
42 
43 pk_trace (xcp, m, dir)
44 struct x25config *xcp;
45 register struct mbuf *m;
46 char *dir;
47 {
48 	register char *s;
49 	struct x25_packet *xp = mtod(m, struct x25_packet *);
50 	register int i, len = 0, cnt = 0;
51 
52 	if (xcp -> xc_ptrace == 0)
53 		return;
54 
55 	i = pk_decode (xp) / MAXSTATES;
56 	for (; m; m = m -> m_next) {
57 		len = len + m -> m_len;
58 		++cnt;
59 	}
60 	printf ("LCN=%d %s:	%s	#=%d, len=%d ",
61 		LCN(xp), dir, pk_name[i], cnt, len);
62 	for (s = (char *) xp, i = 0; i < 5; ++i, ++s)
63 		printf ("%x ", (int) * s & 0xff);
64 	printf ("\n");
65 }
66 
67 mbuf_cache(c, m)
68 register struct mbuf_cache *c;
69 struct mbuf *m;
70 {
71 	register struct mbuf **mp;
72 
73 	if (c->mbc_size != c->mbc_oldsize) {
74 		unsigned zero_size, copy_size;
75 		unsigned new_size = c->mbc_size * sizeof(m);
76 		caddr_t cache = (caddr_t)c->mbc_cache;
77 
78 		if (new_size) {
79 			c->mbc_cache = (struct mbuf **)
80 				malloc(new_size, M_MBUF, M_NOWAIT);
81 			if (c->mbc_cache == 0) {
82 				c->mbc_cache = (struct mbuf **)cache;
83 				return;
84 			}
85 			c->mbc_num %= c->mbc_size;
86 		} else
87 			c->mbc_cache = 0;
88 		if (c->mbc_size < c->mbc_oldsize) {
89 			register struct mbuf **mplim;
90 			mp = c->mbc_size + (struct mbuf **)cache;
91 			mplim = c->mbc_oldsize + (struct mbuf **)cache;
92 			while (mp < mplim)
93 				m_freem(*mp++);
94 			zero_size = 0;
95 		} else
96 			zero_size = (c->mbc_size - c->mbc_oldsize) * sizeof(m);
97 		copy_size = new_size - zero_size;
98 		c->mbc_oldsize = c->mbc_size;
99 		if (copy_size)
100 			bcopy(cache, (caddr_t)c->mbc_cache, copy_size);
101 		if (cache)
102 			free(cache, M_MBUF);
103 		if (zero_size)
104 			bzero(copy_size + (caddr_t)c->mbc_cache, zero_size);
105 	}
106 	if (c->mbc_size == 0)
107 		return;
108 	mp = c->mbc_cache + c->mbc_num;
109 	c->mbc_num = (1 + c->mbc_num) % c->mbc_size;
110 	if (*mp)
111 		m_freem(*mp);
112 	if (*mp = m_copym(m, 0, M_COPYALL, M_DONTWAIT))
113 		(*mp)->m_flags |= m->m_flags & 0x08;
114 }
115