int main()
{
	int hash_table[m];
	char buffer[128];
	char response;
	int value;
	int i;
	for(i=0; i<m; i++)
		hash_table[i] = NIL_VALUE;
	
	do
	{
		printf("Enter a to insert, s to search, d to delete, f to display the hash table and e to exit\n");
		fgets(buffer, 128, stdin);
		sscanf(buffer, "%c", &response);
		switch(response)
		{
			case 'a':
				printf("Enter value to insert: ");
				fgets(buffer, 128, stdin);
				sscanf(buffer, "%d", &value);
				insert_in_table(hash_table, value);
				break;
			case 's':
				printf("Enter value to search: ");
				fgets(buffer, 128, stdin);
				sscanf(buffer, "%d", &value);
				if(search_in_table(hash_table, value) != -1)
					printf("%d is present\n", value);
				else
					printf("%d is not present\n", value);
				break;
			/*case 'd':
				printf("Enter value to delete: ");
				fgets(buffer, 128, stdin);
				sscanf(buffer, "%d", &value);
				delete_from_table(hash_table, value);
				break;*/
			case 'f':
				display_table(hash_table);
				break;
			case 'e':
				break;
			default:
				printf("Wrong option\n");
				break;
		}
	}
	while(response != 'e');
	return 1;
}
Example #2
0
//Parse packet function for files
void parse_packet_file(u_char *user, struct pcap_pkthdr *packethdr, u_char *packetptr)
{
    struct ip* iphdr;
    struct icmphdr* icmphdr;
    struct tcphdr* tcphdr;
    struct udphdr* udphdr;
    char iphdrInfo[256], srcip[256], dstip[256];
    unsigned short id, seq;
    int i;
    int decision_p;
    int decision_t = -1;
    int proto;
    void *other_p;
    int sport, dport;
    static int prev_time = 0;
    int tStamp = packethdr->ts.tv_sec;

    u_char *backup = packetptr;
    struct pcap_pkthdr* backup2 = packethdr;

    struct ethhdr *eth = (struct ethhdr *)packetptr;
    //printf("Timestamp: %ld\n",packethdr->ts.tv_sec);
    if(tStamp - prev_time > TIMEOUT)
    {
        cleanup_State(tStamp);
        prev_time = tStamp;
    }

    // Skip the datalink layer header and get the IP header fields.
    packetptr += linkhdrlen;
    iphdr = (struct ip*)packetptr;
    strcpy(srcip, inet_ntoa(iphdr->ip_src));
    strcpy(dstip, inet_ntoa(iphdr->ip_dst));
    sprintf(iphdrInfo, "ID:%d TOS:0x%x, TTL:%d IpLen:%d DgLen:%d",
            ntohs(iphdr->ip_id), iphdr->ip_tos, iphdr->ip_ttl,
            4*iphdr->ip_hl, ntohs(iphdr->ip_len));

    // Advance to the transport layer header then parse and display
    // the fields based on the type of hearder: tcp, udp or icmp.
    packetptr += 4*iphdr->ip_hl;

    if(iphdr->ip_v == 6)
        return;

    switch (iphdr->ip_p)
    {
    case IPPROTO_TCP:
        tcphdr = (struct tcphdr*)packetptr;
        decision_t = updateState(iphdr,tcphdr,IPPROTO_TCP,1,tStamp);
        other_p = tcphdr;
        proto = IPPROTO_TCP;
        sport = ntohs(tcphdr->source);
        dport = ntohs(tcphdr->dest);
        break;

    case IPPROTO_UDP:
        udphdr = (struct udphdr*)packetptr;
        decision_t = updateState(iphdr,udphdr,IPPROTO_UDP,1,tStamp);
        other_p = udphdr;
        proto = IPPROTO_UDP;
        sport = ntohs(udphdr->source);
        dport = ntohs(udphdr->dest);
        break;

    case IPPROTO_ICMP:
        icmphdr = (struct icmphdr*)packetptr;
        memcpy(&id, (u_char*)icmphdr+4, 2);
        memcpy(&seq, (u_char*)icmphdr+6, 2);
        decision_t = updateState(iphdr,icmphdr,IPPROTO_ICMP,1,tStamp);
        other_p = icmphdr;
        sport = -1;
        dport = -1;
        proto = IPPROTO_ICMP;
        break;
    }
    if(decision_t == -1)
    {
        if(debugging)
            printf("Drop!\n");
        return;
    }
    if(decision_t == 0)
    {
        decision_p = matchWithRules(srcip,dstip,sport,dport,proto);
        if(decision_p == 0)
        {
            if(debugging)
                printf("Drop!\n");
            return;
        }
        else if(decision_p == -1)
        {
            if(debugging)
                printf("Reject!\n");
            return;
        }
        insert_in_table(iphdr,other_p,proto,tStamp);
    }
    pcap_dump(user, backup2, backup);
}
Example #3
0
//Parse packet function for another interface
void parse_packet_p(u_char *user, struct pcap_pkthdr *packethdr, u_char *packetptr)
{
    struct ip* iphdr;
    struct icmphdr* icmphdr;
    struct tcphdr* tcphdr;
    struct udphdr* udphdr;
    char iphdrInfo[256], srcip[256], dstip[256];
    unsigned short id, seq;
    int i;
    int decision_p;
    int decision_t = -1;
    int proto;
    void *other_p;
    int sport,dport;

    struct ethhdr *eth = (struct ethhdr *)packetptr;

    // Skip the datalink layer header and get the IP header fields.
    packetptr += linkhdrlen;
    iphdr = (struct ip*)packetptr;
    strcpy(srcip, inet_ntoa(iphdr->ip_src));
    strcpy(dstip, inet_ntoa(iphdr->ip_dst));
    sprintf(iphdrInfo, "ID:%d TOS:0x%x, TTL:%d IpLen:%d DgLen:%d",
            ntohs(iphdr->ip_id), iphdr->ip_tos, iphdr->ip_ttl,
            4*iphdr->ip_hl, ntohs(iphdr->ip_len));

    // Advance to the transport layer header then parse and display
    // the fields based on the type of hearder: tcp, udp or icmp.
    packetptr += 4*iphdr->ip_hl;
    updateARP(srcip);
    //printf("2 %s %s\n",srcip,dstip);

    if(!isIPInSubnet(dstip,NET_IN,SUB_IN)  || iphdr->ip_v == 6)
    {
        return;
    }

    switch (iphdr->ip_p)
    {
    case IPPROTO_TCP:
        tcphdr = (struct tcphdr*)packetptr;
        decision_t = updateState(iphdr,tcphdr,IPPROTO_TCP,2,-1);
        other_p = tcphdr;
        proto = IPPROTO_TCP;
        sport = ntohs(tcphdr->source);
        dport = ntohs(tcphdr->dest);
        break;

    case IPPROTO_UDP:
        udphdr = (struct udphdr*)packetptr;
        decision_t = updateState(iphdr,udphdr,IPPROTO_UDP,2,-1);
        other_p = udphdr;
        proto = IPPROTO_UDP;
        sport = ntohs(udphdr->source);
        dport = ntohs(udphdr->dest);
        break;

    case IPPROTO_ICMP:
        icmphdr = (struct icmphdr*)packetptr;
        memcpy(&id, (u_char*)icmphdr+4, 2);
        memcpy(&seq, (u_char*)icmphdr+6, 2);
        decision_t = updateState(iphdr,icmphdr,IPPROTO_ICMP,2,-1);
        other_p = icmphdr;
        proto = IPPROTO_ICMP;
        sport = -1;
        dport = -1;
        break;
    }

    if(decision_t == -1)
    {
        if(debugging)
            printf("Drop!\n");
        return;
    }

    if(decision_t == 0)
    {
        decision_p = matchWithRules(srcip,dstip,sport,dport,proto);
        if(decision_p == 0)
        {
            if(debugging)
                printf("Drop!\n");
            return;
        }
        else if(decision_p == -1)
        {
            if(debugging)
                printf("Reject!\n");
            if(iphdr->ip_p == IPPROTO_TCP)
            {
                SendTCPRst(dstip,srcip,dport,sport,ntohl(tcphdr->seq));
            }
            else
            {
                SendICMPError(dstip,srcip);
            }
            return;
        }
        insert_in_table(iphdr,other_p,proto,-1);
    }

    getArrayFromString(MAC_IN);
    for(i=0; i<6; i++)
    {
        eth->h_source[i] = mac_t[i];
    }

    get_Mac_ARP(dstip,INT_IN);

    if(debugging)
        printf("2 %s\n",arp_ans);

    getArrayFromString(arp_ans);
    for(i=0; i<6; i++)
    {
        eth->h_dest[i] = mac_t[i];
    }
    if(debugging)
        printf("2 %d\n",pcap_inject(in_handle,eth,packethdr->len));
    else
        pcap_inject(in_handle,eth,packethdr->len);
}