void grok_discover_init(grok_discover_t *gdt, grok_t *source_grok) { TCLIST *names = NULL; int i = 0, len = 0; if (dgrok_init == 0) { grok_discover_global_init(); } gdt->complexity_tree = tctreenew2(tccmpint32, NULL); gdt->base_grok = source_grok; gdt->logmask = source_grok->logmask; gdt->logdepth = source_grok->logdepth; names = grok_pattern_name_list(source_grok); len = tclistnum(names); /* for each pattern, create a grok. * Sort by complexity. * loop * for each pattern, try replacement * if no replacements, break */ for (i = 0; i < len; i++) { int namelen = 0; const char *name = tclistval(names, i, &namelen); int *key = malloc(sizeof(int)); grok_t *g = grok_new(); grok_clone(g, source_grok); char *gpattern; //if (asprintf(&gpattern, "%%{%.*s =~ /\\b/}", namelen, name) == -1) { if (asprintf(&gpattern, "%%{%.*s}", namelen, name) == -1) { perror("asprintf failed"); abort(); } grok_compile(g, gpattern); *key = complexity(g); /* Low complexity should be skipped */ if (*key > -20) { free((void *)g->pattern); free(key); grok_free_clone(g); free(g); continue; } *key *= 1000; /* Inflate so we can insert duplicates */ grok_log(gdt, LOG_DISCOVER, "Including pattern: (complexity: %d) %.*s", *(int *)key, namelen, name); while (!tctreeputkeep(gdt->complexity_tree, key, sizeof(int), g, sizeof(grok_t))) { *key--; } //grok_free_clone(g); //free(key); } tclistdel(names); }
char signal::getPhoneme(){ sample(); if(power()>SILENCE){ coeff = complexity(power()); if(coeff<30 && coeff>20){ return 'u'; } else { if(coeff<33&&coeff>25){ return 'e'; } else{ if(coeff<46&&coeff>33){ return 'o'; } else{ if(coeff>50&&coeff<65){ return 'a'; } else{ if(coeff>70){ return 'h'; } else{ if(coeff>90){ return 's'; } else{ return 'm'; } } } } } } } else{ return ' '; } }
void cond_t::mutation(){ double c = static_cast<double>(complexity()); if (uni_rng() < 0.33){ /* alternate */ if (root) root->alternate(); }else if (uni_rng() < 0.5){ /* delete */ if (!root) return; if (c <= 1.0){ delete root; root = nullptr; } else{ root->deletion(); } } else { /* add */ if (root) root->addition(); else root = new condnode_t(COND_LEAF, static_cast<int>(uni_rng() * condnum)); } };
/** * The recognizer function: takes 1-4ms to execute */ char signal::getPhoneme() { #ifdef ARDUINO_ENVIRONMENT sample(); #endif unsigned int pp =power(); //Serial.print(F("pp=")); Serial.println(pp); if (pp>SILENCE) { //Perform Division int k = complexity(pp); //Low pass filter for noise removal overview[6] = overview[5]; overview[5] = overview[4]; overview[4] = overview[3]; overview[3] = overview[2]; overview[2] = overview[1]; overview[1] = overview[0]; overview[0] = k; int coeff = 0; for (uint8_t f=0; f<6; f++) { coeff += overview[f]; } coeff /= 7; micPower = 0.05 * maxPower() + (1 - 0.05) * micPower; testCoeff = coeff; //Serial.print(F("coeff: ")); Serial.println(coeff); //Use this for debugging //Twiddle with the numbers here if your getting false triggers //This is the main classifier part if (coeff<econstant) { /*Default value = 2*/ phoneme = 'e'; } else if (coeff<aconstant) { /*Default value = 4*/ phoneme = 'o'; } else if (coeff<vconstant) { /*Default value = 6*/ phoneme = 'v'; } else if (coeff<shconstant) { /*Default value = 10*/ phoneme = 'h'; } else { phoneme = 's'; } if (f_enabled) { //Serial.print(F("micPower: ")); Serial.println(micPower); if (micPower > fconstant) { return 'f'; } } return phoneme; } else{ micPower = 0; testCoeff = 0; return ' '; } }
int Function::complexity(const IRExpr *e) { if (dynamic_cast<const IRExprPlaceholder *>(e)) return 0; switch (e->tag) { case Iex_Get: return 0; case Iex_GetI: return 100 + complexity( ((IRExprGetI *)e)->ix ); case Iex_Qop: return 1 + complexity( ((IRExprQop *)e)->arg1) + complexity( ((IRExprQop *)e)->arg2) + complexity( ((IRExprQop *)e)->arg3) + complexity( ((IRExprQop *)e)->arg4); case Iex_Triop: return 1 + complexity( ((IRExprTriop *)e)->arg1) + complexity( ((IRExprTriop *)e)->arg2) + complexity( ((IRExprTriop *)e)->arg3); case Iex_Binop: return 1 + complexity( ((IRExprBinop *)e)->arg1) + complexity( ((IRExprBinop *)e)->arg2); case Iex_Unop: return 1 + complexity( ((IRExprUnop *)e)->arg ); case Iex_Const: return 1; case Iex_Mux0X: return 1 + complexity( ((IRExprMux0X *)e)->cond ) + complexity( ((IRExprMux0X *)e)->expr0 ) + complexity( ((IRExprMux0X *)e)->exprX ); case Iex_CCall: return 100; case Iex_Associative: { IRExprAssociative *iex = (IRExprAssociative *)e; int acc = 1; for (int i = 0; i < iex->nr_arguments; i++) acc += complexity(iex->contents[i]); return acc; } case Iex_Load: return 1 + complexity(((IRExprLoad *)e)->addr); case Iex_HappensBefore: return 1; case Iex_FreeVariable: return 1; case Iex_EntryPoint: return 1; } abort(); }
int Function::complexity() const { return complexity(result); }
int tx_switch(struct cli_def *cli) { // These handles are only used when creating L3 and above packets. libnet_t *l; // the context libnet_ptag_t t2=0, t3=0, t4=0; // handles to layers double cpu_time_used; switch (mode) { case BYTE_STREAM: send_eth(); break; case ARP: if (send_arp()==-1) return 0; break; case BPDU: if (send_bpdu()==-1) return 0; break; case CDP: if (send_cdp()==-1) return 0; break; case IP: // From now on a new much more modular method is used: l = get_link_context(); t3 = create_ip_packet(l); // t3 can be used for later header changes if (!quiet) complexity(); if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes else send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly break; case ICMP: tx.ip_proto = 1; l = get_link_context(); t4 = create_icmp_packet(l); // t4 can be used for later header changes if (t4==-1) return 0; t3 = create_ip_packet(l); // t3 can be used for later header changes if (!quiet) complexity(); if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes else send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly break; case UDP: tx.ip_proto = 17; l = get_link_context(); t4 = create_udp_packet(l); // t4 can be used for later header changes if (t4==-1) return 0; t3 = create_ip_packet(l); // t3 can be used for later header changes if (!quiet) complexity(); if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes else send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly break; case TCP: tx.ip_proto = 6; l = get_link_context(); t4 = create_tcp_packet(l); // t4 can be used for later header changes if (t4==-1) return 0; t3 = create_ip_packet(l); // t3 can be used for later header changes if (!quiet) complexity(); if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes else send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly break; case DNS: tx.ip_proto = 17; l = get_link_context(); if (create_dns_packet()==-1) return 0; t4 = create_udp_packet(l); // t4 can be used for later header changes t3 = create_ip_packet(l); // t3 can be used for later header changes if (!quiet) complexity(); if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes else send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly break; case RTP: tx.ip_proto = 17; l = get_link_context(); if (create_rtp_packet()==-1) return 0; cli_print(cli, "RTP mode! (count=%u, delay=%u usec)\n", tx.count, tx.delay); t4 = create_udp_packet(l); // t4 can be used for later header changes t3 = create_ip_packet(l); // t3 can be used for later header changes if (!quiet) complexity(); if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes else send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly break; case RX_RTP: // Receive RTP packets rcv_rtp_init(); rcv_rtp(); break; case SYSLOG: tx.ip_proto = 17; l = get_link_context(); if (create_syslog_packet()==-1) return 0; t4 = create_udp_packet(l); // t4 can be used for later header changes t3 = create_ip_packet(l); // t3 can be used for later header changes if (!quiet) complexity(); if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes else send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly break; case LLDP: // start with a new concept here //l = get_link_context(); //(void) create_lldp_packet(); // // // printf("SIZE=%lu\n",sizeof(struct tx_struct)); break; default: cli_print(cli,"Unknown mode!\n"); return (1); } // ***** Re-init packet functions: ***** tx.ip_payload_s = 0; tx.udp_len = 0; tx.tcp_payload_s = 0; tx.icmp_payload_s = 0; tx.cdp_sum = 0; mode = 0; // ************************************** mz_stop = clock(); cpu_time_used = ((double) (mz_stop - mz_start)) / CLOCKS_PER_SEC; if (cpu_time_used > 0) { total_d /= cpu_time_used; cli_print(cli, "%.2f seconds (%.Lf packets per second)\n",cpu_time_used,total_d); } return 0; }
int main(int argc, char **argv) { // These handles are only used when creating L3 and above packets. libnet_t *l; // the context libnet_ptag_t t2=0, t3=0, t4=0; // handles to layers double cpu_time_used; reset(); if ( getopts(argc, argv) ) { (void) fprintf(stderr, " Invalid command line parameters!\n"); help(); } // Check whether hires timers are supported or not: (void) check_timer(); signal(SIGINT, signal_handler); // to close all file pointers etc upon SIGINT switch (mode) { case BYTE_STREAM: send_eth(); break; case ARP: (void) send_arp(); break; case BPDU: (void) send_bpdu(); break; case CDP: (void) send_cdp(); break; case IP: // From now on a new much more modular method is used: l = get_link_context(); t3 = create_ip_packet(l); // t3 can be used for later header changes if (!quiet) complexity(); if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes else send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly break; case ICMP: tx.ip_proto = 1; l = get_link_context(); t4 = create_icmp_packet(l); // t4 can be used for later header changes t3 = create_ip_packet(l); // t3 can be used for later header changes if (!quiet) complexity(); if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes else send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly break; case ICMP6: tx.ip_proto = 58; l = get_link_context(); t4 = create_icmp6_packet(l); // t4 can be used for later header changes t3 = create_ip_packet(l); // t3 can be used for later header changes if (ipv6_mode) update_ISUM(l, t4); if (!quiet) complexity(); if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes else send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly break; case UDP: tx.ip_proto = 17; l = get_link_context(); t4 = create_udp_packet(l); // t4 can be used for later header changes t3 = create_ip_packet(l); // t3 can be used for later header changes if (ipv6_mode) update_USUM(l, t4); if (!quiet) complexity(); if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes else send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly break; case TCP: tx.ip_proto = 6; l = get_link_context(); t4 = create_tcp_packet(l); // t4 can be used for later header changes t3 = create_ip_packet(l); // t3 can be used for later header changes if (ipv6_mode) update_TSUM(l, t4); if (!quiet) complexity(); if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes else send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly break; case DNS: tx.ip_proto = 17; l = get_link_context(); (void) create_dns_packet(); t4 = create_udp_packet(l); // t4 can be used for later header changes t3 = create_ip_packet(l); // t3 can be used for later header changes if (!quiet) complexity(); if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes else send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly break; case RTP: tx.ip_proto = 17; l = get_link_context(); if (!quiet) fprintf(stderr, " mz: RTP mode! (count=%u, delay=%u usec)\n\n", tx.count, tx.delay); (void) create_rtp_packet(); t4 = create_udp_packet(l); // t4 can be used for later header changes t3 = create_ip_packet(l); // t3 can be used for later header changes if (!quiet) complexity(); if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes else send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly break; case RX_RTP: // Receive RTP packets rcv_rtp_init(); rcv_rtp(); break; case SYSLOG: tx.ip_proto = 17; l = get_link_context(); (void) create_syslog_packet(); t4 = create_udp_packet(l); // t4 can be used for later header changes t3 = create_ip_packet(l); // t3 can be used for later header changes if (!quiet) complexity(); if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes else send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly break; case LLDP: // start with a new concept here //l = get_link_context(); //(void) create_lldp_packet(); // // // printf("SIZE=%lu\n",sizeof(struct tx_struct)); fprintf(stderr, "LLDP is currently only supported via the interactive mode\n"); exit(1); break; default: (void) fprintf(stderr," mz/main: unknown mode! Stop.\n"); return (1); } if (!quiet) { mz_stop = clock(); cpu_time_used = ((double) (mz_stop - mz_start)) / CLOCKS_PER_SEC; if (cpu_time_used > 0) { total_d /= cpu_time_used; fprintf(stderr, "%.2f seconds (%.Lf packets per second)\n",cpu_time_used,total_d); } else { fprintf(stderr, "\n"); } } return(0); }