2 * ===================================
3 * HARP | Host ATM Research Platform
4 * ===================================
7 * This Host ATM Research Platform ("HARP") file (the "Software") is
8 * made available by Network Computing Services, Inc. ("NetworkCS")
9 * "AS IS". NetworkCS does not provide maintenance, improvements or
10 * support of any kind.
12 * NETWORKCS MAKES NO WARRANTIES OR REPRESENTATIONS, EXPRESS OR IMPLIED,
13 * INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY
14 * AND FITNESS FOR A PARTICULAR PURPOSE, AS TO ANY ELEMENT OF THE
15 * SOFTWARE OR ANY SUPPORT PROVIDED IN CONNECTION WITH THIS SOFTWARE.
16 * In no event shall NetworkCS be responsible for any damages, including
17 * but not limited to consequential damages, arising from or relating to
18 * any use of the Software or related support.
20 * Copyright 1994-1998 Network Computing Services, Inc.
22 * Copies of this Software may be made, however, the above copyright
23 * notice must be reproduced on all copies.
27 * ATM Forum UNI 3.0/3.1 Signalling Manager
28 * ----------------------------------------
30 * Message buffer handling routines
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #include <sys/types.h>
38 #include <sys/systm.h>
39 #include <sys/errno.h>
41 #include <sys/socket.h>
42 #include <sys/socketvar.h>
44 #include <netatm/port.h>
45 #include <netatm/queue.h>
46 #include <netatm/atm.h>
47 #include <netatm/atm_sys.h>
48 #include <netatm/atm_sap.h>
49 #include <netatm/atm_cm.h>
50 #include <netatm/atm_if.h>
51 #include <netatm/atm_vc.h>
52 #include <netatm/atm_sigmgr.h>
53 #include <netatm/atm_stack.h>
54 #include <netatm/atm_pcb.h>
55 #include <netatm/atm_var.h>
57 #include <netatm/uni/unisig_var.h>
58 #include <netatm/uni/unisig_mbuf.h>
59 #include <netatm/uni/unisig_msg.h>
62 * Initialize a unisig formatting structure
65 * usf pointer to a unisig formatting structure
66 * usp pointer to a unisig protocol instance
67 * buf pointer to a buffer chain (decode only)
68 * op operation code (encode or decode)
69 * headroom headroom to leave in first buffer
73 * errno error encountered
77 usf_init(usf, usp, buf, op, headroom)
86 ATM_DEBUG3("usf_init: usf=%p, buf=%p, op=%d\n",
101 KB_ALLOCPKT(m, USF_MIN_ALLOC, KB_F_NOWAIT, KB_T_DATA);
105 if (headroom < KB_BFRLEN(m)) {
106 KB_HEADSET(m, headroom);
112 * Verify buffer address
124 * Save parameters in formatting structure
137 * Get or put the next byte of a signalling message
140 * usf pointer to a unisig formatting structure
141 * c pointer to the byte to send from or receive into
145 * errno error encountered
154 KBuffer *m = usf->usf_m_addr, *m1;
157 switch (usf->usf_op) {
161 * Make sure we're not past the end of the buffer
162 * (allowing for zero-length buffers)
164 while (usf->usf_loc >= KB_LEN(m)) {
165 if (KB_NEXT(usf->usf_m_addr)) {
166 usf->usf_m_addr = m = KB_NEXT(usf->usf_m_addr);
174 * Get the data from the buffer
176 KB_DATASTART(m, mp, u_char *);
177 *c = mp[usf->usf_loc];
183 * If the current buffer is full, get another
185 KB_TAILROOM(m, space);
187 KB_ALLOC(m1, USF_MIN_ALLOC, KB_F_NOWAIT, KB_T_DATA);
192 usf->usf_m_addr = m = m1;
197 * Put the data into the buffer
199 KB_DATASTART(m, mp, u_char *);
200 mp[usf->usf_loc] = *c;
207 * Invalid operation code
217 * Get or put a short integer
220 * usf pointer to a unisig formatting structure
221 * s pointer to a short to send from or receive into
225 * errno error encountered
237 u_char b[sizeof(u_short)];
241 if (usf->usf_op == USF_ENCODE)
242 tval.value = htons(*s);
244 if ((rc = usf_byte(usf, &tval.b[0])) != 0)
246 if ((rc = usf_byte(usf, &tval.b[1])) != 0)
249 if (usf->usf_op == USF_DECODE)
250 *s = ntohs(tval.value);
257 * Get or put a 3-byte integer
260 * usf pointer to a unisig formatting structure
261 * i pointer to an integer to send from or receive into
265 * errno error encountered
277 u_char b[sizeof(u_int)];
282 if (usf->usf_op == USF_ENCODE)
283 tval.value = htonl(*i);
285 for (j=0; j<3; j++) {
286 rc = usf_byte(usf, &tval.b[j+sizeof(u_int)-3]);
291 if (usf->usf_op == USF_DECODE)
292 *i = ntohl(tval.value);
299 * Get or put an integer
302 * usf pointer to a unisig formatting structure
303 * i pointer to an integer to send from or receive into
307 * errno error encountered
319 u_char b[sizeof(u_int)];
322 if (usf->usf_op == USF_ENCODE)
323 tval.value = htonl(*i);
325 for (j=0; j<4; j++) {
326 rc = usf_byte(usf, &tval.b[j+sizeof(u_int)-4]);
331 if (usf->usf_op == USF_DECODE)
332 *i = ntohl(tval.value);
339 * Get or put an extented field
341 * An extented field consists of a string of bytes. All but the last
342 * byte of the field has the high-order bit set to zero. When decoding,
343 * this routine will read bytes until either the input is exhausted or
344 * a byte with a high-order one is found. Whe encoding, it will take an
345 * unsigned integer and write until the highest-order one bit has been
349 * usf pointer to a unisig formatting structure
350 * i pointer to an integer to send from or receive into
354 * errno error encountered
364 u_char c, buff[sizeof(u_int)+1];
368 u_char b[sizeof(u_int)];
371 switch(usf->usf_op) {
377 tval.value = htonl(val);
378 buff[j] = tval.b[sizeof(u_int)-1] & UNI_IE_EXT_MASK;
383 buff[0] |= UNI_IE_EXT_BIT;
385 rc = usf_byte(usf, &buff[j]);
394 while (!(c & UNI_IE_EXT_BIT)) {
395 rc = usf_byte(usf, &c);
398 val = (val << 7) + (c & UNI_IE_EXT_MASK);
412 * Count the bytes remaining to be decoded
415 * usf pointer to a unisig formatting structure
418 * int the number of bytes in the buffer chain remaining to
427 KBuffer *m = usf->usf_m_addr;
430 * Return zero if we're not decoding
432 if (usf->usf_op != USF_DECODE)
436 * Calculate the length of data remaining in the current buffer
438 count = KB_LEN(m) - usf->usf_loc;
441 * Loop through any remaining buffers, adding in their lengths
454 * Get or put the next byte of a signalling message and return
455 * the byte's buffer address
458 * usf pointer to a unisig formatting structure
459 * c pointer to the byte to send from or receive into
460 * bp address to store the byte's buffer address
464 * errno error encountered
468 usf_byte_mark(usf, c, bp)
477 * First, get/put the data byte
479 rc = usf_byte(usf, c);
490 * Now return the buffer address of that byte
492 KB_DATASTART(usf->usf_m_addr, mp, u_char *);
493 *bp = &mp[usf->usf_loc - 1];