Beispiel #1
0
/*
 *  Entry point of the SNMP server.
 */
PROCESS_THREAD(snmpd_process, ev, data) {
	PROCESS_BEGIN();

	snmp_packets = 0;

        #ifndef CONTIKI_TARGET_AVR_RAVEN
        systemStartTime = clock_time();
        #endif

        #if CHECK_STACK_SIZE
        u16t i = 0;
        u32t pointer;
        u32t* p = &pointer;
        for (i = 0; i < 1000; i++) {
            *p = 0xAAAAAAAA;
            p--;
        }
        marker = &pointer;
        #endif

	udpconn = udp_new(NULL, UIP_HTONS(0), NULL);
	udp_bind(udpconn, UIP_HTONS(LISTEN_PORT));

        /* init MIB */
        if (mib_init() != -1) {
            
            while(1) {
                PROCESS_YIELD();
                udp_handler(ev, data);
            }
        } else {
            snmp_log("error occurs while initializing the MIB\n");
        }
	PROCESS_END();
}
Beispiel #2
0
void
ip_handler (u_char * args, const struct pcap_pkthdr *header, const u_char * ippacket)
{
	// assumes *packet points at the beginning of the IP packet!
	// only call for raw IP or after adjusting pointers in correct
	// frame_handler!
	static bool warning_used = false;
	// packet related
	ip_t * ip = (ip_t *) (ippacket);
	size_t size_ip = (ip->ip_hl*4);
	void * segment = NULL;
	size_t segment_hdr_size = 0;
	u_int16_t dport = 0;
	const u_char * payload;
	// return values from flow / service tables
	pair<map<pic_connection_t,pic_flow_t>::iterator,bool> ret_flows;
	// u_char ret_services;  // warning: variable ‘ret_services’ set but not used
	
#if DEBUG >= 1
	char sip[INET_ADDRSTRLEN], dip[INET_ADDRSTRLEN];
	inet_ntop(AF_INET, &(ip->ip_src), sip, INET_ADDRSTRLEN);
	inet_ntop(AF_INET, &(ip->ip_dst), dip, INET_ADDRSTRLEN);
	printf("(IP) %s >> %s p: %d\n", sip, dip, ip->ip_p);
#endif	
	
	switch (ip->ip_p) {
		case IPPROTO_TCP:
			segment = (tcp_t *) (ippacket + size_ip);
			segment_hdr_size = ((tcp_t *)segment)->th_off * 4;
			dport = ((tcp_t *)segment)->th_dport;
#if DEBUG >= 1
			tcp_handler(args, header, ippacket);
#endif
			break;
		case IPPROTO_UDP:
			segment = (udp_t *) (ippacket + size_ip);
			segment_hdr_size = 8;
			dport = ((udp_t *)segment)->uh_dport;
#if DEBUG >= 1
			udp_handler(args, header, ippacket);
#endif
			break;
		default:
			if (warning_used == false) {
				fprintf(stderr,"WARNING: only TCP / UDP supported\n\n\n\n");
				warning_used = true;
			}
			return;
			break;
	}
		
	// ok, we got TCP or UDP
	payload = (ippacket + size_ip + segment_hdr_size);
	ret_flows = flow_table_update(args, header, ippacket);
	
	// check if we need identification
	if (ret_flows.first->second.flow_type <= type_possible) {
		//ret_services = identify_flow(ret_flows.first,dport,payload);
		identify_flow(ret_flows.first,dport,payload);
	}
#if DEBUG >= 0
	// update and output progress
	cout << "\rip packets scanned: \x1b[33m" << ++ip_packets_scanned << "\x1b[0m" << endl;
	ip_bytes_scanned += header->len;
	cout << "\rip bytes scanned: \x1b[34m" << ++ip_bytes_scanned << "\x1b[0m" << endl;
	cout << "\ractive flows: \x1b[35m" << flows.size() << "       \x1b[0m" << endl;
	cout << "\ridentification entries: \x1b[35m" << services.size() << "       \x1b[0m\x1b[3A";	
#endif
}
Beispiel #3
0
/*
 *  Entry point of the SNMP server.
 */
