2 * Copyright (c) 2001-2003
3 * Fraunhofer Institute for Open Communication Systems (FhG Fokus).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
27 * Author: Hartmut Brandt <harti@freebsd.org>
29 * $Begemot: libunimsg/netnatm/msg/privmsg.c,v 1.8 2003/10/10 14:50:05 hbb Exp $
31 * Private definitions for the MSG code file.
33 * This file is included at the begin of the automatically generated
38 * Decode a UNI message header.
41 * -1 - ignore message (proto, length, CR error)
44 uni_decode_head(struct uni_msg *msg, struct uni_all *out,
45 struct unicx *cx __unused)
50 (void)memset(out, 0, sizeof(struct uni_all));
52 if(uni_msg_len(msg) < 9)
53 return -1; /* Q.2931 5.6.2 */
55 if(*msg->b_rptr++ != PNNI_PROTO)
56 return -1; /* Q.2931 5.6.1 */
58 if(*msg->b_rptr++ != UNI_PROTO)
59 return -1; /* Q.2931 5.6.1 */
61 if(*msg->b_rptr++ != 3)
62 return -1; /* Q.2931 5.6.3.1 */
64 out->u.hdr.cref.flag = (*msg->b_rptr & 0x80) ? 1 : 0;
65 out->u.hdr.cref.cref = (*msg->b_rptr++ & 0x7f) << 16;
66 out->u.hdr.cref.cref |= *msg->b_rptr++ << 8;
67 out->u.hdr.cref.cref |= *msg->b_rptr++;
69 out->mtype = *msg->b_rptr++;
72 * Be not too piggy about this byte
74 switch(*msg->b_rptr & 0x13) {
76 case 0x00: case 0x01: case 0x02: case 0x03:
77 out->u.hdr.act = UNI_MSGACT_DEFAULT;
80 case 0x10: case 0x11: case 0x12:
81 out->u.hdr.act = *msg->b_rptr & 0x3;
84 case 0x13: /* Q.2931 5.7.1 */
85 out->u.hdr.act = UNI_MSGACT_REPORT;
88 if(cx->pnni && (*msg->b_rptr & 0x08))
95 mlen = *msg->b_rptr++ << 8;
96 mlen |= *msg->b_rptr++;
99 * If the message is longer than the indicated length
100 * shorten it. If it is shorter, probably one of the IE
101 * decoders will break, but we should proceed. 5.5.6.5
104 if(uni_msg_len(msg) > mlen)
105 msg->b_wptr = msg->b_rptr + mlen;
112 uni_decode_body_internal(enum uni_msgtype mtype, struct uni_msg *msg,
113 union uni_msgall *out, struct unicx *cx)
115 enum uni_ietype ietype;
116 struct uni_iehdr hdr;
118 const struct iedecl *iedecl;
122 cx->ielast = (enum uni_ietype)0;
123 cx->repeat.h.present = 0;
125 while (uni_msg_len(msg) != 0) {
126 if (uni_decode_ie_hdr(&ietype, &hdr, msg, cx, &ielen)) {
128 * Short header. Set the ielen to an impossible size.
129 * Then we should bump out in the error handling below.
130 * We should have at least an IE type here.
135 printf("IE %x\n", ietype);
138 if ((iedecl = GET_IEDECL(ietype, hdr.coding)) == NULL ||
139 ietype == UNI_IE_UNREC) {
141 * entirly unknown IE. Check the length and skip it.
144 if (ielen > uni_msg_len(msg))
145 msg->b_rptr = msg->b_wptr;
147 msg->b_rptr += ielen;
148 UNI_SAVE_IERR(cx, ietype, hdr.act, UNI_IERR_UNK);
153 printf("IE %x known\n", ietype);
155 if (ielen > iedecl->maxlen - 4 || ielen > uni_msg_len(msg)) {
157 * Information element too long -> content error.
158 * Let the decoding routine set the error flag and
164 * It is not clear how to best handle this error.
166 if (ielen > iedecl->maxlen - 4)
167 ielen = iedecl->maxlen - 4;
170 if (ielen > uni_msg_len(msg))
171 ielen = uni_msg_len(msg);
173 hdr.present |= UNI_IE_ERROR;
176 printf("IE %x length too large\n", ietype);
182 printf("IE %x length ok\n", ietype);
184 end = msg->b_rptr + ielen;
185 ret = uni_msgtable[mtype]->decode(out, msg, ietype,
190 printf("IE %x ret %d\n", ietype, ret);
195 case DEC_OK: /* ok */
198 case DEC_ILL: /* illegal IE */
200 * Unexpected but recognized.
203 UNI_SAVE_IERR(cx, ietype, hdr.act, UNI_IERR_UNK);
207 case DEC_ERR: /* bad IE */
208 if (iedecl->flags & UNIFL_ACCESS)
209 /* this may be wrong: 5.6.8.2 */
210 UNI_SAVE_IERR(cx, ietype, hdr.act, UNI_IERR_ACC);
212 UNI_SAVE_IERR(cx, ietype, hdr.act, UNI_IERR_BAD);
217 PANIC(("bad decode return"));
220 if (ietype != UNI_IE_REPEAT)
221 cx->repeat.h.present = 0;
227 * Decode the body of a message. The header is assumed to be decoded
228 * already and out->hdr is filled in. Only information elements remain.
231 uni_decode_body(struct uni_msg *msg, struct uni_all *out, struct unicx *cx)
234 if (out->mtype >= 256)
236 if (uni_msgtable[out->mtype] == NULL)
238 return (uni_decode_body_internal(out->mtype, msg, &out->u, cx));
243 * Decode a uni message
246 uni_decode(struct uni_msg *msg, struct uni_all *out, struct unicx *cx)
249 if (uni_decode_head(msg, out, cx))
251 if (uni_decode_body(msg, out, cx))
257 uni_encode(struct uni_msg *msg, struct uni_all *in, struct unicx *cx)
259 if (in->mtype >= 256)
261 if (uni_msgtable[in->mtype] == NULL)
264 return ((uni_msgtable[in->mtype]->encode)(msg, &in->u, cx));
268 * Doesn't belong here
271 uni_initcx(struct unicx *cx)
273 memset(cx, 0, sizeof(struct unicx));