Exemplo n.º 1
0
/*
 * If there was an error in the input pdu, creates a clone of the pdu
 * that includes all the variables except the one marked by the errindex.
 * The command is set to the input command and the reqid, errstat, and
 * errindex are set to default values.
 * If the error status didn't indicate an error, the error index didn't
 * indicate a variable, the pdu wasn't a get response message, or there
 * would be no remaining variables, this function will return 0.
 * If everything was successful, a pointer to the fixed cloned pdu will
 * be returned.
 */
netsnmp_pdu    *
snmp_fix_pdu(netsnmp_pdu *pdu, int command)
{
    netsnmp_pdu    *newpdu;

    if ((pdu->command != SNMP_MSG_RESPONSE)
        || (pdu->errstat == SNMP_ERR_NOERROR)
        || (0 == pdu->variables)
        || (pdu->errindex <= 0)) {
        return 0;               /* pre-condition tests fail */
    }

    newpdu = _clone_pdu(pdu, 1);        /* copies all except errored variable */
    if (!newpdu)
        return 0;
    if (!newpdu->variables) {
        snmp_free_pdu(newpdu);
        return 0;               /* no variables. "should not happen" */
    }
    newpdu->command = command;
    newpdu->reqid = snmp_get_next_reqid();
    newpdu->msgid = snmp_get_next_msgid();
    newpdu->errstat = SNMP_DEFAULT_ERRSTAT;
    newpdu->errindex = SNMP_DEFAULT_ERRINDEX;

    return newpdu;
}
int main(void)
{
	
	netsnmp_session session;
	netsnmp_pdu *pdu;
	netsnmp_variable_list *vars;
	int status=45,ctr;
   
	pdu = (netsnmp_pdu *) calloc(1, sizeof(netsnmp_pdu));
    	if (pdu) 
	{
        	pdu->version = SNMP_VERSION_1;
        	pdu->command = SNMP_MSG_RESPONSE;
       		pdu->errstat = SNMP_DEFAULT_ERRSTAT;
        	pdu->errindex = SNMP_DEFAULT_ERRINDEX;
        	pdu->securityModel = SNMP_DEFAULT_SECMODEL;
        	pdu->transport_data = NULL;
        	pdu->transport_data_length = 0;
        	pdu->securityNameLen = 0;
        	pdu->contextNameLen = 0;
        	pdu->time = 0;
        	pdu->reqid = snmp_get_next_reqid();
        	pdu->msgid = snmp_get_next_msgid();
    	}	
	
	printf("Message before parsing");
	for(ctr=0;ctr<pktlen;ctr++)
		printf("%x",pkt[ctr]);
	printf("\n\n");	
	
	status=snmp_parse(&session,pdu,pkt,pktlen);
	printf("Status %d",status);


	for(vars = pdu->variables; vars; vars = vars->next_variable)
        {
		print_variable(vars->name, vars->name_length, vars);
		printf("printing..");
	}
    
	return (0);
} 
Exemplo n.º 3
0
netsnmp_pdu    *
snmp_pdu_create(int command)
{
    netsnmp_pdu    *pdu;

    pdu = (netsnmp_pdu *) calloc(1, sizeof(netsnmp_pdu));
    if (pdu) {
        pdu->version = SNMP_DEFAULT_VERSION;
        pdu->command = command;
        pdu->errstat = SNMP_DEFAULT_ERRSTAT;
        pdu->errindex = SNMP_DEFAULT_ERRINDEX;
        pdu->securityModel = SNMP_DEFAULT_SECMODEL;
        pdu->transport_data = NULL;
        pdu->transport_data_length = 0;
        pdu->securityNameLen = 0;
        pdu->contextNameLen = 0;
        pdu->time = 0;
        pdu->reqid = snmp_get_next_reqid();
        pdu->msgid = snmp_get_next_msgid();
    }
    return pdu;

}
int main(void)
{
	
	netsnmp_session session;
	netsnmp_pdu *pdu;
	netsnmp_variable_list *vars;
	int status=45,ctr,i;
   
	pdu = (netsnmp_pdu *) calloc(1, sizeof(netsnmp_pdu));
    	if (pdu) 
	{
        	//pdu->version = SNMP_VERSION_1;
        	//pdu->command = SNMP_MSG_RESPONSE;
       		//pdu->errstat = SNMP_DEFAULT_ERRSTAT;
        	//pdu->errindex = SNMP_DEFAULT_ERRINDEX;
        	pdu->securityModel = SNMP_DEFAULT_SECMODEL;
        	pdu->transport_data = NULL;
        	pdu->transport_data_length = 0;
        	pdu->securityNameLen = 0;
        	pdu->contextNameLen = 0;
        	pdu->time = 0;
        	pdu->reqid = snmp_get_next_reqid();
        	pdu->msgid = snmp_get_next_msgid();
    	}	
	
	printf("Message before parsing\n");
	for(ctr=0;ctr<pktlen;ctr++)
		printf("%x",pkt[ctr]);
	printf("\n\n");	
	
	status=snmp_parse(&session,pdu,pkt,pktlen);
	printf("Status %d",status);


	for(vars = pdu->variables; vars; vars = vars->next_variable)
        {
		print_variable(vars->name, vars->name_length, vars);
		
	}
    	printf("pdu:\nversion:%ld\ncommand:%d\n",pdu->version,pdu->command);
	
	
	printf("Decoding done. \n Now encoding...");
	
	status=snmp_build(&newpkt,&newpktlen,&offset,&session,pdu);
	printf("\nstatus = %d\n",status);
	printf("\nnewpktlen = %d\n",newpktlen);
	printf("\noffset = %d\n",offset);
	if(status==0 || status==-20)
	{
		//newpktlen=sizeof(newpkt);
		//printf("seems successful\n Here's the new packet:\npktlen :%d\n",newpktlen);
		printf("Packet dump : \n");
		for(ctr=newpktlen-offset;ctr<newpktlen;ctr++)
		{
			printf("%X",newpkt[ctr]);
		}	
		printf("\nPacket dump - END \n");
		for(ctr=newpktlen-offset,i=0;i<pktlen && ctr<newpktlen;i++,ctr++)
		{
			if(newpkt[ctr]!=pkt[i])
			{
				printf("\nMISMATCH");
				exit(1);	
			}
		}
		printf("\nMATCH!!!!\n Parse successful\n");
	}	

	
	
	
	return (0);
}