1/* 2 * Copyright (c) 2001-2003 3 * Fraunhofer Institute for Open Communication Systems (FhG Fokus). 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 * 27 * Author: Hartmut Brandt <harti@freebsd.org> 28 * 29 * $Begemot: libunimsg/libngatm/sscopcust.h,v 1.4 2004/07/08 08:21:40 brandt Exp $ 30 * 31 * Customisation of the SSCOP code for the user space library. 32 */ 33#include <sys/types.h> 34#include <sys/queue.h> 35#include <stdio.h> 36#include <stdlib.h> 37#include <string.h> 38#include <stdarg.h> 39#include <errno.h> 40#ifdef SSCOP_DEBUG 41#include <assert.h> 42#endif 43#include <netinet/in.h> 44#include <arpa/inet.h> 45#include <netnatm/unimsg.h> 46 47/* 48 * Allocate zeroed or non-zeroed memory of some size and cast it. 49 * Return NULL on failure. 50 */ 51#define MEMINIT() 52 53#define MEMZALLOC(PTR,CAST,SIZE) do { \ 54 void *_m = malloc(SIZE); \ 55 if (_m != NULL) \ 56 bzero(_m, SIZE); \ 57 (PTR) = (CAST)_m; \ 58} while(0) 59 60#define MEMFREE(PTR) free(PTR); 61 62#define MSG_ALLOC(PTR) \ 63 MEMZALLOC(PTR, struct sscop_msg *, sizeof(struct sscop_msg)) 64#define MSG_FREE(PTR) \ 65 MEMFREE(PTR) 66 67#define SIG_ALLOC(PTR) \ 68 MEMZALLOC(PTR, struct sscop_sig *, sizeof(struct sscop_sig)) 69#define SIG_FREE(PTR) \ 70 MEMFREE(PTR) 71 72/* 73 * Timer support. 74 */ 75typedef void *sscop_timer_t; 76#define TIMER_INIT(S,T) (S)->t_##T = NULL 77#define TIMER_STOP(S,T) do { \ 78 if ((S)->t_##T != NULL) { \ 79 (S)->funcs->stop_timer((S), (S)->aarg, (S)->t_##T); \ 80 (S)->t_##T = NULL; \ 81 } \ 82 } while(0) 83#define TIMER_RESTART(S,T) do { \ 84 if ((S)->t_##T != NULL) \ 85 (S)->funcs->stop_timer((S), (S)->aarg, (S)->t_##T); \ 86 (S)->t_##T = (S)->funcs->start_timer((S), (S)->aarg, \ 87 (S)->timer##T, T##_func); \ 88 } while(0) 89#define TIMER_ISACT(S,T) ((S)->t_##T != NULL) 90 91#define TIMER_FUNC(T,N) \ 92static void \ 93T##_func(void *varg) \ 94{ \ 95 struct sscop *sscop = varg; \ 96 VERBOSE(sscop, SSCOP_DBG_TIMER, (sscop, sscop->aarg, \ 97 "timer_" #T " expired")); \ 98 sscop->t_##T = NULL; \ 99 sscop_signal(sscop, SIG_T_##N, NULL); \ 100} 101 102 103/* 104 * Message queues 105 */ 106typedef TAILQ_ENTRY(sscop_msg) sscop_msgq_link_t; 107typedef TAILQ_HEAD(sscop_msgq, sscop_msg) sscop_msgq_head_t; 108#define MSGQ_EMPTY(Q) TAILQ_EMPTY(Q) 109#define MSGQ_INIT(Q) TAILQ_INIT(Q) 110#define MSGQ_FOREACH(P,Q) TAILQ_FOREACH(P,Q,link) 111#define MSGQ_REMOVE(Q,M) TAILQ_REMOVE(Q,M,link) 112#define MSGQ_INSERT_BEFORE(B,M) TAILQ_INSERT_BEFORE(B,M,link) 113#define MSGQ_APPEND(Q,M) TAILQ_INSERT_TAIL(Q,M,link) 114#define MSGQ_PEEK(Q) (TAILQ_EMPTY((Q)) ? NULL : TAILQ_FIRST((Q))) 115#define MSGQ_GET(Q) \ 116 ({ \ 117 struct sscop_msg *_m = NULL; \ 118 \ 119 if(!TAILQ_EMPTY(Q)) { \ 120 _m = TAILQ_FIRST(Q); \ 121 TAILQ_REMOVE(Q, _m, link); \ 122 } \ 123 _m; \ 124 }) 125 126#define MSGQ_CLEAR(Q) \ 127 do { \ 128 struct sscop_msg *_m1, *_m2; \ 129 \ 130 _m1 = TAILQ_FIRST(Q); \ 131 while(_m1 != NULL) { \ 132 _m2 = TAILQ_NEXT(_m1, link); \ 133 SSCOP_MSG_FREE(_m1); \ 134 _m1 = _m2; \ 135 } \ 136 TAILQ_INIT((Q)); \ 137 } while(0) 138 139/* 140 * Signal queues 141 */ 142typedef TAILQ_ENTRY(sscop_sig) sscop_sigq_link_t; 143typedef TAILQ_HEAD(sscop_sigq, sscop_sig) sscop_sigq_head_t; 144#define SIGQ_INIT(Q) TAILQ_INIT(Q) 145#define SIGQ_APPEND(Q,S) TAILQ_INSERT_TAIL(Q, S, link) 146#define SIGQ_EMPTY(Q) TAILQ_EMPTY(Q) 147#define SIGQ_GET(Q) \ 148 ({ \ 149 struct sscop_sig *_s = NULL; \ 150 \ 151 if(!TAILQ_EMPTY(Q)) { \ 152 _s = TAILQ_FIRST(Q); \ 153 TAILQ_REMOVE(Q, _s, link); \ 154 } \ 155 _s; \ 156 }) 157 158#define SIGQ_MOVE(F,T) \ 159 do { \ 160 struct sscop_sig *_s; \ 161 \ 162 while(!TAILQ_EMPTY(F)) { \ 163 _s = TAILQ_FIRST(F); \ 164 TAILQ_REMOVE(F, _s, link); \ 165 TAILQ_INSERT_TAIL(T, _s, link); \ 166 } \ 167 } while(0) 168 169#define SIGQ_PREPEND(F,T) \ 170 do { \ 171 struct sscop_sig *_s; \ 172 \ 173 while(!TAILQ_EMPTY(F)) { \ 174 _s = TAILQ_LAST(F, sscop_sigq); \ 175 TAILQ_REMOVE(F, _s, link); \ 176 TAILQ_INSERT_HEAD(T, _s, link); \ 177 } \ 178 } while(0) 179 180#define SIGQ_CLEAR(Q) \ 181 do { \ 182 struct sscop_sig *_s1, *_s2; \ 183 \ 184 _s1 = TAILQ_FIRST(Q); \ 185 while(_s1 != NULL) { \ 186 _s2 = TAILQ_NEXT(_s1, link); \ 187 SSCOP_MSG_FREE(_s1->msg); \ 188 SIG_FREE(_s1); \ 189 _s1 = _s2; \ 190 } \ 191 TAILQ_INIT(Q); \ 192 } while(0) 193 194 195 196/* 197 * Message buffers 198 */ 199/* Free a buffer (if there is one) */ 200#define MBUF_FREE(M) do { if(M) uni_msg_destroy(M); } while(0) 201 202/* duplicate a buffer */ 203#define MBUF_DUP(M) uni_msg_dup(M) 204 205/* compute current length */ 206#define MBUF_LEN(M) uni_msg_len((M)) 207 208/* 209 * Return the i-th word counted from the end of the buffer. 210 * i=-1 will return the last 32bit word, i=-2 the 2nd last. 211 * Assumes that there is enough space. 212 */ 213#define MBUF_TRAIL32(M,I) uni_msg_trail32((M), (I)) 214 215/* 216 * Strip 32bit value from the end 217 */ 218#define MBUF_STRIP32(M) uni_msg_strip32((M)) 219 220/* 221 * Strip 32bit value from head 222 */ 223#define MBUF_GET32(M) uni_msg_get32((M)) 224 225/* 226 * Append a 32bit value to an mbuf. Failures are ignored. 227 */ 228#define MBUF_APPEND32(M,W) uni_msg_append32((M), (W)) 229 230/* 231 * Pad a message to a multiple of four byte and return the amount of padding 232 * Failures are ignored. 233 */ 234#define MBUF_PAD4(M) \ 235 ({ \ 236 int _npad = 0; \ 237 while (uni_msg_len(M) % 4 != 0) { \ 238 uni_msg_append8((M), 0); \ 239 _npad++; \ 240 } \ 241 _npad; \ 242 }) 243 244#define MBUF_UNPAD(M,P) do { (M)->b_wptr -= (P); } while(0) 245 246/* 247 * Allocate a message that will probably hold N bytes. 248 */ 249#define MBUF_ALLOC(N) uni_msg_alloc(N) 250 251#ifdef SSCOP_DEBUG 252#define ASSERT(X) assert(X) 253#else 254#define ASSERT(X) 255#endif 256