From a12087c1e9628485e05dedb322933d7476d18310 Mon Sep 17 00:00:00 2001 From: Peter Haag Date: Sun, 10 Jan 2016 12:42:40 +0100 Subject: [PATCH] Add -Z in nfcapd/sfcapd for timezone extension. Breaks expire for now --- bin/nfcapd.c | 45 ++++++++++++++++++++++++++------------------- bin/sfcapd.c | 47 ++++++++++++++++++++++++++++------------------- 2 files changed, 54 insertions(+), 38 deletions(-) diff --git a/bin/nfcapd.c b/bin/nfcapd.c index 1c7e781..f97cb55 100644 --- a/bin/nfcapd.c +++ b/bin/nfcapd.c @@ -1,4 +1,5 @@ /* + * Copyright (c) 2016, Peter Haag * Copyright (c) 2014, Peter Haag * Copyright (c) 2009, Peter Haag * Copyright (c) 2004-2008, SWITCH - Teleinformatikdienste fuer Lehre und Forschung @@ -28,12 +29,7 @@ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * - * $Author: haag $ - * - * $Id: nfcapd.c 51 2010-01-29 09:01:54Z haag $ - * - * $LastChangedRevision: 51 $ - * + * Author: peter * */ @@ -153,7 +149,7 @@ static void daemonize(void); static void SetPriv(char *userid, char *groupid ); static void run(packet_function_t receive_packet, int socket, send_peer_t peer, - time_t twin, time_t t_begin, int report_seq, int use_subdirs, int compress, int do_xstat); + time_t twin, time_t t_begin, int report_seq, int use_subdirs, char *time_extension, int compress, int do_xstat); /* Functions */ static void usage(char *name) { @@ -185,6 +181,7 @@ static void usage(char *name) { "-4\t\tListen on IPv4 (default).\n" "-6\t\tListen on IPv6.\n" "-V\t\tPrint version and exit.\n" + "-Z\t\tAdd timezone offset to filenamet.\n" , name); } // End of usage @@ -363,7 +360,7 @@ int err; #include "collector_inline.c" static void run(packet_function_t receive_packet, int socket, send_peer_t peer, - time_t twin, time_t t_begin, int report_seq, int use_subdirs, int compress, int do_xstat) { + time_t twin, time_t t_begin, int report_seq, int use_subdirs, char *time_extension, int compress, int do_xstat) { common_flow_header_t *nf_header; FlowSource_t *fs; struct sockaddr_storage nf_sender; @@ -469,10 +466,11 @@ srecord_t *commbuff; if ( ((t_now - t_start) >= twin) || done ) { char subfilename[64]; struct tm *now; - char *subdir; + char *subdir, fmt[64]; alarm(0); now = localtime(&t_start); + strftime(fmt, sizeof fmt, time_extension, now); // prepare sub dir hierarchy if ( use_subdirs ) { @@ -483,16 +481,13 @@ srecord_t *commbuff; // failed to generate subdir path - put flows into base directory subdir = NULL; - snprintf(subfilename, 63, "nfcapd.%i%02i%02i%02i%02i", - now->tm_year + 1900, now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min); + snprintf(subfilename, 63, "nfcapd.%s", fmt); } else { - snprintf(subfilename, 63, "%s/nfcapd.%i%02i%02i%02i%02i", subdir, - now->tm_year + 1900, now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min); + snprintf(subfilename, 63, "%s/nfcapd.%s", subdir, fmt); } } else { subdir = NULL; - snprintf(subfilename, 63, "nfcapd.%i%02i%02i%02i%02i", - now->tm_year + 1900, now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min); + snprintf(subfilename, 63, "nfcapd.%s", fmt); } subfilename[63] = '\0'; @@ -765,7 +760,7 @@ int main(int argc, char **argv) { char *bindhost, *filter, *datadir, pidstr[32], *launch_process; char *userid, *groupid, *checkptr, *listenport, *mcastgroup, *extension_tags; -char *Ident, *dynsrcdir, pidfile[MAXPATHLEN]; +char *Ident, *dynsrcdir, *time_extension, pidfile[MAXPATHLEN]; struct stat fstat; packet_function_t receive_packet; send_peer_t peer; @@ -773,7 +768,7 @@ FlowSource_t *fs; struct sigaction act; int family, bufflen; time_t twin, t_start; -int sock, synctime, do_daemonize, expire, report_sequence, do_xstat; +int sock, synctime, do_daemonize, expire, spec_time_extension, report_sequence, do_xstat; int subdir_index, sampling_rate, compress; int c; #ifdef PCAP @@ -799,6 +794,8 @@ char *pcap_file; twin = TIME_WINDOW; datadir = NULL; subdir_index = 0; + time_extension = "%Y%m%d%H%M"; + spec_time_extension = 0; expire = 0; sampling_rate = 1; compress = NOT_COMPRESSED; @@ -810,7 +807,7 @@ char *pcap_file; extension_tags = DefaultExtensions; dynsrcdir = NULL; - while ((c = getopt(argc, argv, "46ef:whEVI:DB:b:jl:J:M:n:p:P:R:S:s:T:t:x:Xru:g:z")) != EOF) { + while ((c = getopt(argc, argv, "46ef:whEVI:DB:b:jl:J:M:n:p:P:R:S:s:T:t:x:Xru:g:zZ")) != EOF) { switch (c) { case 'h': usage(argv[0]); @@ -988,6 +985,10 @@ char *pcap_file; } compress = LZO_COMPRESSED; break; + case 'Z': + time_extension = "%Y%m%d%H%M%z"; + spec_time_extension = 1; + break; case '4': if ( family == AF_UNSPEC ) family = AF_INET; @@ -1028,6 +1029,11 @@ char *pcap_file; exit(255); } + if ( expire && spec_time_extension ) { + fprintf(stderr, "ERROR, -Z timezone extension breaks expire -e\n"); + exit(255); + } + InitExtensionMaps(NO_EXTENSION_LIST); SetupExtensionDescriptors(strdup(extension_tags)); @@ -1219,7 +1225,8 @@ char *pcap_file; sigaction(SIGCHLD, &act, NULL); LogInfo("Startup."); - run(receive_packet, sock, peer, twin, t_start, report_sequence, subdir_index, compress, do_xstat); + run(receive_packet, sock, peer, twin, t_start, report_sequence, subdir_index, + time_extension, compress, do_xstat); close(sock); kill_launcher(launcher_pid); diff --git a/bin/sfcapd.c b/bin/sfcapd.c index 5dcf95b..3121f20 100644 --- a/bin/sfcapd.c +++ b/bin/sfcapd.c @@ -1,4 +1,5 @@ /* + * Copyright (c) 2016, Peter Haag * Copyright (c) 2014, Peter Haag * Copyright (c) 2009, Peter Haag * Copyright (c) 2004-2008, SWITCH - Teleinformatikdienste fuer Lehre und Forschung @@ -28,12 +29,7 @@ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * - * $Author: haag $ - * - * $Id: sfcapd.c 69 2010-09-09 07:17:43Z haag $ - * - * $LastChangedRevision: 69 $ - * + * Author: peter * */ @@ -129,7 +125,8 @@ static void daemonize(void); static void SetPriv(char *userid, char *groupid ); -static void run(packet_function_t receive_packet, int socket, send_peer_t peer, time_t twin, time_t t_begin, int report_seq, char *datadir, int use_subdirs, int compress, int do_xstat); +static void run(packet_function_t receive_packet, int socket, send_peer_t peer, + time_t twin, time_t t_begin, int report_seq, int use_subdirs, char *time_extension, int compress, int do_xstat); /* Functions */ static void usage(char *name) { @@ -159,6 +156,7 @@ static void usage(char *name) { "-4\t\tListen on IPv4 (default).\n" "-6\t\tListen on IPv6.\n" "-V\t\tPrint version and exit.\n" + "-Z\t\tAdd timezone offset to filenamet.\n" , name); } // End of usage @@ -334,7 +332,8 @@ int err; #include "nffile_inline.c" #include "collector_inline.c" -static void run(packet_function_t receive_packet, int socket, send_peer_t peer, time_t twin, time_t t_begin, int report_seq, char *datadir, int use_subdirs, int compress, int do_xstat) { +static void run(packet_function_t receive_packet, int socket, send_peer_t peer, + time_t twin, time_t t_begin, int report_seq, int use_subdirs, char *time_extension, int compress, int do_xstat) { FlowSource_t *fs; struct sockaddr_storage sf_sender; socklen_t sf_sender_size = sizeof(sf_sender); @@ -437,9 +436,10 @@ srecord_t *commbuff; if ( ((t_now - t_start) >= twin) || done ) { char subfilename[64]; struct tm *now; - char *subdir; + char *subdir, fmt[64]; alarm(0); now = localtime(&t_start); + strftime(fmt, sizeof fmt, time_extension, now); // prepare sub dir hierarchy if ( use_subdirs ) { @@ -450,16 +450,13 @@ srecord_t *commbuff; // failed to generate subdir path - put flows into base directory subdir = NULL; - snprintf(subfilename, 63, "nfcapd.%i%02i%02i%02i%02i", - now->tm_year + 1900, now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min); + snprintf(subfilename, 63, "nfcapd.%s", fmt); } else { - snprintf(subfilename, 63, "%s/nfcapd.%i%02i%02i%02i%02i", subdir, - now->tm_year + 1900, now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min); + snprintf(subfilename, 63, "%s/nfcapd.%s", subdir, fmt); } } else { subdir = NULL; - snprintf(subfilename, 63, "nfcapd.%i%02i%02i%02i%02i", - now->tm_year + 1900, now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min); + snprintf(subfilename, 63, "nfcapd.%s", fmt); } subfilename[63] = '\0'; @@ -675,7 +672,7 @@ int main(int argc, char **argv) { char *bindhost, *filter, *datadir, pidstr[32], *launch_process; char *userid, *groupid, *checkptr, *listenport, *mcastgroup, *extension_tags; -char *Ident, *pcap_file, pidfile[MAXPATHLEN]; +char *Ident, *pcap_file, *time_extension, pidfile[MAXPATHLEN]; struct stat fstat; srecord_t *commbuff; packet_function_t receive_packet; @@ -684,7 +681,7 @@ FlowSource_t *fs; struct sigaction act; int family, bufflen; time_t twin, t_start; -int sock, err, synctime, do_daemonize, expire, report_sequence, do_xstat; +int sock, err, synctime, do_daemonize, expire, spec_time_extension, report_sequence, do_xstat; int subdir_index, compress; int c; @@ -705,7 +702,9 @@ int c; twin = TIME_WINDOW; datadir = NULL; subdir_index = 0; + time_extension = "%Y%m%d%H%M"; expire = 0; + spec_time_extension = 0; compress = NOT_COMPRESSED; do_xstat = 0; memset((void *)&peer, 0, sizeof(send_peer_t)); @@ -715,7 +714,7 @@ int c; extension_tags = DefaultExtensions; pcap_file = NULL; - while ((c = getopt(argc, argv, "46ewhEVI:DB:b:f:jl:n:p:J:P:R:S:T:t:x:ru:g:z")) != EOF) { + while ((c = getopt(argc, argv, "46ewhEVI:DB:b:f:jl:n:p:J:P:R:S:T:t:x:ru:g:zZ")) != EOF) { switch (c) { case 'h': usage(argv[0]); @@ -860,6 +859,10 @@ int c; case 'x': launch_process = optarg; break; + case 'Z': + time_extension = "%Y%m%d%H%M%z"; + spec_time_extension = 1; + break; case '4': if ( family == AF_UNSPEC ) family = AF_INET; @@ -882,6 +885,11 @@ int c; } } + if ( expire && spec_time_extension ) { + fprintf(stderr, "ERROR, -Z timezone extension breaks expire -e\n"); + exit(255); + } + InitExtensionMaps(NO_EXTENSION_LIST); SetupExtensionDescriptors(strdup(extension_tags)); @@ -1081,7 +1089,8 @@ int c; sigaction(SIGCHLD, &act, NULL); syslog(LOG_INFO, "Startup."); - run(receive_packet, sock, peer, twin, t_start, report_sequence, datadir, subdir_index, compress, do_xstat); + run(receive_packet, sock, peer, twin, t_start, report_sequence, subdir_index, + time_extension, compress, do_xstat); close(sock); kill_launcher(launcher_pid);