⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mpls_encdec_02.c

📁 Linux平台下
💻 C
📖 第 1 页 / 共 5 页
字号:
/*******************************************************************************                       Nortel Networks Software License                      **                                                                             ** READ THE TERMS OF THIS LICENSE CAREFULLY.  BY USING, MODIFYING, OR          ** DISTRIBUTING THIS SOFTWARE AND ANY ACCOMPANYING DOCUMENTATION (COLLECTIVELY,** "SOFTWARE") YOU ARE AGREEING TO ALL OF THE TERMS OF THIS LICENSE.           **                                                                             ** 1.      Nortel Telecom Limited, on behalf of itself and its subsidiaries    ** (collectively "Nortel Networks") grants to you a non-exclusive, perpetual,  ** world-wide right to use, copy, modify, and distribute the Software at no    ** charge.                                                                     **                                                                             ** 2.      You may sublicense recipients of redistributed Software to use,     ** copy, modify, and distribute the Software on substantially the same terms as** this License.  You may not impose any further restrictions on the           ** recipient's exercise of the rights in the Software granted under this       ** License.  Software distributed to other parties must be accompanied by a    ** License containing a grant, disclaimer and limitation of liability          ** substantially in the form of 3, 4, and 5 below provided that references to  ** "Nortel Networks" may be changed to "Supplier".                             **                                                                             ** 3.      Nortel Networks reserves the right to modify and release new        ** versions of the Software from time to time which may include modifications  ** made by third parties like you. Accordingly, you agree that you shall       ** automatically grant a license to Nortel Networks to include, at its option, ** in any new version of the Software any modifications to the Software made by** you and made available directly or indirectly to Nortel Networks.  Nortel   ** Networks shall have the right to use, copy, modify, and distribute any such ** modified Software on substantially the same terms as this License.          **                                                                             ** 4.      THE SOFTWARE IS PROVIDED ON AN "AS IS" BASIS.  NORTEL NETWORKS AND  ** ITS AGENTS AND SUPPLIERS DISCLAIM ALL REPRESENTATIONS, WARRANTIES AND       ** CONDITIONS RELATING TO THE SOFTWARE, INCLUDING, BUT NOT LIMITED TO, IMPLIED ** WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND         ** NON-INFRINGEMENT OF THIRD-PARTY INTELLECTUAL PROPERTY RIGHTS.  NORTEL       ** NETWORKS AND ITS AGENTS AND SUPPLIERS DO NOT WARRANT, GUARANTEE, OR MAKE ANY** REPRESENTATIONS REGARDING THE USE, OR THE RESULTS OF THE USE, OF THE        ** SOFTWARE IN TERMS OR CORRECTNESS, ACCURACY, RELIABILITY, CURRENTNESS, OR    ** OTHERWISE.                                                                  **                                                                             ** 5.      NEITHER NORTEL NETWORKS NOR ANY OF ITS AGENTS OR SUPPLIERS SHALL BE ** LIABLE FOR ANY DIRECT, INDIRECT, CONSEQUENTIAL, INCIDENTAL OR EXEMPLARY     ** DAMAGES, OR ECONOMIC LOSSES (INCLUDING DAMAGES FOR LOSS OF BUSINESS PROFITS,** BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION AND THE LIKE), ARISING  ** FROM THE SOFTWARE OR THIS LICENSE AGREEMENT, EVEN IF NORTEL NETWORKS OR SUCH** AGENT OR SUPPLIER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES OR    ** LOSSES, AND WHETHER ANY SUCH DAMAGE OR LOSS ARISES OUT OF CONTRACT, TORT, OR** OTHERWISE.                                                                  **                                                                             ** 6.      This License shall be governed by the laws of the Province of       ** Ontario, Canada.                                                            ********************************************************************************//****************************************************************************** * This file contains the C implementation for encode/decode functions        *  * for the following types of messages: notification, hello, initialization,  * * keepAlive, address, address Withdraw, label Mapping, label Request, label  * * Withdraw and label Release. There are also encode/decode methods for all   *  * tlv types required by the previously enumerated messages.                  *  * Please remember that the pdu will always contain the header followed by 1  * * or more LDP messages. The file contains functions to encode/decode the LDP * * header as well.  							      *  * All the messages, header message and the tlvs are in conformity with the   *  * draft-ietf-mpls-ldp-04  (May 1999) and with draft-ietf-mpls-cr-ldp-01      * * (Jan 1999). 								      *  *								              * * Please note that the U bit in the message and the F bit in the tlv are     * * ignored in this version of the code.                                       * *								              * * Please note that the traffic parameters for traffic TLV have to be IEEE    * * single precision floating point numbers.                                   * *								              * * Please note that there might be a small chance for bit field manipulation  * * portability inconsistency. If such problems occure, the code requires      * * changes for a suitable bit-field manipulation. The code for encoding and   * * decoding makes the assumption that the compiler packs the bit fields in a  * * structure into adjacent bits of the same unit.                             *  *								              * * The usage of the encode/decode functions is described below.               *  *								              * * The encode functions take as arguments: a pointer to the structure which   * * implements msg/tlv, a nbuffer (where the encoding is done) and the buffer   * * length.							              * * If the encode is successfull, the function returns the total encoded       *  * length.								      * * If the encode fails, the function returns an error code.                   * * The encode functions for messages and message headers do not modify the    * * content of the struct which is to be encoded. All the other encode         * * functions will change the content of the structure. The pointer which      * * points to the beginning of the buffer is not changed.                      * *									      * * The decode functions take as arguments: a pointer to the structure which   * * is going to be populated after decoding, a pointer to a buffer and the     * * buffer length.							      * * If the decode is successful, the function returns the total decoded length * * If the decode fails, the function returns an error code. The decode        * * functions do not modify the pointer to the buffer which contains the data  * * to be decoded.							      * *									      * * Example on how to use the encode/decode functions for a keepAlive message: * *									      * *           Encode the keep alive message:                                   *  *           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^    				      * *           u_char buffer[500];	 				      * *           int returnCode; 						      * *           struct mplsLdpKeepAlMsg_s keepAliveMsg;            	      * *           keepAliveMsg.baseMsg.msgType   = MPLS_KEEPAL_MSGTYPE;            * *           keepAliveMsg.baseMsg.msgLength = MPLS_MSGIDFIXLEN;               * *           keepAliveMsg.baseMsg.msgId     = 123;		              * *           bzero(buffer, 500);                                  	      * *           returnCode = Mpls_encodeLdpKeepAliveMsg(&keepAliveMsg,           * *                                                   buffer,                  * *                                                   500);                    * *           if (returnCode < 0)                                              * *              check the error code				              * *           else                                                             * *              write(fd, buffer, returnCode);                                * *									      * *									      * *           Decode the keep alive meesage:                                   * *           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^		                      * *           u_char buffer[500];					      * *           int returnCode;					              * *           struct mplsLdpKeepAlMsg_s keepAliveMsg;	            	      * *           read(fd, buffer, length);                                        * *           returnCode =  Mpls_decodeLdpKeepAliveMsg(&keepAliveMsg,          *  *                                                    buffer,                 * *                                                    500); 		      * *           if (returnCode < 0)	                                      * *              check the error code					      * *           else				 			      * *           { 								      * *              printKeepAliveMsg(&keepAliveMsg);	 	              * *           } 						                      * *								              * * An example on how to use the decode functions for the header and the       * * messages can be found in the main function.                                * *								              * * The code was tested for big endian and little endian for sparc5, linux     * * and i960.                                                                  * *								              * * In order to compile for little endian, the LITTLE_ENDIAN_BYTE_ORDER should * * be defined.								      * *								              * * At the end of this file there is an examples of a hex buffers and its      * * corresponding values.                                                      * *								              * *								              * * Version History                                                            * * Version          Date      Authors            Description                  * * ===========      ========  =========          ======================       * * mpls_encdec_01.c 99/03/15  Antonela Paraschiv draft-ietf-mpls-ldp-03 and   *  *                                               draft-ietf-mpls-cr-ldp-01    * *								              * * mpls_encdec_02.c 99/05/19  Antonela Paraschiv draft-ietf-mpls-ldp-04 and   *  *                                               draft-ietf-mpls-cr-ldp-01    * *								              * ******************************************************************************/#define LITTLE_ENDIAN_BYTE_ORDER 1  /* <----- Please define this accordingly *//* #define VXWORKS                  1 */ #include <stdio.h>#include <stdlib.h>#include <string.h>#ifdef VXWORKS   #include <in.h>         /* htons, htonl, ntohs, ntohl         */   #include <types.h>      /* u_int, u_char, u_short, float etc. */#else   #include <netinet/in.h> /* htons, htonl, ntohs, ntohl         */   #include <sys/types.h>  /* u_int, u_char, u_short, float etc. */#endif VXWORKS #define MEM_COPY(X, Y, Z) memcpy(X, Y, Z)/* macros to handle different byte orders (little endian or big endian) */#ifdef LITTLE_ENDIAN_BYTE_ORDER    #define BITFIELDS_ASCENDING_2(X, Y)                Y; X;   #define BITFIELDS_ASCENDING_3(X, Y, Z)             Z; Y; X;   #define BITFIELDS_ASCENDING_4(X, Y, Z, W)          W; Z; Y; X;   #define BITFIELDS_ASCENDING_7(X, Y, Z, W, U, A, B) B; A; U; W; Z; Y; X;# else   #define BITFIELDS_ASCENDING_2(X, Y)                X; Y;   #define BITFIELDS_ASCENDING_3(X, Y, Z)             X; Y; Z;   #define BITFIELDS_ASCENDING_4(X, Y, Z, W)          X; Y; Z; W;   #define BITFIELDS_ASCENDING_7(X, Y, Z, W, U, A, B) X; Y; Z; W; U; A; B;#endif LITTLE_ENDIAN_BYTE_ORDER /* macros used to decode the entire LDP; they declare local var for   different type of messages */#define MPLS_MSGSTRUCT( e )                 \   mplsLdp ## e ## Msg_t test ## e ## Msg;  \#define MPLS_MSGPARAM( e )                  \   test ## e ## Msg                         \#define DEBUG_INFO              1  /* set this to 0 if no debug info 				      required                      *//* debug macros */#define DEBUG_CALL(X)           if (DEBUG_INFO) X;#define PRINT_2(X, Y)           if (DEBUG_INFO) printf(X, Y)#define PRINT_4(X, Y, Z, W)     if (DEBUG_INFO) printf(X, Y, Z, W)#define PRINT_ERR(X)            if (DEBUG_INFO) printf(X)#define PRINT_ERR_2(X, Y)       if (DEBUG_INFO) printf(X, Y)#define PRINT_ERR_4(X, Y, Z, W) if (DEBUG_INFO) printf(X, Y, Z, W)/* *    MESSAGE TYPE CONSTANS & TLV CONSTANTS */#define MPLS_LDP_HDRSIZE         10      /* the size for mpls ldp hdr  */#define MPLS_TLVFIXLEN           4       /* type + len                 */#define MPLS_MSGIDFIXLEN         4       /* type + len                 */#define MPLS_LDPIDLEN            6 #define MPLS_PDUMAXLEN           4096     /* octets                    */#define MPLS_VERSION             0x0001  #define MPLS_IPV4ADDRFAMILYN     0x0100   /* rfc 1700 (network order)  */#define MPLS_INIFINITE_TIMEVAL   0xfffff/* for initialize message */#define MPLS_INIT_MSGTYPE        0x0200  /* initialization msg          */#define MPLS_CSP_TLVTYPE         0x0500  /* common params for init msg  */#define MPLS_ASP_TLVTYPE         0x0501  /* atm session params          */#define MPLS_FSP_TLVTYPE         0x0502  /* frame relay session params  */#define MPLS_ASPFIXLEN           4       /* M + N + D + res             */#define MPLS_FSPFIXLEN           4       /* M + N + res                 */#define MPLS_CSPFIXLEN           14      /* protocolV + ... + ldp ids   */#define MPLS_ATMLBLMAXLEN        10#define MPLS_ATMLRGFIXLEN        8      #define MPLS_FRLRGFIXLEN         8#define MPLS_ASP_NOMERGE         0#define MPLS_ASP_VPMERGE         1  #define MPLS_ASP_VCMERGE         2  #define MPLS_ASP_VPVCMERGE       3  #define MPLS_FRLBLMAXLEN         10#define MPLS_FRDLCI10BITS        0#define MPLS_FRDLCI17BITS        1#define MPLS_FRDLCI23BITS        2 /* for notification message */#define MPLS_NOT_MSGTYPE         0x0001  /* notification msg            */#define MPLS_NOT_ST_TLVTYPE      0x0300  /* status tlv for not msg      */#define MPLS_NOT_ES_TLVTYPE      0x0301  /* extended status for not msg */#define MPLS_NOT_RP_TLVTYPE      0x0302  /* returned PDU for not msg    */#define MPLS_NOT_RM_TLVTYPE      0x0303  /* returned msg for not msg    */#define MPLS_STATUSFIXLEN        10      /* status code + id + type     */#define MPLS_EXSTATUSLEN         4 #define MPLS_NOT_MAXSIZE         MPLS_PDUMAXLEN - MPLS_TLVFIXLEN - \				 MPLS_MSGIDFIXLEN  /* for hello message */#define MPLS_HELLO_MSGTYPE       0x0100  /* hello msg                   */#define MPLS_CHP_TLVTYPE         0x0400  /* Common Hello Param Tlv      */#define MPLS_TRADR_TLVTYPE       0x0401  /* Transport Address Param Tlv */#define MPLS_CSN_TLVTYPE         0x0402  /* Conf Seq Number Param Tlv   */#define MPLS_CHPFIXLEN           4 #define MPLS_CSNFIXLEN           4 #define MPLS_TRADRFIXLEN         4 /* for keep alive message */#define MPLS_KEEPAL_MSGTYPE      0x0201  /* keep alive msg              *//* for address messages */#define MPLS_ADDR_MSGTYPE        0x0300  /* address msg                 */#define MPLS_ADDRWITH_MSGTYPE    0x0301  /* address withdraw msg        */#define MPLS_ADDRLIST_TLVTYPE    0x0101  /* addrss list tlv type        */#define MPLS_IPv4LEN             4#define MPLS_ADDFAMFIXLEN        2#define MPLS_ADDLISTMAXLEN       MPLS_PDUMAXLEN - 2*MPLS_TLVFIXLEN - \			         MPLS_MSGIDFIXLEN - MPLS_ADDFAMFIXLEN #define MPLS_MAXNUMBERADR        MPLS_ADDLISTMAXLEN / 4 /* for label mapping message */#define MPLS_LBLMAP_MSGTYPE      0x0400  /* label mapping msg           */#define MPLS_FEC_TLVTYPE         0x0100  /* label mapping msg           */#define MPLS_GENLBL_TLVTYPE      0x0200  /* generic label tlv           */#define MPLS_ATMLBL_TLVTYPE      0x0201  /* atm label tlv               */#define MPLS_FRLBL_TLVTYPE       0x0202  /* frame relay label tlv       */#define MPLS_HOPCOUNT_TLVTYPE    0x0103  /* ho count tlv                */#define MPLS_PATH_TLVTYPE        0x0104  /* path vector tlv             */#define MPLS_REQMSGID_TLVTYPE    0x0600  /* lbl request msg id tlv      */#define MPLS_WC_FEC              0x01    /* wildcard fec element        */#define MPLS_PREFIX_FEC          0x02    /* prefix fec element          */#define MPLS_HOSTADR_FEC         0x03    /* host addr fec element       */#define MPLS_CRLSP_FEC           0x04    /* crlsp fec element           */#define MPLS_MARTINIVC_FEC       0x80    /* Martini VC fec element      */#define MPLS_FECMAXLEN           MPLS_PDUMAXLEN - 2*MPLS_TLVFIXLEN - \			         MPLS_MSGIDFIXLEN#define MPLS_LBLFIXLEN           4       /* v + vpi + vci + res         */#define MPLS_HOPCOUNTFIXLEN      1       /* v + vpi + vci + res         */#define MPLS_VCIDLEN             4       /* Martini VC id length        */#define MPLS_VCIFPARAMMAXLEN     80      /* Martini VC Intf param max size */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -