static int matchStringProtocol(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow, char *string_to_match, u_int string_to_match_len) { int i = 0, end = string_to_match_len-1, num_found = 0; struct ndpi_packet_struct *packet = &flow->packet; #ifdef AHOCORASICK AC_TEXT_t ac_input_text; #endif while(end > 0) { if(string_to_match[end] == '.') { num_found++; if(num_found == 2) { end++; break; } } end--; } strncpy(flow->l4.tcp.host_server_name, &string_to_match[end], ndpi_min(sizeof(flow->l4.tcp.host_server_name)-1, string_to_match_len-end)); #ifdef AHOCORASICK matching_protocol_id = -1; ac_input_text.astring = string_to_match; ac_input_text.length = string_to_match_len; ac_automata_search (ac_automa, &ac_input_text, 0); ac_automata_reset(ac_automa); if (matching_protocol_id != -1) { packet->detected_protocol_stack[0] = matching_protocol_id; return(packet->detected_protocol_stack[0]); } #else for (i = 0; i < host_match_num_items; i++) { if(ndpi_strnstr(string_to_match, host_match[i].string_to_match, string_to_match_len) != NULL) { packet->detected_protocol_stack[0] = host_match[i].protocol_id; return(packet->detected_protocol_stack[0]); } else i++; } #endif #ifdef DEBUG string_to_match[string_to_match_len] = '\0'; printf("[NTOP] Unable to find a match for '%s'\n", string_to_match); #endif return(-1); }
static int lahocorasick_reset ( lua_State* L ) { AC_AUTOMATA_t* m = *(AC_AUTOMATA_t**)luaL_checkudata ( L , 1 , AHO_METATABLE_KEY ); ac_automata_reset ( m ); return 0; }
int main (int argc, char ** argv) { //*** 2. Define AC variables AC_AUTOMATA_t * acap; AC_TEXT_t input_text = {0, 0}; #define PATTERN_NUMBER (sizeof(allpattern)/sizeof(AC_PATTERN_t)) unsigned int i; /* Sending parameter to call-back function */ struct sample_param my_param; my_param.anum = 1; my_param.achar = 'a'; /* 'a': find all, 'f': find first */ //*** 3. Get a new automata acap = ac_automata_init (match_handler); //*** 4. add patterns to automata for (i=0; i<PATTERN_NUMBER; i++) { allpattern[i].length = strlen(allpattern[i].astring); ac_automata_add (acap, &allpattern[i]); } //*** 5. Finalize automata. ac_automata_finalize (acap); //*** 5.1 Display automata //ac_automata_display (acap, 's'); /* This illustrates how to search big text chunk by chunk * in this example input buffer size is 2500 and input file is pretty * bigger than that. in fact it imitate reading from input file. * in such situations searching must be done inside a loop. the loop * continues until it consumes all input file. **/ FILE *fp; char *line; size_t len=0; int k=0; char tolower_input_line[10000]; fp = fopen(argv[1], "r"); if(fp == NULL) printf("File Couldn't Open\n"); input_line[0]='_'; tolower_input_line[0]='_'; while(getline(&line, &len, fp)!=-1) { strcpy(input_line+1, line); // printf("%s", input_line); //calling function replace_space_with_underscore() replace_space_with_underscore(input_line); *(input_line+len)='_'; k=0; strcpy(tolower_input_line+1, line); //tolower while(tolower_input_line[k]) { *(tolower_input_line+k)= tolower(tolower_input_line[k]); k++; } /*****appending original line and tolower of the same line *(input_line+strcspn(input_line, "\n"))= '/'; strcat(input_line, tolower_input_line); **********/ //calling function replace_space_with_underscore() replace_space_with_underscore(tolower_input_line); *(tolower_input_line+len)='_'; char * chunk_start = input_line; // char * end_of_file = input_line+sizeof(input_line); char * end_of_file = input_line+strlen(input_line); input_text.astring = buffer; /* Search loop */ while (chunk_start<end_of_file) { //*** 6. Set input text input_text.length = (chunk_start<end_of_file)? sizeof(buffer):(sizeof(input_line)%sizeof(buffer)); strncpy(input_text.astring, chunk_start, input_text.length); //*** 7. Do search if(ac_automata_search (acap, &input_text, (void *)(&my_param))) break; /* according to the return value of ac_automata_search() we decide to * continue or break loop. */ chunk_start += sizeof(buffer); } //*** 8. Reset ac_automata_reset(acap); //Checking multi words after tolowering the i/p. strcpy(input_line,tolower_input_line); chunk_start = input_line; end_of_file = input_line+strlen(input_line); input_text.astring = buffer; /* Search loop */ while (chunk_start<end_of_file) { //*** 6. Set input text input_text.length = (chunk_start<end_of_file)? sizeof(buffer):(sizeof(input_line)%sizeof(buffer)); strncpy(input_text.astring, chunk_start, input_text.length); //*** 7. Do search if(ac_automata_search (acap, &input_text, (void *)(&my_param))) break; /* according to the return value of ac_automata_search() we decide to * continue or break loop. */ chunk_start += sizeof(buffer); } ac_automata_reset(acap); printf(";~~~~~~~~~~\n"); } //*** 9. Release automata ac_automata_release (acap); return 0; }