unimsg.h revision 131827
190075Sobrien/*
2132718Skan * Copyright (c) 1996-2003
390075Sobrien *	Fraunhofer Institute for Open Communication Systems (FhG Fokus).
490075Sobrien * 	All rights reserved.
590075Sobrien *
690075Sobrien * Author: Hartmut Brandt <harti@freebsd.org>
790075Sobrien *
890075Sobrien * Redistribution and use in source and binary forms, with or without
990075Sobrien * modification, are permitted provided that the following conditions
1090075Sobrien * are met:
11132718Skan * 1. Redistributions of source code must retain the above copyright
12132718Skan *    notice, this list of conditions and the following disclaimer.
13132718Skan * 2. Redistributions in binary form must reproduce the above copyright
14132718Skan *    notice, this list of conditions and the following disclaimer in the
15132718Skan *    documentation and/or other materials provided with the distribution.
16132718Skan *
17132718Skan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18132718Skan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19132718Skan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2090075Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2190075Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2290075Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2390075Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2490075Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2590075Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2690075Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27169689Skan * SUCH DAMAGE.
28169689Skan *
2990075Sobrien * $Begemot: libunimsg/netnatm/unimsg.h,v 1.4 2004/07/08 08:21:46 brandt Exp $
3090075Sobrien *
3190075Sobrien * This defines the structure of messages as handled by this library.
3290075Sobrien */
3390075Sobrien#ifndef _NETNATM_UNIMSG_H_
34132718Skan#define _NETNATM_UNIMSG_H_
35132718Skan
36132718Skan#include <sys/types.h>
3790075Sobrien#ifdef _KERNEL
3890075Sobrien#ifdef __FreeBSD__
3990075Sobrien#include <sys/systm.h>
4090075Sobrien#endif
4190075Sobrien#include <sys/stdint.h>
4290075Sobrien#else
4390075Sobrien#include <string.h>
4490075Sobrien#include <stdint.h>
4590075Sobrien#endif
4690075Sobrien
4790075Sobrienstruct uni_msg {
4890075Sobrien	u_char		*b_wptr;	/* tail pointer */
4990075Sobrien	u_char		*b_rptr;	/* head pointer */
5090075Sobrien	u_char		*b_buf;		/* data buffer */
5190075Sobrien	u_char		*b_lim;		/* end of data buffer */
5290075Sobrien};
5390075Sobrien
5490075Sobrien/* return the current length of the message */
5590075Sobrien#define uni_msg_len(M)		((size_t)((M)->b_wptr - (M)->b_rptr))
5690075Sobrien
5790075Sobrien/* return the number of space behind the message */
5890075Sobrien#define uni_msg_space(M)	((size_t)((M)->b_lim - (M)->b_wptr))
5990075Sobrien
6090075Sobrien/* return the amount of leading free space */
6190075Sobrien#define uni_msg_leading(M)	((size_t)((M)->b_rptr - (M)->b_buf))
6290075Sobrien
6390075Sobrien/* return the maximum size of the message (length plus free space) */
6490075Sobrien#define uni_msg_size(M)		((size_t)((M)->b_lim - (M)->b_buf));
6590075Sobrien
6690075Sobrien/* ensure that there is space for another S bytes. If reallocation fails
67132718Skan * free message and return -1 */
68132718Skan#define uni_msg_ensure(M, S)					\
6990075Sobrien	((uni_msg_space(M) >= (S)) ? 0 : uni_msg_extend(M, S))
7090075Sobrien
7190075Sobrienint uni_msg_append(struct uni_msg *, void *, size_t);
7290075Sobrienint uni_msg_extend(struct uni_msg *, size_t);
7390075Sobrien
7490075Sobrien#define uni_msg_rptr(MSG, TYPE) ((TYPE)(void *)(MSG)->b_rptr)
7590075Sobrien#define uni_msg_wptr(MSG, TYPE) ((TYPE)(void *)(MSG)->b_wptr)
7690075Sobrien
7790075Sobrienint uni_msg_prepend(struct uni_msg *, size_t);
7890075Sobrien
7990075Sobrien#ifndef _KERNEL
8090075Sobrien
8190075Sobrienstruct uni_msg *uni_msg_alloc(size_t);
8290075Sobrienstruct uni_msg *uni_msg_build(void *, ...);
8390075Sobrienvoid uni_msg_destroy(struct uni_msg *);
8490075Sobrienu_int uni_msg_strip32(struct uni_msg *);
8590075Sobrienu_int uni_msg_get32(struct uni_msg *);
8690075Sobrienint uni_msg_append32(struct uni_msg *, u_int);
8790075Sobrienint uni_msg_append8(struct uni_msg *, u_int);
8890075Sobrienu_int uni_msg_trail32(const struct uni_msg *, int);
8990075Sobrienstruct uni_msg *uni_msg_dup(const struct uni_msg *);
9090075Sobrien
9190075Sobrien#endif /* _KERNEL */
9290075Sobrien#endif
93132718Skan