//--------------------------------------------------------------------------
// Function Name: snmp_read
// Description  : Checks to see if any of the fd's set in the fdset belong to snmp.
//                Each socket with it's fd set has a packet read from it and
//                snmp_parse is called on the packet received. The resulting pdu is
//                passed to the callback routine for that session. If the callback
//                routine returns successfully, the pdu and it's request are
//                deleted.
// Return Value : NONE
// Calling To   : udp_read, snmp_parse, free_pdu and user's callback
//                function
// Called By    : user
// Status       : some part of this module should be mdoified
//-------------------------------------------------------------------------
void snmp_read (snmp_session *session)
{
        uint8 inpacket[SNMP_MAX_LEN], outpacket[SNMP_MAX_LEN];
        int   outlength, fromlength, inlength;

        inlength = SNMP_MAX_LEN;
        if(udp_read(session, inpacket, &inlength)) {
                snmpInPkts++ ;

                outlength = SNMP_MAX_LEN;
                if(snmp_agent_parse(session, inpacket, inlength, outpacket, &outlength))
                {

#if defined(SNMP_DUMP) && defined(PC_OUTPUT)
                        {
                        int count;

                        printf("sent %d bytes to %s:\n", (int) outlength,
                              inet_ntoa(from.sin_addr));
                        for (count = 0; count < outlength; count++) {
                                printf("%02X ", outpacket[count]);
                                if ((count % 16) == 15)
                                        printf("\n");
                                }
                        printf("\n\n");
                        }
#endif
                        if(snmp_udp_send(session, outpacket,outlength) == 0) return;
                        snmp_outpkts++;

#ifdef WEBADMIN
                          RunChangeIPAddress();
#ifndef _PC
           RunWriteDataToEEPROM();
#endif
#endif WEBADMIN
                }
        }
}
Beispiel #2
0
int
snmp_read_packet(int sd)
{
    struct sockaddr_in	from;
    int length, out_length, fromlength;
    u_char  packet[1500], outpacket[1500];
#ifdef USE_LIBWRAP
    char *addr_string;
#endif
    fromlength = sizeof from;
    length = recvfrom(sd, (char *) packet, 1500, 0, (struct sockaddr *)&from,
		      &fromlength);
    if (length == -1)
	perror("recvfrom");

#ifdef USE_LIBWRAP
	addr_string = inet_ntoa(from.sin_addr);

	if(!addr_string) {
          addr_string = STRING_UNKNOWN;
	}
	if(hosts_ctl("snmpd", addr_string, addr_string, STRING_UNKNOWN)) {
          syslog(allow_severity, "Connection from %s", addr_string);
	} else {
          syslog(deny_severity, "Connection from %s refused", addr_string);
          return(0);
	}
#endif

#ifdef USING_MIBII_SNMP_MIB_MODULE       
    snmp_inpkts++;
#endif
    if (snmp_dump_packet){
	printf("\nreceived %d bytes from %s:\n", length,
	       inet_ntoa(from.sin_addr));
	xdump(packet, length, "");
	printf("\n");
        fflush(stdout);
    } else if (log_addresses){
	int count;
	
	for(count = 0; count < ADDRCACHE; count++){
	    if (addrCache[count].status > UNUSED /* used or old */
		&& from.sin_addr.s_addr == addrCache[count].addr)
		break;
	}
	if (count >= ADDRCACHE || verbose){
	    printf("%s Received SNMP packet(s) from %s\n",
		   sprintf_stamp(NULL), inet_ntoa(from.sin_addr));
	    for(count = 0; count < ADDRCACHE; count++){
		if (addrCache[count].status == UNUSED){
		    addrCache[count].addr = from.sin_addr.s_addr;
		    addrCache[count].status = USED;
		    break;
		}
	    }
	} else {
	    addrCache[count].status = USED;
	}
    }
    out_length = 1500;
    if (snmp_agent_parse(packet, length, outpacket, &out_length,
			 from.sin_addr.s_addr)){
	if (snmp_dump_packet){
	    printf("\nsent %d bytes to %s:\n", out_length,
		   inet_ntoa(from.sin_addr));
	    xdump(outpacket, out_length, "");
	    printf("\n");
            fflush(stdout);
	}
#ifdef USING_MIBII_SNMP_MIB_MODULE       
	snmp_outpkts++;
#endif
	if (sendto(sd, (char *)outpacket, out_length, 0,
		   (struct sockaddr *)&from, sizeof(from)) < 0){
	    perror("sendto");
	    return 0;
	}

    }
    return 1;
}