1121326Sharti/*
2121326Sharti * Copyright (c) 1996-2003
3121326Sharti *	Fraunhofer Institute for Open Communication Systems (FhG Fokus).
4121326Sharti * 	All rights reserved.
5121326Sharti *
6131826Sharti * Author: Hartmut Brandt <harti@freebsd.org>
7131826Sharti *
8121326Sharti * Redistribution and use in source and binary forms, with or without
9121326Sharti * modification, are permitted provided that the following conditions
10121326Sharti * are met:
11121326Sharti * 1. Redistributions of source code must retain the above copyright
12121326Sharti *    notice, this list of conditions and the following disclaimer.
13121326Sharti * 2. Redistributions in binary form must reproduce the above copyright
14121326Sharti *    notice, this list of conditions and the following disclaimer in the
15121326Sharti *    documentation and/or other materials provided with the distribution.
16121326Sharti *
17121326Sharti * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18121326Sharti * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19121326Sharti * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20121326Sharti * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21121326Sharti * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22121326Sharti * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23121326Sharti * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24121326Sharti * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25121326Sharti * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26121326Sharti * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27121326Sharti * SUCH DAMAGE.
28121326Sharti *
29131826Sharti * $Begemot: libunimsg/netnatm/unimsg.h,v 1.4 2004/07/08 08:21:46 brandt Exp $
30121326Sharti *
31121326Sharti * This defines the structure of messages as handled by this library.
32121326Sharti */
33121326Sharti#ifndef _NETNATM_UNIMSG_H_
34121326Sharti#define _NETNATM_UNIMSG_H_
35121326Sharti
36121326Sharti#include <sys/types.h>
37121326Sharti#ifdef _KERNEL
38121326Sharti#ifdef __FreeBSD__
39121326Sharti#include <sys/systm.h>
40121326Sharti#endif
41131826Sharti#include <sys/stdint.h>
42121326Sharti#else
43121326Sharti#include <string.h>
44131826Sharti#include <stdint.h>
45121326Sharti#endif
46121326Sharti
47121326Shartistruct uni_msg {
48121326Sharti	u_char		*b_wptr;	/* tail pointer */
49121326Sharti	u_char		*b_rptr;	/* head pointer */
50121326Sharti	u_char		*b_buf;		/* data buffer */
51121326Sharti	u_char		*b_lim;		/* end of data buffer */
52121326Sharti};
53121326Sharti
54121326Sharti/* return the current length of the message */
55121326Sharti#define uni_msg_len(M)		((size_t)((M)->b_wptr - (M)->b_rptr))
56121326Sharti
57121326Sharti/* return the number of space behind the message */
58121326Sharti#define uni_msg_space(M)	((size_t)((M)->b_lim - (M)->b_wptr))
59121326Sharti
60121326Sharti/* return the amount of leading free space */
61121326Sharti#define uni_msg_leading(M)	((size_t)((M)->b_rptr - (M)->b_buf))
62121326Sharti
63121326Sharti/* return the maximum size of the message (length plus free space) */
64121326Sharti#define uni_msg_size(M)		((size_t)((M)->b_lim - (M)->b_buf));
65121326Sharti
66121326Sharti/* ensure that there is space for another S bytes. If reallocation fails
67121326Sharti * free message and return -1 */
68121326Sharti#define uni_msg_ensure(M, S)					\
69121326Sharti	((uni_msg_space(M) >= (S)) ? 0 : uni_msg_extend(M, S))
70121326Sharti
71121326Shartiint uni_msg_append(struct uni_msg *, void *, size_t);
72121326Shartiint uni_msg_extend(struct uni_msg *, size_t);
73121326Sharti
74121326Sharti#define uni_msg_rptr(MSG, TYPE) ((TYPE)(void *)(MSG)->b_rptr)
75121326Sharti#define uni_msg_wptr(MSG, TYPE) ((TYPE)(void *)(MSG)->b_wptr)
76121326Sharti
77121326Shartiint uni_msg_prepend(struct uni_msg *, size_t);
78121326Sharti
79121326Sharti#ifndef _KERNEL
80121326Sharti
81121326Shartistruct uni_msg *uni_msg_alloc(size_t);
82121326Shartistruct uni_msg *uni_msg_build(void *, ...);
83121326Shartivoid uni_msg_destroy(struct uni_msg *);
84121326Shartiu_int uni_msg_strip32(struct uni_msg *);
85121326Shartiu_int uni_msg_get32(struct uni_msg *);
86121326Shartiint uni_msg_append32(struct uni_msg *, u_int);
87121326Shartiint uni_msg_append8(struct uni_msg *, u_int);
88121326Shartiu_int uni_msg_trail32(const struct uni_msg *, int);
89121326Shartistruct uni_msg *uni_msg_dup(const struct uni_msg *);
90121326Sharti
91121326Sharti#endif /* _KERNEL */
92121326Sharti#endif
93