int main(int argc, char **argv) { #ifdef UNIREC ur_template_t *tmplt = ur_create_template("SRC_IP,DST_IP,SRC_PORT,DST_PORT,PROTOCOL,PACKETS,BYTES,URL", NULL); if (tmplt == NULL) { fprintf(stderr, "Error when creating UniRec template.\n"); return 1; } char *rec = #else flow_rec_t *rec = (flow_rec_t*) #endif "asdfqwerASDFQWER" "asdfqwerASDFQWER" "bytesxyz" "pkts" "dp" "sp" "p" "\00\x00\x13\x00" "http://example.com/"; int x = 0; uint16_t y = 0; uint64_t z = 0; char tmp_str[20]; time_t start_time = time(NULL); for (int i = 0; i < 1000000000; i++) { #ifdef UNIREC if (ip_is4(ur_get_ptr(tmplt, rec, F_SRC_IP))) x += 1; if (ip_is4(ur_get_ptr(tmplt, rec, F_DST_IP))) x += 1; y += ur_get(tmplt, rec, F_SRC_PORT); y += ur_get(tmplt, rec, F_DST_PORT); y += ur_get(tmplt, rec, F_PROTOCOL); z += ur_get(tmplt, rec, F_PACKETS); z += ur_get(tmplt, rec, F_BYTES); memcpy(tmp_str, ur_get_ptr(tmplt, rec, F_URL), ur_get_var_len(tmplt, rec, F_URL)); #else if (ip_is4(&rec->src_ip)) x += 1; if (ip_is4(&rec->dst_ip)) x += 1; y += rec->src_port; y += rec->dst_port; y += rec->protocol; z += rec->packets; z += rec->bytes; memcpy(tmp_str, rec->url, rec->url_len); #endif } printf("%i %hu %lu\n", x, y, z); printf("Time: %fs\n", difftime(time(NULL), start_time)); #ifdef UNIREC ur_free_template(tmplt); #endif return 0; }
int main(int argc, char **argv) { int ret; signed char opt; ur_template_t *in_tmplt = NULL; char *sock_path = NULL; pthread_t accept_thread; pthread_attr_t thrAttr; pthread_attr_init(&thrAttr); pthread_attr_setdetachstate(&thrAttr, PTHREAD_CREATE_DETACHED); /* **** TRAP initialization **** */ /** * Macro allocates and initializes module_info structure according to MODULE_BASIC_INFO and MODULE_PARAMS * definitions on the lines 69 and 77 of this file. It also creates a string with short_opt letters for getopt * function called "module_getopt_string" and long_options field for getopt_long function in variable "long_options" */ INIT_MODULE_INFO_STRUCT(MODULE_BASIC_INFO, MODULE_PARAMS) /** * Let TRAP library parse program arguments, extract its parameters and initialize module interfaces */ TRAP_DEFAULT_INITIALIZATION(argc, argv, *module_info); /** * Register signal handler. */ TRAP_REGISTER_DEFAULT_SIGNAL_HANDLER(); /** * Parse program arguments defined by MODULE_PARAMS macro with getopt() function (getopt_long() if available) * This macro is defined in config.h file generated by configure script */ while ((opt = TRAP_GETOPT(argc, argv, module_getopt_string, long_options)) != -1) { switch (opt) { default: fprintf(stderr, "Error: Invalid arguments.\n"); goto cleanup; } } /* **** Create UniRec templates **** */ in_tmplt = ur_create_input_template(0, "PROTOCOL", NULL); if (!in_tmplt){ fprintf(stderr, "Error: Input template could not be created.\n"); goto cleanup; } ret = pthread_create(&accept_thread, &thrAttr, accept_clients, NULL); if (ret) { fprintf(stderr, "Error: Thread creation failed.\n"); goto cleanup; } /* **** Main processing loop **** */ // Read data from input, process them and write to output while (!stop) { const void *in_rec; uint16_t in_rec_size; // Receive data from input interface 0. // Block if data are not available immediately (unless a timeout is set using trap_ifcctl) ret = TRAP_RECEIVE(0, in_rec, in_rec_size, in_tmplt); // Handle possible errors TRAP_DEFAULT_RECV_ERROR_HANDLING(ret, continue, break); // Check size of received data if (in_rec_size < ur_rec_fixlen_size(in_tmplt)) { if (in_rec_size <= 1) { break; // End of data (used for testing purposes) } else { fprintf(stderr, "Error: data with wrong size received (expected size: >= %hu, received size: %hu)\n", ur_rec_fixlen_size(in_tmplt), in_rec_size); break; } } // PROCESS THE DATA uint8_t prot = ur_get(in_tmplt, in_rec, F_PROTOCOL); switch (prot) { case icmp: stats.icmp_count++; break; case tcp: stats.tcp_count++; break; case udp: stats.udp_count++; break; case sctp: stats.sctp_count++; break; default: stats.others_count++; break; } } /* **** Cleanup **** */ cleanup: if (in_tmplt) { ur_free_template(in_tmplt); } pthread_attr_destroy(&thrAttr); TRAP_DEFAULT_FINALIZATION(); FREE_MODULE_INFO_STRUCT(MODULE_BASIC_INFO, MODULE_PARAMS) ur_finalize(); return 0; }
int main(int argc, char **argv) { int ret; signed char opt; int mult = 1; /* **** TRAP initialization **** */ /* * Macro allocates and initializes module_info structure according to MODULE_BASIC_INFO and MODULE_PARAMS * definitions on the lines 69 and 77 of this file. It also creates a string with short_opt letters for getopt * function called "module_getopt_string" and long_options field for getopt_long function in variable "long_options" */ INIT_MODULE_INFO_STRUCT(MODULE_BASIC_INFO, MODULE_PARAMS) /* * Let TRAP library parse program arguments, extract its parameters and initialize module interfaces */ TRAP_DEFAULT_INITIALIZATION(argc, argv, *module_info); /* * Register signal handler. */ TRAP_REGISTER_DEFAULT_SIGNAL_HANDLER(); /* * Parse program arguments defined by MODULE_PARAMS macro with getopt() function (getopt_long() if available) * This macro is defined in config.h file generated by configure script */ while ((opt = TRAP_GETOPT(argc, argv, module_getopt_string, long_options)) != -1) { switch (opt) { case 'm': mult = atoi(optarg); break; default: fprintf(stderr, "Invalid arguments.\n"); FREE_MODULE_INFO_STRUCT(MODULE_BASIC_INFO, MODULE_PARAMS); TRAP_DEFAULT_FINALIZATION(); return -1; } } /* **** Create UniRec templates **** */ ur_template_t *in_tmplt = ur_create_input_template(0, "FOO,BAR", NULL); if (in_tmplt == NULL){ fprintf(stderr, "Error: Input template could not be created.\n"); return -1; } ur_template_t *out_tmplt = ur_create_output_template(0, "FOO,BAR,BAZ", NULL); if (out_tmplt == NULL){ ur_free_template(in_tmplt); fprintf(stderr, "Error: Output template could not be created.\n"); return -1; } // Allocate memory for output record void *out_rec = ur_create_record(out_tmplt, 0); if (out_rec == NULL){ ur_free_template(in_tmplt); ur_free_template(out_tmplt); fprintf(stderr, "Error: Memory allocation problem (output record).\n"); return -1; } /* **** Main processing loop **** */ // Read data from input, process them and write to output while (!stop) { const void *in_rec; uint16_t in_rec_size; // Receive data from input interface 0. // Block if data are not available immediately (unless a timeout is set using trap_ifcctl) ret = TRAP_RECEIVE(0, in_rec, in_rec_size, in_tmplt); // Handle possible errors TRAP_DEFAULT_RECV_ERROR_HANDLING(ret, continue, break); // Check size of received data if (in_rec_size < ur_rec_fixlen_size(in_tmplt)) { if (in_rec_size <= 1) { break; // End of data (used for testing purposes) } else { fprintf(stderr, "Error: data with wrong size received (expected size: >= %hu, received size: %hu)\n", ur_rec_fixlen_size(in_tmplt), in_rec_size); break; } } // PROCESS THE DATA // Read FOO and BAR from input record and compute their sum uint32_t baz = ur_get(in_tmplt, in_rec, F_FOO) + ur_get(in_tmplt, in_rec, F_BAR); // Fill output record ur_copy_fields(out_tmplt, out_rec, in_tmplt, in_rec); ur_set(out_tmplt, out_rec, F_BAZ, mult * baz); // Send record to interface 0. // Block if ifc is not ready (unless a timeout is set using trap_ifcctl) ret = trap_send(0, out_rec, ur_rec_fixlen_size(out_tmplt)); // Handle possible errors TRAP_DEFAULT_SEND_ERROR_HANDLING(ret, continue, break); } /* **** Cleanup **** */ // Do all necessary cleanup in libtrap before exiting TRAP_DEFAULT_FINALIZATION(); // Release allocated memory for module_info structure FREE_MODULE_INFO_STRUCT(MODULE_BASIC_INFO, MODULE_PARAMS) // Free unirec templates and output record ur_free_record(out_rec); ur_free_template(in_tmplt); ur_free_template(out_tmplt); ur_finalize(); return 0; }