PROCESS_THREAD(snmpd_process, ev, data) {
	PROCESS_BEGIN();

        #if !(CONTIKI_TARGET_AVR_RAVEN || CONTIKI_TARGET_AVR_ZIGBIT)
        systemStartTime = clock_time();
        #endif
	
	/*Switch LED on @ANY Brick On if SNMPD started and set other LEDs as Output*/
	DDRB |= (1 << PIN5);
	DDRB |= (1 << PIN6);
	DDRB |= (1 << PIN7);
	PORTB &= ~(1 << PIN7);
	PORTB |= (1 << PIN6);
	PORTB |= (1 << PIN5);

	/*END LED @ANY Brick*/

	
        #if CHECK_STACK_SIZE
		printf("Starte füllen mit Bit Pattern\n");
        u16t i = 0;
        u32t pointer;
        u32t* p = &pointer;
        for (i = 0; i < 500; i++) {
            *p = 0xAAAAAAAA;
            p--;
        }
        marker = &pointer;
        #endif

	udpconn = udp_new(NULL, UIP_HTONS(0), NULL);
	udp_bind(udpconn, UIP_HTONS(LISTEN_PORT));

	/*sz*/
	#if ENABLE_SNMPv3
	incMsgAuthoritativeEngineBoots();
	#endif
	/*sz*/

	/*sz*/
	#if ENABLE_SNMPv3
	srandom(get_seed());
	privacyLow = random();
	//srandom(get_seed());
	privacyHigh = random();
#if PDEBUG
	printf("Privacy 32Bit Pseudo Random Number: \n%u \n%u\n",privacyLow, privacyHigh);
#endif
	#endif
	/*sz*/


        /* init MIB */
        if (mib_init() != -1) {

            while(1) {
                PROCESS_YIELD();
                udp_handler(ev, data);
            }
        } else {
            snmp_log("error occurs while initializing the MIB\n");
        }
	PROCESS_END();
}
Beispiel #4
0
extern void handle_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet)
{
	static int count = 1;			/* packet counter */

	/* declar pointers to packet headers */
	const struct ethernet_header *ethernet;	/* the ethernet header */
	ip_header *ip;				/* the IP header */
	tcp_header *tcp;			/* the TCP header */
	udp_header *uh;
	const char *payload = "";						/* packet payload */

	int size_ip;
	int size_tcp;
	int size_payload = 0;

	printf("\nPacket number %d:\n", count);
	count++;

	/* define ethernet header */
	ethernet = (struct ethernet_header*)(packet);

	/* define ip header offset */
	ip = (struct ip_header*)(packet + SIZE_ETHERNET);
	size_ip = IP_HL(ip)*4;

	if (size_ip < 20){
		printf("	* Invalid IP header length: %u bytes\n", size_ip);
		return;
	}


	/* determine protocol */
	switch(ip->ip_p){
	case IPPROTO_TCP:
		printf("	Protocol: TCP\n");
		/* Packet is TCP */
		/* define tcp header offset */
		tcp = (struct tcp_header*)(packet + SIZE_ETHERNET + size_ip);
		size_tcp = TH_OFF(tcp)*4;
		if (size_tcp < 20){
			printf("	* Invalid TCP header length: %u bytes\n", size_tcp);
			return;
		}
		tcp_handler(tcp,size_tcp,size_ip,packet,payload,size_payload,ip);
		break;
	case IPPROTO_UDP:

	printf("	Protocol: UDP\n");

		/* retrieve the position of the ip header */
		ip = (ip_header *) (packet + 14);

		/* retrieve the position of the udp header */
		uh = (udp_header *) ((u_char*)ip + size_ip + SIZE_ETHERNET);
		udp_handler(payload,packet,uh,ip,size_ip);
		return;
	case IPPROTO_ICMP:
		printf("	Protocol: ICMP\n");
		return;
	case IPPROTO_IP:
		printf("	Protocol: IP\n");
		return;
	default:
		printf("	Protocol: Unknown\n");
		return;
	}

	return;
}
Beispiel #5
0
//extern int vivid;
//extern int proto[PROTO_CATEGORY];
void ip_handler(const u_char *packet, int len)
{
	
	const struct sniff_ip *ip;				/*The IP header */

	int size_ip;
	u_short ip_len;
	
	ip = (struct sniff_ip*)(packet);
	size_ip = IP_HL(ip)*4;
	if (size_ip < 20) {
		printf("	* Invalid IP header length: %u bytes\n", size_ip);
		return;
	}
	ip_len = ntohs(ip->ip_len);
#if 0
	if(vivid) {
		printf("	Version: %u\n", IP_V(ip));
	    printf("	header length: %u bytes\n", IP_HL(ip)*4);
     	printf("	Tos: 0x%02x\n", ip->ip_tos);
    	printf("	Total length: %u\n", ntohs(ip->ip_len));
    	printf("	Identification: 0x%04x (%u)\n", ntohs(ip->ip_id), ntohs(ip->ip_id));
    	printf("	Flags: 0x%02x\n",((ntohs(ip->ip_off))&(IP_RF|IP_DF|IP_MF))>>13);
    	printf("	Fragment offset: %d\n", (ntohs(ip->ip_off))&IP_OFFMASK);
	    printf("	Time to live: %u\n", ip->ip_ttl);
    	printf("	Protocal: %u\n", ip->ip_p);
    	printf("	Header checksum: 0x%04x\n", ntohs(ip->ip_sum));
	}

	printf("From: %s", inet_ntoa(ip->ip_src));
	printf(" To: %s ", inet_ntoa(ip->ip_dst));
#endif

	memcpy(myRecorder.src_ip, inet_ntoa(ip->ip_src), 20);
	memcpy(myRecorder.dst_ip, inet_ntoa(ip->ip_dst), 20);
	

	packet += size_ip;
	len = ip_len - size_ip;
	switch(ip->ip_p) {
		case IPPROTO_TCP:
			//proto[TCP_INDEX] ++;
			tcp_handler(packet, len);
			break;
		case IPPROTO_UDP:
			//proto[UDP_INDEX] ++;
			udp_handler(packet, len);
			break;
		case IPPROTO_IP:
			//printf("	Dummy protocol for TCP\n");
			break;
		case IPPROTO_ICMP:
			//proto[ICMP_INDEX] ++;
			//printf("	ICMP\n");
			break;
		case IPPROTO_IGMP:
			//printf("	IGMP\n");
			break;
		case IPPROTO_IPIP:
			//printf("	IPIP tunnels\n");
			break;
		case IPPROTO_EGP:
			//printf("	EGP\n");
			break;
		case IPPROTO_PUP:
			//printf("	PUP\n");
			break;
		case IPPROTO_IDP:
			//printf("	IDP\n");
			break;
		case IPPROTO_TP:
			//printf("	TP\n");
			break;
		case IPPROTO_DCCP:
			//printf("	DCCP\n");
			break;
		case IPPROTO_IPV6:
			//printf("	IPV6\n");
			break;
		case IPPROTO_RSVP:
			//printf("	RSVP\n");
			break;
		case IPPROTO_GRE:
			//printf("	GRE\n");
			break;
		case IPPROTO_ESP:
			//printf("	ESP\n");
			break;
		case IPPROTO_AH:
			//printf("	AH\n");
			break;
		case IPPROTO_MTP:
			//printf("	MTP\n");
			break;
		case IPPROTO_BEETPH:
			//printf("	BEETPH\n");
			break;
		case IPPROTO_ENCAP:
			//printf("	ENCAP\n");
			break;
		case IPPROTO_PIM:
			//printf("	PIM\n");
			break;
		case IPPROTO_COMP:
			//printf("	COMP\n");
			break;
		case IPPROTO_SCTP:
			//printf("	SCTP\n");
			break;
		case IPPROTO_UDPLITE:
			//printf("	UDPLITE\n");
			break;
		case IPPROTO_RAW:
			//printf("	Raw IP pakcet\n");
			break;
		default:
			//printf("	unkonwn\n");
			break;
	}
	
	return;
}