Пример #1
0
int main(){
	time(&startup_time);
	int sock;
	int addr_len, bytes_read;
	char recv_data[1024];
	struct sockaddr_in server_addr , client_addr;
	if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1){
		perror("Socket");
		exit(1);
	}
	server_addr.sin_family = AF_INET;
	server_addr.sin_port = htons(161); 
	server_addr.sin_addr.s_addr = INADDR_ANY;
	bzero(&(server_addr.sin_zero),8);
	if (bind(sock,(struct sockaddr *)&server_addr,sizeof(struct sockaddr)) == -1){
		perror("Bind");
		exit(1);
	}
	addr_len = sizeof(struct sockaddr);
	printf("\nWaiting for SNMP-clients on port 161");
	fflush(stdout);
	while (1){
		bytes_read = recvfrom(sock,recv_data,1024,0,(struct sockaddr *)&client_addr, &addr_len);
		recv_data[bytes_read] = '\0';
		printf("\n(%s , %d)\n",inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port));
		printf("\n");
		struct snmp_message_rx* snmp_msg=create_snmp_message_rx(& recv_data[0]);
		if(snmp_msg!=NULL){
			disp_snmp_message_rx(snmp_msg);
			struct snmp_pdu_rx* snmp_pdu=create_snmp_pdu_rx(snmp_msg->snmp_pdu);
			if(snmp_pdu!=NULL){
				printf("\n");
				disp_snmp_pdu_rx(snmp_pdu);
				struct varbind_list_rx* varbind_list=create_varbind_list_rx(snmp_pdu->varbindings);
				printf("\n");
				disp_varbind_list_rx(varbind_list);
				unsigned int a=5; //testing
				struct varbind* varbind=create_varbind(varbind_list->varbind_list[0]->oid,0x02, &a);
				printf("\n");
				disp_varbind(varbind);
				process_varbind_list(varbind_list);
				printf("\n");
				disp_varbind_list_rx(varbind_list);
				struct varbind_list_tx* varbind_list_to_send=create_varbind_list_tx(varbind_list);
				struct snmp_pdu_tx* snmp_pdu_tx=create_snmp_pdu_tx(0xa2,snmp_pdu->request_id,0x00,0x00, varbind_list_to_send);
				printf("\n");
				struct snmp_message_tx* snmp_msg_tx= create_snmp_message_tx(snmp_msg->community, snmp_pdu_tx);
				sendPacket(client_addr.sin_addr,ntohs(client_addr.sin_port),sock, snmp_msg_tx);
				clr_snmp_message_tx(snmp_msg_tx);
				clr_snmp_pdu_tx(snmp_pdu_tx);
				clr_varbind_list_tx(varbind_list_to_send);
				clr_varbind(varbind);
				clr_varbind_list_rx(varbind_list);
				clr_snmp_pdu_rx(snmp_pdu);
			}
			clr_snmp_message_rx(snmp_msg);
		}
		fflush(stdout);
	}
	return 0;
}
Пример #2
0
int main()
{
	time(&startup_time);
	int sock;
	socklen_t addr_len;
	int bytes_read;
	char recv_data[1024];
	struct sockaddr_in server_addr , client_addr;

    oid_db = create_oid_db();

    add_oid(oid_db, (unsigned char *) "1.3.6.1.2.1.1.1.0", PRIMV_OCTSTR, description);
    add_oid(oid_db, (unsigned char *) "1.3.6.1.2.1.1.3.0", PRIMV_TIMTICK, &timeticks);
    add_oid(oid_db, (unsigned char *) "1.3.6.1.4.1.99.1.1.0", PRIMV_OCTSTR, MasterName);
    add_oid(oid_db, (unsigned char *) "1.3.6.1.4.1.99.1.2.0", PRIMV_OCTSTR, MasterLocation);
    add_oid(oid_db, (unsigned char *) "1.3.6.1.4.1.99.1.3.0", PRIMV_INT, &NumberOfAgents);
    add_oid(oid_db, (unsigned char *) "1.3.6.1.4.1.99.2.1.1.1.0", PRIMV_OCTSTR, PID);
    add_oid(oid_db, (unsigned char *) "1.3.6.1.4.1.99.2.1.1.2.0", PRIMV_OCTSTR, HW);
    add_oid(oid_db, (unsigned char *) "1.3.6.1.4.1.99.2.1.1.3.0", PRIMV_OCTSTR, SW);
    add_oid(oid_db, (unsigned char *) "1.3.6.1.4.1.99.2.1.1.4.0", PRIMV_OCTSTR, SN);
    add_oid(oid_db, (unsigned char *) "1.3.6.1.4.1.99.2.1.1.5.0", PRIMV_OCTSTR, ID);
    add_oid(oid_db, (unsigned char *) "1.3.6.1.4.1.99.2.1.1.6.0", PRIMV_INT, &UB);
    add_oid(oid_db, (unsigned char *) "1.3.6.1.4.1.99.2.1.2.1.0", PRIMV_INT, &POW);
    add_oid(oid_db, (unsigned char *) "1.3.6.1.4.1.99.2.1.2.2.0", PRIMV_INT, &WORK);
    add_oid(oid_db, (unsigned char *) "1.3.6.1.4.1.99.2.1.2.3.0", PRIMV_INT, &FREQ);
    add_oid(oid_db, (unsigned char *) "1.3.6.1.4.1.99.2.1.2.4.0", PRIMV_INT, &VRMS);
    add_oid(oid_db, (unsigned char *) "1.3.6.1.4.1.99.2.1.2.5.0", PRIMV_INT, &IRMS);
    add_oid(oid_db, (unsigned char *) "1.3.6.1.4.1.99.2.1.2.6.0", PRIMV_INT, &LOAD);

    print_oid_db(oid_db);

	if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
	{
	    perror("Socket");
	    exit(1);
	}

	server_addr.sin_family = AF_INET;
	server_addr.sin_port = htons(161); 
	server_addr.sin_addr.s_addr = INADDR_ANY;
	bzero(&(server_addr.sin_zero),8);

	if (bind(sock,(struct sockaddr *)&server_addr,sizeof(struct sockaddr)) == -1)
	{
	    perror("Bind");
	    exit(1);
	}
	addr_len = sizeof(struct sockaddr);
	printf("\nWaiting for SNMP-clients on port 161");
	fflush(stdout);

    while (1)
    {
        bytes_read = recvfrom(sock, recv_data, 1024, 0, (struct sockaddr *) &client_addr, &addr_len);
	recv_data[bytes_read] = '\0';

        printf("\n(%s , %d)\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
	printf("\n");

        struct snmp_message_rx *snmp_msg = create_snmp_message_rx((unsigned char *) &recv_data[0]);

	if(snmp_msg!=NULL)
	{
	    disp_snmp_message_rx(snmp_msg);
	    struct snmp_pdu_rx* snmp_pdu=create_snmp_pdu_rx(snmp_msg->snmp_pdu);

	    if(snmp_pdu!=NULL)
	    {
		printf("\n");
		disp_snmp_pdu_rx(snmp_pdu);
                struct varbind_list_rx *varbind_list = create_varbind_list_rx(snmp_pdu->varbindings);

		printf("\n");
		disp_varbind_list_rx(varbind_list);


		unsigned int a=5; //testing
                struct varbind *varbind = create_varbind(varbind_list->varbind_list[0]->oid, PRIMV_INT, &a);

		printf("\n");
		disp_varbind(varbind);

                process_varbind_list(varbind_list, snmp_pdu->snmp_pdu_type);

		printf("\n");
		disp_varbind_list_rx(varbind_list);

                /* Response */
                struct varbind_list_tx *varbind_list_to_send = create_varbind_list_tx(varbind_list);
                struct snmp_pdu_tx *snmp_pdu_tx = create_snmp_pdu_tx(PDU_GET_RESP, snmp_pdu->request_id, 0x00,0x00, varbind_list_to_send);
		printf("\n");

                struct snmp_message_tx *snmp_msg_tx = create_snmp_message_tx(snmp_msg->community,snmp_pdu_tx);
                sendPacket(client_addr.sin_addr, ntohs(client_addr.sin_port), sock,snmp_msg_tx);
		clr_snmp_message_tx(snmp_msg_tx);
		clr_snmp_pdu_tx(snmp_pdu_tx);
		clr_varbind_list_tx(varbind_list_to_send);
		clr_varbind(varbind);
		clr_varbind_list_rx(varbind_list);
		clr_snmp_pdu_rx(snmp_pdu);

	    } // if snmp_pdu!=NULL
	    clr_snmp_message_rx(snmp_msg);

	} // if snmp_msg!=NULL
	fflush(stdout);

    } // While

return 0;
}