snmp_pdu.c

来自「-」· C语言 代码 · 共 698 行 · 第 1/2 页

C
698
字号
    struct snmp_pdu *PDU){    u_char *bufp;#ifdef DEBUG_PDU_ENCODE    snmplib_debug(8, "PDU: Encoding %d\n", PDU->command);#endif    /* ASN.1 Header */    switch (PDU->command) {/**********************************************************************/    case TRP_REQ_MSG:	/* SNMPv1 Trap */	/* enterprise */	bufp = asn_build_objid(DestBuf, DestBufLen,	    (u_char) (ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_OBJECT_ID),	    (oid *) PDU->enterprise, PDU->enterprise_length);	if (bufp == NULL)	    return (NULL);	/* agent-addr */	bufp = asn_build_string(bufp, DestBufLen,	    (u_char) (SMI_IPADDRESS | ASN_PRIMITIVE),	    (u_char *) & PDU->agent_addr.sin_addr.s_addr,	    sizeof(PDU->agent_addr.sin_addr.s_addr));	if (bufp == NULL)	    return (NULL);	/* generic trap */	bufp = asn_build_int(bufp, DestBufLen,	    (u_char) (ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER),	    (int *) &PDU->trap_type, sizeof(PDU->trap_type));	if (bufp == NULL)	    return (NULL);	/* specific trap */	bufp = asn_build_int(bufp, DestBufLen,	    (u_char) (ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER),	    (int *) &PDU->specific_type,	    sizeof(PDU->specific_type));	if (bufp == NULL)	    return (NULL);	/* timestamp */	bufp = asn_build_unsigned_int(bufp, DestBufLen,	    (u_char) (SMI_TIMETICKS | ASN_PRIMITIVE),	    &PDU->time, sizeof(PDU->time));	if (bufp == NULL)	    return (NULL);	break;/**********************************************************************/    case SNMP_PDU_GETBULK:	/* SNMPv2 Bulk Request */	/* request id */	bufp = asn_build_int(DestBuf, DestBufLen,	    (u_char) (ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER),	    &PDU->reqid, sizeof(PDU->reqid));	if (bufp == NULL)	    return (NULL);	/* non-repeaters */	bufp = asn_build_int(bufp, DestBufLen,	    (u_char) (ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER),	    &PDU->non_repeaters,	    sizeof(PDU->non_repeaters));	if (bufp == NULL)	    return (NULL);	/* max-repetitions */	bufp = asn_build_int(bufp, DestBufLen,	    (u_char) (ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER),	    &PDU->max_repetitions,	    sizeof(PDU->max_repetitions));	if (bufp == NULL)	    return (NULL);	break;/**********************************************************************/    default:	/* Normal PDU Encoding */	/* request id */#ifdef DEBUG_PDU_ENCODE	snmplib_debug(8, "PDU: Request ID %d (0x%x)\n", PDU->reqid, DestBuf);#endif	bufp = asn_build_int(DestBuf, DestBufLen,	    (u_char) (ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER),	    &PDU->reqid, sizeof(PDU->reqid));	if (bufp == NULL)	    return (NULL);	/* error status */#ifdef DEBUG_PDU_ENCODE	snmplib_debug(8, "PDU: Error Status %d (0x%x)\n", PDU->errstat, bufp);#endif	bufp = asn_build_int(bufp, DestBufLen,	    (u_char) (ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER),	    &PDU->errstat, sizeof(PDU->errstat));	if (bufp == NULL)	    return (NULL);	/* error index */#ifdef DEBUG_PDU_ENCODE	snmplib_debug(8, "PDU: Error index %d (0x%x)\n", PDU->errindex, bufp);#endif	bufp = asn_build_int(bufp, DestBufLen,	    (u_char) (ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER),	    &PDU->errindex, sizeof(PDU->errindex));	if (bufp == NULL)	    return (NULL);	break;    }				/* End of encoding */    return (bufp);}/**********************************************************************//* Decodes PDU from Packet into PDU. * * Returns a pointer to the next byte of the packet, which is where the * Variable Bindings start. */u_char *snmp_pdu_decode(u_char * Packet,	/* data */    int *Length,		/* &length */    struct snmp_pdu * PDU){				/* pdu */    u_char *bufp;    u_char PDUType;    int four;    u_char ASNType;    oid objid[MAX_NAME_LEN];    bufp = asn_parse_header(Packet, Length, &PDUType);    if (bufp == NULL)	ASN_PARSE_ERROR(NULL);#ifdef DEBUG_PDU_DECODE    snmplib_debug(8, "PDU Type: %d\n", PDUType);#endif    PDU->command = PDUType;    switch (PDUType) {    case TRP_REQ_MSG:	/* SNMPv1 Trap Message */	/* enterprise */	PDU->enterprise_length = MAX_NAME_LEN;	bufp = asn_parse_objid(bufp, Length,	    &ASNType, objid, &PDU->enterprise_length);	if (bufp == NULL)	    ASN_PARSE_ERROR(NULL);	PDU->enterprise = (oid *) xmalloc(PDU->enterprise_length * sizeof(oid));	if (PDU->enterprise == NULL) {	    snmp_set_api_error(SNMPERR_OS_ERR);	    return (NULL);	}	xmemcpy((char *) PDU->enterprise, (char *) objid,	    PDU->enterprise_length * sizeof(oid));	/* Agent-addr */	four = 4;	bufp = asn_parse_string(bufp, Length,	    &ASNType,	    (u_char *) & PDU->agent_addr.sin_addr.s_addr,	    &four);	if (bufp == NULL)	    ASN_PARSE_ERROR(NULL);	/* Generic trap */	bufp = asn_parse_int(bufp, Length,	    &ASNType,	    (int *) &PDU->trap_type,	    sizeof(PDU->trap_type));	if (bufp == NULL)	    ASN_PARSE_ERROR(NULL);	/* Specific Trap */	bufp = asn_parse_int(bufp, Length,	    &ASNType,	    (int *) &PDU->specific_type,	    sizeof(PDU->specific_type));	if (bufp == NULL)	    ASN_PARSE_ERROR(NULL);	/* Timestamp */	bufp = asn_parse_unsigned_int(bufp, Length,	    &ASNType,	    &PDU->time, sizeof(PDU->time));	if (bufp == NULL)	    ASN_PARSE_ERROR(NULL);	break;/**********************************************************************/    case SNMP_PDU_GETBULK:	/* SNMPv2 Bulk Request */	/* request id */	bufp = asn_parse_int(bufp, Length,	    &ASNType,	    &PDU->reqid, sizeof(PDU->reqid));	if (bufp == NULL)	    ASN_PARSE_ERROR(NULL);	/* non-repeaters */	bufp = asn_parse_int(bufp, Length,	    &ASNType,	    &PDU->non_repeaters, sizeof(PDU->non_repeaters));	if (bufp == NULL)	    ASN_PARSE_ERROR(NULL);	/* max-repetitions */	bufp = asn_parse_int(bufp, Length,	    &ASNType,	    &PDU->max_repetitions, sizeof(PDU->max_repetitions));	if (bufp == NULL)	    ASN_PARSE_ERROR(NULL);	break;/**********************************************************************/    default:	/* Normal PDU Encoding */	/* request id */	bufp = asn_parse_int(bufp, Length,	    &ASNType,	    &PDU->reqid, sizeof(PDU->reqid));	if (bufp == NULL)	    ASN_PARSE_ERROR(NULL);#ifdef DEBUG_PDU_DECODE	snmplib_debug(8, "PDU Request ID: %d\n", PDU->reqid);#endif	/* error status */	bufp = asn_parse_int(bufp, Length,	    &ASNType,	    &PDU->errstat, sizeof(PDU->errstat));	if (bufp == NULL)	    ASN_PARSE_ERROR(NULL);#ifdef DEBUG_PDU_DECODE	snmplib_debug(8, "PDU Error Status: %d\n", PDU->errstat);#endif	/* error index */	bufp = asn_parse_int(bufp, Length,	    &ASNType,	    &PDU->errindex, sizeof(PDU->errindex));	if (bufp == NULL)	    ASN_PARSE_ERROR(NULL);#ifdef DEBUG_PDU_DECODE	snmplib_debug(8, "PDU Error Index: %d\n", PDU->errindex);#endif	break;    }    return (bufp);}char *snmp_pdu_type(struct snmp_pdu *PDU){    switch (PDU->command) {    case SNMP_PDU_GET:	return ("GET");	break;    case SNMP_PDU_GETNEXT:	return ("GETNEXT");	break;    case SNMP_PDU_RESPONSE:	return ("RESPONSE");	break;    case SNMP_PDU_SET:	return ("SET");	break;    case SNMP_PDU_GETBULK:	return ("GETBULK");	break;    case SNMP_PDU_INFORM:	return ("INFORM");	break;    case SNMP_PDU_V2TRAP:	return ("V2TRAP");	break;    case SNMP_PDU_REPORT:	return ("REPORT");	break;    case TRP_REQ_MSG:	return ("V1TRAP");	break;    default:	return ("Unknown");	break;    }}/* * Add a null variable with the requested name to the end of the list of * variables for this pdu. */void snmp_add_null_var(struct snmp_pdu *pdu, oid * name, int name_length){    struct variable_list *vars;    struct variable_list *ptr;    vars = snmp_var_new(name, name_length);    if (vars == NULL) {	perror("snmp_add_null_var:xmalloc");	return;    }    if (pdu->variables == NULL) {	pdu->variables = vars;    } else {	/* Insert at the end */	for (ptr = pdu->variables;	    ptr->next_variable;	    ptr = ptr->next_variable)	    /*EXIT */ ;	ptr->next_variable = vars;    }    return;}

⌨️ 快捷键说明

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