int is_localhost(char *address) { unsigned int i_user = ip_to_int(address); unsigned int i_127 = ip_to_int("127.0.0.1"); if (i_user == i_127) { return 0; } return -1; }
int is_our_country(char *address, request_rec *r) { int x; char * read, * temp, * c_low, * c_high, *state; unsigned int i_user,i_low,i_high; i_user = ip_to_int(address); for(x = 0; x < num_lines; x++) { read = apr_pstrdup(r->pool,lines[x]); temp = apr_strtok(read,",",&state); c_low = apr_pstrdup(r->pool,temp); temp = apr_strtok(NULL,",",&state); c_high = apr_pstrdup(r->pool,temp); i_low = ip_to_int(c_low); i_high = ip_to_int(c_high); if (i_user >= i_low && i_user <= i_high) { return 0; } } return -1; }
int is_rfc_1918(char *address) { unsigned int i_user = ip_to_int(address); unsigned int i_10_l = ip_to_int("10.0.0.0"); unsigned int i_10_h = ip_to_int("10.255.255.255"); unsigned int i_172_l = ip_to_int("172.16.0.0"); unsigned int i_172_h = ip_to_int("172.31.255.255"); unsigned int i_192_l = ip_to_int("192.168.0.0"); unsigned int i_192_h = ip_to_int("192.168.255.255"); if ((i_user >= i_10_l && i_user <= i_10_h) || (i_user >= i_172_l && i_user <= i_172_h) || (i_user >= i_192_l && i_user <= i_192_h)) { return 0; } return -1; }
IpRange* find_range_for_ip(IPDB *db, char *ip) { IpRange* search; IpRange* result; search = (IpRange *)malloc(sizeof(IpRange)); if(db == NULL) { printf("ERROR: DB ist NULL! "); return NULL; } if(db->ranges_count == 0) { printf("ERROR: DB has no Ranges Data. Can not search!\n"); return NULL; } search->from = ip_to_int(ip); search->to=0; search->city_index = 0; if(DEBUG) printf("Searching for: ip=%s, ipnum=%lu \n", ip, search->from); result = (IpRange*)bsearch(search, db->ranges, db->ranges_count, sizeof(IpRange), compare_ranges); if(search != NULL) free(search); if(result == NULL) { if(DEBUG) printf("ERROR: Could not find the IP: %s! THIS SHOULD NOT HAPPEN!\n", ip); return NULL; } else { if(DEBUG) { printf("Found Range: \t"); print_range(result); } return (IpRange*)result; } }
MD read_pcap(std::string inFile,std::string target_ip,int quit_early, short exture_version){ unsigned target = ip_to_int(target_ip.c_str()); unsigned their_ip; long raw_time; long n = 20000000; //struct top2 *bbos = (struct top2 *)malloc(n*sizeof(struct top2)); printf("Parsing pcap file %s ... with target:: %u", inFile.c_str(),target); pcap_t *handle; char errbuf[PCAP_ERRBUF_SIZE]; //not sure what to do with this, oh well handle = pcap_open_offline(inFile.c_str(), errbuf); //call pcap library function MD md; if (handle == NULL) { fprintf(stderr,"Couldn't open pcap file %s: %s\n", inFile.c_str(), errbuf); return(md); } md.tick_data.reserve(20000000); const u_char *packet; struct pcap_pkthdr header; unsigned int pktNum(0); while (packet = pcap_next(handle, &header)) { u_char *pktPtr = (u_char *)packet; // Parse the header, grabbing the type field. int ether_type = ((int)(pktPtr[12]) << 8) | (int)pktPtr[13]; int ether_offset = 0; if (ether_type == ETHER_TYPE_IP) { //most common ether_offset = 14; } else { continue; } raw_time = header.ts.tv_sec * 1000000000 +header.ts.tv_usec*1000; //parse the IP header pktPtr += ether_offset; //skip past the Ethernet II header struct ip *ipHdr = (struct ip *)pktPtr; //point to an IP header structure int pktLen = ntohs(ipHdr->ip_len); int msg_type; //used to branch a0/a1/normal md accordingly tick bbo; if (ipHdr->ip_p == 17) { //UDP BRO //TCP MESSAGE EITHER TO OR FROM OUR TARGET IP msg_type = processUDP(pktNum,raw_time,ipHdr,pktLen,bbo,exture_version); if ( msg_type >= 1){ //bbos[pktNum] = bbo; md.tick_data.push_back(bbo); } } pktNum++; //increment number of packets seen if (unlikely(quit_early>0)&&(unlikely(pktNum>quit_early) || unlikely(pktNum>n))) break; } printf("\nParsed %u packets\n",pktNum); return md; pcap_close(handle); }
int main() { struct ip_addr ipaddr, netmask, gw; unsigned FPGA_port_number; char *FPGA_ip_address; char *FPGA_netmask; char *FPGA_gateway; unsigned integer_ip; unsigned int FPGA_ip_address_a1,FPGA_ip_address_a2,FPGA_ip_address_a3,FPGA_ip_address_a4; unsigned int FPGA_netmask_a1,FPGA_netmask_a2,FPGA_netmask_a3,FPGA_netmask_a4; unsigned int FPGA_gateway_a1,FPGA_gateway_a2,FPGA_gateway_a3,FPGA_gateway_a4; //EXAMPLE: set FPGA IP and port number FPGA_ip_address=FPGA_IP; FPGA_netmask=FPGA_NM; FPGA_gateway=FPGA_GW; FPGA_port_number=FPGA_PORT; //extract FPGA IP address from string if (ip_to_int (FPGA_ip_address,&FPGA_ip_address_a1,&FPGA_ip_address_a2,&FPGA_ip_address_a3,&FPGA_ip_address_a4) == INVALID) { printf ("'%s' is not a valid IP address for FPGA server.\n", FPGA_ip_address); return 1; } //extract FPGA netmask address from string if (ip_to_int (FPGA_netmask,&FPGA_netmask_a1,&FPGA_netmask_a2,&FPGA_netmask_a3,&FPGA_netmask_a4) == INVALID) { printf ("'%s' is not a valid netmask address for FPGA server.\n", FPGA_netmask); return 1; } //extract FPGA gateway address from string if (ip_to_int (FPGA_gateway,&FPGA_gateway_a1,&FPGA_gateway_a2,&FPGA_gateway_a3,&FPGA_gateway_a4) == INVALID) { printf ("'%s' is not a valid gateway address for FPGA server.\n", FPGA_gateway); return 1; } /* the mac address of the board. this should be unique per board */ unsigned char mac_ethernet_address[] = { 0x00, 0x0a, 0x35, 0x00, 0x01, 0x02 }; echo_netif = &server_netif; init_platform(); Xil_DCacheDisable(); /* initliaze IP addresses to be used */ IP4_ADDR(&ipaddr, FPGA_ip_address_a4, FPGA_ip_address_a3, FPGA_ip_address_a2, FPGA_ip_address_a1); IP4_ADDR(&netmask, FPGA_netmask_a4, FPGA_netmask_a3, FPGA_netmask_a2, FPGA_netmask_a1); IP4_ADDR(&gw, FPGA_gateway_a4, FPGA_gateway_a3, FPGA_gateway_a2, FPGA_gateway_a1); print_app_header(); print_ip_settings(&ipaddr, &netmask, &gw); lwip_init(); /* Add network interface to the netif_list, and set it as default */ if (!xemac_add(echo_netif, &ipaddr, &netmask,&gw, mac_ethernet_address,PLATFORM_EMAC_BASEADDR)) { xil_printf("Error adding N/W interface\n\r"); return -1; } netif_set_default(echo_netif); /* specify that the network if is up */ netif_set_up(echo_netif); /* now enable interrupts */ platform_enable_interrupts(); /* start the application (web server, rxtest, txtest, etc..) */ start_application(); /* receive and process packets */ while (1) { xemacif_input(echo_netif); transfer_data(); } /* never reached */ cleanup_platform(); return 0; }
// read ip-ranges from csv file, of format: // from_ip|to_ip|contype|city_code void read_ranges_csv(IPDB * db){ struct timeval tim; double t1 = get_time(&tim); db->ranges = malloc(sizeof(IpRange) * db->max_ranges_count); if(DEBUG) printf("Parsing RANGES-CSV-file: %s\n", db->ranges_csv_file); FILE * f = fopen(db->ranges_csv_file, "rt"); if(f == NULL) { if(DEBUG) printf("Could not open the CSV-file: %s", db->ranges_csv_file); return; } char line[256]; char* from; char* to; char* city_code; int city_index; char* con_type; char* isp_name; uint16 isp_index; int invalid_cities_count = 0; IpRange* entry; db->ranges_count = 0; while (fgets(line, sizeof(line) ,f) && db->ranges_count < db->max_ranges_count){ from = NULL; to = NULL; city_code = NULL; city_index = 0; con_type = NULL; isp_name = NULL; int16 isp_index = -1; if(DEBUG && db->ranges_count % 1000000 == 0) printf("Worked lines: %i\n", db->ranges_count); from = strtok(line, RANGES_DELIM); to = strtok(NULL, RANGES_DELIM); con_type = strtok(NULL, RANGES_DELIM); city_code = strtok(NULL, RANGES_DELIM); isp_name = strtok(NULL, RANGES_DELIM); city_index = city_index_by_code(db, atoi(city_code)); isp_index = isp_index_by_name(db, isp_name); if(city_index < 0) { if(DEBUG) printf("Could not find city for code: %i", atoi(city_code)); invalid_cities_count ++; continue; }else{ entry = &(db->ranges[db->ranges_count]); entry->from = ip_to_int(from); entry->to = ip_to_int(to); entry->is_mobile = con_type_to_int(con_type); entry->city_index = city_index; entry->isp_index = isp_index; db->ranges_count++; } } if(invalid_cities_count) { printf("Found invalid cities: %i", invalid_cities_count); } printf("\n Parsing of %i records needed %.6lf seconds\n", db->ranges_count, get_time(&tim)-t1); }