// Loop forever, converting the incoming GTH data to libpcap format static void convert_to_pcap(GTH_api *api, int data_socket, const char *base_name, const int n_sus_per_file, const int duration_per_file, Channel_t channels[], int n_channels, const enum PCap_format format) { u16 length; GTH_mtp2 signal_unit; int su_count; int file_number = 1; HANDLE_OR_FILEPTR file; int write_to_stdout = 0; int write_to_pipe; write_to_stdout = (strcmp(base_name, "-") == 0); write_to_pipe = is_filename_a_pipe(base_name); init_timer(duration_per_file); while (1) { char filename[MAX_FILENAME]; if (!write_to_stdout && !write_to_pipe) { snprintf(filename, MAX_FILENAME, "%s.%d", base_name, file_number); open_file_for_writing(&file, filename); fprintf(stderr, "saving to file %s\n", filename); } else if (write_to_stdout) { file = stdout_handle_or_file(); fprintf(stderr, "saving capture to stdout\n"); } else { fprintf(stderr, "saving capture to a windows named pipe\n"); file = open_windows_pipe(base_name); } write_pcap_global_header(file, format, channels, n_channels); file_number++; su_count = 0; do { if (wait_for_packet(api, data_socket) != 0) { read_exact(data_socket, (void*)&length, sizeof length); length = ntohs(length); assert(length <= sizeof signal_unit); read_exact(data_socket, (void*)&signal_unit, length); length -= (signal_unit.payload - (char*)&(signal_unit.tag)); write_packet(file, ntohs(signal_unit.timestamp_hi), ntohl(signal_unit.timestamp_lo), ntohs(signal_unit.tag), signal_unit.payload, length, format); flush_file(file); su_count++; } } while ( !is_time_to_rotate(su_count, n_sus_per_file, duration_per_file) || write_to_pipe || write_to_stdout ); fclose(file); } }
// Loop forever, converting the incoming GTH data to libpcap format static void convert_to_pcap(GTH_api *api, int data_socket, const char *base_name, const int n_sus_per_file, const int duration_per_file, const int stop_after_interval, const int output_filename_format, Channel_t channels[], int n_channels, const enum PCap_format format) { u16 length; GTH_mtp2 signal_unit; int su_count; int file_number = 1; HANDLE_OR_FILEPTR file; int write_to_stdout = 0; int write_to_pipe; write_to_stdout = (strcmp(base_name, "-") == 0); write_to_pipe = is_filename_a_pipe(base_name); init_timer(duration_per_file); int always_true = 1; time_t rawtime; struct tm * timeinfo = NULL; unsigned long long interval_threshold=0; u32 curr_sec = 0; u32 curr_usec = 0; unsigned long long curr_ts; while (always_true) { char filename[MAX_FILENAME]; if (!write_to_stdout && !write_to_pipe) { if (output_filename_format > 0) { if(timeinfo==NULL) //setting interval time time(&rawtime); else rawtime+=duration_per_file; //hard setting next interval (duration_per_file is in seconds) if (output_filename_format == 1) timeinfo = localtime(&rawtime); // local time else timeinfo = gmtime(&rawtime); // utc time interval_threshold=convert_epoch_micro(rawtime+duration_per_file); fprintf(stderr, "interval started at %04d/%02d/%02d %02d:%02d:%02d\n", timeinfo->tm_year + 1900, timeinfo->tm_mon + 1, timeinfo->tm_mday, timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); fprintf(stderr, "threshold epoch: %llu\n", interval_threshold); } if (!stop_after_interval) { if (output_filename_format > 0) { snprintf(filename, MAX_FILENAME, "%s_%05d_%04d%02d%02d%02d%02d%02d", base_name, file_number, timeinfo->tm_year + 1900, timeinfo->tm_mon + 1, timeinfo->tm_mday, timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); } else { snprintf(filename, MAX_FILENAME, "%s_%05d", base_name, file_number); } } else { if (output_filename_format > 0) { snprintf(filename, MAX_FILENAME, "%s_%04d%02d%02d%02d%02d%02d", base_name, timeinfo->tm_year + 1900, timeinfo->tm_mon + 1, timeinfo->tm_mday, timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); } else { snprintf(filename, MAX_FILENAME, "%s", base_name); } } open_file_for_writing(&file, filename); fprintf(stderr, "saving to file %s\n", filename); } else if (write_to_stdout) { file = stdout_handle_or_file(); fprintf(stderr, "saving capture to stdout\n"); } else { fprintf(stderr, "saving capture to a windows named pipe\n"); file = open_windows_pipe(base_name); } write_pcap_global_header(file, format, channels, n_channels); file_number++; su_count = 0; int rotation_time_reached = 0; do { if (wait_for_packet(api, data_socket) != 0) { read_exact(data_socket, (void*)&length, sizeof length); length = ntohs(length); assert(length <= sizeof signal_unit); read_exact(data_socket, (void*)&signal_unit, length); curr_sec = ntohs(signal_unit.timestamp_hi); curr_usec = ntohl(signal_unit.timestamp_lo); length -= (signal_unit.payload - (char*)&(signal_unit.tag)); write_packet(file, curr_sec, curr_usec, ntohs(signal_unit.tag), signal_unit.payload, length, format); flush_file(file); su_count++; if (!write_to_pipe && !write_to_stdout) { if (duration_per_file) { curr_ts = convert_timestamp_micro(curr_sec, curr_usec, format); if (curr_ts >= interval_threshold) { fprintf(stderr, "interval threshold triggered.\n"); set_timer(duration_per_file); break; } } } } } while ( !(rotation_time_reached = is_time_to_rotate(su_count, n_sus_per_file, duration_per_file)) || write_to_pipe || write_to_stdout ); fclose(file); if (!write_to_pipe && !write_to_stdout) { if (rotation_time_reached && stop_after_interval) { fprintf(stderr, "stopped capturing when rotation time reached\n"); always_true = 0; } } } }