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 $
31 * ATM Forum UNI 3.0/3.1 Signalling Manager
32 * ----------------------------------------
34 * Message buffer handling routines
38 #include <netatm/kern_include.h>
40 #include <netatm/uni/unisig_var.h>
41 #include <netatm/uni/unisig_mbuf.h>
42 #include <netatm/uni/unisig_msg.h>
45 __RCSID("@(#) $FreeBSD: src/sys/netatm/uni/unisig_mbuf.c,v 1.5 1999/08/28 00:49:05 peter Exp $");
50 * Initialize a unisig formatting structure
53 * usf pointer to a unisig formatting structure
54 * usp pointer to a unisig protocol instance
55 * buf pointer to a buffer chain (decode only)
56 * op operation code (encode or decode)
57 * headroom headroom to leave in first buffer
61 * errno error encountered
65 usf_init(usf, usp, buf, op, headroom)
74 ATM_DEBUG3("usf_init: usf=%p, buf=%p, op=%d\n",
89 KB_ALLOCPKT(m, USF_MIN_ALLOC, KB_F_NOWAIT, KB_T_DATA);
93 if (headroom < KB_BFRLEN(m)) {
94 KB_HEADSET(m, headroom);
100 * Verify buffer address
112 * Save parameters in formatting structure
125 * Get or put the next byte of a signalling message
128 * usf pointer to a unisig formatting structure
129 * c pointer to the byte to send from or receive into
133 * errno error encountered
142 KBuffer *m = usf->usf_m_addr, *m1;
145 switch (usf->usf_op) {
149 * Make sure we're not past the end of the buffer
150 * (allowing for zero-length buffers)
152 while (usf->usf_loc >= KB_LEN(m)) {
153 if (KB_NEXT(usf->usf_m_addr)) {
154 usf->usf_m_addr = m = KB_NEXT(usf->usf_m_addr);
162 * Get the data from the buffer
164 KB_DATASTART(m, mp, u_char *);
165 *c = mp[usf->usf_loc];
171 * If the current buffer is full, get another
173 KB_TAILROOM(m, space);
175 KB_ALLOC(m1, USF_MIN_ALLOC, KB_F_NOWAIT, KB_T_DATA);
180 usf->usf_m_addr = m = m1;
185 * Put the data into the buffer
187 KB_DATASTART(m, mp, u_char *);
188 mp[usf->usf_loc] = *c;
195 * Invalid operation code
205 * Get or put a short integer
208 * usf pointer to a unisig formatting structure
209 * s pointer to a short to send from or receive into
213 * errno error encountered
225 u_char b[sizeof(u_short)];
229 if (usf->usf_op == USF_ENCODE)
230 tval.value = htons(*s);
232 if ((rc = usf_byte(usf, &tval.b[0])) != 0)
234 if ((rc = usf_byte(usf, &tval.b[1])) != 0)
237 if (usf->usf_op == USF_DECODE)
238 *s = ntohs(tval.value);
245 * Get or put a 3-byte integer
248 * usf pointer to a unisig formatting structure
249 * i pointer to an integer to send from or receive into
253 * errno error encountered
265 u_char b[sizeof(u_int)];
270 if (usf->usf_op == USF_ENCODE)
271 tval.value = htonl(*i);
273 for (j=0; j<3; j++) {
274 rc = usf_byte(usf, &tval.b[j+sizeof(u_int)-3]);
279 if (usf->usf_op == USF_DECODE)
280 *i = ntohl(tval.value);
287 * Get or put an integer
290 * usf pointer to a unisig formatting structure
291 * i pointer to an integer to send from or receive into
295 * errno error encountered
307 u_char b[sizeof(u_int)];
310 if (usf->usf_op == USF_ENCODE)
311 tval.value = htonl(*i);
313 for (j=0; j<4; j++) {
314 rc = usf_byte(usf, &tval.b[j+sizeof(u_int)-4]);
319 if (usf->usf_op == USF_DECODE)
320 *i = ntohl(tval.value);
327 * Get or put an extented field
329 * An extented field consists of a string of bytes. All but the last
330 * byte of the field has the high-order bit set to zero. When decoding,
331 * this routine will read bytes until either the input is exhausted or
332 * a byte with a high-order one is found. Whe encoding, it will take an
333 * unsigned integer and write until the highest-order one bit has been
337 * usf pointer to a unisig formatting structure
338 * i pointer to an integer to send from or receive into
342 * errno error encountered
352 u_char c, buff[sizeof(u_int)+1];
356 u_char b[sizeof(u_int)];
359 switch(usf->usf_op) {
365 tval.value = htonl(val);
366 buff[j] = tval.b[sizeof(u_int)-1] & UNI_IE_EXT_MASK;
371 buff[0] |= UNI_IE_EXT_BIT;
373 rc = usf_byte(usf, &buff[j]);
382 while (!(c & UNI_IE_EXT_BIT)) {
383 rc = usf_byte(usf, &c);
386 val = (val << 7) + (c & UNI_IE_EXT_MASK);
400 * Count the bytes remaining to be decoded
403 * usf pointer to a unisig formatting structure
406 * int the number of bytes in the buffer chain remaining to
415 KBuffer *m = usf->usf_m_addr;
418 * Return zero if we're not decoding
420 if (usf->usf_op != USF_DECODE)
424 * Calculate the length of data remaining in the current buffer
426 count = KB_LEN(m) - usf->usf_loc;
429 * Loop through any remaining buffers, adding in their lengths
442 * Get or put the next byte of a signalling message and return
443 * the byte's buffer address
446 * usf pointer to a unisig formatting structure
447 * c pointer to the byte to send from or receive into
448 * bp address to store the byte's buffer address
452 * errno error encountered
456 usf_byte_mark(usf, c, bp)
465 * First, get/put the data byte
467 rc = usf_byte(usf, c);
478 * Now return the buffer address of that byte
480 KB_DATASTART(usf->usf_m_addr, mp, u_char *);
481 *bp = &mp[usf->usf_loc - 1];