xref: /f-stack/freebsd/contrib/ngatm/netnatm/unimsg.h (revision a9643ea8)
1 /*
2  * Copyright (c) 1996-2003
3  *	Fraunhofer Institute for Open Communication Systems (FhG Fokus).
4  * 	All rights reserved.
5  *
6  * Author: Hartmut Brandt <[email protected]>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $Begemot: libunimsg/netnatm/unimsg.h,v 1.4 2004/07/08 08:21:46 brandt Exp $
30  *
31  * This defines the structure of messages as handled by this library.
32  */
33 #ifndef _NETNATM_UNIMSG_H_
34 #define _NETNATM_UNIMSG_H_
35 
36 #include <sys/types.h>
37 #ifdef _KERNEL
38 #ifdef __FreeBSD__
39 #include <sys/systm.h>
40 #endif
41 #include <sys/stdint.h>
42 #else
43 #include <string.h>
44 #include <stdint.h>
45 #endif
46 
47 struct uni_msg {
48 	u_char		*b_wptr;	/* tail pointer */
49 	u_char		*b_rptr;	/* head pointer */
50 	u_char		*b_buf;		/* data buffer */
51 	u_char		*b_lim;		/* end of data buffer */
52 };
53 
54 /* return the current length of the message */
55 #define uni_msg_len(M)		((size_t)((M)->b_wptr - (M)->b_rptr))
56 
57 /* return the number of space behind the message */
58 #define uni_msg_space(M)	((size_t)((M)->b_lim - (M)->b_wptr))
59 
60 /* return the amount of leading free space */
61 #define uni_msg_leading(M)	((size_t)((M)->b_rptr - (M)->b_buf))
62 
63 /* return the maximum size of the message (length plus free space) */
64 #define uni_msg_size(M)		((size_t)((M)->b_lim - (M)->b_buf));
65 
66 /* ensure that there is space for another S bytes. If reallocation fails
67  * free message and return -1 */
68 #define uni_msg_ensure(M, S)					\
69 	((uni_msg_space(M) >= (S)) ? 0 : uni_msg_extend(M, S))
70 
71 int uni_msg_append(struct uni_msg *, void *, size_t);
72 int uni_msg_extend(struct uni_msg *, size_t);
73 
74 #define uni_msg_rptr(MSG, TYPE) ((TYPE)(void *)(MSG)->b_rptr)
75 #define uni_msg_wptr(MSG, TYPE) ((TYPE)(void *)(MSG)->b_wptr)
76 
77 int uni_msg_prepend(struct uni_msg *, size_t);
78 
79 #ifndef _KERNEL
80 
81 struct uni_msg *uni_msg_alloc(size_t);
82 struct uni_msg *uni_msg_build(void *, ...);
83 void uni_msg_destroy(struct uni_msg *);
84 u_int uni_msg_strip32(struct uni_msg *);
85 u_int uni_msg_get32(struct uni_msg *);
86 int uni_msg_append32(struct uni_msg *, u_int);
87 int uni_msg_append8(struct uni_msg *, u_int);
88 u_int uni_msg_trail32(const struct uni_msg *, int);
89 struct uni_msg *uni_msg_dup(const struct uni_msg *);
90 
91 #endif /* _KERNEL */
92 #endif
93