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

📄 asn1.c

📁 snmp up 2
💻 C
📖 第 1 页 / 共 5 页
字号:
/* * Abstract Syntax Notation One, ASN.1 * As defined in ISO/IS 8824 and ISO/IS 8825 * This implements a subset of the above International Standards that * is sufficient to implement SNMP. * * Encodes abstract data types into a machine independent stream of bytes. * *//**********************************************************************	Copyright 1988, 1989, 1991, 1992 by Carnegie Mellon University                      All Rights ReservedPermission to use, copy, modify, and distribute this software and its documentation for any purpose and without fee is hereby granted, provided that the above copyright notice appear in all copies and thatboth that copyright notice and this permission notice appear in supporting documentation, and that the name of CMU not beused in advertising or publicity pertaining to distribution of thesoftware without specific, written prior permission.  CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDINGALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALLCMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES ORANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THISSOFTWARE.******************************************************************/#include <net-snmp/net-snmp-config.h>#ifdef KINETICS#include "gw.h"#endif#if HAVE_STRING_H#include <string.h>#else#include <strings.h>#endif#include <sys/types.h>#include <stdio.h>#ifdef HAVE_STDLIB_H#include <stdlib.h>#endif#if HAVE_WINSOCK_H#include <winsock.h>#endif#if HAVE_NETINET_IN_H#include <netinet/in.h>#endif#ifdef vms#include <in.h>#endif#if HAVE_DMALLOC_H#include <dmalloc.h>#endif#include <net-snmp/output_api.h>#include <net-snmp/utilities.h>#include <net-snmp/library/asn1.h>#include <net-snmp/library/int64.h>#include <net-snmp/library/mib.h>#ifndef NULL#define NULL	0#endif#include <net-snmp/library/snmp_api.h>static    void_asn_size_err(const char *str, size_t wrongsize, size_t rightsize){    char            ebuf[128];    snprintf(ebuf, sizeof(ebuf),            "%s size %d: s/b %d", str, wrongsize, rightsize);    ebuf[ sizeof(ebuf)-1 ] = 0;    ERROR_MSG(ebuf);}static    void_asn_length_err(const char *str, size_t wrongsize, size_t rightsize){    char            ebuf[128];    snprintf(ebuf, sizeof(ebuf),            "%s length %d too large: exceeds %d", str, wrongsize,            rightsize);    ebuf[ sizeof(ebuf)-1 ] = 0;    ERROR_MSG(ebuf);}/* * call after asn_parse_length to verify result. */static    int_asn_parse_length_check(const char *str,                        u_char * bufp, u_char * data,                        u_long plen, size_t dlen){    char            ebuf[128];    size_t          header_len;    if (bufp == NULL) {        /*         * error message is set          */        return 1;    }    header_len = bufp - data;    if (plen > 0x7fffffff || header_len > 0x7fffffff ||        ((size_t) plen + header_len) > dlen) {        snprintf(ebuf, sizeof(ebuf),                "%s: message overflow: %d len + %d delta > %d len",                str, (int) plen, (int) header_len, (int) dlen);        ebuf[ sizeof(ebuf)-1 ] = 0;        ERROR_MSG(ebuf);        return 1;    }    return 0;}/* * call after asn_build_header to verify result. */static    int_asn_build_header_check(const char *str, u_char * data,                        size_t datalen, size_t typedlen){    char            ebuf[128];    if (data == NULL) {        /*         * error message is set          */        return 1;    }    if (datalen < typedlen) {        snprintf(ebuf, sizeof(ebuf),                "%s: bad header, length too short: %d < %d", str,                datalen, typedlen);        ebuf[ sizeof(ebuf)-1 ] = 0;        ERROR_MSG(ebuf);        return 1;    }    return 0;}static    int_asn_realloc_build_header_check(const char *str,                                u_char ** pkt, size_t * pkt_len,                                size_t typedlen){    char            ebuf[128];    if (pkt == NULL || *pkt == NULL) {        /*         * Error message is set.           */        return 1;    }    if (*pkt_len < typedlen) {        snprintf(ebuf, sizeof(ebuf),                "%s: bad header, length too short: %d < %d", str,                *pkt_len, typedlen);        ebuf[ sizeof(ebuf)-1 ] = 0;        ERROR_MSG(ebuf);        return 1;    }    return 0;}/* * checks the incoming packet for validity and returns its size or 0  */intasn_check_packet(u_char * pkt, size_t len){    u_long          asn_length;    if (len < 2)        return 0;               /* always too short */    if (*pkt != (u_char) (ASN_SEQUENCE | ASN_CONSTRUCTOR))        return -1;              /* wrong type */    if (*(pkt + 1) & 0x80) {        /*         * long length          */        if ((int) len < (int) (*(pkt + 1) & ~0x80) + 2)            return 0;           /* still to short, incomplete length */        asn_parse_length(pkt + 1, &asn_length);        return (asn_length + 2 + (*(pkt + 1) & ~0x80));    } else {        /*         * short length          */        return (*(pkt + 1) + 2);    }}static    int_asn_bitstring_check(const char *str, size_t asn_length, u_char datum){    char            ebuf[128];    if (asn_length < 1) {        snprintf(ebuf, sizeof(ebuf),                "%s: length %d too small", str, (int) asn_length);        ebuf[ sizeof(ebuf)-1 ] = 0;        ERROR_MSG(ebuf);        return 1;    }    /*     * if (datum > 7){     * sprintf(ebuf,"%s: datum %d >7: too large", str, (int)(datum));     * ERROR_MSG(ebuf);     * return 1;     * }     */    return 0;}/* * asn_parse_int - pulls a long out of an int type. *  On entry, datalength is input as the number of valid bytes following *   "data".  On exit, it is returned as the number of valid bytes *   following the end of this object. * *  Returns a pointer to the first byte past the end *   of this object (i.e. the start of the next object). *  Returns NULL on any error.  u_char * asn_parse_int( u_char     *data         IN - pointer to start of object int        *datalength   IN/OUT - number of valid bytes left in buffer u_char     *type         OUT - asn type of object long       *intp         IN/OUT - pointer to start of output buffer int         intsize      IN - size of output buffer */u_char         *asn_parse_int(u_char * data,              size_t * datalength,              u_char * type, long *intp, size_t intsize){    /*     * ASN.1 integer ::= 0x02 asnlength byte {byte}*     */    static const char *errpre = "parse int";    register u_char *bufp = data;    u_long          asn_length;    register long   value = 0;    if (intsize != sizeof(long)) {        _asn_size_err(errpre, intsize, sizeof(long));        return NULL;    }    *type = *bufp++;    bufp = asn_parse_length(bufp, &asn_length);    if (_asn_parse_length_check        (errpre, bufp, data, asn_length, *datalength))        return NULL;    if ((size_t) asn_length > intsize) {        _asn_length_err(errpre, (size_t) asn_length, intsize);        return NULL;    }    *datalength -= (int) asn_length + (bufp - data);    if (*bufp & 0x80)        value = -1;             /* integer is negative */    DEBUGDUMPSETUP("recv", data, bufp - data + asn_length);    while (asn_length--)        value = (value << 8) | *bufp++;    DEBUGMSG(("dumpv_recv", "  Integer:\t%ld (0x%.2X)\n", value, value));    *intp = value;    return bufp;}/* * asn_parse_unsigned_int - pulls an unsigned long out of an ASN int type. *  On entry, datalength is input as the number of valid bytes following *   "data".  On exit, it is returned as the number of valid bytes *   following the end of this object. * *  Returns a pointer to the first byte past the end *   of this object (i.e. the start of the next object). *  Returns NULL on any error.  u_char * asn_parse_unsigned_int( u_char     *data         IN - pointer to start of object int        *datalength   IN/OUT - number of valid bytes left in buffer u_char     *type         OUT - asn type of object u_long     *intp         IN/OUT - pointer to start of output buffer int         intsize      IN - size of output buffer */u_char         *asn_parse_unsigned_int(u_char * data,                       size_t * datalength,                       u_char * type, u_long * intp, size_t intsize){    /*     * ASN.1 integer ::= 0x02 asnlength byte {byte}*     */    static const char *errpre = "parse uint";    register u_char *bufp = data;    u_long          asn_length;    register u_long value = 0;    if (intsize != sizeof(long)) {        _asn_size_err(errpre, intsize, sizeof(long));        return NULL;    }    *type = *bufp++;    bufp = asn_parse_length(bufp, &asn_length);    if (_asn_parse_length_check        (errpre, bufp, data, asn_length, *datalength))        return NULL;    if (((int) asn_length > (intsize + 1)) ||        (((int) asn_length == intsize + 1) && *bufp != 0x00)) {        _asn_length_err(errpre, (size_t) asn_length, intsize);        return NULL;    }    *datalength -= (int) asn_length + (bufp - data);    if (*bufp & 0x80)        value = ~value;         /* integer is negative */    DEBUGDUMPSETUP("recv", data, bufp - data + asn_length);    while (asn_length--)        value = (value << 8) | *bufp++;    DEBUGMSG(("dumpv_recv", "  UInteger:\t%ld (0x%.2X)\n", value, value));    *intp = value;    return bufp;}/* * asn_build_int - builds an ASN object containing an integer. *  On entry, datalength is input as the number of valid bytes following *   "data".  On exit, it is returned as the number of valid bytes *   following the end of this object. * *  Returns a pointer to the first byte past the end *   of this object (i.e. the start of the next object). *  Returns NULL on any error.  u_char * asn_build_int( u_char     *data         IN - pointer to start of output buffer int        *datalength   IN/OUT - number of valid bytes left in buffer int         type         IN  - asn type of object long       *intp         IN - pointer to start of long integer int         intsize      IN - size of input buffer */u_char         *asn_build_int(u_char * data,              size_t * datalength, u_char type, long *intp, size_t intsize){    /*     * ASN.1 integer ::= 0x02 asnlength byte {byte}*     */    static const char *errpre = "build int";    register long   integer;    register u_long mask;#ifndef SNMP_NO_DEBUGGING    u_char         *initdatap = data;#endif    if (intsize != sizeof(long)) {        _asn_size_err(errpre, intsize, sizeof(long));        return NULL;    }    integer = *intp;    /*     * Truncate "unnecessary" bytes off of the most significant end of this     * 2's complement integer.  There should be no sequence of 9     * consecutive 1's or 0's at the most significant end of the     * integer.     */    mask = ((u_long) 0x1FF) << ((8 * (sizeof(long) - 1)) - 1);    /*     * mask is 0xFF800000 on a big-endian machine      */    while ((((integer & mask) == 0) || ((integer & mask) == mask))           && intsize > 1) {        intsize--;        integer <<= 8;    }    data = asn_build_header(data, datalength, type, intsize);    if (_asn_build_header_check(errpre, data, *datalength, intsize))        return NULL;    *datalength -= intsize;    mask = ((u_long) 0xFF) << (8 * (sizeof(long) - 1));    /*     * mask is 0xFF000000 on a big-endian machine      */    while (intsize--) {        *data++ = (u_char) ((integer & mask) >> (8 * (sizeof(long) - 1)));        integer <<= 8;    }    DEBUGDUMPSETUP("send", initdatap, data - initdatap);    DEBUGMSG(("dumpv_send", "  Integer:\t%ld (0x%.2X)\n", *intp, *intp));    return data;

⌨️ 快捷键说明

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