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; }
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; INIT_MODULE_INFO_STRUCT(MODULE_BASIC_INFO, MODULE_PARAMS) // Declare progress structure, pointer to this struct, initialize progress limit NMCM_PROGRESS_DEF; get_o_param(argc, argv, module_getopt_string, long_options); /* output have to be known before TRAP init */ // ***** TRAP initialization ***** TRAP_DEFAULT_INITIALIZATION(argc, argv, *module_info); // Register signal handler. TRAP_REGISTER_DEFAULT_SIGNAL_HANDLER(); // Handles SIGTERM and SIGINT signal(SIGUSR1, signal_handler); signal(SIGALRM, send_handler); // ***** Create UniRec template ***** char *unirec_specifier = "PACKETS,BYTES"; signed char opt; while ((opt = TRAP_GETOPT(argc, argv, module_getopt_string, long_options)) != -1) { switch (opt) { case 'p': NMCM_PROGRESS_INIT(atoi(optarg), return 1); break; case 'P': nmcm_progress_ptr->print_char = optarg[0]; break; case 'o': /* proccessed earlier */ break; default: fprintf(stderr, "Invalid arguments.\n"); FREE_MODULE_INFO_STRUCT(MODULE_BASIC_INFO, MODULE_PARAMS); TRAP_DEFAULT_FINALIZATION(); return 3; } } ur_template_t *tmplt = ur_create_input_template(0, unirec_specifier, NULL); if (tmplt == NULL) { fprintf(stderr, "Error: Invalid UniRec specifier.\n"); TRAP_DEFAULT_FINALIZATION(); FREE_MODULE_INFO_STRUCT(MODULE_BASIC_INFO, MODULE_PARAMS); return 4; } if (send_interval) { /* in case of -o option */ /* create new output tempate */ out_tmplt = ur_create_output_template(0,"FLOWS,PACKETS,BYTES", NULL); if (!out_tmplt) { fprintf(stderr, "Error: Invalid UniRec specifier (this is implementation error, contact author of the module).\n"); trap_finalize(); FREE_MODULE_INFO_STRUCT(MODULE_BASIC_INFO, MODULE_PARAMS); ur_free_template(tmplt); return 4; } /* allocate space for output record with no dynamic part */ out_rec = ur_create_record(out_tmplt, 0); if (!out_rec) { ur_free_template(out_tmplt); TRAP_DEFAULT_FINALIZATION(); FREE_MODULE_INFO_STRUCT(MODULE_BASIC_INFO, MODULE_PARAMS); ur_free_template(tmplt); ur_free_template(out_tmplt); return 4; } /* Set NO_WAIT to output interface */ ret = trap_ifcctl(TRAPIFC_OUTPUT, 0, TRAPCTL_SETTIMEOUT, TRAP_NO_WAIT); if (ret != TRAP_E_OK) { ur_free_template(tmplt); ur_free_template(out_tmplt); ur_free_record(out_rec); fprintf(stderr, "Error: trap_ifcctl.\n"); trap_finalize(); FREE_MODULE_INFO_STRUCT(MODULE_BASIC_INFO, MODULE_PARAMS) return 4; } alarm(send_interval); /* arrange SIGARLM in send_interval seconds */ }