Don't allow the user to delete a fec that still has nexthops
[mpls-ldp-portable.git] / ldp / ldp_nortel.c
blob3ae2554c70a713fd4596165982c5c5903269d301
2 /******************************************************************************
3 * Nortel Networks Software License *
4 * *
5 * READ THE TERMS OF THIS LICENSE CAREFULLY. BY USING, MODIFYING, OR *
6 * DISTRIBUTING THIS SOFTWARE AND ANY ACCOMPANYING DOCUMENTATION (COLLECTIVELY,*
7 * "SOFTWARE") YOU ARE AGREEING TO ALL OF THE TERMS OF THIS LICENSE. *
8 * *
9 * 1. Nortel Telecom Limited, on behalf of itself and its subsidiaries *
10 * (collectively "Nortel Networks") grants to you a non-exclusive, perpetual, *
11 * world-wide right to use, copy, modify, and distribute the Software at no *
12 * charge. *
13 * *
14 * 2. You may sublicense recipients of redistributed Software to use, *
15 * copy, modify, and distribute the Software on substantially the same terms as*
16 * this License. You may not impose any further restrictions on the *
17 * recipient's exercise of the rights in the Software granted under this *
18 * License. Software distributed to other parties must be accompanied by a *
19 * License containing a grant, disclaimer and limitation of liability *
20 * substantially in the form of 3, 4, and 5 below provided that references to *
21 * "Nortel Networks" may be changed to "Supplier". *
22 * *
23 * 3. Nortel Networks reserves the right to modify and release new *
24 * versions of the Software from time to time which may include modifications *
25 * made by third parties like you. Accordingly, you agree that you shall *
26 * automatically grant a license to Nortel Networks to include, at its option, *
27 * in any new version of the Software any modifications to the Software made by*
28 * you and made available directly or indirectly to Nortel Networks. Nortel *
29 * Networks shall have the right to use, copy, modify, and distribute any such *
30 * modified Software on substantially the same terms as this License. *
31 * *
32 * 4. THE SOFTWARE IS PROVIDED ON AN "AS IS" BASIS. NORTEL NETWORKS AND *
33 * ITS AGENTS AND SUPPLIERS DISCLAIM ALL REPRESENTATIONS, WARRANTIES AND *
34 * CONDITIONS RELATING TO THE SOFTWARE, INCLUDING, BUT NOT LIMITED TO, IMPLIED *
35 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND *
36 * NON-INFRINGEMENT OF THIRD-PARTY INTELLECTUAL PROPERTY RIGHTS. NORTEL *
37 * NETWORKS AND ITS AGENTS AND SUPPLIERS DO NOT WARRANT, GUARANTEE, OR MAKE ANY*
38 * REPRESENTATIONS REGARDING THE USE, OR THE RESULTS OF THE USE, OF THE *
39 * SOFTWARE IN TERMS OR CORRECTNESS, ACCURACY, RELIABILITY, CURRENTNESS, OR *
40 * OTHERWISE. *
41 * *
42 * 5. NEITHER NORTEL NETWORKS NOR ANY OF ITS AGENTS OR SUPPLIERS SHALL BE *
43 * LIABLE FOR ANY DIRECT, INDIRECT, CONSEQUENTIAL, INCIDENTAL OR EXEMPLARY *
44 * DAMAGES, OR ECONOMIC LOSSES (INCLUDING DAMAGES FOR LOSS OF BUSINESS PROFITS,*
45 * BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION AND THE LIKE), ARISING *
46 * FROM THE SOFTWARE OR THIS LICENSE AGREEMENT, EVEN IF NORTEL NETWORKS OR SUCH*
47 * AGENT OR SUPPLIER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES OR *
48 * LOSSES, AND WHETHER ANY SUCH DAMAGE OR LOSS ARISES OUT OF CONTRACT, TORT, OR*
49 * OTHERWISE. *
50 * *
51 * 6. This License shall be governed by the laws of the Province of *
52 * Ontario, Canada. *
53 *******************************************************************************/
55 /******************************************************************************
56 * This file contains the C implementation for encode/decode functions *
57 * for the following types of messages: notification, hello, initialization, *
58 * keepAlive, address, address Withdraw, label Mapping, label Request, label *
59 * Withdraw and label Release. There are also encode/decode methods for all *
60 * tlv types required by the previously enumerated messages. *
61 * Please remember that the pdu will always contain the header followed by 1 *
62 * or more LDP messages. The file contains functions to encode/decode the LDP *
63 * header as well. *
64 * All the messages, header message and the tlvs are in conformity with the *
65 * draft-ietf-mpls-ldp-04 (May 1999) and with draft-ietf-mpls-cr-ldp-01 *
66 * (Jan 1999). *
67 * *
68 * Please note that the U bit in the message and the F bit in the tlv are *
69 * ignored in this version of the code. *
70 * *
71 * Please note that the traffic parameters for traffic TLV have to be IEEE *
72 * single precision floating point numbers. *
73 * *
74 * Please note that there might be a small chance for bit field manipulation *
75 * portability inconsistency. If such problems occure, the code requires *
76 * changes for a suitable bit-field manipulation. The code for encoding and *
77 * decoding makes the assumption that the compiler packs the bit fields in a *
78 * structure into adjacent bits of the same unit. *
79 * *
80 * The usage of the encode/decode functions is described below. *
81 * *
82 * The encode functions take as arguments: a pointer to the structure which *
83 * implements msg/tlv, a buffer (where the encoding is done) and the buffer *
84 * length. *
85 * If the encode is successfull, the function returns the total encoded *
86 * length. *
87 * If the encode fails, the function returns an error code. *
88 * The encode functions for messages and message headers do not modify the *
89 * content of the struct which is to be encoded. All the other encode *
90 * functions will change the content of the structure. The pointer which *
91 * points to the beginning of the buffer is not changed. *
92 * *
93 * The decode functions take as arguments: a pointer to the structure which *
94 * is going to be populated after decoding, a pointer to a buffer and the *
95 * buffer length. *
96 * If the decode is successful, the function returns the total decoded length *
97 * If the decode fails, the function returns an error code. The decode *
98 * functions do not modify the pointer to the buffer which contains the data *
99 * to be decoded. *
101 * Example on how to use the encode/decode functions for a keepAlive message: *
103 * Encode the keep alive message: *
104 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ *
105 * u_char buffer[500]; *
106 * int returnCode; *
107 * struct mplsLdpKeepAlMsg_s keepAliveMsg; *
108 * keepAliveMsg.baseMsg.msgType = MPLS_KEEPAL_MSGTYPE; *
109 * keepAliveMsg.baseMsg.msgLength = MPLS_MSGIDFIXLEN; *
110 * keepAliveMsg.baseMsg.msgId = 123; *
111 * memset(buffer, 0, 500); *
112 * returnCode = Mpls_encodeLdpKeepAliveMsg(&keepAliveMsg, *
113 * buffer, *
114 * 500); *
115 * if (returnCode < 0) *
116 * check the error code *
117 * else *
118 * write(fd, buffer, returnCode); *
121 * Decode the keep alive meesage: *
122 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ *
123 * u_char buffer[500]; *
124 * int returnCode; *
125 * struct mplsLdpKeepAlMsg_s keepAliveMsg; *
126 * read(fd, buffer, length); *
127 * returnCode = Mpls_decodeLdpKeepAliveMsg(&keepAliveMsg, *
128 * buffer, *
129 * 500); *
130 * if (returnCode < 0) *
131 * check the error code *
132 * else *
133 * { *
134 * printKeepAliveMsg(&keepAliveMsg); *
135 * } *
137 * An example on how to use the decode functions for the header and the *
138 * messages can be found in the main function. *
140 * The code was tested for big endian and little endian for sparc5, linux *
141 * and i960. *
143 * In order to compile for little endian, the LITTLE_ENDIAN_BYTE_ORDER should *
144 * be defined. *
146 * At the end of this file there is an examples of a hex buffers and its *
147 * corresponding values. *
150 * Version History *
151 * Version Date Authors Description *
152 * =========== ======== ========= ====================== *
153 * mpls_encdec_01.c 99/03/15 Antonela Paraschiv draft-ietf-mpls-ldp-03 and *
154 * draft-ietf-mpls-cr-ldp-01 *
156 * mpls_encdec_02.c 99/05/19 Antonela Paraschiv draft-ietf-mpls-ldp-04 and *
157 * draft-ietf-mpls-cr-ldp-01 *
159 ******************************************************************************/
161 #ifdef VXWORKS
162 #include <in.h> /* htons, htonl, ntohs, ntohl */
163 #include <types.h> /* u_int, u_char, u_short, float etc. */
164 #else
165 #include <netinet/in.h> /* htons, htonl, ntohs, ntohl */
166 #include <sys/types.h> /* u_int, u_char, u_short, float etc. */
167 #endif /* VXWORKS */
169 #include "ldp_struct.h"
170 #include "mpls_trace_impl.h"
171 #include "ldp_nortel.h"
173 int global_ldp_pdu_debug = 0;
176 * Encode-decode for Ldp Msg Header
180 * Encode:
182 int Mpls_encodeLdpMsgHeader
183 (mplsLdpHeader_t * header, u_char * buff, int bufSize) {
184 mplsLdpHeader_t headerCopy;
186 if (MPLS_LDP_HDRSIZE > bufSize) {
187 /* not enough room for header */
188 return MPLS_ENC_BUFFTOOSMALL;
191 headerCopy = *header;
192 headerCopy.protocolVersion = htons(headerCopy.protocolVersion);
193 headerCopy.pduLength = htons(headerCopy.pduLength);
194 headerCopy.lsrAddress = htonl(headerCopy.lsrAddress);
195 headerCopy.labelSpace = htons(headerCopy.labelSpace);
197 MEM_COPY(buff, (u_char *) & headerCopy, MPLS_LDP_HDRSIZE);
199 return MPLS_LDP_HDRSIZE;
201 } /* End : Mpls_encodeLdpMsgHeader */
204 * Decode:
206 int Mpls_decodeLdpMsgHeader
207 (mplsLdpHeader_t * header, u_char * buff, int bufSize) {
208 if (MPLS_LDP_HDRSIZE > bufSize) {
209 return MPLS_DEC_BUFFTOOSMALL;
212 MEM_COPY((u_char *) header, buff, MPLS_LDP_HDRSIZE);
214 header->protocolVersion = ntohs(header->protocolVersion);
215 header->pduLength = ntohs(header->pduLength);
216 header->lsrAddress = ntohl(header->lsrAddress);
217 header->labelSpace = ntohs(header->labelSpace);
219 /* check if the length is over the max length */
220 if (header->pduLength > MPLS_PDUMAXLEN) {
221 return MPLS_PDU_LENGTH_ERROR;
224 return MPLS_LDP_HDRSIZE;
226 } /* End: Mpls_decodeLdpMsgHeader */
229 * Encode-decode for Ldp Base Message
233 * Encode:
235 int Mpls_encodeLdpBaseMsg(mplsLdpMsg_t * ldpMsg, u_char * buff, int bufSize)
237 if (MPLS_MSGIDFIXLEN + MPLS_TLVFIXLEN > bufSize) {
238 /* not enough room for header */
239 return MPLS_ENC_BUFFTOOSMALL;
242 ldpMsg->flags.mark = htons(ldpMsg->flags.mark);
243 ldpMsg->msgLength = htons(ldpMsg->msgLength);
244 ldpMsg->msgId = htonl(ldpMsg->msgId);
246 MEM_COPY(buff, (u_char *) ldpMsg, MPLS_MSGIDFIXLEN + MPLS_TLVFIXLEN);
248 return (MPLS_MSGIDFIXLEN + MPLS_TLVFIXLEN);
250 } /* End : Mpls_encodeLdpBaseMsg */
253 * Decode:
255 int Mpls_decodeLdpBaseMsg(mplsLdpMsg_t * ldpMsg, u_char * buff, int bufSize)
257 if (MPLS_MSGIDFIXLEN + MPLS_TLVFIXLEN > bufSize) {
258 return MPLS_DEC_BUFFTOOSMALL;
261 MEM_COPY((u_char *) ldpMsg, buff, MPLS_MSGIDFIXLEN + MPLS_TLVFIXLEN);
263 ldpMsg->flags.mark = ntohs(ldpMsg->flags.mark);
264 ldpMsg->msgLength = ntohs(ldpMsg->msgLength);
265 ldpMsg->msgId = ntohl(ldpMsg->msgId);
267 return MPLS_MSGIDFIXLEN + MPLS_TLVFIXLEN;
269 } /* End: Mpls_decodeLdpBaseMsg */
272 * Encode-decode for ATM Label Range Component
276 * encode:
278 int Mpls_encodeLdpAtmLblRng
279 (mplsLdpAtmLblRng_t * atmLbl, u_char * buff, int bufSize) {
280 if (MPLS_ATMLRGFIXLEN > bufSize) {
281 /* not enough room for label */
282 return MPLS_ENC_BUFFTOOSMALL;
285 atmLbl->flags.flags.res1 = 0;
286 atmLbl->flags.flags.res2 = 0;
287 atmLbl->flags.mark[0] = htonl(atmLbl->flags.mark[0]);
288 atmLbl->flags.mark[1] = htonl(atmLbl->flags.mark[1]);
290 MEM_COPY(buff, (u_char *) atmLbl, MPLS_ATMLRGFIXLEN);
292 return MPLS_ATMLRGFIXLEN;
294 } /* End Mpls_encodeLdpAtmLblRng */
297 * decode:
299 int Mpls_decodeLdpAtmLblRng
300 (mplsLdpAtmLblRng_t * atmLbl, u_char * buff, int bufSize) {
301 if (MPLS_ATMLRGFIXLEN > bufSize) {
302 PRINT_ERR("failed decoding the Atm Lbl Rng\n");
303 return MPLS_DEC_BUFFTOOSMALL;
306 MEM_COPY((u_char *) atmLbl, buff, MPLS_ATMLRGFIXLEN);
308 atmLbl->flags.mark[0] = ntohl(atmLbl->flags.mark[0]);
309 atmLbl->flags.mark[1] = ntohl(atmLbl->flags.mark[1]);
311 return MPLS_ATMLRGFIXLEN;
313 } /* End Mpls_decodeLdpAtmLblRng */
316 * Encode-decode for ATM Session Parameters
320 * encode:
322 int Mpls_encodeLdpAsp(mplsLdpAspTlv_t * atmAsp, u_char * buff, int bufSize)
324 int encodedSize = 0;
325 u_short totalSize = 0;
326 u_char *tempBuf = buff; /* no change for the buff ptr */
327 u_int i, numLblRng;
329 /* get the size of the atmAsp to be encoded and check it against
330 the buffer size */
332 if (MPLS_TLVFIXLEN + (int)(atmAsp->baseTlv.length) > bufSize) {
333 /* not enough room */
334 return MPLS_ENC_BUFFTOOSMALL;
338 * encode for tlv
340 encodedSize = Mpls_encodeLdpTlv(&(atmAsp->baseTlv), tempBuf, bufSize);
341 if (encodedSize < 0) {
342 return MPLS_ENC_TLVERROR;
344 tempBuf += encodedSize;
345 totalSize += encodedSize;
348 * encode for M + N + D + res
350 numLblRng = atmAsp->flags.flags.numLblRng;
351 atmAsp->flags.flags.res = 0;
352 atmAsp->flags.mark = htonl(atmAsp->flags.mark);
354 MEM_COPY(tempBuf, (u_char *) & (atmAsp->flags.mark), MPLS_ASPFIXLEN);
355 tempBuf += MPLS_ASPFIXLEN;
356 totalSize += MPLS_ASPFIXLEN;
359 * encode for ATM labels
361 for (i = 0; i < numLblRng; i++) {
362 encodedSize = Mpls_encodeLdpAtmLblRng(&(atmAsp->lblRngList[i]),
363 tempBuf, bufSize - totalSize);
364 if (encodedSize < 0) {
365 return MPLS_ENC_ATMLBLERROR;
367 tempBuf += encodedSize;
368 totalSize += encodedSize;
371 return totalSize;
373 } /* End Mpls_encodeLdpAsp */
376 * decode:
378 int Mpls_decodeLdpAsp(mplsLdpAspTlv_t * atmAsp, u_char * buff, int bufSize)
380 int decodedSize = 0;
381 u_short totalSize = 0;
382 u_char *tempBuf = buff; /* no change for the buff ptr */
383 u_int i;
385 if (MPLS_ASPFIXLEN > bufSize) {
386 /* the buffer does not contain even the required field */
387 PRINT_ERR("failed in decoding LdpAsp\n");
388 return MPLS_DEC_BUFFTOOSMALL;
392 * decode for M + N + D + res
394 MEM_COPY((u_char *) & (atmAsp->flags.mark), tempBuf, MPLS_ASPFIXLEN);
395 tempBuf += MPLS_ASPFIXLEN;
396 totalSize += MPLS_ASPFIXLEN;
398 atmAsp->flags.mark = ntohl(atmAsp->flags.mark);
401 * decode for ATM labels
403 for (i = 0; i < atmAsp->flags.flags.numLblRng; i++) {
404 decodedSize = Mpls_decodeLdpAtmLblRng(&(atmAsp->lblRngList[i]),
405 tempBuf, bufSize - totalSize);
406 if (decodedSize < 0) {
407 PRINT_ERR("failed in decoding LdpAtmLabel[%d] for LdpAsp\n", i);
408 return MPLS_DEC_ATMLBLERROR;
410 tempBuf += decodedSize;
411 totalSize += decodedSize;
414 return totalSize;
416 } /* End Mpls_decodeLdpAsp */
419 * Encode-decode for TLV
423 * encode:
425 int Mpls_encodeLdpTlv(mplsLdpTlv_t * tlv, u_char * buff, int bufSize)
427 if (MPLS_TLVFIXLEN > bufSize) {
428 /* not enough room for label */
429 return MPLS_ENC_BUFFTOOSMALL;
432 tlv->flags.mark = htons(tlv->flags.mark);
433 tlv->length = htons(tlv->length);
435 MEM_COPY(buff, (u_char *) tlv, MPLS_TLVFIXLEN);
437 return MPLS_TLVFIXLEN;
439 } /* End: Mpls_encodeLdpTlv */
442 * decode:
444 int Mpls_decodeLdpTlv(mplsLdpTlv_t * tlv, u_char * buff, int bufSize)
446 if (MPLS_TLVFIXLEN > bufSize) {
447 PRINT_ERR("Failed decoding TLV\n");
448 return MPLS_DEC_BUFFTOOSMALL;
451 MEM_COPY((u_char *) tlv, buff, MPLS_TLVFIXLEN);
453 tlv->flags.mark = ntohs(tlv->flags.mark);
454 tlv->length = ntohs(tlv->length);
456 return MPLS_TLVFIXLEN;
458 } /* End: Mpls_decodeLdpTlv */
461 * Encode-decode for CSP (common session param)
465 * encode:
467 int Mpls_encodeLdpCsp(mplsLdpCspTlv_t * csp, u_char * buff, int bufSize)
469 int encodedSize = 0;
470 u_char *tempBuf = buff; /* no change for the buff ptr */
471 u_char *cspPtr;
473 if (MPLS_CSPFIXLEN + MPLS_TLVFIXLEN > bufSize) {
474 /* not enough room */
475 return MPLS_ENC_BUFFTOOSMALL;
478 cspPtr = (u_char *) csp;
481 * encode for tlv
483 encodedSize = Mpls_encodeLdpTlv(&(csp->baseTlv), tempBuf, bufSize);
484 if (encodedSize < 0) {
485 PRINT_ERR("failed encoding the tlv in CSP\n");
486 return MPLS_ENC_TLVERROR;
488 tempBuf += encodedSize;
489 cspPtr += encodedSize;
492 * encode for the rest of the Csp
494 csp->protocolVersion = htons(csp->protocolVersion);
495 csp->holdTime = htons(csp->holdTime);
496 csp->flags.mark = htons(csp->flags.mark);
497 csp->maxPduLen = htons(csp->maxPduLen);
498 csp->rcvLsrAddress = htonl(csp->rcvLsrAddress);
499 csp->rcvLsId = htons(csp->rcvLsId);
501 MEM_COPY(tempBuf, cspPtr, MPLS_CSPFIXLEN);
503 return (MPLS_CSPFIXLEN + MPLS_TLVFIXLEN);
505 } /* End: Mpls_encodeLdpCsp */
508 * decode:
510 int Mpls_decodeLdpCsp(mplsLdpCspTlv_t * csp, u_char * buff, int bufSize)
512 u_char *cspPtr;
514 if (MPLS_CSPFIXLEN > bufSize) {
515 /* not enough data for Csp */
516 PRINT_ERR("failed decoding LdpCsp\n");
517 return MPLS_DEC_BUFFTOOSMALL;
520 cspPtr = (u_char *) csp;
521 cspPtr += MPLS_TLVFIXLEN; /* we want to point to the flags since the
522 tlv was decoded before we reach here */
525 * decode for the rest of the Csp
527 MEM_COPY(cspPtr, buff, MPLS_CSPFIXLEN);
529 csp->protocolVersion = ntohs(csp->protocolVersion);
530 csp->holdTime = ntohs(csp->holdTime);
531 csp->flags.mark = ntohs(csp->flags.mark);
532 csp->maxPduLen = ntohs(csp->maxPduLen);
533 csp->rcvLsrAddress = ntohl(csp->rcvLsrAddress);
534 csp->rcvLsId = ntohs(csp->rcvLsId);
536 return MPLS_CSPFIXLEN;
538 } /* Mpls_decodeLdpCsp */
541 * Encode-decode for Fr Session Parameters
545 * encode
547 int Mpls_encodeLdpFsp(mplsLdpFspTlv_t * frFsp, u_char * buff, int bufSize)
549 int encodedSize = 0;
550 u_short totalSize = 0;
551 u_char *tempBuf = buff; /* no change for the buff ptr */
552 u_int i, numLblRng;
554 /* get the size of the frAsp to be encoded and check it against
555 the buffer size */
557 if (MPLS_TLVFIXLEN + (int)(frFsp->baseTlv.length) > bufSize) {
558 /* not enough room */
559 return MPLS_ENC_BUFFTOOSMALL;
563 * encode for tlv
565 encodedSize = Mpls_encodeLdpTlv(&(frFsp->baseTlv), tempBuf, bufSize);
566 if (encodedSize < 0) {
567 return MPLS_ENC_TLVERROR;
569 tempBuf += encodedSize;
570 totalSize += encodedSize;
573 * encode for M + N + dir + res
575 numLblRng = frFsp->flags.flags.numLblRng;
576 frFsp->flags.flags.res = 0;
577 frFsp->flags.mark = htonl(frFsp->flags.mark);
579 MEM_COPY(tempBuf, (u_char *) & (frFsp->flags.mark), MPLS_FSPFIXLEN);
580 tempBuf += MPLS_FSPFIXLEN;
581 totalSize += MPLS_FSPFIXLEN;
584 * encode for FR labels
586 for (i = 0; i < numLblRng; i++) {
587 encodedSize = Mpls_encodeLdpFrLblRng(&(frFsp->lblRngList[i]),
588 tempBuf, bufSize - totalSize);
589 if (encodedSize < 0) {
590 return MPLS_ENC_FSPLBLERROR;
592 tempBuf += encodedSize;
593 totalSize += encodedSize;
596 return totalSize;
598 } /* End: Mpls_encodeLdpFsp */
601 * decode
603 int Mpls_decodeLdpFsp(mplsLdpFspTlv_t * frFsp, u_char * buff, int bufSize)
605 int decodedSize = 0;
606 u_short totalSize = 0;
607 u_char *tempBuf = buff; /* no change for the buff ptr */
608 u_int i;
610 if (MPLS_FSPFIXLEN > bufSize) {
611 /* the buffer does not contain even the required field */
612 PRINT_ERR("failed in decoding LdpFsp\n");
613 return MPLS_DEC_BUFFTOOSMALL;
617 * decode for M + N + res
619 MEM_COPY((u_char *) & (frFsp->flags.mark), tempBuf, MPLS_FSPFIXLEN);
620 tempBuf += MPLS_FSPFIXLEN;
621 totalSize += MPLS_FSPFIXLEN;
623 frFsp->flags.mark = ntohl(frFsp->flags.mark);
626 * decode for FR labels
628 for (i = 0; i < frFsp->flags.flags.numLblRng; i++) {
629 decodedSize = Mpls_decodeLdpFrLblRng(&(frFsp->lblRngList[i]),
630 tempBuf, bufSize - totalSize);
631 if (decodedSize < 0) {
632 PRINT_ERR("failed in decoding LdpFrLabel[%d] for LdpFsp\n", i);
633 return MPLS_DEC_FSPLBLERROR;
635 tempBuf += decodedSize;
636 totalSize += decodedSize;
639 return totalSize;
641 } /* End: Mpls_decodeLdpFsp */
644 * Encode-decode for INIT msg
648 * encode for init message
650 int Mpls_encodeLdpInitMsg
651 (mplsLdpInitMsg_t * initMsg, u_char * buff, int bufSize) {
652 mplsLdpInitMsg_t initMsgCopy;
653 int encodedSize, totalSize;
654 u_char *tempBuf = buff; /* no change for the buff ptr */
656 initMsgCopy = *initMsg;
657 totalSize = 0;
659 /* check the length of the messageId + mandatory param +
660 optional param */
661 if ((int)(initMsgCopy.baseMsg.msgLength) + MPLS_TLVFIXLEN > bufSize) {
662 PRINT_ERR("failed to encode the init msg: BUFFER TOO SMALL\n");
663 return MPLS_ENC_BUFFTOOSMALL;
667 * encode the base part of the pdu message
669 encodedSize = Mpls_encodeLdpBaseMsg(&(initMsgCopy.baseMsg), tempBuf, bufSize);
670 if (encodedSize < 0) {
671 return MPLS_ENC_BASEMSGERROR;
673 PRINT_OUT("Encode BaseMsg for init on %d bytes\n", encodedSize);
674 tempBuf += encodedSize;
675 totalSize += encodedSize;
678 * encode the csp if any
680 if (initMsgCopy.cspExists) {
681 encodedSize = Mpls_encodeLdpCsp(&(initMsgCopy.csp),
682 tempBuf, bufSize - totalSize);
683 if (encodedSize < 0) {
684 return MPLS_ENC_CSPERROR;
686 PRINT_OUT("Encoded for CSP %d bytes\n", encodedSize);
687 tempBuf += encodedSize;
688 totalSize += encodedSize;
692 * encode the asp if any
694 if (initMsgCopy.aspExists) {
696 encodedSize = Mpls_encodeLdpAsp(&(initMsgCopy.asp),
697 tempBuf, bufSize - totalSize);
698 if (encodedSize < 0) {
699 return MPLS_ENC_ASPERROR;
701 PRINT_OUT("Encoded for ASP %d bytes\n", encodedSize);
702 tempBuf += encodedSize;
703 totalSize += encodedSize;
707 * encode the fsp if any
709 if (initMsgCopy.fspExists) {
710 encodedSize = Mpls_encodeLdpFsp(&(initMsgCopy.fsp),
711 tempBuf, bufSize - totalSize);
712 if (encodedSize < 0) {
713 return MPLS_ENC_FSPERROR;
715 tempBuf += encodedSize;
716 totalSize += encodedSize;
719 return totalSize;
721 } /* End: Mpls_encodeLdpInitMsg */
724 * decode for unknown message
726 int Mpls_decodeLdpUnknownMsg
727 (mplsLdpUnknownMsg_t * msg, u_char * buff, int bufSize) {
728 int decodedSize = 0;
729 u_int totalSize = 0;
730 u_char *tempBuf = buff; /* no change for the buff ptr */
733 * decode the base part of the pdu message
735 memset(msg, 0, sizeof(mplsLdpMsg_t));
736 decodedSize = Mpls_decodeLdpBaseMsg(&(msg->baseMsg), tempBuf, bufSize);
737 if (decodedSize < 0) {
738 return MPLS_DEC_BASEMSGERROR;
740 PRINT_OUT("Decode BaseMsg for unknown on %d bytes\n", decodedSize);
742 tempBuf += decodedSize;
743 totalSize += decodedSize;
745 if (bufSize - totalSize <= 0) {
746 /* nothing left for decoding */
747 PRINT_ERR("Init msg does not have anything beside base msg\n");
748 return totalSize;
751 if (msg->baseMsg.msgLength > MPLS_NOT_MAXSIZE) {
752 PRINT_ERR("Message is too big for unknow message buffer.\n");
753 return MPLS_DEC_BASEMSGERROR;
756 memcpy(msg->data, tempBuf, msg->baseMsg.msgLength);
757 decodedSize = msg->baseMsg.msgLength;
759 tempBuf += decodedSize;
760 totalSize += decodedSize;
762 PRINT_OUT("totalsize for Mpls_decodeLdpUnknowntMsg is %d\n", totalSize);
764 return totalSize;
768 * decode for init message
770 int Mpls_decodeLdpInitMsg
771 (mplsLdpInitMsg_t * initMsg, u_char * buff, int bufSize) {
772 int decodedSize = 0;
773 u_int totalSize = 0;
774 u_int stopLength = 0;
775 u_char *tempBuf = buff; /* no change for the buff ptr */
776 u_int totalSizeParam = 0;
777 mplsLdpTlv_t tlvTemp;
780 * decode the base part of the pdu message
782 memset(initMsg, 0, sizeof(mplsLdpInitMsg_t));
783 decodedSize = Mpls_decodeLdpBaseMsg(&(initMsg->baseMsg), tempBuf, bufSize);
784 if (decodedSize < 0) {
785 return MPLS_DEC_BASEMSGERROR;
787 PRINT_OUT("Decode BaseMsg for init on %d bytes\n", decodedSize);
789 if (initMsg->baseMsg.flags.flags.msgType != MPLS_INIT_MSGTYPE) {
790 PRINT_ERR("Not the right message type; expected init and got %x\n",
791 initMsg->baseMsg.flags.flags.msgType);
792 return MPLS_MSGTYPEERROR;
794 tempBuf += decodedSize;
795 totalSize += decodedSize;
797 if (bufSize - totalSize <= 0) {
798 /* nothing left for decoding */
799 PRINT_ERR("Init msg does not have anything beside base msg\n");
800 return totalSize;
803 PRINT_OUT("bufSize = %d, totalSize = %d, initMsg->baseMsg.msgLength = %d\n",
804 bufSize, totalSize, initMsg->baseMsg.msgLength);
806 /* Have to check the baseMsg.msgLength to know when to finish.
807 * We finsh when the totalSizeParam is >= to the base message length - the
808 * message id length (4)
811 stopLength = initMsg->baseMsg.msgLength - MPLS_MSGIDFIXLEN;
812 while (stopLength > totalSizeParam) {
814 * decode the tlv to check what's next
816 decodedSize = Mpls_decodeLdpTlv(&tlvTemp, tempBuf, bufSize - totalSize);
817 if (decodedSize < 0) {
818 /* something wrong */
819 PRINT_ERR("INIT msg decode failed for tlv\n");
820 return MPLS_DEC_TLVERROR;
823 tempBuf += decodedSize;
824 totalSize += decodedSize;
825 totalSizeParam += decodedSize;
827 switch (tlvTemp.flags.flags.tBit) {
828 case MPLS_CSP_TLVTYPE:
830 decodedSize = Mpls_decodeLdpCsp(&(initMsg->csp),
831 tempBuf, bufSize - totalSize);
832 if (decodedSize < 0) {
833 PRINT_ERR("Failure when decoding Csp from init msg\n");
834 return MPLS_DEC_CSPERROR;
836 PRINT_OUT("Decoded for CSP %d bytes\n", decodedSize);
837 tempBuf += decodedSize;
838 totalSize += decodedSize;
839 totalSizeParam += decodedSize;
840 initMsg->cspExists = 1;
841 initMsg->csp.baseTlv = tlvTemp;
842 break;
844 case MPLS_ASP_TLVTYPE:
846 decodedSize = Mpls_decodeLdpAsp(&(initMsg->asp),
847 tempBuf, bufSize - totalSize);
848 if (decodedSize < 0) {
849 PRINT_ERR("Failure when decoding Asp from init msg\n");
850 return MPLS_DEC_ASPERROR;
852 PRINT_OUT("Decoded for ASP %d bytes\n", decodedSize);
853 tempBuf += decodedSize;
854 totalSize += decodedSize;
855 totalSizeParam += decodedSize;
856 initMsg->aspExists = 1;
857 initMsg->asp.baseTlv = tlvTemp;
858 break;
860 case MPLS_FSP_TLVTYPE:
862 decodedSize = Mpls_decodeLdpFsp(&(initMsg->fsp),
863 tempBuf, bufSize - totalSize);
864 if (decodedSize < 0) {
865 PRINT_ERR("Failure when decoding Fsp from init msg\n");
866 return MPLS_DEC_FSPERROR;
868 tempBuf += decodedSize;
869 totalSize += decodedSize;
870 totalSizeParam += decodedSize;
871 initMsg->fspExists = 1;
872 initMsg->fsp.baseTlv = tlvTemp;
873 break;
875 default:
877 PRINT_ERR("Found wrong tlv type while decoding init msg (%d)\n",
878 tlvTemp.flags.flags.tBit);
879 if (tlvTemp.flags.flags.uBit == 1) {
880 /* ignore the Tlv and continue processing */
881 tempBuf += tlvTemp.length;
882 totalSize += tlvTemp.length;
883 totalSizeParam += tlvTemp.length;
884 break;
885 } else {
886 /* drop the message; return error */
887 return MPLS_TLVTYPEERROR;
890 } /* switch type */
892 } /* while */
894 PRINT_OUT("totalsize for Mpls_decodeLdpInitMsg is %d\n", totalSize);
896 return totalSize;
898 } /* End: Mpls_decodeLdpInitMsg */
901 * Encode-decode for Fr Label Range
905 * encode
907 int Mpls_encodeLdpFrLblRng
908 (mplsLdpFrLblRng_t * frLabel, u_char * buff, int bufSize) {
909 if (MPLS_FRLRGFIXLEN > bufSize) {
910 /* not enough room for label */
911 return MPLS_ENC_BUFFTOOSMALL;
914 frLabel->flags.flags.res_min = 0;
915 frLabel->flags.flags.res_max = 0;
916 frLabel->flags.mark[0] = htonl(frLabel->flags.mark[0]);
917 frLabel->flags.mark[1] = htonl(frLabel->flags.mark[1]);
919 MEM_COPY(buff, (u_char *) frLabel, MPLS_FRLRGFIXLEN);
921 return MPLS_FRLRGFIXLEN;
923 } /* End: Mpls_encodeLdpFrLblRng */
926 * decode
928 int Mpls_decodeLdpFrLblRng
929 (mplsLdpFrLblRng_t * frLabel, u_char * buff, int bufSize) {
930 if (MPLS_FRLRGFIXLEN > bufSize) {
931 PRINT_ERR("failed decoding the Fr Lbl Rng\n");
932 return MPLS_DEC_BUFFTOOSMALL;
935 MEM_COPY((u_char *) frLabel, buff, MPLS_FRLRGFIXLEN);
937 frLabel->flags.mark[0] = ntohl(frLabel->flags.mark[0]);
938 frLabel->flags.mark[1] = ntohl(frLabel->flags.mark[1]);
940 return MPLS_FRLRGFIXLEN;
942 } /* End: Mpls_decodeLdpFrLblRng */
945 * Encode-decode for NOTIFICATION msg
949 * encode for notification message
951 int Mpls_encodeLdpNotMsg(mplsLdpNotifMsg_t * notMsg, u_char * buff, int bufSize)
953 mplsLdpNotifMsg_t notMsgCopy;
954 int encodedSize = 0;
955 u_int totalSize = 0;
956 u_char *tempBuf = buff; /* no change for the buff ptr */
958 /* check the length of the messageId + mandatory param +
959 optional param */
960 if ((int)(notMsg->baseMsg.msgLength) + MPLS_TLVFIXLEN > bufSize) {
961 PRINT_ERR("failed to encode the not msg: BUFFER TOO SMALL\n");
962 return MPLS_ENC_BUFFTOOSMALL;
965 notMsgCopy = *notMsg;
968 * encode the base part of the pdu message
970 encodedSize = Mpls_encodeLdpBaseMsg(&(notMsgCopy.baseMsg), tempBuf, bufSize);
971 if (encodedSize < 0) {
972 return MPLS_ENC_BASEMSGERROR;
974 PRINT_OUT("Encode BaseMsg for not on %d bytes\n", encodedSize);
976 tempBuf += encodedSize;
977 totalSize += encodedSize;
979 if (notMsgCopy.statusTlvExists) {
980 encodedSize = Mpls_encodeLdpStatus(&(notMsgCopy.status),
981 tempBuf, bufSize - totalSize);
982 if (encodedSize < 0) {
983 return MPLS_ENC_STATUSERROR;
985 PRINT_OUT("Encoded for STATUS %d bytes\n", encodedSize);
986 tempBuf += encodedSize;
987 totalSize += encodedSize;
989 if (notMsgCopy.exStatusTlvExists) {
990 encodedSize = Mpls_encodeLdpExStatus(&(notMsgCopy.exStatus),
991 tempBuf, bufSize - totalSize);
992 if (encodedSize < 0) {
993 return MPLS_ENC_EXSTATERROR;
995 PRINT_OUT("Encoded for EXTENDED STATUS %d bytes\n", encodedSize);
996 tempBuf += encodedSize;
997 totalSize += encodedSize;
999 if (notMsgCopy.retPduTlvExists) {
1000 encodedSize = Mpls_encodeLdpRetPdu(&(notMsgCopy.retPdu),
1001 tempBuf, bufSize - totalSize);
1002 if (encodedSize < 0) {
1003 return MPLS_ENC_RETPDUERROR;
1005 PRINT_OUT("Encoded for RET PDU %d bytes\n", encodedSize);
1006 tempBuf += encodedSize;
1007 totalSize += encodedSize;
1009 if (notMsgCopy.retMsgTlvExists) {
1010 encodedSize = Mpls_encodeLdpRetMsg(&(notMsgCopy.retMsg),
1011 tempBuf, bufSize - totalSize);
1012 if (encodedSize < 0) {
1013 return MPLS_ENC_RETMSGERROR;
1015 PRINT_OUT("Encoded for RET MSG %d bytes\n", encodedSize);
1016 tempBuf += encodedSize;
1017 totalSize += encodedSize;
1019 if (notMsgCopy.lspidTlvExists) {
1020 encodedSize = Mpls_encodeLdpLspIdTlv(&(notMsgCopy.lspidTlv),
1021 tempBuf, bufSize - totalSize);
1022 if (encodedSize < 0) {
1023 return MPLS_ENC_LSPIDERROR;
1025 PRINT_OUT("Encoded for LSPID Tlv %d bytes\n", encodedSize);
1026 tempBuf += encodedSize;
1027 totalSize += encodedSize;
1030 return totalSize;
1032 } /* End: Mpls_encodeLdpNotMsg */
1035 * decode for notification message
1037 int Mpls_decodeLdpNotMsg(mplsLdpNotifMsg_t * notMsg, u_char * buff, int bufSize)
1039 int decodedSize = 0;
1040 u_int totalSize = 0;
1041 u_int stopLength = 0;
1042 u_int totalSizeParam = 0;
1043 u_char *tempBuf = buff; /* no change for the buff ptr */
1044 mplsLdpTlv_t tlvTemp;
1047 * decode the base part of the pdu message
1049 memset(notMsg, 0, sizeof(mplsLdpNotifMsg_t));
1050 decodedSize = Mpls_decodeLdpBaseMsg(&(notMsg->baseMsg), tempBuf, bufSize);
1051 if (decodedSize < 0) {
1052 return MPLS_DEC_BASEMSGERROR;
1054 PRINT_OUT("Decode BaseMsg for not on %d bytes\n", decodedSize);
1056 if (notMsg->baseMsg.flags.flags.msgType != MPLS_NOT_MSGTYPE) {
1057 PRINT_ERR("Not the right message type; expected not and got %x\n",
1058 notMsg->baseMsg.flags.flags.msgType);
1059 return MPLS_MSGTYPEERROR;
1062 tempBuf += decodedSize;
1063 totalSize += decodedSize;
1065 if (bufSize - totalSize <= 0) {
1066 /* nothing left for decoding */
1067 PRINT_ERR("Not msg does not have anything beside base msg\n");
1068 return totalSize;
1071 PRINT_OUT("bufSize = %d, totalSize = %d, notMsg->baseMsg.msgLength = %d\n",
1072 bufSize, totalSize, notMsg->baseMsg.msgLength);
1074 /* Have to check the baseMsg.msgLength to know when to finish.
1075 * We finsh when the totalSizeParam is >= to the base message length - the
1076 * message id length (4)
1079 stopLength = notMsg->baseMsg.msgLength - MPLS_MSGIDFIXLEN;
1080 while (stopLength > totalSizeParam) {
1082 * decode the tlv to check what's next
1084 memset(&tlvTemp, 0, MPLS_TLVFIXLEN);
1085 decodedSize = Mpls_decodeLdpTlv(&tlvTemp, tempBuf, bufSize - totalSize);
1086 if (decodedSize < 0) {
1087 /* something wrong */
1088 PRINT_ERR("NOT msg decode failed for tlv\n");
1089 return MPLS_DEC_TLVERROR;
1092 tempBuf += decodedSize;
1093 totalSize += decodedSize;
1094 totalSizeParam += decodedSize;
1096 switch (tlvTemp.flags.flags.tBit) {
1097 case MPLS_NOT_ST_TLVTYPE:
1099 decodedSize = Mpls_decodeLdpStatus(&(notMsg->status),
1100 tempBuf, bufSize - totalSize);
1101 if (decodedSize < 0) {
1102 PRINT_ERR("Failure when decoding Status from not msg\n");
1103 return MPLS_DEC_STATUSERROR;
1105 PRINT_OUT("Decoded for STATUS %d bytes\n", decodedSize);
1106 tempBuf += decodedSize;
1107 totalSize += decodedSize;
1108 totalSizeParam += decodedSize;
1110 notMsg->statusTlvExists = 1;
1111 notMsg->status.baseTlv = tlvTemp;
1112 break;
1114 case MPLS_NOT_ES_TLVTYPE:
1116 decodedSize = Mpls_decodeLdpExStatus(&(notMsg->exStatus),
1117 tempBuf, bufSize - totalSize);
1118 if (decodedSize < 0) {
1119 PRINT_ERR("Failure when decoding Extended Status from not msg\n");
1120 return MPLS_DEC_EXSTATERROR;
1122 PRINT_OUT("Decoded for EX_STATUS %d bytes\n", decodedSize);
1123 tempBuf += decodedSize;
1124 totalSize += decodedSize;
1125 totalSizeParam += decodedSize;
1127 notMsg->exStatusTlvExists = 1;
1128 notMsg->exStatus.baseTlv = tlvTemp;
1129 break;
1131 case MPLS_NOT_RP_TLVTYPE:
1133 decodedSize = Mpls_decodeLdpRetPdu(&(notMsg->retPdu),
1134 tempBuf, bufSize - totalSize, tlvTemp.length);
1135 if (decodedSize < 0) {
1136 PRINT_ERR("Failure when decoding Returned PDU from not msg\n");
1137 return MPLS_DEC_RETPDUERROR;
1139 PRINT_OUT("Decoded for RET_PDU %d bytes\n", decodedSize);
1140 tempBuf += decodedSize;
1141 totalSize += decodedSize;
1142 totalSizeParam += decodedSize;
1144 notMsg->retPduTlvExists = 1;
1145 notMsg->retPdu.baseTlv = tlvTemp;
1146 break;
1148 case MPLS_NOT_RM_TLVTYPE:
1150 decodedSize = Mpls_decodeLdpRetMsg(&(notMsg->retMsg),
1151 tempBuf, bufSize - totalSize, tlvTemp.length);
1152 if (decodedSize < 0) {
1153 PRINT_ERR("Failure when decoding Returned MSG from not msg\n");
1154 return MPLS_DEC_RETMSGERROR;
1156 PRINT_OUT("Decoded for RET_MSG %d bytes\n", decodedSize);
1157 tempBuf += decodedSize;
1158 totalSize += decodedSize;
1159 totalSizeParam += decodedSize;
1161 notMsg->retMsgTlvExists = 1;
1162 notMsg->retMsg.baseTlv = tlvTemp;
1163 break;
1165 case MPLS_LSPID_TLVTYPE:
1167 decodedSize = Mpls_decodeLdpLspIdTlv(&(notMsg->lspidTlv),
1168 tempBuf, bufSize - totalSize);
1169 if (decodedSize < 0) {
1170 PRINT_ERR("Failure when dec LSPID tlv from Not msg\n");
1171 return MPLS_DEC_LSPIDERROR;
1173 PRINT_OUT("Decoded for lspid tlv %d bytes\n", decodedSize);
1174 tempBuf += decodedSize;
1175 totalSize += decodedSize;
1176 totalSizeParam += decodedSize;
1178 notMsg->lspidTlvExists = 1;
1179 notMsg->lspidTlv.baseTlv = tlvTemp;
1180 break;
1182 default:
1184 PRINT_ERR("Found wrong tlv type while decoding not msg (%d)\n",
1185 tlvTemp.flags.flags.tBit);
1186 if (tlvTemp.flags.flags.uBit == 1) {
1187 /* ignore the Tlv and continue processing */
1188 tempBuf += tlvTemp.length;
1189 totalSize += tlvTemp.length;
1190 totalSizeParam += tlvTemp.length;
1191 break;
1192 } else {
1193 /* drop the message; return error */
1194 return MPLS_TLVTYPEERROR;
1197 } /* switch type */
1199 } /* while */
1201 PRINT_OUT("totalsize for Mpls_decodeLdpNotMsg is %d\n", totalSize);
1203 return totalSize;
1205 } /* End: Mpls_decodeLdpNotMsg */
1208 * Encode-decode for Status TLV
1212 * encode:
1214 int Mpls_encodeLdpStatus
1215 (mplsLdpStatusTlv_t * status, u_char * buff, int bufSize) {
1216 int encodedSize = 0;
1217 u_char *tempBuf = buff; /* no change for the buff ptr */
1218 u_char *statusPtr;
1220 if (MPLS_STATUSFIXLEN + MPLS_TLVFIXLEN > bufSize) {
1221 /* not enough room */
1222 return MPLS_ENC_BUFFTOOSMALL;
1226 * encode for tlv
1228 encodedSize = Mpls_encodeLdpTlv(&(status->baseTlv), tempBuf, bufSize);
1229 if (encodedSize < 0) {
1230 PRINT_ERR("failed encoding the tlv in STATUS\n");
1231 return MPLS_ENC_TLVERROR;
1234 statusPtr = (u_char *) status;
1235 tempBuf += encodedSize;
1236 statusPtr += encodedSize;
1239 * encode for the rest of the Status
1241 status->flags.mark = htonl(status->flags.mark);
1242 status->msgId = htonl(status->msgId);
1243 status->msgType = htons(status->msgType);
1245 MEM_COPY(tempBuf, statusPtr, MPLS_STATUSFIXLEN);
1247 return (MPLS_STATUSFIXLEN + MPLS_TLVFIXLEN);
1249 } /* End: Mpls_encodeLdpStatus */
1252 * decode:
1254 int Mpls_decodeLdpStatus
1255 (mplsLdpStatusTlv_t * status, u_char * buff, int bufSize) {
1256 u_char *statusPtr;
1258 if (MPLS_STATUSFIXLEN > bufSize) {
1259 /* not enough data for Status */
1260 PRINT_ERR("failed decoding Status\n");
1261 return MPLS_DEC_BUFFTOOSMALL;
1264 statusPtr = (u_char *) status;
1265 statusPtr += MPLS_TLVFIXLEN; /* we want to point to the flags since the
1266 tlv was decoded before we reach here */
1269 * decode for the rest of the Status
1271 MEM_COPY(statusPtr, buff, MPLS_STATUSFIXLEN);
1273 status->flags.mark = ntohl(status->flags.mark);
1274 status->msgId = ntohl(status->msgId);
1275 status->msgType = ntohs(status->msgType);
1277 return MPLS_STATUSFIXLEN;
1279 } /* End: Mpls_decodeLdpStatus */
1282 * Encode-decode for Extended Status TLV
1286 * encode:
1288 int Mpls_encodeLdpExStatus
1289 (mplsLdpExStatusTlv_t * status, u_char * buff, int bufSize) {
1290 int encodedSize = 0;
1291 u_char *tempBuf = buff; /* no change for the buff ptr */
1293 if (MPLS_EXSTATUSLEN + MPLS_TLVFIXLEN > bufSize) {
1294 /* not enough room */
1295 return MPLS_ENC_BUFFTOOSMALL;
1299 * encode for tlv
1301 encodedSize = Mpls_encodeLdpTlv(&(status->baseTlv), tempBuf, bufSize);
1302 if (encodedSize < 0) {
1303 PRINT_ERR("failed encoding the tlv in EX_STATUS\n");
1304 return MPLS_ENC_TLVERROR;
1306 tempBuf += encodedSize;
1308 status->value = htonl(status->value);
1310 MEM_COPY(tempBuf, (u_char *) & (status->value), sizeof(u_int));
1312 return (MPLS_EXSTATUSLEN + MPLS_TLVFIXLEN);
1314 } /* End: Mpls_encodeLdpExStatus */
1317 * decode:
1319 int Mpls_decodeLdpExStatus
1320 (mplsLdpExStatusTlv_t * status, u_char * buff, int bufSize) {
1321 if (MPLS_EXSTATUSLEN > bufSize) {
1322 /* not enough data for ExStatus */
1323 PRINT_ERR("failed decoding ExStatus\n");
1324 return MPLS_DEC_BUFFTOOSMALL;
1328 * decode for the rest of the Status
1330 MEM_COPY(&(status->value), buff, MPLS_EXSTATUSLEN);
1332 status->value = ntohl(status->value);
1334 return MPLS_EXSTATUSLEN;
1336 } /* End: Mpls_decodeLdpExStatus */
1339 * Encode-decode for Return PDU TLV
1343 * encode:
1345 int Mpls_encodeLdpRetPdu
1346 (mplsLdpRetPduTlv_t * retPdu, u_char * buff, int bufSize) {
1347 int encodedSize = 0;
1348 u_char *tempBuf = buff; /* no change for the buff ptr */
1349 u_short tempLength; /* to store the tlv length for
1351 later use */
1353 if (MPLS_TLVFIXLEN + (int)(retPdu->baseTlv.length) > bufSize) {
1354 /* not enough room */
1355 return MPLS_ENC_BUFFTOOSMALL;
1358 tempLength = retPdu->baseTlv.length;
1360 * encode for tlv
1362 encodedSize = Mpls_encodeLdpTlv(&(retPdu->baseTlv), tempBuf, bufSize);
1363 if (encodedSize < 0) {
1364 PRINT_ERR("failed encoding the tlv in RET_PDU\n");
1365 return MPLS_ENC_TLVERROR;
1367 tempBuf += encodedSize;
1370 * encode the data of the ret pdu
1373 encodedSize = Mpls_encodeLdpMsgHeader(&(retPdu->headerTlv),
1374 tempBuf, bufSize - encodedSize);
1375 if (encodedSize < 0) {
1376 PRINT_ERR("failed encoding the header Tlv in RET_PDU\n");
1377 return MPLS_ENC_HDRTLVERROR;
1379 tempBuf += encodedSize;
1381 MEM_COPY(tempBuf, retPdu->data, tempLength);
1383 return (MPLS_TLVFIXLEN + tempLength);
1385 } /* End: Mpls_encodeLdpRetPdu */
1388 * decode:
1390 int Mpls_decodeLdpRetPdu
1391 (mplsLdpRetPduTlv_t * retPdu, u_char * buff, int bufSize, u_short tlvLength) {
1392 u_char *tempBuf = buff; /* no change for the buff ptr */
1393 int decodedSize;
1395 if ((int)tlvLength > bufSize) {
1396 /* not enough data for ExStatus */
1397 PRINT_ERR("failed decoding Ret pdu\n");
1398 return MPLS_DEC_BUFFTOOSMALL;
1402 * decode data for ret pdu
1404 decodedSize = Mpls_decodeLdpMsgHeader(&(retPdu->headerTlv), tempBuf, bufSize);
1405 if (decodedSize < 0) {
1406 PRINT_ERR("failed decoding the header Tlv in RET_PDU\n");
1407 return MPLS_DEC_HDRTLVERROR;
1409 tempBuf += decodedSize;
1411 MEM_COPY(retPdu->data, tempBuf, tlvLength);
1413 return tlvLength;
1415 } /* End: Mpls_decodeLdpRetPdu */
1418 * Encode-decode for Return Msg TLV
1422 * encode:
1424 int Mpls_encodeLdpRetMsg
1425 (mplsLdpRetMsgTlv_t * retMsg, u_char * buff, int bufSize) {
1426 u_char *retMsgPtr;
1427 int encodedSize = 0;
1428 u_char *tempBuf = buff; /* no change for the buff ptr */
1429 u_short tempLength; /* to store the tlv length for
1431 later use */
1433 if (MPLS_TLVFIXLEN + (int)(retMsg->baseTlv.length) > bufSize) {
1434 /* not enough room */
1435 return MPLS_ENC_BUFFTOOSMALL;
1438 tempLength = retMsg->baseTlv.length;
1440 * encode for tlv
1442 encodedSize = Mpls_encodeLdpTlv(&(retMsg->baseTlv), tempBuf, bufSize);
1443 if (encodedSize < 0) {
1444 PRINT_ERR("failed encoding the tlv in RET_MSG\n");
1445 return MPLS_ENC_TLVERROR;
1448 retMsgPtr = (u_char *) retMsg;
1449 tempBuf += encodedSize;
1450 retMsgPtr += encodedSize;
1453 * encode the data of the ret pdu
1456 retMsg->msgType = htons(retMsg->msgType);
1457 retMsg->msgLength = htons(retMsg->msgLength);
1459 MEM_COPY(tempBuf, retMsgPtr, tempLength);
1461 return (MPLS_TLVFIXLEN + tempLength);
1463 } /* End: Mpls_encodeLdpRetMsg */
1466 * decode:
1468 int Mpls_decodeLdpRetMsg
1469 (mplsLdpRetMsgTlv_t * retMsg, u_char * buff, int bufSize, u_short tlvLength) {
1470 u_char *tempBuf = buff; /* no change for the buff ptr */
1471 u_char *retMsgPtr;
1473 if ((int)tlvLength > bufSize) {
1474 /* not enough data for ExStatus */
1475 PRINT_ERR("failed decoding Ret msg\n");
1476 return MPLS_DEC_BUFFTOOSMALL;
1478 retMsgPtr = (u_char *) retMsg;
1479 retMsgPtr += MPLS_TLVFIXLEN;
1482 * decode data for ret msg
1484 MEM_COPY(retMsgPtr, tempBuf, tlvLength);
1486 retMsg->msgType = ntohs(retMsg->msgType);
1487 retMsg->msgLength = ntohs(retMsg->msgLength);
1489 return tlvLength;
1491 } /* End: Mpls_decodeLdpRetMsg */
1494 * Encode-decode for HELLO msg
1498 * encode for HELLO message
1500 int Mpls_encodeLdpHelloMsg
1501 (mplsLdpHelloMsg_t * helloMsg, u_char * buff, int bufSize) {
1502 mplsLdpHelloMsg_t helloMsgCopy;
1503 int encodedSize = 0;
1504 u_int totalSize = 0;
1505 u_char *tempBuf = buff; /* no change for the buff ptr */
1507 /* check the length of the messageId + mandatory param +
1508 optional param */
1509 if ((int)(helloMsg->baseMsg.msgLength) + MPLS_TLVFIXLEN > bufSize) {
1510 PRINT_ERR("failed to encode the hello msg: BUFFER TOO SMALL\n");
1511 return MPLS_ENC_BUFFTOOSMALL;
1514 helloMsgCopy = *helloMsg;
1517 * encode the base part of the pdu message
1519 encodedSize = Mpls_encodeLdpBaseMsg(&(helloMsgCopy.baseMsg),
1520 tempBuf, bufSize);
1521 if (encodedSize < 0) {
1522 return MPLS_ENC_BASEMSGERROR;
1524 PRINT_OUT("Encode BaseMsg for hello on %d bytes\n", encodedSize);
1525 tempBuf += encodedSize;
1526 totalSize += encodedSize;
1529 * encode the status tlv if any
1531 if (helloMsgCopy.chpTlvExists) {
1532 encodedSize = Mpls_encodeLdpChp(&(helloMsgCopy.chp),
1533 tempBuf, bufSize - totalSize);
1534 if (encodedSize < 0) {
1535 return MPLS_ENC_CHPERROR;
1537 PRINT_OUT("Encoded for CHP %d bytes\n", encodedSize);
1538 tempBuf += encodedSize;
1539 totalSize += encodedSize;
1541 if (helloMsgCopy.trAdrTlvExists) {
1542 encodedSize = Mpls_encodeLdpTrAdr(&(helloMsgCopy.trAdr),
1543 tempBuf, bufSize - totalSize);
1544 if (encodedSize < 0) {
1545 return MPLS_ENC_TRADRERROR;
1547 PRINT_OUT("Encoded for TR ADDR %d bytes\n", encodedSize);
1548 tempBuf += encodedSize;
1549 totalSize += encodedSize;
1551 if (helloMsgCopy.csnTlvExists) {
1552 encodedSize = Mpls_encodeLdpCsn(&(helloMsgCopy.csn),
1553 tempBuf, bufSize - totalSize);
1554 if (encodedSize < 0) {
1555 return MPLS_ENC_CSNERROR;
1557 PRINT_OUT("Encoded for CSN %d bytes\n", encodedSize);
1558 tempBuf += encodedSize;
1559 totalSize += encodedSize;
1562 return totalSize;
1564 } /* End: Mpls_encodeLdpHelloMsg */
1567 * decode for HELLO message
1569 int Mpls_decodeLdpHelloMsg
1570 (mplsLdpHelloMsg_t * helloMsg, u_char * buff, int bufSize) {
1571 int decodedSize = 0;
1572 u_int totalSize = 0;
1573 u_int stopLength = 0;
1574 u_int totalSizeParam = 0;
1575 u_char *tempBuf = buff; /* no change for the buff ptr */
1576 mplsLdpTlv_t tlvTemp;
1579 * decode the base part of the pdu message
1581 memset(helloMsg, 0, sizeof(mplsLdpHelloMsg_t));
1582 decodedSize = Mpls_decodeLdpBaseMsg(&(helloMsg->baseMsg), tempBuf, bufSize);
1583 if (decodedSize < 0) {
1584 return MPLS_DEC_BASEMSGERROR;
1586 PRINT_OUT("Decode BaseMsg for hello on %d bytes\n", decodedSize);
1588 if (helloMsg->baseMsg.flags.flags.msgType != MPLS_HELLO_MSGTYPE) {
1589 PRINT_ERR("Not the right message type; expected hello and got %x\n",
1590 helloMsg->baseMsg.flags.flags.msgType);
1591 return MPLS_MSGTYPEERROR;
1594 tempBuf += decodedSize;
1595 totalSize += decodedSize;
1597 if (bufSize - totalSize <= 0) {
1598 /* nothing left for decoding */
1599 PRINT_ERR("Hello msg does not have anything beside base msg\n");
1600 return totalSize;
1603 PRINT_OUT("bufSize = %d, totalSize = %d, helloMsg->baseMsg.msgLength = %d\n",
1604 bufSize, totalSize, helloMsg->baseMsg.msgLength);
1606 /* Have to check the baseMsg.msgLength to know when to finish.
1607 * We finsh when the totalSizeParam is >= to the base message length - the
1608 * message id length (4)
1611 stopLength = helloMsg->baseMsg.msgLength - MPLS_MSGIDFIXLEN;
1612 while (stopLength > totalSizeParam) {
1614 * decode the tlv to check what's next
1616 memset(&tlvTemp, 0, MPLS_TLVFIXLEN);
1617 decodedSize = Mpls_decodeLdpTlv(&tlvTemp, tempBuf, bufSize - totalSize);
1618 if (decodedSize < 0) {
1619 /* something wrong */
1620 PRINT_ERR("NOT msg decode failed for tlv\n");
1621 return MPLS_DEC_TLVERROR;
1624 tempBuf += decodedSize;
1625 totalSize += decodedSize;
1626 totalSizeParam += decodedSize;
1628 switch (tlvTemp.flags.flags.tBit) {
1629 case MPLS_CHP_TLVTYPE:
1631 decodedSize = Mpls_decodeLdpChp(&(helloMsg->chp),
1632 tempBuf, bufSize - totalSize);
1633 if (decodedSize < 0) {
1634 PRINT_ERR("Failure when decoding Chp from hello msg\n");
1635 return MPLS_DEC_CHPERROR;
1637 PRINT_OUT("Decoded for CHP %d bytes\n", decodedSize);
1638 tempBuf += decodedSize;
1639 totalSize += decodedSize;
1640 totalSizeParam += decodedSize;
1642 helloMsg->chpTlvExists = 1;
1643 helloMsg->chp.baseTlv = tlvTemp;
1644 break;
1646 case MPLS_TRADR_TLVTYPE:
1648 decodedSize = Mpls_decodeLdpTrAdr(&(helloMsg->trAdr),
1649 tempBuf, bufSize - totalSize);
1650 if (decodedSize < 0) {
1651 PRINT_ERR("Failure when decoding TrAdr from hello msg\n");
1652 return MPLS_DEC_TRADRERROR;
1654 PRINT_OUT("Decoded for TrAdr %d bytes\n", decodedSize);
1655 tempBuf += decodedSize;
1656 totalSize += decodedSize;
1657 totalSizeParam += decodedSize;
1659 helloMsg->trAdrTlvExists = 1;
1660 helloMsg->trAdr.baseTlv = tlvTemp;
1661 break;
1663 case MPLS_CSN_TLVTYPE:
1665 decodedSize = Mpls_decodeLdpCsn(&(helloMsg->csn),
1666 tempBuf, bufSize - totalSize);
1667 if (decodedSize < 0) {
1668 PRINT_ERR("Failure when decoding Csn from hello msg\n");
1669 return MPLS_DEC_CSNERROR;
1671 PRINT_OUT("Decoded for CSN %d bytes\n", decodedSize);
1672 tempBuf += decodedSize;
1673 totalSize += decodedSize;
1674 totalSizeParam += decodedSize;
1676 helloMsg->csnTlvExists = 1;
1677 helloMsg->csn.baseTlv = tlvTemp;
1678 break;
1680 default:
1682 PRINT_ERR("Found wrong tlv type while decoding hello msg (%d)\n",
1683 tlvTemp.flags.flags.tBit);
1684 if (tlvTemp.flags.flags.uBit == 1) {
1685 /* ignore the Tlv and continue processing */
1686 tempBuf += tlvTemp.length;
1687 totalSize += tlvTemp.length;
1688 totalSizeParam += tlvTemp.length;
1689 break;
1690 } else {
1691 /* drop the message; return error */
1692 return MPLS_TLVTYPEERROR;
1695 } /* switch type */
1697 } /* while */
1699 PRINT_OUT("totalsize for Mpls_decodeLdpHelloMsg is %d\n", totalSize);
1701 return totalSize;
1703 } /* End: Mpls_decodeLdpHelloMsg */
1706 * Encode for Common Hello Parameters TLV
1710 * encode
1712 int Mpls_encodeLdpChp(mplsLdpChpTlv_t * chp, u_char * buff, int bufSize)
1714 int encodedSize = 0;
1715 u_char *tempBuf = buff; /* no change for the buff ptr */
1716 u_char *chpPtr;
1718 /* get the size of the chp to be encoded and check it against
1719 the buffer size */
1721 if (MPLS_TLVFIXLEN + MPLS_CHPFIXLEN > bufSize) {
1722 /* not enough room */
1723 return MPLS_ENC_BUFFTOOSMALL;
1727 * encode for tlv
1729 encodedSize = Mpls_encodeLdpTlv(&(chp->baseTlv), tempBuf, MPLS_TLVFIXLEN);
1730 if (encodedSize < 0) {
1731 return MPLS_ENC_TLVERROR;
1734 chpPtr = (u_char *) chp;
1735 tempBuf += encodedSize;
1736 chpPtr += encodedSize;
1739 * encode for hold time + T + R + res
1741 chp->flags.flags.res = 0;
1742 chp->flags.mark = htons(chp->flags.mark);
1743 chp->holdTime = htons(chp->holdTime);
1745 MEM_COPY(tempBuf, chpPtr, MPLS_CHPFIXLEN);
1747 return (MPLS_TLVFIXLEN + MPLS_CHPFIXLEN);
1749 } /* End: Mpls_encodeLdpChp */
1752 * decode
1754 int Mpls_decodeLdpChp(mplsLdpChpTlv_t * chp, u_char * buff, int bufSize)
1756 u_char *chpPtr;
1758 if (MPLS_CHPFIXLEN > bufSize) {
1759 /* not enough data for Chp */
1760 PRINT_ERR("failed decoding hello Chp\n");
1761 return MPLS_DEC_BUFFTOOSMALL;
1764 chpPtr = (u_char *) chp;
1765 chpPtr += MPLS_TLVFIXLEN; /* we want to point to the flags since the
1766 tlv was decoded before we reach here */
1769 * decode for the rest of the Chp
1771 MEM_COPY(chpPtr, buff, MPLS_CHPFIXLEN);
1773 chp->holdTime = ntohs(chp->holdTime);
1774 chp->flags.mark = ntohs(chp->flags.mark);
1776 return MPLS_CHPFIXLEN;
1778 } /* End: Mpls_decodeLdpChp */
1781 * Encode for Configuration Sequence Number TLV
1785 * encode
1787 int Mpls_encodeLdpCsn(mplsLdpCsnTlv_t * csn, u_char * buff, int bufSize)
1789 int encodedSize = 0;
1790 u_char *tempBuf = buff; /* no change for the buff ptr */
1792 if (MPLS_CSNFIXLEN + MPLS_TLVFIXLEN > bufSize) {
1793 /* not enough room */
1794 return MPLS_ENC_BUFFTOOSMALL;
1798 * encode for tlv
1800 encodedSize = Mpls_encodeLdpTlv(&(csn->baseTlv), tempBuf, bufSize);
1801 if (encodedSize < 0) {
1802 PRINT_ERR("failed encoding the tlv in hello Csn\n");
1803 return MPLS_ENC_TLVERROR;
1805 tempBuf += encodedSize;
1807 csn->seqNumber = htonl(csn->seqNumber);
1809 MEM_COPY(tempBuf, (u_char *) & (csn->seqNumber), MPLS_CSNFIXLEN);
1811 return (MPLS_CSNFIXLEN + MPLS_TLVFIXLEN);
1813 } /* End: Mpls_encodeLdpCsn */
1816 * decode
1818 int Mpls_decodeLdpCsn(mplsLdpCsnTlv_t * csn, u_char * buff, int bufSize)
1820 u_char *tempBuf = buff; /* no change for the buff ptr */
1822 if (MPLS_CSNFIXLEN > bufSize) {
1823 /* not enough data for csn data */
1824 PRINT_ERR("failed decoding hello Csn\n");
1825 return MPLS_DEC_BUFFTOOSMALL;
1829 * decode for the rest of the Csn
1831 MEM_COPY(&(csn->seqNumber), tempBuf, MPLS_CSNFIXLEN);
1833 csn->seqNumber = ntohl(csn->seqNumber);
1835 return MPLS_CSNFIXLEN;
1837 } /* End: Mpls_decodeLdpCsn */
1840 * Encode for Transport Address TLV
1844 * encode
1846 int Mpls_encodeLdpTrAdr(mplsLdpTrAdrTlv_t * trAdr, u_char * buff, int bufSize)
1848 int encodedSize = 0;
1849 u_char *tempBuf = buff; /* no change for the buff ptr */
1851 if (MPLS_TRADRFIXLEN + MPLS_TLVFIXLEN > bufSize) {
1852 /* not enough room */
1853 return MPLS_ENC_BUFFTOOSMALL;
1857 * encode for tlv
1859 encodedSize = Mpls_encodeLdpTlv(&(trAdr->baseTlv), tempBuf, bufSize);
1860 if (encodedSize < 0) {
1861 PRINT_ERR("failed encoding the tlv in hello TrAdr\n");
1862 return MPLS_ENC_TLVERROR;
1864 tempBuf += encodedSize;
1866 trAdr->address = htonl(trAdr->address);
1868 MEM_COPY(tempBuf, (u_char *) & (trAdr->address), MPLS_TRADRFIXLEN);
1870 return (MPLS_TRADRFIXLEN + MPLS_TLVFIXLEN);
1872 } /* End: Mpls_encodeLdpTrAdr */
1875 * decode
1877 int Mpls_decodeLdpTrAdr(mplsLdpTrAdrTlv_t * trAdr, u_char * buff, int bufSize)
1879 u_char *tempBuf = buff; /* no change for the buff ptr */
1881 if (MPLS_TRADRFIXLEN > bufSize) {
1882 /* not enough data for csn data */
1883 PRINT_ERR("failed decoding hello TrAdr\n");
1884 return MPLS_DEC_BUFFTOOSMALL;
1888 * decode for the rest of the TrAdr
1890 MEM_COPY(&(trAdr->address), tempBuf, MPLS_TRADRFIXLEN);
1892 trAdr->address = ntohl(trAdr->address);
1894 return MPLS_TRADRFIXLEN;
1896 } /* End: Mpls_decodeLdpTrAdr */
1899 * Encode for KeepAlive Message
1903 * encode
1905 int Mpls_encodeLdpKeepAliveMsg
1906 (mplsLdpKeepAlMsg_t * keepAlive, u_char * buff, int bufSize) {
1907 mplsLdpKeepAlMsg_t keepAliveCopy;
1908 int encodedSize = 0;
1910 if (MPLS_MSGIDFIXLEN + MPLS_TLVFIXLEN > bufSize) {
1911 PRINT_ERR("failed to encode the keep alive msg: BUFFER TOO SMALL\n");
1912 return MPLS_ENC_BUFFTOOSMALL;
1915 keepAliveCopy = *keepAlive;
1917 encodedSize = Mpls_encodeLdpBaseMsg(&(keepAliveCopy.baseMsg), buff, bufSize);
1918 if (encodedSize < 0) {
1919 return MPLS_ENC_BASEMSGERROR;
1921 PRINT_OUT("Encode BaseMsg for keep alive on %d bytes\n", encodedSize);
1923 return (MPLS_MSGIDFIXLEN + MPLS_TLVFIXLEN);
1925 } /* End: Mpls_encodeLdpKeepAliveMsg */
1928 * decode
1930 int Mpls_decodeLdpKeepAliveMsg
1931 (mplsLdpKeepAlMsg_t * keepAlive, u_char * buff, int bufSize) {
1932 int decodedSize = 0;
1934 memset(keepAlive, 0, MPLS_MSGIDFIXLEN + MPLS_TLVFIXLEN);
1935 decodedSize = Mpls_decodeLdpBaseMsg(&(keepAlive->baseMsg), buff, bufSize);
1936 if (decodedSize < 0) {
1937 return MPLS_DEC_BASEMSGERROR;
1939 PRINT_OUT("Decode BaseMsg for keep alive on %d bytes\n", decodedSize);
1941 if (keepAlive->baseMsg.flags.flags.msgType != MPLS_KEEPAL_MSGTYPE) {
1942 PRINT_ERR("Not the right message type; expected keep alive and got %x\n",
1943 keepAlive->baseMsg.flags.flags.msgType);
1944 return MPLS_MSGTYPEERROR;
1947 return (MPLS_MSGIDFIXLEN + MPLS_TLVFIXLEN);
1949 } /* End: Mpls_decodeLdpKeepAliveMsg */
1952 * Encode for Address List TLV
1956 * encode
1958 int Mpls_encodeLdpAdrTlv(mplsLdpAdrTlv_t * adrList, u_char * buff, int bufSize)
1960 int i, numberAdr;
1961 int encodedSize = 0;
1962 u_char *tempBuf = buff; /* no change for the buff ptr */
1963 u_short tempLength; /* to store the tlv length for
1965 later use */
1967 if (MPLS_TLVFIXLEN + (int)(adrList->baseTlv.length) > bufSize) {
1968 /* not enough room */
1969 return MPLS_ENC_BUFFTOOSMALL;
1972 tempLength = adrList->baseTlv.length;
1974 * encode for tlv
1976 encodedSize = Mpls_encodeLdpTlv(&(adrList->baseTlv), tempBuf, bufSize);
1977 if (encodedSize < 0) {
1978 PRINT_ERR("failed encoding the tlv in AdrList\n");
1979 return MPLS_ENC_TLVERROR;
1982 tempBuf += encodedSize;
1984 adrList->addrFamily = htons(adrList->addrFamily);
1986 numberAdr = (tempLength - sizeof(u_short)) / sizeof(u_int);
1987 for (i = 0; i < numberAdr; i++) {
1988 adrList->address[i] = htonl(adrList->address[i]);
1991 MEM_COPY(tempBuf, (u_char *) & adrList->addrFamily, sizeof(u_short));
1993 tempBuf += sizeof(u_short);
1995 MEM_COPY(tempBuf,
1996 (u_char *) & adrList->address, tempLength - sizeof(u_short));
1998 return (tempLength + MPLS_TLVFIXLEN);
2000 } /* End: Mpls_encodeLdpAdrTlv */
2003 * decode
2005 * Note: the tlvLength is used to specify what is the length of the
2006 * encoding in the AdrTlv.
2008 int Mpls_decodeLdpAdrTlv
2009 (mplsLdpAdrTlv_t * adrList, u_char * buff, int bufSize, u_short tlvLength) {
2010 u_char *tempBuf = buff; /* no change for the buff ptr */
2011 int i, numberAdr;
2013 if ((int)tlvLength > bufSize) {
2014 /* not enough data for Adr list tlv */
2015 PRINT_ERR("failed decoding AddrList tlv\n");
2016 return MPLS_DEC_BUFFTOOSMALL;
2020 * decode for the addressFamily and addresses of the address list
2022 MEM_COPY((u_char *) & adrList->addrFamily, tempBuf, sizeof(u_short));
2023 tempBuf += sizeof(u_short);
2025 adrList->addrFamily = ntohs(adrList->addrFamily);
2027 MEM_COPY((u_char *) & adrList->address, tempBuf, tlvLength - sizeof(u_short));
2029 numberAdr = (tlvLength - sizeof(u_short)) / sizeof(u_int);
2030 for (i = 0; i < numberAdr; i++) {
2031 adrList->address[i] = ntohl(adrList->address[i]);
2034 return tlvLength;
2036 } /* End: Mpls_decodeLdpAdrTlv */
2039 * Encode for Address / Address Withdraw messages
2043 * encode
2045 int Mpls_encodeLdpAdrMsg(mplsLdpAdrMsg_t * addrMsg, u_char * buff, int bufSize)
2047 mplsLdpAdrMsg_t addrMsgCopy;
2048 int encodedSize = 0;
2049 u_int totalSize = 0;
2050 u_char *tempBuf = buff; /* no change for the buff ptr */
2052 /* check the length of the messageId + param */
2053 if ((int)(addrMsg->baseMsg.msgLength) + MPLS_TLVFIXLEN > bufSize) {
2054 PRINT_ERR("failed to encode the address msg: BUFFER TOO SMALL\n");
2055 return MPLS_ENC_BUFFTOOSMALL;
2058 addrMsgCopy = *addrMsg;
2061 * encode the base part of the pdu message
2063 encodedSize = Mpls_encodeLdpBaseMsg(&(addrMsgCopy.baseMsg), tempBuf, bufSize);
2064 if (encodedSize < 0) {
2065 return MPLS_ENC_BASEMSGERROR;
2067 PRINT_OUT("Encode BaseMsg for address on %d bytes\n", encodedSize);
2068 tempBuf += encodedSize;
2069 totalSize += encodedSize;
2072 * encode the address list tlv if any
2074 if (addrMsg->adrListTlvExists) {
2075 encodedSize = Mpls_encodeLdpAdrTlv(&(addrMsgCopy.addressList),
2076 tempBuf, bufSize - totalSize);
2077 if (encodedSize < 0) {
2078 return MPLS_ENC_ADRLISTERROR;
2080 PRINT_OUT("Encoded for AddressList Tlv %d bytes\n", encodedSize);
2083 return (addrMsg->baseMsg.msgLength + MPLS_TLVFIXLEN);
2085 } /* End: */
2088 * decode
2090 int Mpls_decodeLdpAdrMsg(mplsLdpAdrMsg_t * addrMsg, u_char * buff, int bufSize)
2092 int decodedSize = 0;
2093 u_int totalSize = 0;
2094 u_int stopLength = 0;
2095 u_int totalSizeParam = 0;
2096 u_char *tempBuf = buff; /* no change for the buff ptr */
2097 mplsLdpTlv_t tlvTemp;
2100 * decode the base part of the pdu message
2102 memset(addrMsg, 0, sizeof(mplsLdpAdrMsg_t));
2103 decodedSize = Mpls_decodeLdpBaseMsg(&(addrMsg->baseMsg), tempBuf, bufSize);
2104 if (decodedSize < 0) {
2105 return MPLS_DEC_BASEMSGERROR;
2107 PRINT_OUT("Decode BaseMsg for address msg on %d bytes\n", decodedSize);
2109 if ((addrMsg->baseMsg.flags.flags.msgType != MPLS_ADDR_MSGTYPE) &&
2110 (addrMsg->baseMsg.flags.flags.msgType != MPLS_ADDRWITH_MSGTYPE)) {
2111 PRINT_ERR("Not the right message type; expected adr and got %x\n",
2112 addrMsg->baseMsg.flags.flags.msgType);
2113 return MPLS_MSGTYPEERROR;
2116 tempBuf += decodedSize;
2117 totalSize += decodedSize;
2119 if (bufSize - totalSize <= 0) {
2120 /* nothing left for decoding */
2121 PRINT_ERR("Adr msg does not have anything beside base msg\n");
2122 return totalSize;
2125 PRINT_OUT("bufSize = %d, totalSize = %d, addrMsg->baseMsg.msgLength = %d\n",
2126 bufSize, totalSize, addrMsg->baseMsg.msgLength);
2128 /* Have to check the baseMsg.msgLength to know when to finish.
2129 * We finsh when the totalSizeParam is >= to the base message length - the
2130 * message id length (4)
2133 stopLength = addrMsg->baseMsg.msgLength - MPLS_MSGIDFIXLEN;
2134 while (stopLength > totalSizeParam) {
2136 * decode the tlv to check what's next
2138 memset(&tlvTemp, 0, MPLS_TLVFIXLEN);
2139 decodedSize = Mpls_decodeLdpTlv(&tlvTemp, tempBuf, bufSize - totalSize);
2140 if (decodedSize < 0) {
2141 /* something wrong */
2142 PRINT_ERR("ADR msg decode failed for tlv\n");
2143 return MPLS_DEC_TLVERROR;
2146 tempBuf += decodedSize;
2147 totalSize += decodedSize;
2148 totalSizeParam += decodedSize;
2150 switch (tlvTemp.flags.flags.tBit) {
2151 case MPLS_ADDRLIST_TLVTYPE:
2153 decodedSize = Mpls_decodeLdpAdrTlv(&(addrMsg->addressList),
2154 tempBuf, bufSize - totalSize, tlvTemp.length);
2155 if (decodedSize < 0) {
2156 PRINT_ERR("Failure when decoding AdrList tlv from adr msg\n");
2157 return MPLS_DEC_ADRLISTERROR;
2159 PRINT_OUT("Decoded for ADRLIST %d bytes\n", decodedSize);
2160 tempBuf += decodedSize;
2161 totalSize += decodedSize;
2162 totalSizeParam += decodedSize;
2164 addrMsg->adrListTlvExists = 1;
2165 addrMsg->addressList.baseTlv = tlvTemp;
2166 break;
2168 default:
2170 PRINT_ERR("Found wrong tlv type while decoding adr msg (%x)\n",
2171 tlvTemp.flags.flags.tBit);
2172 if (tlvTemp.flags.flags.uBit == 1) {
2173 /* ignore the Tlv and continue processing */
2174 tempBuf += tlvTemp.length;
2175 totalSize += tlvTemp.length;
2176 totalSizeParam += tlvTemp.length;
2177 break;
2178 } else {
2179 /* drop the message; return error */
2180 return MPLS_TLVTYPEERROR;
2183 } /* switch type */
2185 } /* while */
2187 PRINT_OUT("totalsize for Mpls_decodeLdpAdrMsg is %d\n", totalSize);
2189 return totalSize;
2191 } /* End: Mpls_decodeLdpAdrMsg */
2194 * Encode for FEC ELEMENT
2198 * encode
2200 int Mpls_encodeLdpFecAdrEl
2201 (mplsFecElement_t * fecAdrEl, u_char * buff, int bufSize, u_char type) {
2202 int encodedSize = 0;
2203 u_char *tempBuf = buff;
2205 switch (type) {
2206 case MPLS_WC_FEC:
2208 if (MPLS_FEC_ELEMTYPELEN > bufSize) {
2209 return MPLS_ENC_BUFFTOOSMALL;
2211 *buff = fecAdrEl->wildcardEl.type;
2212 encodedSize = MPLS_FEC_ELEMTYPELEN;
2213 break;
2215 case MPLS_PREFIX_FEC:
2217 int preLenOctets;
2219 fecAdrEl->addressEl.addressFam = htons(fecAdrEl->addressEl.addressFam);
2220 fecAdrEl->addressEl.address = htonl(fecAdrEl->addressEl.address);
2221 preLenOctets = (int)(fecAdrEl->addressEl.preLen / 8) +
2222 ((int)(fecAdrEl->addressEl.preLen % 8) > 0 ? 1 : 0);
2224 encodedSize = MPLS_FEC_ADRFAMLEN + MPLS_FEC_ELEMTYPELEN +
2225 MPLS_FEC_PRELENLEN + preLenOctets;
2227 if (encodedSize > bufSize) {
2228 return MPLS_ENC_BUFFTOOSMALL;
2230 *tempBuf = fecAdrEl->addressEl.type;
2231 tempBuf++; /* for MPLS_FEC_ELEMTYPELEN */
2233 MEM_COPY(tempBuf,
2234 (u_char *) & (fecAdrEl->addressEl.addressFam), MPLS_FEC_ADRFAMLEN);
2235 tempBuf += MPLS_FEC_ADRFAMLEN;
2237 *tempBuf = fecAdrEl->addressEl.preLen;
2238 tempBuf++; /* for MPLS_FEC_PRELENLEN */
2240 MEM_COPY(tempBuf, (u_char *) & (fecAdrEl->addressEl.address),
2241 preLenOctets);
2242 break;
2244 case MPLS_HOSTADR_FEC:
2246 fecAdrEl->addressEl.addressFam = htons(fecAdrEl->addressEl.addressFam);
2247 fecAdrEl->addressEl.address = htonl(fecAdrEl->addressEl.address);
2249 encodedSize = MPLS_FEC_ADRFAMLEN + MPLS_FEC_ELEMTYPELEN +
2250 MPLS_FEC_PRELENLEN + fecAdrEl->addressEl.preLen;
2252 if (encodedSize > bufSize) {
2253 return MPLS_ENC_BUFFTOOSMALL;
2255 *tempBuf = fecAdrEl->addressEl.type;
2256 tempBuf++; /* for MPLS_FEC_ELEMTYPELEN */
2258 MEM_COPY(tempBuf,
2259 (u_char *) & (fecAdrEl->addressEl.addressFam), MPLS_FEC_ADRFAMLEN);
2260 tempBuf += MPLS_FEC_ADRFAMLEN;
2262 *tempBuf = fecAdrEl->addressEl.preLen;
2263 tempBuf++; /* for MPLS_FEC_PRELENLEN */
2265 MEM_COPY(tempBuf,
2266 (u_char *) & (fecAdrEl->addressEl.address),
2267 fecAdrEl->addressEl.preLen);
2268 break;
2270 case MPLS_CRLSP_FEC:
2272 if (MPLS_FEC_CRLSPLEN > bufSize) {
2273 return MPLS_ENC_BUFFTOOSMALL;
2275 fecAdrEl->crlspEl.res1 = 0;
2276 fecAdrEl->crlspEl.res2 = 0;
2277 MEM_COPY(tempBuf, (u_char *) & (fecAdrEl->crlspEl), MPLS_FEC_CRLSPLEN);
2278 encodedSize = MPLS_FEC_CRLSPLEN;
2279 break;
2281 default:
2283 PRINT_ERR("Found wrong FEC type while encoding FEC elem (%d)\n", type);
2284 return MPLS_ENC_FECELEMERROR;
2285 break;
2287 } /* end: switch */
2289 return encodedSize;
2291 } /* End: Mpls_encodeLdpFecAdrEl */
2294 * decode
2296 int Mpls_decodeLdpFecAdrEl
2297 (mplsFecElement_t * fecAdrEl, u_char * buff, int bufSize, u_char type) {
2298 int decodedSize = 0;
2299 u_char *tempBuff = buff;
2300 u_int preLen = 0;
2302 switch (type) {
2303 case MPLS_WC_FEC:
2305 fecAdrEl->wildcardEl.type = *buff;
2306 decodedSize = MPLS_FEC_ELEMTYPELEN;
2307 break;
2309 case MPLS_PREFIX_FEC:
2311 decodedSize = MPLS_FEC_ADRFAMLEN + MPLS_FEC_ELEMTYPELEN +
2312 MPLS_FEC_PRELENLEN;
2313 if (decodedSize > bufSize) {
2314 return MPLS_DEC_BUFFTOOSMALL;
2316 fecAdrEl->addressEl.type = *tempBuff;
2317 tempBuff++; /* for MPLS_FEC_ELEMTYPELEN */
2319 MEM_COPY((u_char *) & (fecAdrEl->addressEl.addressFam),
2320 tempBuff, MPLS_FEC_ADRFAMLEN);
2321 tempBuff += MPLS_FEC_ADRFAMLEN;
2323 fecAdrEl->addressEl.preLen = *tempBuff;
2324 tempBuff++; /* for MPLS_FEC_PRELENLEN */
2326 fecAdrEl->addressEl.addressFam = ntohs(fecAdrEl->addressEl.addressFam);
2328 /* now we get the prefix; we need to use the preLen which was
2329 decoded from buff */
2331 preLen = (int)(fecAdrEl->addressEl.preLen / 8) +
2332 ((int)(fecAdrEl->addressEl.preLen % 8) > 0 ? 1 : 0);
2334 if (fecAdrEl->addressEl.preLen > sizeof(u_int) * 8) {
2335 /* error - the length cannot exeed 32 bits */
2336 /* skip the FEC and return error code */
2337 /* fill in the preLen field to the number of bytes for this
2338 fec; we need it to know how much to skip from the buffer
2339 when we do the decoding for the following fec element */
2340 fecAdrEl->addressEl.preLen = preLen + decodedSize;
2341 return MPLS_FECERROR;
2343 if ((int)preLen > bufSize - decodedSize) {
2344 return MPLS_DEC_BUFFTOOSMALL;
2346 MEM_COPY((u_char *) & (fecAdrEl->addressEl.address), tempBuff, preLen);
2348 fecAdrEl->addressEl.address = ntohl(fecAdrEl->addressEl.address);
2349 decodedSize += preLen;
2350 break;
2352 case MPLS_HOSTADR_FEC:
2354 decodedSize = MPLS_FEC_ADRFAMLEN + MPLS_FEC_ELEMTYPELEN +
2355 MPLS_FEC_PRELENLEN;
2356 if (decodedSize > bufSize) {
2357 return MPLS_DEC_BUFFTOOSMALL;
2359 fecAdrEl->addressEl.type = *tempBuff;
2360 tempBuff++; /* for MPLS_FEC_ELEMTYPELEN */
2362 MEM_COPY((u_char *) & (fecAdrEl->addressEl.addressFam),
2363 tempBuff, MPLS_FEC_ADRFAMLEN);
2364 tempBuff += MPLS_FEC_ADRFAMLEN;
2366 fecAdrEl->addressEl.preLen = *tempBuff;
2367 tempBuff++; /* for MPLS_FEC_PRELENLEN */
2369 fecAdrEl->addressEl.addressFam = ntohs(fecAdrEl->addressEl.addressFam);
2371 /* now we get the host address; we need to use the preLen which was
2372 decoded from buff */
2374 preLen = fecAdrEl->addressEl.preLen;
2375 if (fecAdrEl->addressEl.preLen > sizeof(u_int)) {
2376 /* error - the length cannot exeed 32 bits */
2377 /* skip the FEC and return error code */
2378 /* fill in the preLen field to the number of bytes for this
2379 fec; we need it to know how much to skip from the buffer
2380 when we do the decoding for the following fec element */
2381 fecAdrEl->addressEl.preLen = preLen + decodedSize;
2382 return MPLS_FECERROR;
2384 if ((int)preLen > bufSize - decodedSize) {
2385 return MPLS_DEC_BUFFTOOSMALL;
2387 MEM_COPY((u_char *) & (fecAdrEl->addressEl.address), tempBuff, preLen);
2389 fecAdrEl->addressEl.address = ntohl(fecAdrEl->addressEl.address);
2390 decodedSize += preLen;
2391 break;
2393 case MPLS_CRLSP_FEC:
2395 if (MPLS_FEC_CRLSPLEN > bufSize) {
2396 return MPLS_DEC_BUFFTOOSMALL;
2398 MEM_COPY((u_char *) & (fecAdrEl->crlspEl), tempBuff, MPLS_FEC_CRLSPLEN);
2399 decodedSize = MPLS_FEC_CRLSPLEN;
2400 break;
2402 default:
2404 PRINT_ERR("Found wrong FEC type while decoding FEC elem (%d)\n", type);
2405 return MPLS_DEC_FECELEMERROR;
2406 break;
2408 } /* end: switch */
2410 return decodedSize;
2412 } /* End: Mpls_decodeLdpFecAdrEl */
2415 * Encode for FEC TLV
2419 * encode
2421 int Mpls_encodeLdpFecTlv(mplsLdpFecTlv_t * fecTlv, u_char * buff, int bufSize)
2423 u_char *tempBuf = buff; /* no change for the buff ptr */
2424 u_short i;
2425 int encodedSize = 0;
2426 u_int fecElSize = 0; /* used to compute the sum of
2428 all fec elements */
2430 if ((int)fecTlv->baseTlv.length + MPLS_TLVFIXLEN > bufSize) {
2431 /* not enough room */
2432 return MPLS_ENC_BUFFTOOSMALL;
2435 /* check how many fec elements we have */
2436 if (fecTlv->numberFecElements > MPLS_MAXNUMFECELEMENT) {
2437 /* too many fec elem; need to increase MPLS_MAXNUMFECELEMENT */
2438 PRINT_ERR("Too many fec elem\n");
2439 return MPLS_FECTLVERROR;
2442 for (i = 0; i < fecTlv->numberFecElements; i++) {
2443 if ((fecTlv->fecElemTypes[i] == MPLS_WC_FEC) &&
2444 (fecTlv->numberFecElements != 1)) {
2445 return MPLS_WC_FECERROR;
2450 * encode for tlv
2452 encodedSize = Mpls_encodeLdpTlv(&(fecTlv->baseTlv), tempBuf, bufSize);
2453 if (encodedSize < 0) {
2454 PRINT_ERR("failed encoding the tlv in FEC tlv\n");
2455 return MPLS_ENC_TLVERROR;
2457 tempBuf += encodedSize;
2458 fecElSize += encodedSize;
2460 /* encode now the FEC elements; check if wc exists; if it is there
2461 then it should be the only element */
2463 for (i = 0; i < fecTlv->numberFecElements; i++) {
2464 encodedSize = Mpls_encodeLdpFecAdrEl(&(fecTlv->fecElArray[i]),
2465 tempBuf, bufSize - fecElSize, fecTlv->fecElemTypes[i]);
2466 if (encodedSize < 0) {
2467 return MPLS_ENC_FECELEMERROR;
2469 tempBuf += encodedSize;
2470 fecElSize += encodedSize;
2473 return fecElSize;
2475 } /* End: Mpls_encodeLdpFecTlv */
2478 * decode
2480 int Mpls_decodeLdpFecTlv
2481 (mplsLdpFecTlv_t * fecTlv, u_char * buff, int bufSize, u_short tlvLength) {
2482 u_char *tempBuf = buff; /* no change for the buff ptr */
2483 int decodedSize = 0;
2484 u_int fecElSize = 0; /* used to compute the sum of
2486 all fec elements */
2487 u_short i = 0;
2488 u_char type;
2490 if ((int)tlvLength > bufSize) {
2491 /* not enough data for Fec elements tlv */
2492 PRINT_ERR("failed decoding FEC elements tlv\n");
2493 return MPLS_DEC_BUFFTOOSMALL;
2497 * decode for the FEC elements; check also that if we have a wc element,
2498 * it is the only element encoded in the FEC;
2500 type = *tempBuf; /* first thing after the TLV base should be the type
2501 of the fec element */
2503 fecTlv->numberFecElements = 0;
2505 while (tlvLength > fecElSize) {
2507 /* check how many fec elements we have */
2508 if (fecTlv->numberFecElements > (u_short) (MPLS_MAXNUMFECELEMENT - 1)) {
2509 /* too many fec elem; need to increase MPLS_MAXNUMFECELEMENT */
2510 PRINT_ERR("Too many fec elem\n");
2511 return MPLS_FECTLVERROR;
2514 decodedSize = Mpls_decodeLdpFecAdrEl(&(fecTlv->fecElArray[i]),
2515 tempBuf, bufSize - fecElSize, type);
2516 if ((decodedSize < 0) && (decodedSize != MPLS_FECERROR)) {
2517 return MPLS_DEC_FECELEMERROR;
2518 } else {
2519 /* if the element had wrong preLen value, just skip it */
2520 if (decodedSize != MPLS_FECERROR) {
2521 fecTlv->fecElemTypes[i] = type;
2522 fecTlv->numberFecElements++;
2523 i++;
2525 tempBuf += decodedSize;
2526 fecElSize += decodedSize;
2527 } else {
2528 /* the preLen was filled with the total length
2529 of the fec element to be skipped */
2530 tempBuf += fecTlv->fecElArray[i].addressEl.preLen;
2531 fecElSize += fecTlv->fecElArray[i].addressEl.preLen;
2535 /* get the type of the next element */
2536 type = *tempBuf;
2538 } /* end while */
2540 for (i = 0; i < fecTlv->numberFecElements; i++) {
2541 if ((fecTlv->fecElemTypes[i] == MPLS_WC_FEC) &&
2542 (fecTlv->numberFecElements != 1)) {
2543 return MPLS_WC_FECERROR;
2547 return fecElSize; /* fecElSize should be equal to tlvLength */
2549 } /* End: Mpls_decodeLdpFecTlv */
2552 * Encode for Generic label TLV
2556 * encode
2558 int Mpls_encodeLdpGenLblTlv
2559 (mplsLdpGenLblTlv_t * genLbl, u_char * buff, int bufSize) {
2560 int encodedSize = 0;
2561 u_char *tempBuf = buff; /* no change for the buff ptr */
2563 if (MPLS_TLVFIXLEN + (int)(genLbl->baseTlv.length) > bufSize) {
2564 /* not enough room */
2565 return MPLS_ENC_BUFFTOOSMALL;
2569 * encode for tlv
2571 encodedSize = Mpls_encodeLdpTlv(&(genLbl->baseTlv), tempBuf, bufSize);
2572 if (encodedSize < 0) {
2573 PRINT_ERR("failed encoding the tlv in Generic Label\n");
2574 return MPLS_ENC_TLVERROR;
2576 tempBuf += encodedSize;
2578 genLbl->label = htonl(genLbl->label);
2580 MEM_COPY(tempBuf, (u_char *) & (genLbl->label), MPLS_LBLFIXLEN);
2582 return (MPLS_TLVFIXLEN + MPLS_LBLFIXLEN);
2584 } /* End: Mpls_encodeLdpGenLblTlv */
2587 * decode
2589 int Mpls_decodeLdpGenLblTlv
2590 (mplsLdpGenLblTlv_t * genLbl, u_char * buff, int bufSize) {
2591 if (MPLS_LBLFIXLEN > bufSize) {
2592 /* not enough data for generic label tlv */
2593 PRINT_ERR("failed decoding Generic tlv\n");
2594 return MPLS_DEC_BUFFTOOSMALL;
2598 * decode the label
2600 MEM_COPY((u_char *) & (genLbl->label), buff, MPLS_LBLFIXLEN);
2602 genLbl->label = ntohl(genLbl->label);
2604 return MPLS_LBLFIXLEN;
2606 } /* End: Mpls_decodeLdpGenLblTlv */
2609 * Encode for ATM Label TLV
2613 * encode
2615 int Mpls_encodeLdpAtmLblTlv
2616 (mplsLdpAtmLblTlv_t * atmLblTlv, u_char * buff, int bufSize) {
2617 int encodedSize = 0;
2618 u_char *tempBuf = buff; /* no change for the buff ptr */
2619 u_char *atmLblPtr;
2621 if (MPLS_TLVFIXLEN + MPLS_LBLFIXLEN > bufSize) {
2622 /* not enough room */
2623 return MPLS_ENC_BUFFTOOSMALL;
2626 atmLblPtr = (u_char *) atmLblTlv;
2629 * encode for tlv
2631 encodedSize = Mpls_encodeLdpTlv(&(atmLblTlv->baseTlv),
2632 tempBuf, MPLS_TLVFIXLEN);
2633 if (encodedSize < 0) {
2634 return MPLS_ENC_TLVERROR;
2636 tempBuf += encodedSize;
2637 atmLblPtr += encodedSize;
2640 * encode for flags
2642 atmLblTlv->flags.flags.res = 0;
2643 atmLblTlv->flags.mark = htons(atmLblTlv->flags.mark);
2644 atmLblTlv->vci = htons(atmLblTlv->vci);
2646 MEM_COPY(tempBuf, atmLblPtr, MPLS_LBLFIXLEN);
2648 return (MPLS_LBLFIXLEN + MPLS_TLVFIXLEN);
2650 } /* End: Mpls_encodeLdpAtmLblTlv */
2653 * decode
2655 int Mpls_decodeLdpAtmLblTlv
2656 (mplsLdpAtmLblTlv_t * atmLblTlv, u_char * buff, int bufSize) {
2657 u_char *atmLblTlvPtr;
2659 if (MPLS_LBLFIXLEN > bufSize) {
2660 /* not enough data for AtmLabel */
2661 PRINT_ERR("failed decoding atm label tlv\n");
2662 return MPLS_DEC_BUFFTOOSMALL;
2665 atmLblTlvPtr = (u_char *) atmLblTlv;
2666 atmLblTlvPtr += MPLS_TLVFIXLEN; /* to point after the Tlv which was
2667 decoded before we reach here */
2669 * decode for the rest of the AtmLblTlv
2671 MEM_COPY(atmLblTlvPtr, buff, MPLS_LBLFIXLEN);
2673 atmLblTlv->flags.mark = ntohs(atmLblTlv->flags.mark);
2674 atmLblTlv->vci = ntohs(atmLblTlv->vci);
2676 return MPLS_LBLFIXLEN;
2678 } /* End: Mpls_decodeLdpAtmLblTlv */
2681 * Encode for FR Label TLV
2685 * encode
2687 int Mpls_encodeLdpFrLblTlv
2688 (mplsLdpFrLblTlv_t * frLblTlv, u_char * buff, int bufSize) {
2689 int encodedSize = 0;
2690 u_char *tempBuf = buff; /* no change for the buff ptr */
2692 if (MPLS_TLVFIXLEN + MPLS_LBLFIXLEN > bufSize) {
2693 /* not enough room */
2694 return MPLS_ENC_BUFFTOOSMALL;
2698 * encode for tlv
2700 encodedSize = Mpls_encodeLdpTlv(&(frLblTlv->baseTlv),
2701 tempBuf, MPLS_TLVFIXLEN);
2702 if (encodedSize < 0) {
2703 return MPLS_ENC_TLVERROR;
2705 tempBuf += encodedSize;
2708 * encode for flags
2710 frLblTlv->flags.mark = htonl(frLblTlv->flags.mark);
2712 MEM_COPY(tempBuf, (u_char *) & (frLblTlv->flags.mark), MPLS_LBLFIXLEN);
2714 return (MPLS_LBLFIXLEN + MPLS_TLVFIXLEN);
2716 } /* End: Mpls_encodeLdpFrLblTlv */
2719 * decode
2721 int Mpls_decodeLdpFrLblTlv
2722 (mplsLdpFrLblTlv_t * frLblTlv, u_char * buff, int bufSize) {
2723 u_char *tempBuf = buff; /* no change for the buff ptr */
2725 if (MPLS_LBLFIXLEN > bufSize) {
2726 /* not enough data for FrLabel */
2727 PRINT_ERR("failed decoding fr label tlv\n");
2728 return MPLS_DEC_BUFFTOOSMALL;
2732 * decode for the rest of the FrLblTlv
2734 MEM_COPY((u_char *) & (frLblTlv->flags.mark), tempBuf, MPLS_LBLFIXLEN);
2736 frLblTlv->flags.mark = ntohl(frLblTlv->flags.mark);
2738 return MPLS_LBLFIXLEN;
2740 } /* End: Mpls_decodeLdpFrLblTlv */
2743 * Encode for Hop Count TLV
2747 * encode
2749 int Mpls_encodeLdpHopTlv
2750 (mplsLdpHopTlv_t * hopCountTlv, u_char * buff, int bufSize) {
2751 int encodedSize = 0;
2752 u_char *tempBuf = buff; /* no change for the buff ptr */
2754 if (MPLS_TLVFIXLEN + MPLS_HOPCOUNTFIXLEN > bufSize) {
2755 /* not enough room */
2756 return MPLS_ENC_BUFFTOOSMALL;
2760 * encode for tlv
2762 encodedSize = Mpls_encodeLdpTlv(&(hopCountTlv->baseTlv),
2763 tempBuf, MPLS_TLVFIXLEN);
2764 if (encodedSize < 0) {
2765 return MPLS_ENC_TLVERROR;
2767 tempBuf += encodedSize;
2770 * encode for hop count value
2772 *tempBuf = hopCountTlv->hcValue;
2774 return (MPLS_HOPCOUNTFIXLEN + MPLS_TLVFIXLEN);
2776 } /* End: Mpls_encodeLdpFrLblTlv */
2779 * decode
2781 int Mpls_decodeLdpHopTlv
2782 (mplsLdpHopTlv_t * hopCountTlv, u_char * buff, int bufSize) {
2783 if (MPLS_HOPCOUNTFIXLEN > bufSize) {
2784 /* not enough data for hop count value */
2785 PRINT_ERR("failed decoding hop count tlv\n");
2786 return MPLS_DEC_BUFFTOOSMALL;
2790 * decode for the hop count value
2792 hopCountTlv->hcValue = *buff;
2794 return MPLS_HOPCOUNTFIXLEN;
2796 } /* End: Mpls_decodeLdpHopTlv */
2799 * Encode for Lbl Msg Id TLV
2803 * encode
2805 int Mpls_encodeLdpLblMsgIdTlv
2806 (mplsLdpLblMsgIdTlv_t * lblMsgIdTlv, u_char * buff, int bufSize) {
2807 int encodedSize = 0;
2808 u_char *tempBuf = buff; /* no change for the buff ptr */
2810 if (MPLS_TLVFIXLEN + MPLS_LBLFIXLEN > bufSize) {
2811 /* not enough room */
2812 return MPLS_ENC_BUFFTOOSMALL;
2816 * encode for tlv
2818 encodedSize = Mpls_encodeLdpTlv(&(lblMsgIdTlv->baseTlv),
2819 tempBuf, MPLS_TLVFIXLEN);
2820 if (encodedSize < 0) {
2821 return MPLS_ENC_TLVERROR;
2823 tempBuf += encodedSize;
2826 * encode for msg id
2829 lblMsgIdTlv->msgId = htonl(lblMsgIdTlv->msgId);
2831 MEM_COPY(tempBuf, (u_char *) & (lblMsgIdTlv->msgId), MPLS_LBLFIXLEN);
2833 return (MPLS_LBLFIXLEN + MPLS_TLVFIXLEN);
2835 } /* End: Mpls_encodeLdpFrLblTlv */
2838 * decode
2840 int Mpls_decodeLdpLblMsgIdTlv
2841 (mplsLdpLblMsgIdTlv_t * lblMsgIdTlv, u_char * buff, int bufSize) {
2842 if (MPLS_LBLFIXLEN > bufSize) {
2843 /* not enough data for msg id tlv */
2844 PRINT_ERR("failed decoding lbl msg id tlv\n");
2845 return MPLS_DEC_BUFFTOOSMALL;
2849 * decode for the rest of the LblMsgId Tlv
2851 MEM_COPY((u_char *) & (lblMsgIdTlv->msgId), buff, MPLS_LBLFIXLEN);
2853 lblMsgIdTlv->msgId = ntohl(lblMsgIdTlv->msgId);
2855 return MPLS_LBLFIXLEN;
2857 } /* End: Mpls_decodeLdpLblMsgIdTlv */
2860 * Encode for Path Vector TLV
2864 * encode
2866 int Mpls_encodeLdpPathVectorTlv
2867 (mplsLdpPathTlv_t * pathVectorTlv, u_char * buff, int bufSize) {
2868 u_char *pathVectorTlvPtr;
2869 u_char *tempBuf = buff; /* no change for the buff ptr */
2870 int encodedSize = 0;
2871 u_int i, numLsrIds;
2872 u_short tempLength; /* to store the tlv length for
2874 later use */
2876 if (MPLS_TLVFIXLEN + (int)(pathVectorTlv->baseTlv.length) > bufSize) {
2877 /* not enough room */
2878 return MPLS_ENC_BUFFTOOSMALL;
2881 pathVectorTlvPtr = (u_char *) pathVectorTlv;
2882 tempLength = pathVectorTlv->baseTlv.length;
2884 * encode for tlv
2886 encodedSize = Mpls_encodeLdpTlv(&(pathVectorTlv->baseTlv),
2887 tempBuf, MPLS_TLVFIXLEN);
2888 if (encodedSize < 0) {
2889 return MPLS_ENC_TLVERROR;
2891 tempBuf += encodedSize;
2892 pathVectorTlvPtr += encodedSize;
2895 * encode for labels
2898 if (tempLength % MPLS_LBLFIXLEN != 0) {
2899 return MPLS_PATHVECTORERROR;
2902 numLsrIds = tempLength / MPLS_LBLFIXLEN;
2903 if (numLsrIds > MPLS_MAXHOPSNUMBER) {
2904 /* too many lsrIds; need to increase MPLS_MAXHOPSNUMBER */
2905 PRINT_ERR("Too many lsr ids (%d)\n", numLsrIds);
2906 return MPLS_PATHVECTORERROR;
2909 for (i = 0; i < numLsrIds; i++) {
2910 pathVectorTlv->lsrId[i] = htonl(pathVectorTlv->lsrId[i]);
2913 MEM_COPY(tempBuf, pathVectorTlvPtr, tempLength);
2915 return (tempLength + MPLS_TLVFIXLEN);
2917 } /* End: Mpls_encodeLdpPathVectorTlv */
2920 * decode
2922 int Mpls_decodeLdpPathVectorTlv
2923 (mplsLdpPathTlv_t * pathVectorTlv,
2924 u_char * buff, int bufSize, u_short tlvLength) {
2925 u_char *tempBuf = buff; /* no change for the buff ptr */
2926 u_int i, numLsrIds;
2928 if (MPLS_LBLFIXLEN > bufSize) {
2929 /* not enough data for msg id tlv */
2930 PRINT_ERR("failed decoding lbl msg id tlv\n");
2931 return MPLS_DEC_BUFFTOOSMALL;
2934 if (tlvLength % MPLS_LBLFIXLEN != 0) {
2935 PRINT_ERR("Wrong length for Path vector tlv (%d)\n", tlvLength);
2936 return MPLS_PATHVECTORERROR;
2939 numLsrIds = tlvLength / MPLS_LBLFIXLEN;
2940 if (numLsrIds > MPLS_MAXHOPSNUMBER) {
2941 /* too many lsrIds; need to increase MPLS_MAXHOPSNUMBER */
2942 PRINT_ERR("Too many lsr ids (%d)\n", numLsrIds);
2943 return MPLS_PATHVECTORERROR;
2947 * decode for the rest of the LblMsgId Tlv
2949 MEM_COPY((u_char *) (pathVectorTlv->lsrId), tempBuf, tlvLength);
2951 for (i = 0; i < numLsrIds; i++) {
2952 pathVectorTlv->lsrId[i] = ntohl(pathVectorTlv->lsrId[i]);
2955 return tlvLength;
2957 } /* End: Mpls_decodeLdpPathVectorTlv */
2960 * Encode for Label Mapping Message
2964 * encode
2966 int Mpls_encodeLdpLblMapMsg
2967 (mplsLdpLblMapMsg_t * lblMapMsg, u_char * buff, int bufSize) {
2968 mplsLdpLblMapMsg_t lblMapMsgCopy;
2969 int encodedSize = 0;
2970 u_int totalSize = 0;
2971 u_char *tempBuf = buff; /* no change for the buff ptr */
2973 /* check the length of the messageId + param */
2974 if ((int)(lblMapMsg->baseMsg.msgLength) + MPLS_TLVFIXLEN > bufSize) {
2975 PRINT_ERR("failed to encode the lbl mapping msg: BUFFER TOO SMALL\n");
2976 return MPLS_ENC_BUFFTOOSMALL;
2979 lblMapMsgCopy = *lblMapMsg;
2982 * encode the base part of the pdu message
2984 encodedSize = Mpls_encodeLdpBaseMsg(&(lblMapMsgCopy.baseMsg),
2985 tempBuf, bufSize);
2986 if (encodedSize < 0) {
2987 return MPLS_ENC_BASEMSGERROR;
2989 PRINT_OUT("Encode BaseMsg for label mapping on %d bytes\n", encodedSize);
2990 tempBuf += encodedSize;
2991 totalSize += encodedSize;
2994 * encode the tlv if any
2996 if (lblMapMsgCopy.fecTlvExists) {
2997 encodedSize = Mpls_encodeLdpFecTlv(&(lblMapMsgCopy.fecTlv),
2998 tempBuf, bufSize - totalSize);
2999 if (encodedSize < 0) {
3000 return MPLS_ENC_FECERROR;
3002 PRINT_OUT("Encoded for FEC Tlv %d bytes\n", encodedSize);
3003 tempBuf += encodedSize;
3004 totalSize += encodedSize;
3007 if (lblMapMsgCopy.genLblTlvExists) {
3008 encodedSize = Mpls_encodeLdpGenLblTlv(&(lblMapMsgCopy.genLblTlv),
3009 tempBuf, bufSize - totalSize);
3010 if (encodedSize < 0) {
3011 return MPLS_ENC_GENLBLERROR;
3013 PRINT_OUT("Encoded for Generic Label Tlv %d bytes\n", encodedSize);
3014 tempBuf += encodedSize;
3015 totalSize += encodedSize;
3017 if (lblMapMsgCopy.atmLblTlvExists) {
3018 encodedSize = Mpls_encodeLdpAtmLblTlv(&(lblMapMsgCopy.atmLblTlv),
3019 tempBuf, bufSize - totalSize);
3020 if (encodedSize < 0) {
3021 return MPLS_ENC_MAPATMERROR;
3023 PRINT_OUT("Encoded for Atm Label Tlv %d bytes\n", encodedSize);
3024 tempBuf += encodedSize;
3025 totalSize += encodedSize;
3027 if (lblMapMsgCopy.frLblTlvExists) {
3028 encodedSize = Mpls_encodeLdpFrLblTlv(&(lblMapMsgCopy.frLblTlv),
3029 tempBuf, bufSize - totalSize);
3030 if (encodedSize < 0) {
3031 return MPLS_ENC_FRLBLERROR;
3033 PRINT_OUT("Encoded for Fr Label Tlv %d bytes\n", encodedSize);
3034 tempBuf += encodedSize;
3035 totalSize += encodedSize;
3037 if (lblMapMsgCopy.hopCountTlvExists) {
3038 encodedSize = Mpls_encodeLdpHopTlv(&(lblMapMsgCopy.hopCountTlv),
3039 tempBuf, bufSize - totalSize);
3040 if (encodedSize < 0) {
3041 return MPLS_ENC_HOPCOUNTERROR;
3043 PRINT_OUT("Encoded for Hop Count Tlv %d bytes\n", encodedSize);
3044 tempBuf += encodedSize;
3045 totalSize += encodedSize;
3047 if (lblMapMsgCopy.pathVecTlvExists) {
3048 encodedSize = Mpls_encodeLdpPathVectorTlv(&(lblMapMsgCopy.pathVecTlv),
3049 tempBuf, bufSize - totalSize);
3050 if (encodedSize < 0) {
3051 return MPLS_ENC_PATHVECERROR;
3053 PRINT_OUT("Encoded for Path Vector Tlv %d bytes\n", encodedSize);
3054 tempBuf += encodedSize;
3055 totalSize += encodedSize;
3057 if (lblMapMsgCopy.lblMsgIdTlvExists) {
3058 encodedSize = Mpls_encodeLdpLblMsgIdTlv(&(lblMapMsgCopy.lblMsgIdTlv),
3059 tempBuf, bufSize - totalSize);
3060 if (encodedSize < 0) {
3061 return MPLS_ENC_LBLMSGIDERROR;
3063 PRINT_OUT("Encoded for lbl request msg id Tlv %d bytes\n", encodedSize);
3064 tempBuf += encodedSize;
3065 totalSize += encodedSize;
3067 if (lblMapMsgCopy.trafficTlvExists) {
3068 encodedSize = Mpls_encodeLdpTrafficTlv(&(lblMapMsgCopy.trafficTlv),
3069 tempBuf, bufSize - totalSize);
3070 if (encodedSize < 0) {
3071 return MPLS_ENC_TRAFFICERROR;
3073 PRINT_OUT("Encoded for Traffic Tlv %d bytes\n", encodedSize);
3074 tempBuf += encodedSize;
3075 totalSize += encodedSize;
3077 if (lblMapMsgCopy.lspidTlvExists) {
3078 encodedSize = Mpls_encodeLdpLspIdTlv(&(lblMapMsgCopy.lspidTlv),
3079 tempBuf, bufSize - totalSize);
3080 if (encodedSize < 0) {
3081 return MPLS_ENC_LSPIDERROR;
3083 PRINT_OUT("Encoded for LSPID Tlv %d bytes\n", encodedSize);
3084 tempBuf += encodedSize;
3085 totalSize += encodedSize;
3088 return totalSize;
3090 } /* End: Mpls_encodeLdpLblMapMsg */
3093 * decode
3095 int Mpls_decodeLdpLblMapMsg
3096 (mplsLdpLblMapMsg_t * lblMapMsg, u_char * buff, int bufSize) {
3097 int decodedSize = 0;
3098 u_int totalSize = 0;
3099 u_int stopLength = 0;
3100 u_int totalSizeParam = 0;
3101 u_char *tempBuf = buff; /* no change for the buff ptr */
3102 mplsLdpTlv_t tlvTemp;
3105 * decode the base part of the pdu message
3107 memset(lblMapMsg, 0, sizeof(mplsLdpLblMapMsg_t));
3108 decodedSize = Mpls_decodeLdpBaseMsg(&(lblMapMsg->baseMsg), tempBuf, bufSize);
3109 if (decodedSize < 0) {
3110 return MPLS_DEC_BASEMSGERROR;
3112 PRINT_OUT("Decode BaseMsg for Lbl Mapping on %d bytes\n", decodedSize);
3114 if (lblMapMsg->baseMsg.flags.flags.msgType != MPLS_LBLMAP_MSGTYPE) {
3115 PRINT_ERR("Not the right message type; expected lbl map and got %x\n",
3116 lblMapMsg->baseMsg.flags.flags.msgType);
3117 return MPLS_MSGTYPEERROR;
3120 tempBuf += decodedSize;
3121 totalSize += decodedSize;
3123 if (bufSize - totalSize <= 0) {
3124 /* nothing left for decoding */
3125 PRINT_ERR("Lbl Mapping msg does not have anything beside base msg\n");
3126 return totalSize;
3129 PRINT_OUT
3130 ("bufSize = %d, totalSize = %d, lblMapMsg->baseMsg.msgLength = %d\n",
3131 bufSize, totalSize, lblMapMsg->baseMsg.msgLength);
3133 /* Have to check the baseMsg.msgLength to know when to finish.
3134 * We finsh when the totalSizeParam is >= to the base message length - the
3135 * message id length (4)
3138 stopLength = lblMapMsg->baseMsg.msgLength - MPLS_MSGIDFIXLEN;
3139 while (stopLength > totalSizeParam) {
3141 * decode the tlv to check what's next
3143 memset(&tlvTemp, 0, MPLS_TLVFIXLEN);
3144 decodedSize = Mpls_decodeLdpTlv(&tlvTemp, tempBuf, bufSize - totalSize);
3145 if (decodedSize < 0) {
3146 /* something wrong */
3147 PRINT_ERR("Label Mapping msg decode failed for tlv\n");
3148 return MPLS_DEC_TLVERROR;
3151 tempBuf += decodedSize;
3152 totalSize += decodedSize;
3153 totalSizeParam += decodedSize;
3155 switch (tlvTemp.flags.flags.tBit) {
3156 case MPLS_FEC_TLVTYPE:
3158 decodedSize = Mpls_decodeLdpFecTlv(&(lblMapMsg->fecTlv),
3159 tempBuf, bufSize - totalSize, tlvTemp.length);
3160 if (decodedSize < 0) {
3161 PRINT_ERR("Failure when decoding FEC tlv from LblMap msg\n");
3162 return MPLS_DEC_FECERROR;
3164 PRINT_OUT("Decoded for FEC %d bytes\n", decodedSize);
3165 tempBuf += decodedSize;
3166 totalSize += decodedSize;
3167 totalSizeParam += decodedSize;
3169 lblMapMsg->fecTlvExists = 1;
3170 lblMapMsg->fecTlv.baseTlv = tlvTemp;
3171 break;
3173 case MPLS_GENLBL_TLVTYPE:
3175 decodedSize = Mpls_decodeLdpGenLblTlv(&(lblMapMsg->genLblTlv),
3176 tempBuf, bufSize - totalSize);
3177 if (decodedSize < 0) {
3178 PRINT_ERR("Failure when dec GEN Lbl tlv from LblMap msg\n");
3179 return MPLS_DEC_GENLBLERROR;
3181 PRINT_OUT("Decoded for Gen Lbl %d bytes\n", decodedSize);
3182 tempBuf += decodedSize;
3183 totalSize += decodedSize;
3184 totalSizeParam += decodedSize;
3186 lblMapMsg->genLblTlvExists = 1;
3187 lblMapMsg->genLblTlv.baseTlv = tlvTemp;
3188 break;
3190 case MPLS_ATMLBL_TLVTYPE:
3192 decodedSize = Mpls_decodeLdpAtmLblTlv(&(lblMapMsg->atmLblTlv),
3193 tempBuf, bufSize - totalSize);
3194 if (decodedSize < 0) {
3195 PRINT_ERR("Failure when dec ATM Lbl tlv from LblMap msg\n");
3196 return MPLS_DEC_MAPATMERROR;
3198 PRINT_OUT("Decoded for Atm Lbl %d bytes\n", decodedSize);
3199 tempBuf += decodedSize;
3200 totalSize += decodedSize;
3201 totalSizeParam += decodedSize;
3203 lblMapMsg->atmLblTlvExists = 1;
3204 lblMapMsg->atmLblTlv.baseTlv = tlvTemp;
3205 break;
3207 case MPLS_FRLBL_TLVTYPE:
3209 decodedSize = Mpls_decodeLdpFrLblTlv(&(lblMapMsg->frLblTlv),
3210 tempBuf, bufSize - totalSize);
3211 if (decodedSize < 0) {
3212 PRINT_ERR("Failure when dec FR Lbl tlv from LblMap msg\n");
3213 return MPLS_DEC_FRLBLERROR;
3215 PRINT_OUT("Decoded for Fr Lbl %d bytes\n", decodedSize);
3216 tempBuf += decodedSize;
3217 totalSize += decodedSize;
3218 totalSizeParam += decodedSize;
3220 lblMapMsg->frLblTlvExists = 1;
3221 lblMapMsg->frLblTlv.baseTlv = tlvTemp;
3222 break;
3224 case MPLS_HOPCOUNT_TLVTYPE:
3226 decodedSize = Mpls_decodeLdpHopTlv(&(lblMapMsg->hopCountTlv),
3227 tempBuf, bufSize - totalSize);
3228 if (decodedSize < 0) {
3229 PRINT_ERR("Failure when dec HopCount tlv from LblMap msg\n");
3230 return MPLS_DEC_HOPCOUNTERROR;
3232 PRINT_OUT("Decoded for HopCount %d bytes\n", decodedSize);
3233 tempBuf += decodedSize;
3234 totalSize += decodedSize;
3235 totalSizeParam += decodedSize;
3237 lblMapMsg->hopCountTlvExists = 1;
3238 lblMapMsg->hopCountTlv.baseTlv = tlvTemp;
3239 break;
3241 case MPLS_PATH_TLVTYPE:
3243 decodedSize = Mpls_decodeLdpPathVectorTlv(&(lblMapMsg->pathVecTlv),
3244 tempBuf, bufSize - totalSize, tlvTemp.length);
3245 if (decodedSize < 0) {
3246 PRINT_ERR("Failure when dec Path Vec tlv from LblMap msg\n");
3247 return MPLS_DEC_PATHVECERROR;
3249 PRINT_OUT("Decoded for PATH VECTOR %d bytes\n", decodedSize);
3250 tempBuf += decodedSize;
3251 totalSize += decodedSize;
3252 totalSizeParam += decodedSize;
3254 lblMapMsg->pathVecTlvExists = 1;
3255 lblMapMsg->pathVecTlv.baseTlv = tlvTemp;
3256 break;
3258 case MPLS_REQMSGID_TLVTYPE:
3260 decodedSize = Mpls_decodeLdpLblMsgIdTlv(&(lblMapMsg->lblMsgIdTlv),
3261 tempBuf, bufSize - totalSize);
3262 if (decodedSize < 0) {
3263 PRINT_ERR("Failure when dec LblMsgId tlv from LblMap msg\n");
3264 return MPLS_DEC_LBLMSGIDERROR;
3266 PRINT_OUT("Decoded for LblMsgId %d bytes\n", decodedSize);
3267 tempBuf += decodedSize;
3268 totalSize += decodedSize;
3269 totalSizeParam += decodedSize;
3271 lblMapMsg->lblMsgIdTlvExists = 1;
3272 lblMapMsg->lblMsgIdTlv.baseTlv = tlvTemp;
3273 break;
3275 case MPLS_TRAFFIC_TLVTYPE:
3277 decodedSize = Mpls_decodeLdpTrafficTlv(&(lblMapMsg->trafficTlv),
3278 tempBuf, bufSize - totalSize, tlvTemp.length);
3279 if (decodedSize < 0) {
3280 PRINT_ERR("Failure when dec Traffic tlv from LblMap msg\n");
3281 return MPLS_DEC_TRAFFICERROR;
3283 PRINT_OUT("Decoded for Traffic %d bytes\n", decodedSize);
3284 tempBuf += decodedSize;
3285 totalSize += decodedSize;
3286 totalSizeParam += decodedSize;
3288 lblMapMsg->trafficTlvExists = 1;
3289 lblMapMsg->trafficTlv.baseTlv = tlvTemp;
3290 break;
3292 case MPLS_LSPID_TLVTYPE:
3294 decodedSize = Mpls_decodeLdpLspIdTlv(&(lblMapMsg->lspidTlv),
3295 tempBuf, bufSize - totalSize);
3296 if (decodedSize < 0) {
3297 PRINT_ERR("Failure when dec LSPID tlv from LblMap msg\n");
3298 return MPLS_DEC_LSPIDERROR;
3300 PRINT_OUT("Decoded for lspid tlv %d bytes\n", decodedSize);
3301 tempBuf += decodedSize;
3302 totalSize += decodedSize;
3303 totalSizeParam += decodedSize;
3305 lblMapMsg->lspidTlvExists = 1;
3306 lblMapMsg->lspidTlv.baseTlv = tlvTemp;
3307 break;
3309 default:
3311 PRINT_ERR("Found wrong tlv type while decoding lbl map msg (%x)\n",
3312 tlvTemp.flags.flags.tBit);
3313 if (tlvTemp.flags.flags.uBit == 1) {
3314 /* ignore the Tlv and continue processing */
3315 tempBuf += tlvTemp.length;
3316 totalSize += tlvTemp.length;
3317 totalSizeParam += tlvTemp.length;
3318 break;
3319 } else {
3320 /* drop the message; return error */
3321 return MPLS_TLVTYPEERROR;
3324 } /* switch type */
3326 } /* while */
3328 PRINT_OUT("totalsize for Mpls_decodeLdpLblMapMsg is %d\n", totalSize);
3330 return totalSize;
3332 } /* End: Mpls_decodeLdpLblMapMsg */
3335 * Encode for Retrun MessageId TLV
3339 * encode
3341 int Mpls_encodeLdpLblRetMsgIdTlv
3342 (mplsLdpLblRetMsgIdTlv_t * lblMsgIdTlv, u_char * buff, int bufSize) {
3344 * encode for tlv
3346 if (Mpls_encodeLdpTlv(&(lblMsgIdTlv->baseTlv), buff, MPLS_TLVFIXLEN) < 0) {
3347 return MPLS_ENC_TLVERROR;
3350 return MPLS_TLVFIXLEN;
3352 } /* End: Mpls_encodeLdpLblRetMsgIdTlv */
3355 * decode
3357 int Mpls_decodeLdpLblRetMsgIdTlv
3358 (mplsLdpLblRetMsgIdTlv_t * lblMsgIdTlv, u_char * buff, int bufSize) {
3359 /* this function does not need to do anything */
3360 return 0;
3361 } /* End: Mpls_decodeLdpLblRetMsgIdTlv */
3364 * Encode for Label Request Message
3368 * encode
3370 int Mpls_encodeLdpLblReqMsg
3371 (mplsLdpLblReqMsg_t * lblReqMsg, u_char * buff, int bufSize) {
3372 mplsLdpLblReqMsg_t lblReqMsgCopy;
3373 int encodedSize;
3374 u_int totalSize = 0;
3375 u_char *tempBuf = buff; /* no change for the buff ptr */
3377 /* check the length of the messageId + param */
3378 if ((int)(lblReqMsg->baseMsg.msgLength) + MPLS_TLVFIXLEN > bufSize) {
3379 PRINT_ERR("failed to encode the lbl request msg: BUFFER TOO SMALL\n");
3380 return MPLS_ENC_BUFFTOOSMALL;
3383 lblReqMsgCopy = *lblReqMsg;
3386 * encode the base part of the pdu message
3388 encodedSize = Mpls_encodeLdpBaseMsg(&(lblReqMsgCopy.baseMsg),
3389 tempBuf, bufSize);
3390 if (encodedSize < 0) {
3391 return MPLS_ENC_BASEMSGERROR;
3393 PRINT_OUT("Encode BaseMsg for label request on %d bytes\n", encodedSize);
3394 tempBuf += encodedSize;
3395 totalSize += encodedSize;
3398 * encode the tlv if any
3400 if (lblReqMsgCopy.fecTlvExists) {
3401 encodedSize = Mpls_encodeLdpFecTlv(&(lblReqMsgCopy.fecTlv),
3402 tempBuf, bufSize - totalSize);
3403 if (encodedSize < 0) {
3404 return MPLS_ENC_FECERROR;
3406 PRINT_OUT("Encoded for FEC Tlv %d bytes\n", encodedSize);
3407 tempBuf += encodedSize;
3408 totalSize += encodedSize;
3410 if (lblReqMsgCopy.hopCountTlvExists) {
3411 encodedSize = Mpls_encodeLdpHopTlv(&(lblReqMsgCopy.hopCountTlv),
3412 tempBuf, bufSize - totalSize);
3413 if (encodedSize < 0) {
3414 return MPLS_ENC_HOPCOUNTERROR;
3416 PRINT_OUT("Encoded for Hop Count Tlv %d bytes\n", encodedSize);
3417 tempBuf += encodedSize;
3418 totalSize += encodedSize;
3420 if (lblReqMsgCopy.pathVecTlvExists) {
3421 encodedSize = Mpls_encodeLdpPathVectorTlv(&(lblReqMsgCopy.pathVecTlv),
3422 tempBuf, bufSize - totalSize);
3423 if (encodedSize < 0) {
3424 return MPLS_ENC_PATHVECERROR;
3426 PRINT_OUT("Encoded for Hop Count Tlv %d bytes\n", encodedSize);
3427 tempBuf += encodedSize;
3428 totalSize += encodedSize;
3430 if (lblReqMsgCopy.lblMsgIdTlvExists) {
3431 encodedSize = Mpls_encodeLdpLblRetMsgIdTlv(&(lblReqMsgCopy.lblMsgIdTlv),
3432 tempBuf, bufSize - totalSize);
3433 if (encodedSize < 0) {
3434 return MPLS_ENC_LBLMSGIDERROR;
3436 PRINT_OUT("Encoded for Hop Count Tlv %d bytes\n", encodedSize);
3437 tempBuf += encodedSize;
3438 totalSize += encodedSize;
3440 if (lblReqMsgCopy.erTlvExists) {
3441 encodedSize = Mpls_encodeLdpERTlv(&(lblReqMsgCopy.erTlv),
3442 tempBuf, bufSize - totalSize);
3443 if (encodedSize < 0) {
3444 return MPLS_ENC_ERTLVERROR;
3446 PRINT_OUT("Encoded for CR Tlv %d bytes\n", encodedSize);
3447 tempBuf += encodedSize;
3448 totalSize += encodedSize;
3450 if (lblReqMsgCopy.trafficTlvExists) {
3451 encodedSize = Mpls_encodeLdpTrafficTlv(&(lblReqMsgCopy.trafficTlv),
3452 tempBuf, bufSize - totalSize);
3453 if (encodedSize < 0) {
3454 return MPLS_ENC_TRAFFICERROR;
3456 PRINT_OUT("Encoded for Traffic Tlv %d bytes\n", encodedSize);
3457 tempBuf += encodedSize;
3458 totalSize += encodedSize;
3460 if (lblReqMsgCopy.lspidTlvExists) {
3461 encodedSize = Mpls_encodeLdpLspIdTlv(&(lblReqMsgCopy.lspidTlv),
3462 tempBuf, bufSize - totalSize);
3463 if (encodedSize < 0) {
3464 return MPLS_ENC_LSPIDERROR;
3466 PRINT_OUT("Encoded for LSPID Tlv %d bytes\n", encodedSize);
3467 tempBuf += encodedSize;
3468 totalSize += encodedSize;
3470 if (lblReqMsgCopy.pinningTlvExists) {
3471 encodedSize = Mpls_encodeLdpPinningTlv(&(lblReqMsgCopy.pinningTlv),
3472 tempBuf, bufSize - totalSize);
3473 if (encodedSize < 0) {
3474 return MPLS_ENC_PINNINGERROR;
3476 PRINT_OUT("Encoded for Pinning Tlv %d bytes\n", encodedSize);
3477 tempBuf += encodedSize;
3478 totalSize += encodedSize;
3480 if (lblReqMsgCopy.recClassTlvExists) {
3481 encodedSize = Mpls_encodeLdpResClsTlv(&(lblReqMsgCopy.resClassTlv),
3482 tempBuf, bufSize - totalSize);
3483 if (encodedSize < 0) {
3484 return MPLS_ENC_RESCLSERROR;
3486 PRINT_OUT("Encoded for Resource class Tlv %d bytes\n", encodedSize);
3487 tempBuf += encodedSize;
3488 totalSize += encodedSize;
3490 if (lblReqMsgCopy.preemptTlvExists) {
3491 encodedSize = Mpls_encodeLdpPreemptTlv(&(lblReqMsgCopy.preemptTlv),
3492 tempBuf, bufSize - totalSize);
3493 if (encodedSize < 0) {
3494 return MPLS_ENC_PREEMPTERROR;
3496 PRINT_OUT("Encoded for Preempt Tlv %d bytes\n", encodedSize);
3497 tempBuf += encodedSize;
3498 totalSize += encodedSize;
3501 return totalSize;
3503 } /* End: Mpls_encodeLdpLblReqMsg */
3506 * decode
3508 int Mpls_decodeLdpLblReqMsg
3509 (mplsLdpLblReqMsg_t * lblReqMsg, u_char * buff, int bufSize) {
3510 int decodedSize = 0;
3511 u_int totalSize = 0;
3512 u_int stopLength = 0;
3513 u_int totalSizeParam = 0;
3514 u_char *tempBuf = buff; /* no change for the buff ptr */
3515 mplsLdpTlv_t tlvTemp;
3518 * decode the base part of the pdu message
3520 memset(lblReqMsg, 0, sizeof(mplsLdpLblReqMsg_t));
3521 decodedSize = Mpls_decodeLdpBaseMsg(&(lblReqMsg->baseMsg), tempBuf, bufSize);
3522 if (decodedSize < 0) {
3523 return MPLS_DEC_BASEMSGERROR;
3525 PRINT_OUT("Decode BaseMsg for Lbl Request on %d bytes\n", decodedSize);
3527 if (lblReqMsg->baseMsg.flags.flags.msgType != MPLS_LBLREQ_MSGTYPE) {
3528 PRINT_ERR("Not the right message type; expected lbl req and got %x\n",
3529 lblReqMsg->baseMsg.flags.flags.msgType);
3530 return MPLS_MSGTYPEERROR;
3533 tempBuf += decodedSize;
3534 totalSize += decodedSize;
3536 if (bufSize - totalSize <= 0) {
3537 /* nothing left for decoding */
3538 PRINT_ERR("Lbl Request msg does not have anything beside base msg\n");
3539 return totalSize;
3542 PRINT_OUT
3543 ("bufSize = %d, totalSize = %d, lblReqMsg->baseMsg.msgLength = %d\n",
3544 bufSize, totalSize, lblReqMsg->baseMsg.msgLength);
3546 /* Have to check the baseMsg.msgLength to know when to finish.
3547 * We finsh when the totalSizeParam is >= to the base message length - the
3548 * message id length (4)
3551 stopLength = lblReqMsg->baseMsg.msgLength - MPLS_MSGIDFIXLEN;
3552 while (stopLength > totalSizeParam) {
3554 * decode the tlv to check what's next
3556 memset(&tlvTemp, 0, MPLS_TLVFIXLEN);
3557 decodedSize = Mpls_decodeLdpTlv(&tlvTemp, tempBuf, bufSize - totalSize);
3558 if (decodedSize < 0) {
3559 /* something wrong */
3560 PRINT_ERR("Label Request msg decode failed for tlv\n");
3561 return MPLS_DEC_TLVERROR;
3564 tempBuf += decodedSize;
3565 totalSize += decodedSize;
3566 totalSizeParam += decodedSize;
3568 switch (tlvTemp.flags.flags.tBit) {
3569 case MPLS_FEC_TLVTYPE:
3571 decodedSize = Mpls_decodeLdpFecTlv(&(lblReqMsg->fecTlv),
3572 tempBuf, bufSize - totalSize, tlvTemp.length);
3573 if (decodedSize < 0) {
3574 PRINT_ERR("Failure when decoding FEC tlv from LblReq msg\n");
3575 return MPLS_DEC_FECERROR;
3577 PRINT_OUT("Decoded for FEC %d bytes\n", decodedSize);
3578 tempBuf += decodedSize;
3579 totalSize += decodedSize;
3580 totalSizeParam += decodedSize;
3582 lblReqMsg->fecTlvExists = 1;
3583 lblReqMsg->fecTlv.baseTlv = tlvTemp;
3584 break;
3586 case MPLS_HOPCOUNT_TLVTYPE:
3588 decodedSize = Mpls_decodeLdpHopTlv(&(lblReqMsg->hopCountTlv),
3589 tempBuf, bufSize - totalSize);
3590 if (decodedSize < 0) {
3591 PRINT_ERR("Failure when dec HopCount tlv from LblReq msg\n");
3592 return MPLS_DEC_HOPCOUNTERROR;
3594 PRINT_OUT("Decoded for HopCount %d bytes\n", decodedSize);
3595 tempBuf += decodedSize;
3596 totalSize += decodedSize;
3597 totalSizeParam += decodedSize;
3599 lblReqMsg->hopCountTlvExists = 1;
3600 lblReqMsg->hopCountTlv.baseTlv = tlvTemp;
3601 break;
3603 case MPLS_PATH_TLVTYPE:
3605 decodedSize = Mpls_decodeLdpPathVectorTlv(&(lblReqMsg->pathVecTlv),
3606 tempBuf, bufSize - totalSize, tlvTemp.length);
3607 if (decodedSize < 0) {
3608 PRINT_ERR("Failure when dec Path Vec tlv from LblReq msg\n");
3609 return MPLS_DEC_PATHVECERROR;
3611 PRINT_OUT("Decoded for PATH VECTOR %d bytes\n", decodedSize);
3612 tempBuf += decodedSize;
3613 totalSize += decodedSize;
3614 totalSizeParam += decodedSize;
3616 lblReqMsg->pathVecTlvExists = 1;
3617 lblReqMsg->pathVecTlv.baseTlv = tlvTemp;
3618 break;
3620 case MPLS_LBLMSGID_TLVTYPE:
3622 lblReqMsg->lblMsgIdTlvExists = 1;
3623 lblReqMsg->lblMsgIdTlv.baseTlv = tlvTemp;
3624 break;
3626 case MPLS_ER_TLVTYPE:
3628 decodedSize = Mpls_decodeLdpERTlv(&(lblReqMsg->erTlv),
3629 tempBuf, bufSize - totalSize, tlvTemp.length);
3630 if (decodedSize < 0) {
3631 PRINT_ERR("Failure when dec CR tlv from LblReq msg\n");
3632 return MPLS_DEC_ERTLVERROR;
3634 PRINT_OUT("Decoded for CR %d bytes\n", decodedSize);
3635 tempBuf += decodedSize;
3636 totalSize += decodedSize;
3637 totalSizeParam += decodedSize;
3639 lblReqMsg->erTlvExists = 1;
3640 lblReqMsg->erTlv.baseTlv = tlvTemp;
3641 break;
3643 case MPLS_TRAFFIC_TLVTYPE:
3645 decodedSize = Mpls_decodeLdpTrafficTlv(&(lblReqMsg->trafficTlv),
3646 tempBuf, bufSize - totalSize, tlvTemp.length);
3647 if (decodedSize < 0) {
3648 PRINT_ERR("Failure when dec Traffic tlv from LblReq msg\n");
3649 return MPLS_DEC_TRAFFICERROR;
3651 PRINT_OUT("Decoded for Traffic %d bytes\n", decodedSize);
3652 tempBuf += decodedSize;
3653 totalSize += decodedSize;
3654 totalSizeParam += decodedSize;
3656 lblReqMsg->trafficTlvExists = 1;
3657 lblReqMsg->trafficTlv.baseTlv = tlvTemp;
3658 break;
3660 case MPLS_LSPID_TLVTYPE:
3662 decodedSize = Mpls_decodeLdpLspIdTlv(&(lblReqMsg->lspidTlv),
3663 tempBuf, bufSize - totalSize);
3664 if (decodedSize < 0) {
3665 PRINT_ERR("Failure when dec LSPID tlv from LblReq msg\n");
3666 return MPLS_DEC_LSPIDERROR;
3668 PRINT_OUT("Decoded for lspid tlv %d bytes\n", decodedSize);
3669 tempBuf += decodedSize;
3670 totalSize += decodedSize;
3671 totalSizeParam += decodedSize;
3673 lblReqMsg->lspidTlvExists = 1;
3674 lblReqMsg->lspidTlv.baseTlv = tlvTemp;
3675 break;
3677 case MPLS_PINNING_TLVTYPE:
3679 decodedSize = Mpls_decodeLdpPinningTlv(&(lblReqMsg->pinningTlv),
3680 tempBuf, bufSize - totalSize);
3681 if (decodedSize < 0) {
3682 PRINT_ERR("Failure when dec Pinning tlv from LblReq msg\n");
3683 return MPLS_DEC_PINNINGERROR;
3685 PRINT_OUT("Decoded for pining tlv %d bytes\n", decodedSize);
3686 tempBuf += decodedSize;
3687 totalSize += decodedSize;
3688 totalSizeParam += decodedSize;
3690 lblReqMsg->pinningTlvExists = 1;
3691 lblReqMsg->pinningTlv.baseTlv = tlvTemp;
3692 break;
3694 case MPLS_RESCLASS_TLVTYPE:
3696 decodedSize = Mpls_decodeLdpResClsTlv(&(lblReqMsg->resClassTlv),
3697 tempBuf, bufSize - totalSize);
3698 if (decodedSize < 0) {
3699 PRINT_ERR("Failure when dec ResClass tlv from LblReq msg\n");
3700 return MPLS_DEC_RESCLSERROR;
3702 PRINT_OUT("Decoded for %d bytes\n", decodedSize);
3703 tempBuf += decodedSize;
3704 totalSize += decodedSize;
3705 totalSizeParam += decodedSize;
3707 lblReqMsg->recClassTlvExists = 1;
3708 lblReqMsg->resClassTlv.baseTlv = tlvTemp;
3709 break;
3711 case MPLS_PREEMPT_TLVTYPE:
3713 decodedSize = Mpls_decodeLdpPreemptTlv(&(lblReqMsg->preemptTlv),
3714 tempBuf, bufSize - totalSize);
3715 if (decodedSize < 0) {
3716 PRINT_ERR("Failure when dec preempt tlv from LblReq msg\n");
3717 return MPLS_DEC_PREEMPTERROR;
3719 PRINT_OUT("Decoded for preempt tlv %d bytes\n", decodedSize);
3720 tempBuf += decodedSize;
3721 totalSize += decodedSize;
3722 totalSizeParam += decodedSize;
3724 lblReqMsg->preemptTlvExists = 1;
3725 lblReqMsg->preemptTlv.baseTlv = tlvTemp;
3726 break;
3728 default:
3730 PRINT_ERR("Found wrong type while decoding lbl req msg (%x)\n",
3731 tlvTemp.flags.flags.tBit);
3732 if (tlvTemp.flags.flags.uBit == 1) {
3733 /* ignore the Tlv and continue processing */
3734 tempBuf += tlvTemp.length;
3735 totalSize += tlvTemp.length;
3736 totalSizeParam += tlvTemp.length;
3737 break;
3738 } else {
3739 /* drop the message; return error */
3740 return MPLS_TLVTYPEERROR;
3743 } /* switch type */
3745 } /* while */
3747 PRINT_OUT("totalsize for Mpls_decodeLdpLblReqMsg is %d\n", totalSize);
3748 return totalSize;
3750 } /*End: Mpls_decodeLdpLblReqMsg */
3753 * Encode for Label Withdraw and Label Release Message
3757 * encode
3759 int Mpls_encodeLdpLbl_W_R_Msg
3760 (mplsLdpLbl_W_R_Msg_t * lbl_W_R_Msg, u_char * buff, int bufSize) {
3761 mplsLdpLbl_W_R_Msg_t lbl_W_R_MsgCopy;
3762 int encodedSize;
3763 u_int totalSize = 0;
3764 u_char *tempBuf = buff; /* no change for the buff ptr */
3766 /* check the length of the messageId + param */
3767 if ((int)(lbl_W_R_Msg->baseMsg.msgLength) + MPLS_TLVFIXLEN > bufSize) {
3768 PRINT_ERR("failed to encode the lbl mapping msg: BUFFER TOO SMALL\n");
3769 return MPLS_ENC_BUFFTOOSMALL;
3772 lbl_W_R_MsgCopy = *lbl_W_R_Msg;
3775 * encode the base part of the pdu message
3777 encodedSize = Mpls_encodeLdpBaseMsg(&(lbl_W_R_MsgCopy.baseMsg),
3778 tempBuf, bufSize);
3779 if (encodedSize < 0) {
3780 return MPLS_ENC_BASEMSGERROR;
3782 PRINT_OUT("Encode BaseMsg for label withdraw on %d bytes\n", encodedSize);
3783 tempBuf += encodedSize;
3784 totalSize += encodedSize;
3787 * encode the tlv if any
3789 if (lbl_W_R_MsgCopy.fecTlvExists) {
3790 encodedSize = Mpls_encodeLdpFecTlv(&(lbl_W_R_MsgCopy.fecTlv),
3791 tempBuf, bufSize - totalSize);
3792 if (encodedSize < 0) {
3793 return MPLS_ENC_FECERROR;
3795 PRINT_OUT("Encoded for FEC Tlv %d bytes\n", encodedSize);
3796 tempBuf += encodedSize;
3797 totalSize += encodedSize;
3800 if (lbl_W_R_MsgCopy.genLblTlvExists) {
3801 encodedSize = Mpls_encodeLdpGenLblTlv(&(lbl_W_R_MsgCopy.genLblTlv),
3802 tempBuf, bufSize - totalSize);
3803 if (encodedSize < 0) {
3804 return MPLS_ENC_GENLBLERROR;
3806 PRINT_OUT("Encoded for Generic Label Tlv %d bytes\n", encodedSize);
3807 tempBuf += encodedSize;
3808 totalSize += encodedSize;
3810 if (lbl_W_R_MsgCopy.atmLblTlvExists) {
3811 encodedSize = Mpls_encodeLdpAtmLblTlv(&(lbl_W_R_MsgCopy.atmLblTlv),
3812 tempBuf, bufSize - totalSize);
3813 if (encodedSize < 0) {
3814 return MPLS_ENC_MAPATMERROR;
3816 PRINT_OUT("Encoded for Atm Label Tlv %d bytes\n", encodedSize);
3817 tempBuf += encodedSize;
3818 totalSize += encodedSize;
3820 if (lbl_W_R_MsgCopy.frLblTlvExists) {
3821 encodedSize = Mpls_encodeLdpFrLblTlv(&(lbl_W_R_MsgCopy.frLblTlv),
3822 tempBuf, bufSize - totalSize);
3823 if (encodedSize < 0) {
3824 return MPLS_ENC_FRLBLERROR;
3826 PRINT_OUT("Encoded for Fr Label Tlv %d bytes\n", encodedSize);
3827 tempBuf += encodedSize;
3828 totalSize += encodedSize;
3830 if (lbl_W_R_MsgCopy.lspidTlvExists) {
3831 encodedSize = Mpls_encodeLdpLspIdTlv(&(lbl_W_R_MsgCopy.lspidTlv),
3832 tempBuf, bufSize - totalSize);
3833 if (encodedSize < 0) {
3834 return MPLS_ENC_LSPIDERROR;
3836 PRINT_OUT("Encoded for LSPID Tlv %d bytes\n", encodedSize);
3837 tempBuf += encodedSize;
3838 totalSize += encodedSize;
3841 return totalSize;
3843 } /* End: Mpls_encodeLdpLbl_W_R_Msg */
3846 * decode
3848 int Mpls_decodeLdpLbl_W_R_Msg
3849 (mplsLdpLbl_W_R_Msg_t * lbl_W_R_Msg, u_char * buff, int bufSize) {
3850 int decodedSize;
3851 u_int totalSize = 0;
3852 u_int stopLength = 0;
3853 u_int totalSizeParam = 0;
3854 u_char *tempBuf = buff; /* no change for the buff ptr */
3855 mplsLdpTlv_t tlvTemp;
3858 * decode the base part of the pdu message
3860 memset(lbl_W_R_Msg, 0, sizeof(mplsLdpLbl_W_R_Msg_t));
3861 decodedSize = Mpls_decodeLdpBaseMsg(&(lbl_W_R_Msg->baseMsg),
3862 tempBuf, bufSize);
3863 if (decodedSize < 0) {
3864 return MPLS_DEC_BASEMSGERROR;
3866 PRINT_OUT("Decode BaseMsg for Lbl Withdraw on %d bytes\n", decodedSize);
3868 if ((lbl_W_R_Msg->baseMsg.flags.flags.msgType != MPLS_LBLWITH_MSGTYPE) &&
3869 (lbl_W_R_Msg->baseMsg.flags.flags.msgType != MPLS_LBLREL_MSGTYPE)) {
3870 PRINT_ERR("Not the right message type; expected lbl W_R and got %x\n",
3871 lbl_W_R_Msg->baseMsg.flags.flags.msgType);
3872 return MPLS_MSGTYPEERROR;
3875 tempBuf += decodedSize;
3876 totalSize += decodedSize;
3878 if (bufSize - totalSize <= 0) {
3879 /* nothing left for decoding */
3880 PRINT_ERR("Lbl Withdraw msg does not have anything beside base msg\n");
3881 return totalSize;
3884 PRINT_OUT
3885 ("bufSize = %d, totalSize = %d, lbl_W_R_Msg->baseMsg.msgLength = %d\n",
3886 bufSize, totalSize, lbl_W_R_Msg->baseMsg.msgLength);
3888 /* Have to check the baseMsg.msgLength to know when to finish.
3889 * We finsh when the totalSizeParam is >= to the base message length - the
3890 * message id length (4)
3893 stopLength = lbl_W_R_Msg->baseMsg.msgLength - MPLS_MSGIDFIXLEN;
3894 while (stopLength > totalSizeParam) {
3896 * decode the tlv to check what's next
3898 memset(&tlvTemp, 0, MPLS_TLVFIXLEN);
3899 decodedSize = Mpls_decodeLdpTlv(&tlvTemp, tempBuf, bufSize - totalSize);
3900 if (decodedSize < 0) {
3901 /* something wrong */
3902 PRINT_ERR("Label Mapping msg decode failed for tlv\n");
3903 return MPLS_DEC_TLVERROR;
3906 tempBuf += decodedSize;
3907 totalSize += decodedSize;
3908 totalSizeParam += decodedSize;
3910 switch (tlvTemp.flags.flags.tBit) {
3911 case MPLS_FEC_TLVTYPE:
3913 decodedSize = Mpls_decodeLdpFecTlv(&(lbl_W_R_Msg->fecTlv),
3914 tempBuf, bufSize - totalSize, tlvTemp.length);
3915 if (decodedSize < 0) {
3916 PRINT_ERR("Failure when decoding FEC tlv from LblWithdr msg\n");
3917 return MPLS_DEC_FECERROR;
3919 PRINT_OUT("Decoded for FEC %d bytes\n", decodedSize);
3920 tempBuf += decodedSize;
3921 totalSize += decodedSize;
3922 totalSizeParam += decodedSize;
3924 lbl_W_R_Msg->fecTlvExists = 1;
3925 lbl_W_R_Msg->fecTlv.baseTlv = tlvTemp;
3926 break;
3928 case MPLS_GENLBL_TLVTYPE:
3930 decodedSize = Mpls_decodeLdpGenLblTlv(&(lbl_W_R_Msg->genLblTlv),
3931 tempBuf, bufSize - totalSize);
3932 if (decodedSize < 0) {
3933 PRINT_ERR("Failure when dec GEN Lbl tlv from LblWithdr msg\n");
3934 return MPLS_DEC_GENLBLERROR;
3936 PRINT_OUT("Decoded for Gen Lbl %d bytes\n", decodedSize);
3937 tempBuf += decodedSize;
3938 totalSize += decodedSize;
3939 totalSizeParam += decodedSize;
3941 lbl_W_R_Msg->genLblTlvExists = 1;
3942 lbl_W_R_Msg->genLblTlv.baseTlv = tlvTemp;
3943 break;
3945 case MPLS_ATMLBL_TLVTYPE:
3947 decodedSize = Mpls_decodeLdpAtmLblTlv(&(lbl_W_R_Msg->atmLblTlv),
3948 tempBuf, bufSize - totalSize);
3949 if (decodedSize < 0) {
3950 PRINT_ERR("Failure when dec ATM Lbl tlv from LblWithdr msg\n");
3951 return MPLS_DEC_MAPATMERROR;
3953 PRINT_OUT("Decoded for Atm Lbl %d bytes\n", decodedSize);
3954 tempBuf += decodedSize;
3955 totalSize += decodedSize;
3956 totalSizeParam += decodedSize;
3958 lbl_W_R_Msg->atmLblTlvExists = 1;
3959 lbl_W_R_Msg->atmLblTlv.baseTlv = tlvTemp;
3960 break;
3962 case MPLS_FRLBL_TLVTYPE:
3964 decodedSize = Mpls_decodeLdpFrLblTlv(&(lbl_W_R_Msg->frLblTlv),
3965 tempBuf, bufSize - totalSize);
3966 if (decodedSize < 0) {
3967 PRINT_ERR("Failure when dec FR Lbl tlv from LblWithdr msg\n");
3968 return MPLS_DEC_FRLBLERROR;
3970 PRINT_OUT("Decoded for Fr Lbl %d bytes\n", decodedSize);
3971 tempBuf += decodedSize;
3972 totalSize += decodedSize;
3973 totalSizeParam += decodedSize;
3975 lbl_W_R_Msg->frLblTlvExists = 1;
3976 lbl_W_R_Msg->frLblTlv.baseTlv = tlvTemp;
3977 break;
3979 case MPLS_LSPID_TLVTYPE:
3981 decodedSize = Mpls_decodeLdpLspIdTlv(&(lbl_W_R_Msg->lspidTlv),
3982 tempBuf, bufSize - totalSize);
3983 if (decodedSize < 0) {
3984 PRINT_ERR("Failure when dec LSPID tlv from LblW_R msg\n");
3985 return MPLS_DEC_LSPIDERROR;
3987 PRINT_OUT("Decoded for lspid tlv %d bytes\n", decodedSize);
3988 tempBuf += decodedSize;
3989 totalSize += decodedSize;
3990 totalSizeParam += decodedSize;
3992 lbl_W_R_Msg->lspidTlvExists = 1;
3993 lbl_W_R_Msg->lspidTlv.baseTlv = tlvTemp;
3994 break;
3996 default:
3998 PRINT_ERR("Found wrong tlv type while decoding lbl withdr msg (%x)\n",
3999 tlvTemp.flags.flags.tBit);
4000 if (tlvTemp.flags.flags.uBit == 1) {
4001 /* ignore the Tlv and continue processing */
4002 tempBuf += tlvTemp.length;
4003 totalSize += tlvTemp.length;
4004 totalSizeParam += tlvTemp.length;
4005 break;
4006 } else {
4007 /* drop the message; return error */
4008 return MPLS_TLVTYPEERROR;
4011 } /* switch type */
4013 } /* while */
4015 PRINT_OUT("totalsize for Mpls_decodeLdpLblWithdrawMsgIdTlv is %d\n",
4016 totalSize);
4018 return totalSize;
4020 } /* End: Mpls_decodeLdpLbl_W_R_Msg */
4023 * Encode for CR Tlv
4027 * encode
4029 int Mpls_encodeLdpERTlv(mplsLdpErTlv_t * erTlv, u_char * buff, int bufSize)
4031 int encodedSize = 0;
4032 u_int totalSize = 0;
4033 u_char *tempBuf = buff; /* no change for the buff ptr */
4034 u_int i;
4036 if (MPLS_TLVFIXLEN + (int)(erTlv->baseTlv.length) > bufSize) {
4037 /* not enough room */
4038 return MPLS_ENC_BUFFTOOSMALL;
4042 * encode for tlv
4044 encodedSize = Mpls_encodeLdpTlv(&(erTlv->baseTlv), tempBuf, MPLS_TLVFIXLEN);
4045 if (encodedSize < 0) {
4046 return MPLS_ENC_TLVERROR;
4048 tempBuf += encodedSize;
4049 totalSize += encodedSize;
4051 if (erTlv->numberErHops > MPLS_MAX_ER_HOPS) {
4052 PRINT_ERR("MPLS_MAX_ER_HOPS is too small. Increase it if nec\n");
4053 return MPLS_ER_HOPSNUMERROR;
4057 * encode for ER hops
4059 for (i = 0; i < erTlv->numberErHops; i++) {
4060 encodedSize = Mpls_encodeLdpErHop(&(erTlv->erHopArray[i]),
4061 tempBuf, bufSize - totalSize, erTlv->erHopTypes[i]);
4062 if (encodedSize < 0) {
4063 return MPLS_ENC_ERHOPERROR;
4065 tempBuf += encodedSize;
4066 totalSize += encodedSize;
4069 return totalSize;
4071 } /* End: Mpls_encodeLdpERTlv */
4074 * decode
4076 int Mpls_decodeLdpERTlv
4077 (mplsLdpErTlv_t * erTlv, u_char * buff, int bufSize, u_short tlvLength) {
4078 u_char *tempBuf = buff; /* no change for the buff ptr */
4079 u_char *erTlvPtr;
4080 u_int i = 0;
4081 int decodedSize = 0;
4082 u_int erHopSize = 0; /* used to compute the sum of
4084 all er hop elements + flags */
4085 u_short type; /* filled in by Mpls_decodeLdpErHop
4087 with the type of the ER hop
4088 decoded */
4090 if ((int)tlvLength > bufSize) {
4091 /* not enough data for Fec elements tlv */
4092 PRINT_ERR("failed decoding CR tlv \n");
4093 return MPLS_DEC_BUFFTOOSMALL;
4096 erTlvPtr = (u_char *) erTlv;
4097 erTlvPtr += MPLS_TLVFIXLEN; /* we want to point to the flags since the
4098 tlv was decoded before we reach here */
4100 while (tlvLength > erHopSize) {
4101 if (erTlv->numberErHops > (u_short) (MPLS_MAX_ER_HOPS - 1)) {
4102 PRINT_ERR("MPLS_MAX_ER_HOPS is too small. Increase it if nec\n");
4103 return MPLS_ER_HOPSNUMERROR;
4106 decodedSize = Mpls_decodeLdpErHop(&(erTlv->erHopArray[i]),
4107 tempBuf, bufSize - erHopSize, &type);
4108 if (decodedSize < 0) {
4109 return MPLS_DEC_ERHOPERROR;
4112 erTlv->erHopTypes[i] = type;
4113 erTlv->numberErHops++;
4114 i++;
4116 tempBuf += decodedSize;
4117 erHopSize += decodedSize;
4119 } /* end while */
4121 return erHopSize;
4123 } /* End: Mpls_decodeLdpERTlv */
4126 * Encode for ER Hop
4130 * encode
4132 int Mpls_encodeLdpErHop
4133 (mplsLdpErHop_t * erHop, u_char * buff, int bufSize, u_short type) {
4134 int encodedSize = 0;
4135 u_char *tempBuff = buff;
4136 u_char *startPtr;
4138 switch (type) {
4139 case MPLS_ERHOP_IPV4_TLVTYPE:
4141 if (MPLS_ERHOP_IPV4_FIXLEN + MPLS_TLVFIXLEN > bufSize) {
4142 return MPLS_ENC_BUFFTOOSMALL;
4145 /* check how much is the preLen; should be between 0-32 */
4146 if (erHop->erIpv4.flags.flags.preLen > 32) {
4147 return MPLS_IPV4LENGTHERROR;
4150 encodedSize = Mpls_encodeLdpTlv(&(erHop->erIpv4.baseTlv),
4151 tempBuff, bufSize);
4152 if (encodedSize < 0) {
4153 return MPLS_ENC_TLVERROR;
4155 tempBuff += encodedSize;
4156 startPtr = (u_char *) & (erHop->erIpv4);
4157 startPtr += encodedSize;
4159 erHop->erIpv4.flags.flags.res = 0;
4160 erHop->erIpv4.flags.mark = htonl(erHop->erIpv4.flags.mark);
4161 erHop->erIpv4.address = htonl(erHop->erIpv4.address);
4163 MEM_COPY(tempBuff, startPtr, MPLS_ERHOP_IPV4_FIXLEN);
4164 encodedSize += MPLS_ERHOP_IPV4_FIXLEN;
4165 break;
4167 case MPLS_ERHOP_IPV6_TLVTYPE:
4169 if (MPLS_ERHOP_IPV6_FIXLEN + MPLS_TLVFIXLEN > bufSize) {
4170 return MPLS_ENC_BUFFTOOSMALL;
4172 encodedSize = Mpls_encodeLdpTlv(&(erHop->erIpv6.baseTlv),
4173 tempBuff, bufSize);
4174 if (encodedSize < 0) {
4175 return MPLS_ENC_TLVERROR;
4177 tempBuff += encodedSize;
4178 startPtr = (u_char *) & (erHop->erIpv6);
4179 startPtr += encodedSize;
4181 erHop->erIpv6.flags.flags.res = 0;
4182 erHop->erIpv6.flags.mark = htonl(erHop->erIpv6.flags.mark);
4184 MEM_COPY(tempBuff, startPtr, MPLS_ERHOP_IPV6_FIXLEN);
4186 encodedSize += MPLS_ERHOP_IPV6_FIXLEN;
4187 break;
4189 case MPLS_ERHOP_AS_TLVTYPE:
4191 if (MPLS_ERHOP_AS_FIXLEN + MPLS_TLVFIXLEN > bufSize) {
4192 return MPLS_ENC_BUFFTOOSMALL;
4194 encodedSize =
4195 Mpls_encodeLdpTlv(&(erHop->erAs.baseTlv), tempBuff, bufSize);
4196 if (encodedSize < 0) {
4197 return MPLS_ENC_TLVERROR;
4199 tempBuff += encodedSize;
4200 startPtr = (u_char *) & (erHop->erAs);
4201 startPtr += encodedSize;
4203 erHop->erAs.flags.flags.res = 0;
4204 erHop->erAs.flags.mark = htons(erHop->erAs.flags.mark);
4205 erHop->erAs.asNumber = htons(erHop->erAs.asNumber);
4207 MEM_COPY(tempBuff, startPtr, MPLS_ERHOP_AS_FIXLEN);
4209 encodedSize += MPLS_ERHOP_AS_FIXLEN;
4210 break;
4212 case MPLS_ERHOP_LSPID_TLVTYPE:
4214 if (MPLS_ERHOP_LSPID_FIXLEN + MPLS_TLVFIXLEN > bufSize) {
4215 return MPLS_ENC_BUFFTOOSMALL;
4217 encodedSize = Mpls_encodeLdpTlv(&(erHop->erLspId.baseTlv),
4218 tempBuff, bufSize);
4219 if (encodedSize < 0) {
4220 return MPLS_ENC_TLVERROR;
4222 tempBuff += encodedSize;
4223 startPtr = (u_char *) & (erHop->erLspId);
4224 startPtr += encodedSize;
4226 erHop->erLspId.flags.flags.res = 0;
4227 erHop->erLspId.flags.mark = htons(erHop->erLspId.flags.mark);
4228 erHop->erLspId.lspid = htons(erHop->erLspId.lspid);
4229 erHop->erLspId.routerId = htonl(erHop->erLspId.routerId);
4231 MEM_COPY(tempBuff, startPtr, MPLS_ERHOP_LSPID_FIXLEN);
4233 encodedSize += MPLS_ERHOP_LSPID_FIXLEN;
4234 break;
4236 default:
4238 PRINT_ERR("Found wrong ER hop type while encoding FEC elem (%d)\n",
4239 type);
4240 return MPLS_ENC_ERHOPERROR;
4241 break;
4243 } /* end: switch */
4245 return encodedSize;
4247 } /* End: Mpls_encodeLdpErHop */
4250 * decode
4252 int Mpls_decodeLdpErHop
4253 (mplsLdpErHop_t * erHop, u_char * buff, int bufSize, u_short * type) {
4254 int decodedSize = 0;
4255 u_char *tempBuf = buff;
4256 u_char *startPtr;
4257 mplsLdpTlv_t tlvTemp;
4260 * decode the tlv to check what is the type of the ER hop
4262 decodedSize = Mpls_decodeLdpTlv(&tlvTemp, tempBuf, bufSize);
4263 if (decodedSize < 0) {
4264 /* something wrong */
4265 PRINT_ERR("ErHop decode failed for tlv\n");
4266 return MPLS_DEC_TLVERROR;
4268 tempBuf += decodedSize;
4270 switch (tlvTemp.flags.flags.tBit) {
4271 case MPLS_ERHOP_IPV4_TLVTYPE:
4273 if (MPLS_ERHOP_IPV4_FIXLEN > bufSize - MPLS_TLVFIXLEN) {
4274 return MPLS_DEC_BUFFTOOSMALL;
4276 startPtr = (u_char *) & (erHop->erIpv4);
4277 startPtr += decodedSize; /* skip the tlv */
4279 MEM_COPY(startPtr, tempBuf, MPLS_ERHOP_IPV4_FIXLEN);
4280 erHop->erIpv4.flags.mark = ntohl(erHop->erIpv4.flags.mark);
4281 erHop->erIpv4.address = ntohl(erHop->erIpv4.address);
4282 erHop->erIpv4.baseTlv = tlvTemp;
4284 /* check how much is the preLen; should be between 0-32 */
4285 if (erHop->erIpv4.flags.flags.preLen > 32) {
4286 return MPLS_IPV4LENGTHERROR;
4289 decodedSize += MPLS_ERHOP_IPV4_FIXLEN;
4290 break;
4292 case MPLS_ERHOP_IPV6_TLVTYPE:
4294 if (MPLS_ERHOP_IPV6_FIXLEN > bufSize - MPLS_TLVFIXLEN) {
4295 return MPLS_DEC_BUFFTOOSMALL;
4297 startPtr = (u_char *) & (erHop->erIpv6);
4298 startPtr += decodedSize; /* skip the tlv */
4300 MEM_COPY(startPtr, tempBuf, MPLS_ERHOP_IPV6_FIXLEN);
4301 erHop->erIpv6.flags.mark = ntohl(erHop->erIpv6.flags.mark);
4302 erHop->erIpv6.baseTlv = tlvTemp;
4304 decodedSize += MPLS_ERHOP_IPV6_FIXLEN;
4305 break;
4307 case MPLS_ERHOP_AS_TLVTYPE:
4309 if (MPLS_ERHOP_AS_FIXLEN > bufSize - MPLS_TLVFIXLEN) {
4310 return MPLS_DEC_BUFFTOOSMALL;
4312 startPtr = (u_char *) & (erHop->erAs);
4313 startPtr += decodedSize; /* skip the tlv */
4315 MEM_COPY(startPtr, tempBuf, MPLS_ERHOP_AS_FIXLEN);
4316 erHop->erAs.flags.mark = ntohs(erHop->erAs.flags.mark);
4317 erHop->erAs.asNumber = ntohs(erHop->erAs.asNumber);
4318 erHop->erAs.baseTlv = tlvTemp;
4320 decodedSize += MPLS_ERHOP_AS_FIXLEN;
4321 break;
4323 case MPLS_ERHOP_LSPID_TLVTYPE:
4325 if (MPLS_ERHOP_LSPID_FIXLEN > bufSize - MPLS_TLVFIXLEN) {
4326 return MPLS_DEC_BUFFTOOSMALL;
4328 startPtr = (u_char *) & (erHop->erLspId);
4329 startPtr += decodedSize; /* skip the tlv */
4331 MEM_COPY(startPtr, tempBuf, MPLS_ERHOP_LSPID_FIXLEN);
4332 erHop->erLspId.flags.mark = ntohs(erHop->erLspId.flags.mark);
4333 erHop->erLspId.lspid = ntohs(erHop->erLspId.lspid);
4334 erHop->erLspId.routerId = ntohl(erHop->erLspId.routerId);
4335 erHop->erLspId.baseTlv = tlvTemp;
4337 decodedSize += MPLS_ERHOP_LSPID_FIXLEN;
4338 break;
4340 default:
4342 PRINT_ERR("Found wrong ER hop type while decoding ER (%d)\n", *type);
4343 return MPLS_DEC_ERHOPERROR;
4344 break;
4346 } /* end: switch */
4348 *type = tlvTemp.flags.flags.tBit;
4349 return decodedSize;
4351 } /* End: Mpls_decodeLdpErHop */
4354 * Encode for Traffic Tlv
4358 * encode
4360 int Mpls_encodeLdpTrafficTlv
4361 (mplsLdpTrafficTlv_t * trafficTlv, u_char * buff, int bufSize) {
4362 int encodedSize = 0;
4363 u_char *tempBuf = buff; /* no change for the buff ptr */
4364 u_char *trafficTlvPtr;
4365 u_short tempLength; /* to store the tlv length for
4367 later use */
4369 if (MPLS_TLVFIXLEN + (int)(trafficTlv->baseTlv.length) > bufSize) {
4370 /* not enough room */
4371 return MPLS_ENC_BUFFTOOSMALL;
4374 tempLength = trafficTlv->baseTlv.length;
4376 * encode for tlv
4378 encodedSize = Mpls_encodeLdpTlv(&(trafficTlv->baseTlv),
4379 tempBuf, MPLS_TLVFIXLEN);
4380 if (encodedSize < 0) {
4381 return MPLS_ENC_TLVERROR;
4383 tempBuf += encodedSize;
4384 trafficTlvPtr = (u_char *) trafficTlv;
4385 trafficTlvPtr += encodedSize;
4388 * encode Traffic flags + Frequency + Reserved + Weight
4390 encodedSize = sizeof(u_char) * 4;
4391 MEM_COPY(tempBuf, trafficTlvPtr, encodedSize);
4392 tempBuf += encodedSize;
4393 trafficTlvPtr += encodedSize;
4396 * encode for Traffic parameters
4398 if ((MPLS_TRAFFICPARAMLENGTH != sizeof(float)) ||
4399 (sizeof(float) != sizeof(u_int))) {
4400 PRINT_ERR("There is not compatibility for float type (%d)\n",
4402 (int)sizeof(float));
4403 return MPLS_FLOATTYPEERROR;
4406 trafficTlv->pdr.mark = htonl(trafficTlv->pdr.mark);
4407 trafficTlv->pbs.mark = htonl(trafficTlv->pbs.mark);
4408 trafficTlv->cdr.mark = htonl(trafficTlv->cdr.mark);
4409 trafficTlv->cbs.mark = htonl(trafficTlv->cbs.mark);
4410 trafficTlv->ebs.mark = htonl(trafficTlv->ebs.mark);
4412 MEM_COPY(tempBuf, trafficTlvPtr, MPLS_TRAFFICPARAMLENGTH * 5);
4414 return (MPLS_TLVFIXLEN + tempLength);
4416 } /* End: Mpls_encodeLdpTrafficTlv */
4419 * decode
4421 int Mpls_decodeLdpTrafficTlv
4422 (mplsLdpTrafficTlv_t * trafficTlv,
4423 u_char * buff, int bufSize, u_short tlvLength) {
4424 u_char *tempBuf = buff; /* no change for the buff ptr */
4425 int decodedSize = 0;
4426 u_char *trafficTlvPtr;
4428 if ((int)tlvLength > bufSize) {
4429 /* not enough data for Fec elements tlv */
4430 PRINT_ERR("failed decoding Traffic tlv \n");
4431 return MPLS_DEC_BUFFTOOSMALL;
4433 trafficTlvPtr = (u_char *) trafficTlv;
4434 trafficTlvPtr += MPLS_TLVFIXLEN;
4437 * decode Traffic flags + Frequency + Reserved + Weight
4439 decodedSize = sizeof(u_char) * 4;
4440 MEM_COPY(trafficTlvPtr, tempBuf, decodedSize);
4441 tempBuf += decodedSize;
4442 trafficTlvPtr += decodedSize;
4445 * decode the traffic parameters
4447 if (MPLS_TRAFFICPARAMLENGTH != sizeof(float)) {
4448 PRINT_ERR("There is not compatibility for float type (%d)\n", decodedSize);
4449 return MPLS_FLOATTYPEERROR;
4451 MEM_COPY(trafficTlvPtr, tempBuf, MPLS_TRAFFICPARAMLENGTH * 5);
4453 trafficTlv->pdr.mark = ntohl(trafficTlv->pdr.mark);
4454 trafficTlv->pbs.mark = ntohl(trafficTlv->pbs.mark);
4455 trafficTlv->cdr.mark = ntohl(trafficTlv->cdr.mark);
4456 trafficTlv->cbs.mark = ntohl(trafficTlv->cbs.mark);
4457 trafficTlv->ebs.mark = ntohl(trafficTlv->ebs.mark);
4459 return tlvLength;
4461 } /* End: Mpls_decodeLdpTrafficTlv */
4464 * Encode for Preempt Tlv
4468 * encode
4470 int Mpls_encodeLdpPreemptTlv
4471 (mplsLdpPreemptTlv_t * preemptTlv, u_char * buff, int bufSize) {
4472 int encodedSize = 0;
4473 u_char *tempBuf = buff; /* no change for the buff ptr */
4474 u_char *preemptTlvPtr;
4476 if (MPLS_TLVFIXLEN + MPLS_PREEMPTTLV_FIXLEN > bufSize) {
4477 /* not enough room */
4478 return MPLS_ENC_BUFFTOOSMALL;
4482 * encode for tlv
4484 encodedSize = Mpls_encodeLdpTlv(&(preemptTlv->baseTlv),
4485 tempBuf, MPLS_TLVFIXLEN);
4486 if (encodedSize < 0) {
4487 return MPLS_ENC_TLVERROR;
4489 tempBuf += encodedSize;
4491 preemptTlv->res = 0;
4492 preemptTlvPtr = (u_char *) preemptTlv;
4493 preemptTlvPtr += encodedSize;
4495 MEM_COPY(tempBuf, preemptTlvPtr, MPLS_PREEMPTTLV_FIXLEN);
4497 return (MPLS_TLVFIXLEN + MPLS_PREEMPTTLV_FIXLEN);
4499 } /* End: Mpls_encodeLdpPreemptTlv */
4502 * decode
4504 int Mpls_decodeLdpPreemptTlv
4505 (mplsLdpPreemptTlv_t * preemptTlv, u_char * buff, int bufSize) {
4506 u_char *preemptTlvPtr;
4508 if (MPLS_PREEMPTTLV_FIXLEN > bufSize) {
4509 PRINT_ERR("failed decoding preempt tlv\n");
4510 return MPLS_DEC_BUFFTOOSMALL;
4512 preemptTlvPtr = (u_char *) preemptTlv;
4513 preemptTlvPtr += MPLS_TLVFIXLEN;
4515 MEM_COPY(preemptTlvPtr, buff, MPLS_PREEMPTTLV_FIXLEN);
4517 return MPLS_PREEMPTTLV_FIXLEN;
4519 } /* End: Mpls_decodeLdpPreemptTlv */
4522 * Encode for LSPID Tlv
4526 * encode
4528 int Mpls_encodeLdpLspIdTlv
4529 (mplsLdpLspIdTlv_t * lspIdTlv, u_char * buff, int bufSize) {
4530 int encodedSize = 0;
4531 u_char *tempBuf = buff; /* no change for the buff ptr */
4532 u_char *lspIdTlvPtr;
4534 if (MPLS_TLVFIXLEN + MPLS_LSPIDTLV_FIXLEN > bufSize) {
4535 /* not enough room */
4536 return MPLS_ENC_BUFFTOOSMALL;
4540 * encode for tlv
4542 encodedSize = Mpls_encodeLdpTlv(&(lspIdTlv->baseTlv),
4543 tempBuf, MPLS_TLVFIXLEN);
4544 if (encodedSize < 0) {
4545 return MPLS_ENC_TLVERROR;
4547 tempBuf += encodedSize;
4549 lspIdTlvPtr = (u_char *) lspIdTlv;
4550 lspIdTlvPtr += encodedSize;
4552 lspIdTlv->res = 0;
4553 lspIdTlv->localCrlspId = htons(lspIdTlv->localCrlspId);
4554 lspIdTlv->routerId = htonl(lspIdTlv->routerId);
4556 MEM_COPY(tempBuf, lspIdTlvPtr, MPLS_LSPIDTLV_FIXLEN);
4558 return (MPLS_TLVFIXLEN + MPLS_LSPIDTLV_FIXLEN);
4560 } /* End: Mpls_encodeLdpLspIdTlv */
4563 * decode
4565 int Mpls_decodeLdpLspIdTlv
4566 (mplsLdpLspIdTlv_t * lspIdTlv, u_char * buff, int bufSize) {
4567 u_char *lspIdTlvPtr;
4569 if (MPLS_PREEMPTTLV_FIXLEN > bufSize) {
4570 PRINT_ERR("failed decoding LspId\n");
4571 return MPLS_DEC_BUFFTOOSMALL;
4573 lspIdTlvPtr = (u_char *) lspIdTlv;
4574 lspIdTlvPtr += MPLS_TLVFIXLEN;
4576 MEM_COPY(lspIdTlvPtr, buff, MPLS_LSPIDTLV_FIXLEN);
4578 lspIdTlv->localCrlspId = ntohs(lspIdTlv->localCrlspId);
4579 lspIdTlv->routerId = ntohl(lspIdTlv->routerId);
4581 return MPLS_LSPIDTLV_FIXLEN;
4583 } /* End: Mpls_decodeLdpLspIdTlv */
4586 * Encode for Resource Class Tlv
4590 * encode
4592 int Mpls_encodeLdpResClsTlv
4593 (mplsLdpResClsTlv_t * resClsTlv, u_char * buff, int bufSize) {
4594 int encodedSize = 0;
4595 u_char *tempBuf = buff; /* no change for the buff ptr */
4597 if (MPLS_TLVFIXLEN + (int)sizeof(u_int) > bufSize) {
4598 /* not enough room */
4599 return MPLS_ENC_BUFFTOOSMALL;
4603 * encode for tlv
4605 encodedSize = Mpls_encodeLdpTlv(&(resClsTlv->baseTlv),
4606 tempBuf, MPLS_TLVFIXLEN);
4607 if (encodedSize < 0) {
4608 return MPLS_ENC_TLVERROR;
4610 tempBuf += encodedSize;
4612 resClsTlv->rsCls = htonl(resClsTlv->rsCls);
4614 MEM_COPY(tempBuf, (u_char *) & (resClsTlv->rsCls), sizeof(u_int));
4616 return (MPLS_TLVFIXLEN + sizeof(u_int));
4618 } /* End: Mpls_encodeLdpResClsTlv */
4621 * decode
4623 int Mpls_decodeLdpResClsTlv
4624 (mplsLdpResClsTlv_t * resClsTlv, u_char * buff, int bufSize) {
4625 if ((int)sizeof(u_int) > bufSize) {
4626 PRINT_ERR("failed decoding resClass tlv\n");
4627 return MPLS_DEC_BUFFTOOSMALL;
4630 MEM_COPY((u_char *) & (resClsTlv->rsCls), buff, sizeof(u_int));
4631 resClsTlv->rsCls = ntohl(resClsTlv->rsCls);
4633 return sizeof(u_int);
4635 } /* End: Mpls_decodeLdpResClsTlv */
4638 * Encode for Route Pinning Tlv
4642 * encode
4644 int Mpls_encodeLdpPinningTlv
4645 (mplsLdpPinningTlv_t * pinningTlv, u_char * buff, int bufSize) {
4646 int encodedSize = 0;
4647 u_char *tempBuf = buff; /* no change for the buff ptr */
4649 if (MPLS_TLVFIXLEN + (int)sizeof(u_int) > bufSize) {
4650 /* not enough room */
4651 return MPLS_ENC_BUFFTOOSMALL;
4655 * encode for tlv
4657 encodedSize = Mpls_encodeLdpTlv(&(pinningTlv->baseTlv),
4658 tempBuf, MPLS_TLVFIXLEN);
4659 if (encodedSize < 0) {
4660 return MPLS_ENC_TLVERROR;
4662 tempBuf += encodedSize;
4664 pinningTlv->flags.flags.res = 0;
4665 pinningTlv->flags.mark = htonl(pinningTlv->flags.mark);
4667 MEM_COPY(tempBuf, (u_char *) & (pinningTlv->flags.mark), sizeof(u_int));
4669 return (MPLS_TLVFIXLEN + sizeof(u_int));
4671 } /* End: Mpls_encodeLdpPinningTlv */
4674 * decode
4676 int Mpls_decodeLdpPinningTlv
4677 (mplsLdpPinningTlv_t * pinningTlv, u_char * buff, int bufSize) {
4678 if ((int)sizeof(u_int) > bufSize) {
4679 PRINT_ERR("failed decoding route pinning tlv\n");
4680 return MPLS_DEC_BUFFTOOSMALL;
4683 MEM_COPY((u_char *) & (pinningTlv->flags.mark), buff, sizeof(u_int));
4684 pinningTlv->flags.mark = ntohl(pinningTlv->flags.mark);
4686 return sizeof(u_int);
4688 } /* End: Mpls_decodeLdpPinningTlv */
4691 * Label Abort Request Message
4695 * encode
4697 int Mpls_encodeLdpLblAbortMsg
4698 (mplsLdpLblAbortMsg_t * lblAbortMsg, u_char * buff, int bufSize) {
4699 mplsLdpLblAbortMsg_t lblAbortMsgCopy;
4700 int encodedSize = 0;
4701 u_int totalSize = 0;
4702 u_char *tempBuf = buff; /* no change for the buff ptr */
4704 /* check the length of the messageId + param */
4705 if ((int)(lblAbortMsg->baseMsg.msgLength) + MPLS_TLVFIXLEN > bufSize) {
4706 PRINT_ERR("failed to encode the lbl abort request msg: BUFFER TOO SMALL\n");
4707 return MPLS_ENC_BUFFTOOSMALL;
4710 lblAbortMsgCopy = *lblAbortMsg;
4713 * encode the base part of the pdu message
4715 encodedSize = Mpls_encodeLdpBaseMsg(&(lblAbortMsgCopy.baseMsg),
4716 tempBuf, bufSize);
4717 if (encodedSize < 0) {
4718 return MPLS_ENC_BASEMSGERROR;
4720 PRINT_OUT("Encode BaseMsg for label abort request msg on %d bytes\n",
4721 encodedSize);
4722 tempBuf += encodedSize;
4723 totalSize += encodedSize;
4726 * encode the tlv if any
4728 if (lblAbortMsgCopy.fecTlvExists) {
4729 encodedSize = Mpls_encodeLdpFecTlv(&(lblAbortMsgCopy.fecTlv),
4730 tempBuf, bufSize - totalSize);
4731 if (encodedSize < 0) {
4732 return MPLS_ENC_FECERROR;
4734 PRINT_OUT("Encoded for FEC Tlv %d bytes\n", encodedSize);
4735 tempBuf += encodedSize;
4736 totalSize += encodedSize;
4738 if (lblAbortMsgCopy.lblMsgIdTlvExists) {
4739 encodedSize = Mpls_encodeLdpLblMsgIdTlv(&(lblAbortMsgCopy.lblMsgIdTlv),
4740 tempBuf, bufSize - totalSize);
4741 if (encodedSize < 0) {
4742 return MPLS_ENC_LBLMSGIDERROR;
4744 PRINT_OUT("Encoded for lbl request msg id Tlv %d bytes\n", encodedSize);
4745 tempBuf += encodedSize;
4746 totalSize += encodedSize;
4749 return totalSize;
4751 } /* End: Mpls_encodeLdpLblAbortMsg */
4754 * decode
4756 int Mpls_decodeLdpLblAbortMsg
4757 (mplsLdpLblAbortMsg_t * lblAbortMsg, u_char * buff, int bufSize) {
4758 int decodedSize = 0;
4759 u_int totalSize = 0;
4760 u_int stopLength = 0;
4761 u_int totalSizeParam = 0;
4762 u_char *tempBuf = buff; /* no change for the buff ptr */
4763 mplsLdpTlv_t tlvTemp;
4766 * decode the base part of the pdu message
4768 memset(lblAbortMsg, 0, sizeof(mplsLdpLblAbortMsg_t));
4769 decodedSize = Mpls_decodeLdpBaseMsg(&(lblAbortMsg->baseMsg),
4770 tempBuf, bufSize);
4771 if (decodedSize < 0) {
4772 return MPLS_DEC_BASEMSGERROR;
4774 PRINT_OUT("Decode BaseMsg for Lbl Abort Request Msg on %d bytes\n",
4775 decodedSize);
4777 if (lblAbortMsg->baseMsg.flags.flags.msgType != MPLS_LBLABORT_MSGTYPE) {
4778 PRINT_ERR("Not the right message type; expected lbl abort and got %x\n",
4779 lblAbortMsg->baseMsg.flags.flags.msgType);
4780 return MPLS_MSGTYPEERROR;
4783 tempBuf += decodedSize;
4784 totalSize += decodedSize;
4786 if (bufSize - totalSize <= 0) {
4787 /* nothing left for decoding */
4788 PRINT_ERR("Lbl Abort msg does not have anything beside base msg\n");
4789 return totalSize;
4792 PRINT_OUT
4793 ("bufSize = %d, totalSize = %d, lblAbortMsg->baseMsg.msgLength = %d\n",
4794 bufSize, totalSize, lblAbortMsg->baseMsg.msgLength);
4796 /* Have to check the baseMsg.msgLength to know when to finish.
4797 * We finsh when the totalSizeParam is >= to the base message length - the
4798 * message id length (4)
4801 stopLength = lblAbortMsg->baseMsg.msgLength - MPLS_MSGIDFIXLEN;
4802 while (stopLength > totalSizeParam) {
4804 * decode the tlv to check what's next
4806 memset(&tlvTemp, 0, MPLS_TLVFIXLEN);
4807 decodedSize = Mpls_decodeLdpTlv(&tlvTemp, tempBuf, bufSize - totalSize);
4808 if (decodedSize < 0) {
4809 /* something wrong */
4810 PRINT_ERR("Label Abort msg decode failed for tlv\n");
4811 return MPLS_DEC_TLVERROR;
4814 tempBuf += decodedSize;
4815 totalSize += decodedSize;
4816 totalSizeParam += decodedSize;
4818 switch (tlvTemp.flags.flags.tBit) {
4819 case MPLS_FEC_TLVTYPE:
4821 decodedSize = Mpls_decodeLdpFecTlv(&(lblAbortMsg->fecTlv),
4822 tempBuf, bufSize - totalSize, tlvTemp.length);
4823 if (decodedSize < 0) {
4824 PRINT_ERR("Failure when decoding FEC tlv from LblAbort msg\n");
4825 return MPLS_DEC_FECERROR;
4827 PRINT_OUT("Decoded for FEC %d bytes\n", decodedSize);
4828 tempBuf += decodedSize;
4829 totalSize += decodedSize;
4830 totalSizeParam += decodedSize;
4832 lblAbortMsg->fecTlvExists = 1;
4833 lblAbortMsg->fecTlv.baseTlv = tlvTemp;
4834 break;
4836 case MPLS_REQMSGID_TLVTYPE:
4838 decodedSize = Mpls_decodeLdpLblMsgIdTlv(&(lblAbortMsg->lblMsgIdTlv),
4839 tempBuf, bufSize - totalSize);
4840 if (decodedSize < 0) {
4841 PRINT_ERR("Failure when dec LblMsgId tlv from LblAbort msg\n");
4842 return MPLS_DEC_LBLMSGIDERROR;
4844 PRINT_OUT("Decoded for LblMsgId %d bytes\n", decodedSize);
4845 tempBuf += decodedSize;
4846 totalSize += decodedSize;
4847 totalSizeParam += decodedSize;
4849 lblAbortMsg->lblMsgIdTlvExists = 1;
4850 lblAbortMsg->lblMsgIdTlv.baseTlv = tlvTemp;
4851 break;
4853 default:
4855 PRINT_ERR("Found wrong tlv type while decoding lbl abort msg (%x)\n",
4856 tlvTemp.flags.flags.tBit);
4857 if (tlvTemp.flags.flags.uBit == 1) {
4858 /* ignore the Tlv and continue processing */
4859 tempBuf += tlvTemp.length;
4860 totalSize += tlvTemp.length;
4861 totalSizeParam += tlvTemp.length;
4862 break;
4863 } else {
4864 /* drop the message; return error */
4865 return MPLS_TLVTYPEERROR;
4868 } /* switch type */
4870 } /* while */
4872 PRINT_OUT("totalsize for Mpls_decodeLdpLblAbortMsg is %d\n", totalSize);
4874 return totalSize;
4876 } /* End: Mpls_decodeLdpLblAbortMsg */
4879 * DEBUG functions
4881 void printTlv(mpls_instance_handle handle, mplsLdpTlv_t * tlv)
4883 LDP_TRACE_OUT(handle, "\t Tlv:\n");
4884 LDP_TRACE_OUT(handle, "\t BaseTlv: uBit = %d\n", tlv->flags.flags.uBit);
4885 LDP_TRACE_OUT(handle, "\t\t fBit = %d\n", tlv->flags.flags.fBit);
4886 LDP_TRACE_OUT(handle, "\t\t type = %x\n", tlv->flags.flags.tBit);
4887 LDP_TRACE_OUT(handle, "\t\t length = %d\n", tlv->length);
4890 void printHeader(mpls_instance_handle handle, mplsLdpHeader_t * header)
4892 LDP_TRACE_OUT(handle, "LPD Header : protocolVersion = %d\n",
4893 header->protocolVersion);
4894 LDP_TRACE_OUT(handle, "\tpduLength = %d\n", header->pduLength);
4895 LDP_TRACE_OUT(handle, "\tlsrAddress = %x\n", header->lsrAddress);
4896 LDP_TRACE_OUT(handle, "\tlabelSpace = %x\n", header->labelSpace);
4899 void printCspFlags(mpls_instance_handle handle, mplsLdpCspFlag_t * cspFlags)
4901 LDP_TRACE_OUT(handle, "\tCSP Flags: lad = %d, ld = %d, pvl = %d, res = %d\n",
4902 cspFlags->lad, cspFlags->ld, cspFlags->pvl, cspFlags->res);
4905 void printCspFlagsPerByte(mpls_instance_handle handle, u_short * cspFlags)
4907 u_char *ptr;
4909 ptr = (u_char *) cspFlags;
4910 LDP_TRACE_OUT(handle, "\tCSP Flags: (byte 0) %x\n", *ptr++);
4911 LDP_TRACE_OUT(handle, "\t\t (byte 1) %x\n", *ptr);
4914 void printCspTlv(mpls_instance_handle handle, mplsLdpCspTlv_t * csp)
4916 LDP_TRACE_OUT(handle, "\tCSP:\n");
4917 printTlv(handle, &(csp->baseTlv));
4918 LDP_TRACE_OUT(handle, "\tcsp : protocolVersion = %d\n",
4919 csp->protocolVersion);
4920 LDP_TRACE_OUT(handle, "\t\tholdTime = %d\n", csp->holdTime);
4921 LDP_TRACE_OUT(handle, "\t\tmaxPduLen = %d\n", csp->maxPduLen);
4922 LDP_TRACE_OUT(handle, "\t\trcvLsrAddress = %08x\n", csp->rcvLsrAddress);
4923 LDP_TRACE_OUT(handle, "\t\trcvLsId = %d\n", csp->rcvLsId);
4925 printCspFlags(handle, &(csp->flags.flags));
4928 void printAspFlags(mpls_instance_handle handle, mplsLdpSPFlag_t * aspFlags)
4930 LDP_TRACE_OUT(handle,
4931 "\t ASP Flags: mergeType = %d, numLblRng = %d, dir = %d, res = %d\n",
4932 aspFlags->mergeType, aspFlags->numLblRng, aspFlags->dir, aspFlags->res);
4935 void printAspFlagsPerByte(mpls_instance_handle handle, u_int * aspFlags)
4937 u_char *ptr;
4939 ptr = (u_char *) aspFlags;
4940 LDP_TRACE_OUT(handle, "\tASP Flags: (byte 0) %x\n", *ptr++);
4941 LDP_TRACE_OUT(handle, "\t\t (byte 1) %x\n", *ptr++);
4942 LDP_TRACE_OUT(handle, "\t\t (byte 2) %x\n", *ptr++);
4943 LDP_TRACE_OUT(handle, "\t\t (byte 3) %x\n", *ptr);
4946 void printAtmLabel(mpls_instance_handle handle, mplsLdpAtmLblRng_t * label,
4947 int i)
4949 LDP_TRACE_OUT(handle,
4950 "\tATM LABEL (%d) : res1 = %d, minVci = %d, minVpi = %d, res2 = %d, maxVci = %d, maxVpi = %d\n",
4951 i, label->flags.flags.res1, label->flags.flags.minVci,
4952 label->flags.flags.minVpi, label->flags.flags.res2,
4953 label->flags.flags.maxVci, label->flags.flags.maxVpi);
4956 void printAspTlv(mpls_instance_handle handle, mplsLdpAspTlv_t * asp)
4958 int i = 0;
4960 LDP_TRACE_OUT(handle, "\t asp:\n");
4961 printTlv(handle, &(asp->baseTlv));
4962 LDP_TRACE_OUT(handle, "\t asp labes (%d)\n",
4963 (int)(asp->flags.flags.numLblRng));
4964 for (i = 0; i < (int)(asp->flags.flags.numLblRng); i++) {
4965 printAtmLabel(handle, &(asp->lblRngList[i]), i);
4967 printAspFlags(handle, &(asp->flags.flags));
4970 void printFspFlags(mpls_instance_handle handle, mplsLdpSPFlag_t * fspFlags)
4972 LDP_TRACE_OUT(handle,
4973 "\t FSP Flags: mergeType = %d, numLblRng = %d, dir = %d, res = %d\n",
4974 fspFlags->mergeType, fspFlags->numLblRng, fspFlags->dir, fspFlags->res);
4977 void printFspLabel(mpls_instance_handle handle, mplsLdpFrLblRng_t * label, int i)
4979 LDP_TRACE_OUT(handle,
4980 "\tFR LABEL (%d) : res_max = %d, maxDlci = %d, res_min = %d, len = %d minDlci = %d\n",
4981 i, label->flags.flags.res_max, label->flags.flags.maxDlci,
4982 label->flags.flags.res_min, label->flags.flags.len,
4983 label->flags.flags.minDlci);
4986 void printFspTlv(mpls_instance_handle handle, mplsLdpFspTlv_t * fsp)
4988 int i = 0;
4990 LDP_TRACE_OUT(handle, "\t fsp:\n");
4991 printTlv(handle, &(fsp->baseTlv));
4992 LDP_TRACE_OUT(handle, "\t fsp labes (%d)\n",
4993 (int)(fsp->flags.flags.numLblRng));
4994 for (i = 0; i < (int)(fsp->flags.flags.numLblRng); i++) {
4995 printFspLabel(handle, &(fsp->lblRngList[i]), i);
4997 printFspFlags(handle, &(fsp->flags.flags));
5000 void printMsgBase(mpls_instance_handle handle, mplsLdpMsg_t * msg)
5002 LDP_TRACE_OUT(handle, "\tbaseMsg : uBit = %d\n", msg->flags.flags.uBit);
5003 LDP_TRACE_OUT(handle, "\t\t msgType = %x\n", msg->flags.flags.msgType);
5004 LDP_TRACE_OUT(handle, "\t\t msgLength = %d\n", msg->msgLength);
5005 LDP_TRACE_OUT(handle, "\t\t msgId = %d\n", msg->msgId);
5008 void printInitMsg(mpls_instance_handle handle, mplsLdpInitMsg_t * initMsg)
5010 LDP_TRACE_OUT(handle, "INIT MSG ***START***:\n");
5011 printMsgBase(handle, &(initMsg->baseMsg));
5012 if (initMsg->cspExists) {
5013 printCspTlv(handle, &(initMsg->csp));
5014 } else {
5015 LDP_TRACE_OUT(handle, "\tINIT msg does NOT have CSP\n");
5017 if (initMsg->aspExists) {
5018 printAspTlv(handle, &(initMsg->asp));
5019 } else {
5020 LDP_TRACE_OUT(handle, "\tINIT msg does NOT have ASP\n");
5022 if (initMsg->fspExists) {
5023 printFspTlv(handle, &(initMsg->fsp));
5024 } else {
5025 LDP_TRACE_OUT(handle, "\tINIT msg does NOT have FSP\n");
5027 LDP_TRACE_OUT(handle, "\nINIT MSG ***END***\n");
5030 void printRetMsgTlv(mpls_instance_handle handle, mplsLdpRetMsgTlv_t * retMsg)
5032 LDP_TRACE_OUT(handle, "\t retMsgTlv:\n");
5033 printTlv(handle, &(retMsg->baseTlv));
5034 LDP_TRACE_OUT(handle, "\t retMsgTlv.data is %s\n", retMsg->data);
5037 void printRetPduTlv(mpls_instance_handle handle, mplsLdpRetPduTlv_t * retPdu)
5039 LDP_TRACE_OUT(handle, "\t retPduTlv:\n");
5040 printTlv(handle, &(retPdu->baseTlv));
5041 LDP_TRACE_OUT(handle, "\t retPduTlv.data is %s\n", retPdu->data);
5044 void printExStatusTlv(mpls_instance_handle handle, mplsLdpExStatusTlv_t * status)
5046 LDP_TRACE_OUT(handle, "\t exStatusTlv:\n");
5047 printTlv(handle, &(status->baseTlv));
5048 LDP_TRACE_OUT(handle, "\t exStatus data: value = %d\n", status->value);
5051 void printStatusTlv(mpls_instance_handle handle, mplsLdpStatusTlv_t * status)
5053 LDP_TRACE_OUT(handle, "\t statusTlv:\n");
5054 printTlv(handle, &(status->baseTlv));
5055 LDP_TRACE_OUT(handle, "\t status data: msgId = %x\n", status->msgId);
5056 LDP_TRACE_OUT(handle, "\t\t\tmsgType = %x\n", status->msgType);
5057 LDP_TRACE_OUT(handle, "\t status Flags: error = %d\n",
5058 status->flags.flags.error);
5059 LDP_TRACE_OUT(handle, "\t\t\tforward = %d\n", status->flags.flags.forward);
5060 LDP_TRACE_OUT(handle, "\t\t\tstatus = %d\n", status->flags.flags.status);
5063 void printNotMsg(mpls_instance_handle handle, mplsLdpNotifMsg_t * notMsg)
5065 LDP_TRACE_OUT(handle, "NOTIF MSG ***START***:\n");
5066 printMsgBase(handle, &(notMsg->baseMsg));
5068 if (notMsg->statusTlvExists) {
5069 printStatusTlv(handle, &(notMsg->status));
5070 } else {
5071 LDP_TRACE_OUT(handle, "\tNotif msg does not have Status TLV\n");
5073 if (notMsg->exStatusTlvExists) {
5074 printExStatusTlv(handle, &(notMsg->exStatus));
5075 } else {
5076 LDP_TRACE_OUT(handle, "\tNotif msg does not have Extended Status TLV\n");
5078 if (notMsg->retPduTlvExists) {
5079 printRetPduTlv(handle, &(notMsg->retPdu));
5080 } else {
5081 LDP_TRACE_OUT(handle, "\tNotif msg does not have Return PDU\n");
5083 if (notMsg->retMsgTlvExists) {
5084 printRetMsgTlv(handle, &(notMsg->retMsg));
5085 } else {
5086 LDP_TRACE_OUT(handle, "\tNotif msg does not have Return MSG\n");
5088 LDP_TRACE_OUT(handle, "NOTIF MSG ***END***:\n");
5091 void printCsnTlv(mpls_instance_handle handle, mplsLdpCsnTlv_t * csn)
5093 LDP_TRACE_OUT(handle, "\t csnTlv:\n");
5094 printTlv(handle, &(csn->baseTlv));
5095 LDP_TRACE_OUT(handle, "\t csnTlv data: value = %d\n", csn->seqNumber);
5098 void printTrAdrTlv(mpls_instance_handle handle, mplsLdpTrAdrTlv_t * trAdr)
5100 LDP_TRACE_OUT(handle, "\t trAdrTlv:\n");
5101 printTlv(handle, &(trAdr->baseTlv));
5102 LDP_TRACE_OUT(handle, "\t trAdrTlv data: value = %08x\n", trAdr->address);
5105 void printChpTlv(mpls_instance_handle handle, mplsLdpChpTlv_t * chp)
5107 LDP_TRACE_OUT(handle, "\t chpTlv:\n");
5108 printTlv(handle, &(chp->baseTlv));
5109 LDP_TRACE_OUT(handle, "\t chpTlv data: holdTime = %d\n", chp->holdTime);
5110 LDP_TRACE_OUT(handle, "\t chpTlv Flags: target = %d\n",
5111 chp->flags.flags.target);
5112 LDP_TRACE_OUT(handle, "\t\t\trequest = %d\n", chp->flags.flags.request);
5113 LDP_TRACE_OUT(handle, "\t\t\tres = %d\n", chp->flags.flags.res);
5116 void printHelloMsg(mpls_instance_handle handle, mplsLdpHelloMsg_t * helloMsg)
5118 LDP_TRACE_OUT(handle, "HELLO MSG ***START***:\n");
5119 printMsgBase(handle, &(helloMsg->baseMsg));
5121 if (helloMsg->chpTlvExists) {
5122 printChpTlv(handle, &(helloMsg->chp));
5123 } else {
5124 LDP_TRACE_OUT(handle, "\tHello msg does not have Chp TLV\n");
5126 if (helloMsg->trAdrTlvExists) {
5127 printTrAdrTlv(handle, &(helloMsg->trAdr));
5128 } else {
5129 LDP_TRACE_OUT(handle, "\tHello msg does not have TrAdr TLV\n");
5131 if (helloMsg->csnTlvExists) {
5132 printCsnTlv(handle, &(helloMsg->csn));
5133 } else {
5134 LDP_TRACE_OUT(handle, "\tHello msg does not have Csn TLV\n");
5136 LDP_TRACE_OUT(handle, "HELLO MSG ***END***:\n");
5139 void printKeepAliveMsg(mpls_instance_handle handle,
5140 mplsLdpKeepAlMsg_t * keepAliveMsg)
5142 LDP_TRACE_OUT(handle, "KEEP ALIVE MSG ***START***:\n");
5143 printMsgBase(handle, &(keepAliveMsg->baseMsg));
5144 LDP_TRACE_OUT(handle, "KEEP ALIVE MSG ***END***:\n");
5147 void printAdrListTlv(mpls_instance_handle handle, mplsLdpAdrTlv_t * adrList)
5149 u_short i;
5150 u_short length;
5152 LDP_TRACE_OUT(handle, "\t adrListTlv:\n");
5153 printTlv(handle, &(adrList->baseTlv));
5154 LDP_TRACE_OUT(handle, "\t adrListTlv data: addrFamily = %x\n",
5155 adrList->addrFamily);
5157 /* get the current length of the encoding for addresses */
5159 length = adrList->baseTlv.length - MPLS_ADDFAMFIXLEN;
5160 LDP_TRACE_OUT(handle, "\t adrListTlv addresses (with %d addresses) :\n",
5161 length / 4);
5162 for (i = 0; i < (u_short) (length / 4); i++) {
5163 if (i % 4 == 0) {
5164 LDP_TRACE_OUT(handle, "\n\t\t\t");
5166 LDP_TRACE_OUT(handle, "%02x ", adrList->address[i]);
5168 LDP_TRACE_OUT(handle, "\n");
5171 void printAddressMsg(mpls_instance_handle handle, mplsLdpAdrMsg_t * adrMsg)
5173 if (adrMsg->baseMsg.flags.flags.msgType == MPLS_ADDR_MSGTYPE) {
5174 LDP_TRACE_OUT(handle, "ADDRESS MSG ***START***:\n");
5175 } else if (adrMsg->baseMsg.flags.flags.msgType == MPLS_ADDRWITH_MSGTYPE) {
5176 LDP_TRACE_OUT(handle, "ADDRESS WITHDRAW MSG ***START***:\n");
5179 printMsgBase(handle, &(adrMsg->baseMsg));
5181 if (adrMsg->adrListTlvExists) {
5182 printAdrListTlv(handle, &(adrMsg->addressList));
5183 } else {
5184 LDP_TRACE_OUT(handle, "\tAddress msg does not have addrList Tlv\n");
5186 if (adrMsg->baseMsg.flags.flags.msgType == MPLS_ADDR_MSGTYPE) {
5187 LDP_TRACE_OUT(handle, "ADDRESS MSG ***END***:\n");
5188 } else if (adrMsg->baseMsg.flags.flags.msgType == MPLS_ADDRWITH_MSGTYPE) {
5189 LDP_TRACE_OUT(handle, "ADDRESS WITHDRAW MSG ***END***:\n");
5193 void printFecListTlv(mpls_instance_handle handle, mplsLdpFecTlv_t * fecTlv)
5195 u_short i;
5197 LDP_TRACE_OUT(handle, "\t fecTlv:\n");
5198 printTlv(handle, &(fecTlv->baseTlv));
5199 LDP_TRACE_OUT(handle, "\t\tfecTlv->numberFecElements = %d\n",
5200 fecTlv->numberFecElements);
5201 for (i = 0; i < fecTlv->numberFecElements; i++) {
5202 LDP_TRACE_OUT(handle, "\t\telem %d type is %d\n", i,
5203 fecTlv->fecElemTypes[i]);
5204 if ((fecTlv->fecElemTypes[i] == 2) || (fecTlv->fecElemTypes[i] == 3)) {
5205 LDP_TRACE_OUT(handle,
5206 "\t\tFec Element : type = %d, addFam = %x, preLen = %d, address = %x\n",
5207 fecTlv->fecElArray[i].addressEl.type,
5208 fecTlv->fecElArray[i].addressEl.addressFam,
5209 fecTlv->fecElArray[i].addressEl.preLen,
5210 fecTlv->fecElArray[i].addressEl.address);
5213 LDP_TRACE_OUT(handle, "\n");
5214 LDP_TRACE_OUT(handle, "\tfecTlv.wcElemExists = %d\n", fecTlv->wcElemExists);
5218 void printLblMsgIdTlv(mpls_instance_handle handle,
5219 mplsLdpLblMsgIdTlv_t * lblMsgId)
5221 LDP_TRACE_OUT(handle, "\t lblMsgIdTlv:\n");
5222 printTlv(handle, &(lblMsgId->baseTlv));
5223 LDP_TRACE_OUT(handle, "\t LblMsgId data: msgId = %d\n", lblMsgId->msgId);
5226 void printPathVecTlv(mpls_instance_handle handle, mplsLdpPathTlv_t * pathVec)
5228 u_int i, numlsrId;
5230 LDP_TRACE_OUT(handle, "\t pathVecTlv:\n");
5231 printTlv(handle, &(pathVec->baseTlv));
5232 LDP_TRACE_OUT(handle, "\t PathVec data: ");
5234 numlsrId = pathVec->baseTlv.length / 4;
5236 for (i = 0; i < numlsrId; i++) {
5237 if (i == 0) {
5238 LDP_TRACE_OUT(handle, "lsrId[%d] = %x\n", i, pathVec->lsrId[i]);
5239 } else {
5240 LDP_TRACE_OUT(handle, "\t\t\tlsrId[%d] = %x\n", i, pathVec->lsrId[i]);
5243 LDP_TRACE_OUT(handle, "\n");
5246 void printHopTlv(mpls_instance_handle handle, mplsLdpHopTlv_t * hopCount)
5248 LDP_TRACE_OUT(handle, "\t hopCountTlv:\n");
5249 printTlv(handle, &(hopCount->baseTlv));
5250 LDP_TRACE_OUT(handle, "\t hopCount data: hcValue = %d\n", hopCount->hcValue);
5253 void printFrLblTlv(mpls_instance_handle handle, mplsLdpFrLblTlv_t * fr)
5255 LDP_TRACE_OUT(handle, "\t frTlv :\n");
5256 printTlv(handle, &(fr->baseTlv));
5257 LDP_TRACE_OUT(handle, "\t Fr flags: res = %d\n", fr->flags.flags.res);
5258 LDP_TRACE_OUT(handle, "\t\t len = %d\n", fr->flags.flags.len);
5259 LDP_TRACE_OUT(handle, "\t\tdlci = %d\n", fr->flags.flags.dlci);
5262 void printAtmLblTlv(mpls_instance_handle handle, mplsLdpAtmLblTlv_t * atm)
5264 LDP_TRACE_OUT(handle, "\t atmTlv :\n");
5265 printTlv(handle, &(atm->baseTlv));
5266 LDP_TRACE_OUT(handle, "\t Atm flags: res = %d\n", atm->flags.flags.res);
5267 LDP_TRACE_OUT(handle, "\t\t v = %d\n", atm->flags.flags.v);
5268 LDP_TRACE_OUT(handle, "\t\tvpi = %d\n", atm->flags.flags.vpi);
5269 LDP_TRACE_OUT(handle, "\t Atm data : vci = %d\n", atm->vci);
5272 void printGenLblTlv(mpls_instance_handle handle, mplsLdpGenLblTlv_t * genLbl)
5274 LDP_TRACE_OUT(handle, "\t genLblTlv:\n");
5275 printTlv(handle, &(genLbl->baseTlv));
5276 LDP_TRACE_OUT(handle, "\t genLbl data: label = %d\n", genLbl->label);
5279 void printLlbMapMsg(mpls_instance_handle handle, mplsLdpLblMapMsg_t * lblMapMsg)
5281 LDP_TRACE_OUT(handle, "LABEL MAPPING MSG ***START***:\n");
5282 printMsgBase(handle, &(lblMapMsg->baseMsg));
5284 if (lblMapMsg->fecTlvExists) {
5285 printFecListTlv(handle, &(lblMapMsg->fecTlv));
5286 } else {
5287 LDP_TRACE_OUT(handle, "\tLabel mapping msg does not have fec Tlv\n");
5289 if (lblMapMsg->genLblTlvExists) {
5290 printGenLblTlv(handle, &(lblMapMsg->genLblTlv));
5291 } else {
5292 LDP_TRACE_OUT(handle, "\tLabel mapping msg does not have gen label Tlv\n");
5294 if (lblMapMsg->atmLblTlvExists) {
5295 printAtmLblTlv(handle, &(lblMapMsg->atmLblTlv));
5296 } else {
5297 LDP_TRACE_OUT(handle, "\tLabel mapping msg does not have atm label Tlv\n");
5299 if (lblMapMsg->frLblTlvExists) {
5300 printFrLblTlv(handle, &(lblMapMsg->frLblTlv));
5301 } else {
5302 LDP_TRACE_OUT(handle, "\tLabel mapping msg does not have fr label Tlv\n");
5304 if (lblMapMsg->hopCountTlvExists) {
5305 printHopTlv(handle, &(lblMapMsg->hopCountTlv));
5306 } else {
5307 LDP_TRACE_OUT(handle, "\tLabel mapping msg does not have hop count Tlv\n");
5309 if (lblMapMsg->pathVecTlvExists) {
5310 printPathVecTlv(handle, &(lblMapMsg->pathVecTlv));
5311 } else {
5312 LDP_TRACE_OUT(handle,
5313 "\tLabel mapping msg does not have path vector Tlv\n");
5315 if (lblMapMsg->lblMsgIdTlvExists) {
5316 printLblMsgIdTlv(handle, &(lblMapMsg->lblMsgIdTlv));
5317 } else {
5318 LDP_TRACE_OUT(handle,
5319 "\tLabel mapping msg does not have label messageId Tlv\n");
5321 if (lblMapMsg->lspidTlvExists) {
5322 printLspIdTlv(handle, &(lblMapMsg->lspidTlv));
5323 } else {
5324 LDP_TRACE_OUT(handle, "\tLabel mapping msg does not have LSPID Tlv\n");
5326 if (lblMapMsg->trafficTlvExists) {
5327 printTrafficTlv(handle, &(lblMapMsg->trafficTlv));
5328 } else {
5329 LDP_TRACE_OUT(handle, "\tLabel mapping msg does not have traffic Tlv\n");
5331 LDP_TRACE_OUT(handle, "LABEL MAPPING MSG ***END***:\n");
5334 void printErFlags(mpls_instance_handle handle, mplsLdpErFlag_t * flags)
5336 LDP_TRACE_OUT(handle, "\t\tER FLAGS: l = %d, res = %d\n",
5337 flags->l, flags->res);
5340 void printErIPFlags(mpls_instance_handle handle, mplsLdpErIPFlag_t * flags)
5342 LDP_TRACE_OUT(handle, "\t\tER IP FLAGS: l = %d, res = %d, preLen = %d\n",
5343 flags->l, flags->res, flags->preLen);
5346 void printErHop(mpls_instance_handle handle, mplsLdpErHop_t * erHop,
5347 u_short type)
5349 int i;
5351 switch (type) {
5352 case MPLS_ERHOP_IPV4_TLVTYPE:
5354 printErIPFlags(handle, &(erHop->erIpv4.flags.flags));
5355 LDP_TRACE_OUT(handle, "\t\t IPv4: address = %x\n",
5356 erHop->erIpv4.address);
5357 break;
5359 case MPLS_ERHOP_IPV6_TLVTYPE:
5361 printErIPFlags(handle, &(erHop->erIpv6.flags.flags));
5362 LDP_TRACE_OUT(handle, "\t\t IPv6: address ");
5363 for (i = 0; i < MPLS_IPV6ADRLENGTH; i++) {
5364 LDP_TRACE_OUT(handle, "\t\t a[%d] = %x\n", i,
5365 erHop->erIpv6.address[i]);
5367 break;
5369 case MPLS_ERHOP_AS_TLVTYPE:
5371 printErFlags(handle, &(erHop->erAs.flags.flags));
5372 LDP_TRACE_OUT(handle, "\t\t ASnumber: asNumber = %d\n",
5373 erHop->erAs.asNumber);
5374 break;
5376 case MPLS_ERHOP_LSPID_TLVTYPE:
5378 printErFlags(handle, &(erHop->erLspId.flags.flags));
5379 LDP_TRACE_OUT(handle, "\t\t LSPID: lspid = %d, routerId = %d\n",
5380 erHop->erLspId.lspid, erHop->erLspId.routerId);
5381 break;
5383 default:
5385 LDP_TRACE_OUT(handle, "UNKNWON ER HOP type = %d\n", type);
5390 void printErTlv(mpls_instance_handle handle, mplsLdpErTlv_t * erTlv)
5392 u_short i;
5394 LDP_TRACE_OUT(handle, "\t erTlv:\n");
5395 printTlv(handle, &(erTlv->baseTlv));
5396 LDP_TRACE_OUT(handle, "\t erTlv has %d ErHops\n", erTlv->numberErHops);
5397 for (i = 0; i < erTlv->numberErHops; i++) {
5398 LDP_TRACE_OUT(handle, "\tTYPE[%i] = %x\n", i, erTlv->erHopTypes[i]);
5399 printErHop(handle, &(erTlv->erHopArray[i]), erTlv->erHopTypes[i]);
5403 void printTrafficFlags(mpls_instance_handle handle,
5404 mplsLdpTrafficFlag_t * traflag)
5406 LDP_TRACE_OUT(handle,
5407 "\t\tTraffic flags: res = %d, F6 = %d, F5 = %d, F4 = %d, F3 = %d, F2 = %d, F1 = %d\n",
5408 traflag->res, traflag->f6Bit, traflag->f5Bit, traflag->f4Bit,
5409 traflag->f3Bit, traflag->f2Bit, traflag->f1Bit);
5412 void printTrafficTlv(mpls_instance_handle handle,
5413 mplsLdpTrafficTlv_t * trafficTlv)
5415 LDP_TRACE_OUT(handle, "\t trafficTlv:\n");
5416 printTlv(handle, &(trafficTlv->baseTlv));
5417 printTrafficFlags(handle, &(trafficTlv->flags.flags));
5418 LDP_TRACE_OUT(handle,
5419 "\t trafficTlv data: freq = %d, res = %d, weight = %d\n", trafficTlv->freq,
5420 trafficTlv->res, trafficTlv->weight);
5421 LDP_TRACE_OUT(handle, "\t trafficTlv param: \n");
5422 LDP_TRACE_OUT(handle, "\t\t PDR = %f (%x)\n", trafficTlv->pdr.pdr,
5423 *(u_int *) & (trafficTlv->pdr.pdr));
5424 LDP_TRACE_OUT(handle, "\t\t PBS = %f (%x)\n", trafficTlv->pbs.pbs,
5425 *(u_int *) & (trafficTlv->pbs.pbs));
5426 LDP_TRACE_OUT(handle, "\t\t CDR = %f (%x)\n", trafficTlv->cdr.cdr,
5427 *(u_int *) & (trafficTlv->cdr.cdr));
5428 LDP_TRACE_OUT(handle, "\t\t CBS = %f (%x)\n", trafficTlv->cbs.cbs,
5429 *(u_int *) & (trafficTlv->cbs.cbs));
5430 LDP_TRACE_OUT(handle, "\t\t EBS = %f (%x)\n", trafficTlv->ebs.ebs,
5431 *(u_int *) & (trafficTlv->ebs.ebs));
5434 void printLlbReqMsg(mpls_instance_handle handle, mplsLdpLblReqMsg_t * lblReqMsg)
5436 LDP_TRACE_OUT(handle, "LABEL REQUEST MSG ***START***:\n");
5437 printMsgBase(handle, &(lblReqMsg->baseMsg));
5439 if (lblReqMsg->fecTlvExists) {
5440 printFecListTlv(handle, &(lblReqMsg->fecTlv));
5441 } else {
5442 LDP_TRACE_OUT(handle, "\tLabel request msg does not have fec Tlv\n");
5444 if (lblReqMsg->hopCountTlvExists) {
5445 printHopTlv(handle, &(lblReqMsg->hopCountTlv));
5446 } else {
5447 LDP_TRACE_OUT(handle, "\tLabel request msg does not have hop count Tlv\n");
5449 if (lblReqMsg->pathVecTlvExists) {
5450 printPathVecTlv(handle, &(lblReqMsg->pathVecTlv));
5451 } else {
5452 LDP_TRACE_OUT(handle,
5453 "\tLabel request msg does not have path vector Tlv\n");
5455 if (lblReqMsg->lblMsgIdTlvExists) {
5456 printTlv(handle, &(lblReqMsg->lblMsgIdTlv.baseTlv));
5457 } else {
5458 LDP_TRACE_OUT(handle,
5459 "\tLabel request msg does not have return msgId Tlv\n");
5461 if (lblReqMsg->erTlvExists) {
5462 printErTlv(handle, &(lblReqMsg->erTlv));
5463 } else {
5464 LDP_TRACE_OUT(handle, "\tLabel request msg does not have cr Tlv\n");
5466 if (lblReqMsg->trafficTlvExists) {
5467 printTrafficTlv(handle, &(lblReqMsg->trafficTlv));
5468 } else {
5469 LDP_TRACE_OUT(handle, "\tLabel request msg does not have traffic Tlv\n");
5471 if (lblReqMsg->lspidTlvExists) {
5472 printLspIdTlv(handle, &(lblReqMsg->lspidTlv));
5473 } else {
5474 LDP_TRACE_OUT(handle, "\tLabel request msg does not have LSPID Tlv\n");
5476 if (lblReqMsg->pinningTlvExists) {
5477 printPinningTlv(handle, &(lblReqMsg->pinningTlv));
5478 } else {
5479 LDP_TRACE_OUT(handle, "\tLabel request msg does not have Pinning Tlv\n");
5481 if (lblReqMsg->recClassTlvExists) {
5482 printResClsTlv(handle, &(lblReqMsg->resClassTlv));
5483 } else {
5484 LDP_TRACE_OUT(handle, "\tLabel request msg does not have ResClass Tlv\n");
5486 if (lblReqMsg->preemptTlvExists) {
5487 printPreemptTlv(handle, &(lblReqMsg->preemptTlv));
5488 } else {
5489 LDP_TRACE_OUT(handle, "\tLabel request msg does not have Preempt Tlv\n");
5491 LDP_TRACE_OUT(handle, "LABEL REQUEST MSG ***END***:\n");
5494 void printLbl_W_R_Msg(mpls_instance_handle handle, mplsLdpLbl_W_R_Msg_t * msg)
5496 if (msg->baseMsg.flags.flags.msgType == MPLS_LBLWITH_MSGTYPE) {
5497 LDP_TRACE_OUT(handle, "LABEL WITHDRAW MSG ***START***:\n");
5498 } else if (msg->baseMsg.flags.flags.msgType == MPLS_LBLREL_MSGTYPE) {
5499 LDP_TRACE_OUT(handle, "LABEL RELEASE MSG ***START***:\n");
5501 printMsgBase(handle, &(msg->baseMsg));
5503 if (msg->fecTlvExists) {
5504 printFecListTlv(handle, &(msg->fecTlv));
5505 } else {
5506 LDP_TRACE_OUT(handle, "\tLabel msg does not have fec Tlv\n");
5508 if (msg->genLblTlvExists) {
5509 printGenLblTlv(handle, &(msg->genLblTlv));
5510 } else {
5511 LDP_TRACE_OUT(handle, "\tLabel msg does not have gen Tlv\n");
5513 if (msg->atmLblTlvExists) {
5514 printAtmLblTlv(handle, &(msg->atmLblTlv));
5515 } else {
5516 LDP_TRACE_OUT(handle, "\tLabel msg does not have atm Tlv\n");
5518 if (msg->frLblTlvExists) {
5519 printFrLblTlv(handle, &(msg->frLblTlv));
5520 } else {
5521 LDP_TRACE_OUT(handle, "\tLabel msg does not have fr Tlv\n");
5523 if (msg->lspidTlvExists) {
5524 printLspIdTlv(handle, &(msg->lspidTlv));
5525 } else {
5526 LDP_TRACE_OUT(handle, "\tLabel msg does not have LSPID Tlv\n");
5528 if (msg->baseMsg.flags.flags.msgType == MPLS_LBLWITH_MSGTYPE) {
5529 LDP_TRACE_OUT(handle, "LABEL WITHDRAW MSG *** END ***:\n");
5530 } else if (msg->baseMsg.flags.flags.msgType == MPLS_LBLREL_MSGTYPE) {
5531 LDP_TRACE_OUT(handle, "LABEL RELEASE MSG *** END ***:\n");
5535 void printPreemptTlv(mpls_instance_handle handle,
5536 mplsLdpPreemptTlv_t * preemptTlv)
5538 LDP_TRACE_OUT(handle, "\t preemptTlv:\n");
5539 printTlv(handle, &(preemptTlv->baseTlv));
5540 LDP_TRACE_OUT(handle,
5541 "\t preemptTlv data: setPrio = %d, holdPrio = %d, res = %d\n",
5542 preemptTlv->setPrio, preemptTlv->holdPrio, preemptTlv->res);
5545 void printResClsTlv(mpls_instance_handle handle, mplsLdpResClsTlv_t * tlv)
5547 LDP_TRACE_OUT(handle, "\t resClsTlv:\n");
5548 printTlv(handle, &(tlv->baseTlv));
5549 LDP_TRACE_OUT(handle, "\t resClsTlv data: rsCls = %x\n", tlv->rsCls);
5552 void printLspIdTlv(mpls_instance_handle handle, mplsLdpLspIdTlv_t * tlv)
5554 LDP_TRACE_OUT(handle, "\t lspIdTlv:\n");
5555 printTlv(handle, &(tlv->baseTlv));
5556 LDP_TRACE_OUT(handle,
5557 "\t lspIdTlv data: res = %d, localCrlspId = %d, routerId = %x\n", tlv->res,
5558 tlv->localCrlspId, tlv->routerId);
5561 void printPinningTlv(mpls_instance_handle handle, mplsLdpPinningTlv_t * tlv)
5563 LDP_TRACE_OUT(handle, "\t pinningTlv:\n");
5564 printTlv(handle, &(tlv->baseTlv));
5565 LDP_TRACE_OUT(handle, "\t pinningTlv data: pBit = %d, res = %d\n",
5566 tlv->flags.flags.pBit, tlv->flags.flags.res);
5569 void printLlbAbortMsg(mpls_instance_handle handle, mplsLdpLblAbortMsg_t * lblMsg)
5571 LDP_TRACE_OUT(handle, "LABEL ABORT MSG ***START***:\n");
5572 printMsgBase(handle, &(lblMsg->baseMsg));
5574 if (lblMsg->fecTlvExists) {
5575 printFecListTlv(handle, &(lblMsg->fecTlv));
5576 } else {
5577 LDP_TRACE_OUT(handle, "\tLabel abort msg does not have fec Tlv\n");
5579 if (lblMsg->lblMsgIdTlvExists) {
5580 printLblMsgIdTlv(handle, &(lblMsg->lblMsgIdTlv));
5581 } else {
5582 LDP_TRACE_OUT(handle,
5583 "\tLabel abort msg does not have label messageId Tlv\n");
5585 LDP_TRACE_OUT(handle, "LABEL ABORT MSG ***END***:\n");
5589 * Routine to convert hex string to ascii string
5592 int converHexToAscii(u_char * buffHex, int buffHexLen, u_char * buffAscii)
5594 /* convert the hexEncrypP hex string to a char sting */
5595 int i = 0;
5596 int j = 0;
5597 char c, c1;
5598 u_char *p = buffHex;
5599 u_char *q = buffAscii;
5601 for (i = 0; i < buffHexLen; i += 2, j++) {
5602 c = *p;
5603 p++;
5604 c1 = *p;
5605 p++;
5606 if (c >= '0' && c <= '9')
5607 c -= '0';
5608 else if (c >= 'A' && c <= 'F')
5609 c -= 'A' - 0xa;
5610 else if (c >= 'a' && c <= 'f')
5611 c -= 'a' - 0xa;
5612 else
5613 return 0;
5614 if (c1 >= '0' && c1 <= '9')
5615 c1 -= '0';
5616 else if (c1 >= 'A' && c1 <= 'F')
5617 c1 -= 'A' - 0xa;
5618 else if (c1 >= 'a' && c1 <= 'f')
5619 c1 -= 'a' - 0xa;
5620 else
5621 return 0;
5623 *q++ = (c << 4) + (c1 & 0x0f);
5625 return j;
5627 } /* End : converHexToAscii */
5630 * Routine to convert ascii string to hex string
5632 int converAsciiToHex(u_char * buffHex, int buffAsciiLen, u_char * buffAscii)
5634 int i;
5635 u_char *p2 = buffHex;
5636 u_char *p1 = buffAscii;
5637 u_char buf[3];
5639 for (i = 0; i < buffAsciiLen; i++) {
5640 memset(buf, 0, 3);
5641 sprintf((char *)buf, "%02x", *p1++);
5642 memcpy(p2, buf, 2);
5643 p2 += strlen((char *)buf);
5645 *p2 = '\0';
5647 p2 = buffHex;
5648 for (i = 0; i < 2 * buffAsciiLen; i++) {
5649 PRINT_OUT("%c", *p2++);
5651 PRINT_OUT("\n");
5652 return i;
5654 } /* End : converAsciiToHex */
5656 /*****************************************************************************
5657 * This section includes one example of hex buffers which contains encoding *
5658 * for a pdu header and a request message. *
5660 * the hex buffer for the request message contains (where q represents the end*
5661 * of the buffer): *
5663 * 0001009AC00003050002040100900000000806010000010000100200011B00194059030001 *
5664 * 042F7D308308210008000000013A1D65030800003008040008000000193A1D651708040008 *
5665 * 800000053A1D6503080400088000000F3A1D650D08040008000000233A1D65210810001824 *
5666 * 01000040e3333342c8000040e00000447a0000412000000823000480000000082200040ABC *
5667 * DEFF0820000407070000q *
5669 * Make sure that when copy and paste the buffer, there are no new line chars *
5670 * or blanks. *
5671 * When decoding the above buffer, the following debug output should show if *
5672 * the debug flag is defined and set: *
5674 *LPD Header : protocolVersion = 1 *
5675 * pduLength = 154 *
5676 * lsrAddress = c0000305 *
5677 * labelSpace = 2 *
5679 *LABEL REQUEST MSG ***START***: *
5680 * baseMsg : msgType = 401 *
5681 * msgLength = 144 *
5682 * msgId = 8 *
5683 * fecTlv: *
5684 * Tlv: *
5685 * BaseTlv: type = 100 *
5686 * length = 16 *
5687 * uBit = 0 *
5688 * fBit = 0 *
5689 * fecTlv->numberFecElements = 2 *
5690 * elem 0 type is 2 *
5691 * Fec Element : type = 2, addFam = 1, preLen = 27, *
5692 * address = 194059 *
5693 * elem 1 type is 3 *
5694 * Fec Element : type = 3, addFam = 1, preLen = 4, *
5695 * address = 2f7d3083 *
5696 * *
5697 * fecTlv.wcElemExists = 0 *
5698 * Label request msg does not have cos label Tlv *
5699 * Label request msg does not have hop count Tlv *
5700 * Label request msg does not have path vector Tlv *
5701 * Tlv: *
5702 * BaseTlv: type = 601 *
5703 * length = 0 *
5704 * uBit = 0 *
5705 * fBit = 0 *
5706 * erTlv: *
5707 * Tlv: *
5708 * BaseTlv: type = 800 *
5709 * length = 48 *
5710 * uBit = 0 *
5711 * fBit = 0 *
5712 * erTlv has 4 ErHops *
5713 * TYPE[0] = 804 *
5714 * ER FLAGS: l = 0, res = 0 *
5715 * LSPID: lspid = 25, routerId = 975004951 *
5716 * TYPE[1] = 804 *
5717 * ER FLAGS: l = 1, res = 0 *
5718 * LSPID: lspid = 5, routerId = 975004931 *
5719 * TYPE[2] = 804 *
5720 * ER FLAGS: l = 1, res = 0 *
5721 * LSPID: lspid = 15, routerId = 975004941 *
5722 * TYPE[3] = 804 *
5723 * ER FLAGS: l = 0, res = 0 *
5724 * LSPID: lspid = 35, routerId = 975004961 *
5725 * trafficTlv: *
5726 * Tlv: *
5727 * BaseTlv: type = 810 *
5728 * length = 24 *
5729 * uBit = 0 *
5730 * fBit = 0 *
5731 * Traffic flags: res = 0, F6 = 1, F5 = 0, F4 = 0, F3 = 1, *
5732 * F2 = 0, F1 = 0 *
5733 * trafficTlv data: freq = 1, res = 0, weight = 0 *
5734 * trafficTlv param: *
5735 * PDR = 7.1(40e33333) *
5736 * PBS = 100.0(42c80000) *
5737 * CDR = 7.0(40e00000) *
5738 * CBS = 1000.0(447a0000) *
5739 * EBS = 10.0(41200000) *
5740 * lspIdTlv: *
5741 * Tlv: *
5742 * BaseTlv: type = 821 *
5743 * length = 8 *
5744 * uBit = 0 *
5745 * fBit = 0 *
5746 * lspIdTlv data: res = 0, localCrlspId = 1, routerId = 3a1d6503 *
5747 * pinningTlv: *
5748 * Tlv: *
5749 * BaseTlv: type = 823 *
5750 * length = 4 *
5751 * uBit = 0 *
5752 * fBit = 0 *
5753 * pinningTlv data: pBit = 1, res = 0 *
5754 * resClsTlv: *
5755 * Tlv: *
5756 * BaseTlv: type = 822 *
5757 * length = 4 *
5758 * uBit = 0 *
5759 * fBit = 0 *
5760 * resClsTlv data: rsCls = abcdeff *
5761 * preemptTlv: *
5762 * Tlv: *
5763 * BaseTlv: type = 820 *
5764 * length = 4 *
5765 * uBit = 0 *
5766 * fBit = 0 *
5767 * preemptTlv data: setPrio = 7, holdPrio = 7, res = 0 *
5768 *LABEL REQUEST MSG ***END***: *
5769 *****************************************************************************/