xref: /titanic_44/usr/src/uts/common/netsmb/mchain.h (revision 4bff34e37def8a90f9194d81bc345c52ba20086a)
1*4bff34e3Sthurlow /*
2*4bff34e3Sthurlow  * Copyright (c) 2000, 2001 Boris Popov
3*4bff34e3Sthurlow  * All rights reserved.
4*4bff34e3Sthurlow  *
5*4bff34e3Sthurlow  * Redistribution and use in source and binary forms, with or without
6*4bff34e3Sthurlow  * modification, are permitted provided that the following conditions
7*4bff34e3Sthurlow  * are met:
8*4bff34e3Sthurlow  * 1. Redistributions of source code must retain the above copyright
9*4bff34e3Sthurlow  *    notice, this list of conditions and the following disclaimer.
10*4bff34e3Sthurlow  * 2. Redistributions in binary form must reproduce the above copyright
11*4bff34e3Sthurlow  *    notice, this list of conditions and the following disclaimer in the
12*4bff34e3Sthurlow  *    documentation and/or other materials provided with the distribution.
13*4bff34e3Sthurlow  * 3. All advertising materials mentioning features or use of this software
14*4bff34e3Sthurlow  *    must display the following acknowledgement:
15*4bff34e3Sthurlow  *    This product includes software developed by Boris Popov.
16*4bff34e3Sthurlow  * 4. Neither the name of the author nor the names of any co-contributors
17*4bff34e3Sthurlow  *    may be used to endorse or promote products derived from this software
18*4bff34e3Sthurlow  *    without specific prior written permission.
19*4bff34e3Sthurlow  *
20*4bff34e3Sthurlow  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21*4bff34e3Sthurlow  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22*4bff34e3Sthurlow  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23*4bff34e3Sthurlow  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24*4bff34e3Sthurlow  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25*4bff34e3Sthurlow  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26*4bff34e3Sthurlow  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27*4bff34e3Sthurlow  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28*4bff34e3Sthurlow  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29*4bff34e3Sthurlow  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30*4bff34e3Sthurlow  * SUCH DAMAGE.
31*4bff34e3Sthurlow  *
32*4bff34e3Sthurlow  * $FreeBSD: src/sys/sys/mchain.h,v 1.1 2001/02/24 15:44:30 bp Exp $
33*4bff34e3Sthurlow  */
34*4bff34e3Sthurlow /*
35*4bff34e3Sthurlow  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
36*4bff34e3Sthurlow  * Use is subject to license terms.
37*4bff34e3Sthurlow  */
38*4bff34e3Sthurlow 
39*4bff34e3Sthurlow #ifndef _MCHAIN_H_
40*4bff34e3Sthurlow #define	_MCHAIN_H_
41*4bff34e3Sthurlow 
42*4bff34e3Sthurlow #pragma ident	"%Z%%M%	%I%	%E% SMI"
43*4bff34e3Sthurlow 
44*4bff34e3Sthurlow #include <sys/types.h>
45*4bff34e3Sthurlow #include <sys/isa_defs.h>
46*4bff34e3Sthurlow #include <sys/byteorder.h>
47*4bff34e3Sthurlow 
48*4bff34e3Sthurlow #ifdef _LITTLE_ENDIAN
49*4bff34e3Sthurlow 
50*4bff34e3Sthurlow /* little-endian values on little-endian */
51*4bff34e3Sthurlow #define	htoles(x)	((uint16_t)(x))
52*4bff34e3Sthurlow #define	letohs(x)	((uint16_t)(x))
53*4bff34e3Sthurlow #define	htolel(x)	((uint32_t)(x))
54*4bff34e3Sthurlow #define	letohl(x)	((uint32_t)(x))
55*4bff34e3Sthurlow #define	htoleq(x)	((uint64_t)(x))
56*4bff34e3Sthurlow #define	letohq(x)	((uint64_t)(x))
57*4bff34e3Sthurlow 
58*4bff34e3Sthurlow /*
59*4bff34e3Sthurlow  * big-endian values on little-endian (swap)
60*4bff34e3Sthurlow  *
61*4bff34e3Sthurlow  * Use the BSWAP macros because they're fastest, and they're
62*4bff34e3Sthurlow  * available in all environments where we use this header.
63*4bff34e3Sthurlow  */
64*4bff34e3Sthurlow #define	htobes(x)	BSWAP_16(x)
65*4bff34e3Sthurlow #define	betohs(x)	BSWAP_16(x)
66*4bff34e3Sthurlow #define	htobel(x)	BSWAP_32(x)
67*4bff34e3Sthurlow #define	betohl(x)	BSWAP_32(x)
68*4bff34e3Sthurlow #define	htobeq(x)	BSWAP_64(x)
69*4bff34e3Sthurlow #define	betohq(x)	BSWAP_64(x)
70*4bff34e3Sthurlow 
71*4bff34e3Sthurlow #else	/* (BYTE_ORDER == LITTLE_ENDIAN) */
72*4bff34e3Sthurlow 
73*4bff34e3Sthurlow /* little-endian values on big-endian (swap) */
74*4bff34e3Sthurlow #define	letohs(x) 	BSWAP_16(x)
75*4bff34e3Sthurlow #define	htoles(x) 	BSWAP_16(x)
76*4bff34e3Sthurlow #define	letohl(x) 	BSWAP_32(x)
77*4bff34e3Sthurlow #define	htolel(x) 	BSWAP_32(x)
78*4bff34e3Sthurlow #define	letohq(x)	BSWAP_64(x)
79*4bff34e3Sthurlow #define	htoleq(x)	BSWAP_64(x)
80*4bff34e3Sthurlow 
81*4bff34e3Sthurlow /* big-endian values on big-endian */
82*4bff34e3Sthurlow #define	htobes(x)	((uint16_t)(x))
83*4bff34e3Sthurlow #define	betohs(x)	((uint16_t)(x))
84*4bff34e3Sthurlow #define	htobel(x)	((uint32_t)(x))
85*4bff34e3Sthurlow #define	betohl(x)	((uint32_t)(x))
86*4bff34e3Sthurlow #define	htobeq(x)	((uint64_t)(x))
87*4bff34e3Sthurlow #define	betohq(x)	((uint64_t)(x))
88*4bff34e3Sthurlow #endif	/* (BYTE_ORDER == LITTLE_ENDIAN) */
89*4bff34e3Sthurlow 
90*4bff34e3Sthurlow 
91*4bff34e3Sthurlow #ifdef _KERNEL
92*4bff34e3Sthurlow 
93*4bff34e3Sthurlow /* BEGIN CSTYLED */
94*4bff34e3Sthurlow /*
95*4bff34e3Sthurlow  * BSD-style mbufs, vs SysV-style mblks:
96*4bff34e3Sthurlow  * One big difference: the mbuf payload is:
97*4bff34e3Sthurlow  *   m_data ... (m_data + m_len)
98*4bff34e3Sthurlow  * In Unix STREAMS, the mblk payload is:
99*4bff34e3Sthurlow  *   b_rptr ... b_wptr
100*4bff34e3Sthurlow  *
101*4bff34e3Sthurlow  * Here are some handy conversion notes:
102*4bff34e3Sthurlow  *
103*4bff34e3Sthurlow  * struct mbuf                     struct mblk
104*4bff34e3Sthurlow  *   m->m_next                       m->b_cont
105*4bff34e3Sthurlow  *   m->m_nextpkt                    m->b_next
106*4bff34e3Sthurlow  *   m->m_data                       m->b_rptr
107*4bff34e3Sthurlow  *   m->m_len                        MBLKL(m)
108*4bff34e3Sthurlow  *   m->m_dat[]                      m->b_datap->db_base
109*4bff34e3Sthurlow  *   &m->m_dat[MLEN]                 m->b_datap->db_lim
110*4bff34e3Sthurlow  *   M_TRAILINGSPACE(m)              MBLKTAIL(m)
111*4bff34e3Sthurlow  *   m_freem(m)                      freemsg(m)
112*4bff34e3Sthurlow  *
113*4bff34e3Sthurlow  * Note that mbufs chains also have a special "packet" header,
114*4bff34e3Sthurlow  * which has the length of the whole message.  In STREAMS one
115*4bff34e3Sthurlow  * typically just calls msgdsize(m) to get that.
116*4bff34e3Sthurlow  */
117*4bff34e3Sthurlow /* END CSTYLED */
118*4bff34e3Sthurlow 
119*4bff34e3Sthurlow #include <sys/stream.h> /* mblk_t */
120*4bff34e3Sthurlow 
121*4bff34e3Sthurlow /*
122*4bff34e3Sthurlow  * Type of copy for mb_{put|get}_mem()
123*4bff34e3Sthurlow  */
124*4bff34e3Sthurlow #define	MB_MSYSTEM	0		/* use bcopy() */
125*4bff34e3Sthurlow #define	MB_MUSER	1		/* use copyin()/copyout() */
126*4bff34e3Sthurlow #define	MB_MINLINE	2		/* use an inline copy loop */
127*4bff34e3Sthurlow #define	MB_MZERO	3		/* bzero(), mb_put_mem only */
128*4bff34e3Sthurlow #define	MB_MCUSTOM	4		/* use an user defined function */
129*4bff34e3Sthurlow 
130*4bff34e3Sthurlow struct mbchain {
131*4bff34e3Sthurlow 	mblk_t *mb_top;
132*4bff34e3Sthurlow 	mblk_t *mb_cur;
133*4bff34e3Sthurlow 	uint_t mb_count;
134*4bff34e3Sthurlow };
135*4bff34e3Sthurlow typedef struct mbchain mbchain_t;
136*4bff34e3Sthurlow 
137*4bff34e3Sthurlow struct mdchain {
138*4bff34e3Sthurlow 	mblk_t *md_top;		/* head of mblk chain */
139*4bff34e3Sthurlow 	mblk_t *md_cur;		/* current mblk */
140*4bff34e3Sthurlow 	uchar_t *md_pos;		/* offset in the current mblk */
141*4bff34e3Sthurlow };
142*4bff34e3Sthurlow typedef struct mdchain mdchain_t;
143*4bff34e3Sthurlow 
144*4bff34e3Sthurlow int  m_fixhdr(mblk_t *m);
145*4bff34e3Sthurlow 
146*4bff34e3Sthurlow int  mb_init(struct mbchain *mbp);
147*4bff34e3Sthurlow void mb_initm(struct mbchain *mbp, mblk_t *m);
148*4bff34e3Sthurlow void mb_done(struct mbchain *mbp);
149*4bff34e3Sthurlow mblk_t *mb_detach(struct mbchain *mbp);
150*4bff34e3Sthurlow int  mb_fixhdr(struct mbchain *mbp);
151*4bff34e3Sthurlow void *mb_reserve(struct mbchain *mbp, int size);
152*4bff34e3Sthurlow 
153*4bff34e3Sthurlow int  mb_put_padbyte(struct mbchain *mbp);
154*4bff34e3Sthurlow int  mb_put_uint8(struct mbchain *mbp, uint8_t x);
155*4bff34e3Sthurlow int  mb_put_uint16be(struct mbchain *mbp, uint16_t x);
156*4bff34e3Sthurlow int  mb_put_uint16le(struct mbchain *mbp, uint16_t x);
157*4bff34e3Sthurlow int  mb_put_uint32be(struct mbchain *mbp, uint32_t x);
158*4bff34e3Sthurlow int  mb_put_uint32le(struct mbchain *mbp, uint32_t x);
159*4bff34e3Sthurlow int  mb_put_uint64be(struct mbchain *mbp, uint64_t x);
160*4bff34e3Sthurlow int  mb_put_uint64le(struct mbchain *mbp, uint64_t x);
161*4bff34e3Sthurlow int  mb_put_mem(struct mbchain *mbp, const char *src, int size, int type);
162*4bff34e3Sthurlow 
163*4bff34e3Sthurlow int  mb_put_mblk(struct mbchain *mbp, mblk_t *m);
164*4bff34e3Sthurlow int  mb_put_uio(struct mbchain *mbp, uio_t *uiop, int size);
165*4bff34e3Sthurlow 
166*4bff34e3Sthurlow int  md_init(struct mdchain *mdp);
167*4bff34e3Sthurlow void md_initm(struct mdchain *mbp, mblk_t *m);
168*4bff34e3Sthurlow void md_done(struct mdchain *mdp);
169*4bff34e3Sthurlow void md_append_record(struct mdchain *mdp, mblk_t *top);
170*4bff34e3Sthurlow int  md_next_record(struct mdchain *mdp);
171*4bff34e3Sthurlow int  md_get_uint8(struct mdchain *mdp, uint8_t *x);
172*4bff34e3Sthurlow int  md_get_uint16(struct mdchain *mdp, uint16_t *x);
173*4bff34e3Sthurlow int  md_get_uint16le(struct mdchain *mdp, uint16_t *x);
174*4bff34e3Sthurlow int  md_get_uint16be(struct mdchain *mdp, uint16_t *x);
175*4bff34e3Sthurlow int  md_get_uint32(struct mdchain *mdp, uint32_t *x);
176*4bff34e3Sthurlow int  md_get_uint32be(struct mdchain *mdp, uint32_t *x);
177*4bff34e3Sthurlow int  md_get_uint32le(struct mdchain *mdp, uint32_t *x);
178*4bff34e3Sthurlow int  md_get_uint64(struct mdchain *mdp, uint64_t *x);
179*4bff34e3Sthurlow int  md_get_uint64be(struct mdchain *mdp, uint64_t *x);
180*4bff34e3Sthurlow int  md_get_uint64le(struct mdchain *mdp, uint64_t *x);
181*4bff34e3Sthurlow int  md_get_mem(struct mdchain *mdp, caddr_t target, int size, int type);
182*4bff34e3Sthurlow int  md_get_mblk(struct mdchain *mdp, int size, mblk_t **m);
183*4bff34e3Sthurlow int  md_get_uio(struct mdchain *mdp, uio_t *uiop, int size);
184*4bff34e3Sthurlow 
185*4bff34e3Sthurlow /*
186*4bff34e3Sthurlow  * Additions for Solaris to replace things that came from
187*4bff34e3Sthurlow  * <sys/mbuf.h> in the Darwin code.  These are mostly just
188*4bff34e3Sthurlow  * wrappers for streams functions.  See: subr_mchain.c
189*4bff34e3Sthurlow  */
190*4bff34e3Sthurlow 
191*4bff34e3Sthurlow #define	mtod(m, t) ((t)((m)->b_rptr))
192*4bff34e3Sthurlow 
193*4bff34e3Sthurlow /* length to m_copym to copy all */
194*4bff34e3Sthurlow #define	M_COPYALL		-1
195*4bff34e3Sthurlow 
196*4bff34e3Sthurlow mblk_t *m_copym(mblk_t *, int, int, int);
197*4bff34e3Sthurlow mblk_t *m_pullup(mblk_t *, int);
198*4bff34e3Sthurlow mblk_t *m_split(mblk_t *, int, int);
199*4bff34e3Sthurlow void m_cat(mblk_t *, mblk_t *);
200*4bff34e3Sthurlow #define	m_freem(x)	freemsg(x)
201*4bff34e3Sthurlow mblk_t *m_getblk(int, int);
202*4bff34e3Sthurlow 
203*4bff34e3Sthurlow #endif	/* ifdef _KERNEL */
204*4bff34e3Sthurlow #endif	/* !_MCHAIN_H_ */
205