void send_handler(int signal) { int ret; if (signal != SIGALRM) { return; } ur_set(out_tmplt, out_rec, F_FLOWS, cnt_flows); ur_set(out_tmplt, out_rec, F_PACKETS, cnt_packets); ur_set(out_tmplt, out_rec, F_BYTES, cnt_bytes); ret = trap_send(0, out_rec, ur_rec_fixlen_size(out_tmplt)); TRAP_DEFAULT_SEND_ERROR_HANDLING(ret, goto set_alarm, exit(EXIT_FAILURE)); set_alarm: alarm(send_interval); }
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; }