3 * ===================================
4 * HARP | Host ATM Research Platform
5 * ===================================
8 * This Host ATM Research Platform ("HARP") file (the "Software") is
9 * made available by Network Computing Services, Inc. ("NetworkCS")
10 * "AS IS". NetworkCS does not provide maintenance, improvements or
11 * support of any kind.
13 * NETWORKCS MAKES NO WARRANTIES OR REPRESENTATIONS, EXPRESS OR IMPLIED,
14 * INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY
15 * AND FITNESS FOR A PARTICULAR PURPOSE, AS TO ANY ELEMENT OF THE
16 * SOFTWARE OR ANY SUPPORT PROVIDED IN CONNECTION WITH THIS SOFTWARE.
17 * In no event shall NetworkCS be responsible for any damages, including
18 * but not limited to consequential damages, arising from or relating to
19 * any use of the Software or related support.
21 * Copyright 1994-1998 Network Computing Services, Inc.
23 * Copies of this Software may be made, however, the above copyright
24 * notice must be reproduced on all copies.
26 * @(#) $FreeBSD: src/sys/netatm/uni/unisig_mbuf.c,v 1.5 1999/08/28 00:49:05 peter Exp $
27 * @(#) $DragonFly: src/sys/netproto/atm/uni/unisig_mbuf.c,v 1.4 2003/08/07 21:54:34 dillon Exp $
31 * ATM Forum UNI 3.0/3.1 Signalling Manager
32 * ----------------------------------------
34 * Message buffer handling routines
38 #include <netproto/atm/kern_include.h>
40 #include "unisig_var.h"
41 #include "unisig_mbuf.h"
42 #include "unisig_msg.h"
45 * Initialize a unisig formatting structure
48 * usf pointer to a unisig formatting structure
49 * usp pointer to a unisig protocol instance
50 * buf pointer to a buffer chain (decode only)
51 * op operation code (encode or decode)
52 * headroom headroom to leave in first buffer
56 * errno error encountered
60 usf_init(usf, usp, buf, op, headroom)
69 ATM_DEBUG3("usf_init: usf=%p, buf=%p, op=%d\n",
84 KB_ALLOCPKT(m, USF_MIN_ALLOC, KB_F_NOWAIT, KB_T_DATA);
88 if (headroom < KB_BFRLEN(m)) {
89 KB_HEADSET(m, headroom);
95 * Verify buffer address
107 * Save parameters in formatting structure
120 * Get or put the next byte of a signalling message
123 * usf pointer to a unisig formatting structure
124 * c pointer to the byte to send from or receive into
128 * errno error encountered
137 KBuffer *m = usf->usf_m_addr, *m1;
140 switch (usf->usf_op) {
144 * Make sure we're not past the end of the buffer
145 * (allowing for zero-length buffers)
147 while (usf->usf_loc >= KB_LEN(m)) {
148 if (KB_NEXT(usf->usf_m_addr)) {
149 usf->usf_m_addr = m = KB_NEXT(usf->usf_m_addr);
157 * Get the data from the buffer
159 KB_DATASTART(m, mp, u_char *);
160 *c = mp[usf->usf_loc];
166 * If the current buffer is full, get another
168 KB_TAILROOM(m, space);
170 KB_ALLOC(m1, USF_MIN_ALLOC, KB_F_NOWAIT, KB_T_DATA);
175 usf->usf_m_addr = m = m1;
180 * Put the data into the buffer
182 KB_DATASTART(m, mp, u_char *);
183 mp[usf->usf_loc] = *c;
190 * Invalid operation code
200 * Get or put a short integer
203 * usf pointer to a unisig formatting structure
204 * s pointer to a short to send from or receive into
208 * errno error encountered
220 u_char b[sizeof(u_short)];
224 if (usf->usf_op == USF_ENCODE)
225 tval.value = htons(*s);
227 if ((rc = usf_byte(usf, &tval.b[0])) != 0)
229 if ((rc = usf_byte(usf, &tval.b[1])) != 0)
232 if (usf->usf_op == USF_DECODE)
233 *s = ntohs(tval.value);
240 * Get or put a 3-byte integer
243 * usf pointer to a unisig formatting structure
244 * i pointer to an integer to send from or receive into
248 * errno error encountered
260 u_char b[sizeof(u_int)];
265 if (usf->usf_op == USF_ENCODE)
266 tval.value = htonl(*i);
268 for (j=0; j<3; j++) {
269 rc = usf_byte(usf, &tval.b[j+sizeof(u_int)-3]);
274 if (usf->usf_op == USF_DECODE)
275 *i = ntohl(tval.value);
282 * Get or put an integer
285 * usf pointer to a unisig formatting structure
286 * i pointer to an integer to send from or receive into
290 * errno error encountered
302 u_char b[sizeof(u_int)];
305 if (usf->usf_op == USF_ENCODE)
306 tval.value = htonl(*i);
308 for (j=0; j<4; j++) {
309 rc = usf_byte(usf, &tval.b[j+sizeof(u_int)-4]);
314 if (usf->usf_op == USF_DECODE)
315 *i = ntohl(tval.value);
322 * Get or put an extented field
324 * An extented field consists of a string of bytes. All but the last
325 * byte of the field has the high-order bit set to zero. When decoding,
326 * this routine will read bytes until either the input is exhausted or
327 * a byte with a high-order one is found. Whe encoding, it will take an
328 * unsigned integer and write until the highest-order one bit has been
332 * usf pointer to a unisig formatting structure
333 * i pointer to an integer to send from or receive into
337 * errno error encountered
347 u_char c, buff[sizeof(u_int)+1];
351 u_char b[sizeof(u_int)];
354 switch(usf->usf_op) {
360 tval.value = htonl(val);
361 buff[j] = tval.b[sizeof(u_int)-1] & UNI_IE_EXT_MASK;
366 buff[0] |= UNI_IE_EXT_BIT;
368 rc = usf_byte(usf, &buff[j]);
377 while (!(c & UNI_IE_EXT_BIT)) {
378 rc = usf_byte(usf, &c);
381 val = (val << 7) + (c & UNI_IE_EXT_MASK);
395 * Count the bytes remaining to be decoded
398 * usf pointer to a unisig formatting structure
401 * int the number of bytes in the buffer chain remaining to
410 KBuffer *m = usf->usf_m_addr;
413 * Return zero if we're not decoding
415 if (usf->usf_op != USF_DECODE)
419 * Calculate the length of data remaining in the current buffer
421 count = KB_LEN(m) - usf->usf_loc;
424 * Loop through any remaining buffers, adding in their lengths
437 * Get or put the next byte of a signalling message and return
438 * the byte's buffer address
441 * usf pointer to a unisig formatting structure
442 * c pointer to the byte to send from or receive into
443 * bp address to store the byte's buffer address
447 * errno error encountered
451 usf_byte_mark(usf, c, bp)
460 * First, get/put the data byte
462 rc = usf_byte(usf, c);
473 * Now return the buffer address of that byte
475 KB_DATASTART(usf->usf_m_addr, mp, u_char *);
476 *bp = &mp[usf->usf_loc - 1];