mirror of
				https://github.com/optim-enterprises-bv/nDPId-2.git
				synced 2025-10-31 02:07:51 +00:00 
			
		
		
		
	added utils module to share some code parts with other apps
Signed-off-by: Toni Uhlig <matzeton@googlemail.com>
This commit is contained in:
		
							
								
								
									
										8
									
								
								Makefile
									
									
									
									
									
								
							
							
						
						
									
										8
									
								
								Makefile
									
									
									
									
									
								
							| @@ -43,11 +43,11 @@ all: help nDPId nDPIsrvd | ||||
|  | ||||
| examples: examples/c-json-stdout/c-json-stdout | ||||
|  | ||||
| nDPId: nDPId.c | ||||
| 	$(CC) $(PROJECT_CFLAGS) $(CFLAGS) $@.c -o $@ $(LDFLAGS) $(LIBS) | ||||
| nDPId: nDPId.c utils.c | ||||
| 	$(CC) $(PROJECT_CFLAGS) $(CFLAGS) $^ -o $@ $(LDFLAGS) $(LIBS) | ||||
|  | ||||
| nDPIsrvd: nDPIsrvd.c | ||||
| 	$(CC) $(PROJECT_CFLAGS) $(CFLAGS) $@.c -o $@ $(LDFLAGS) $(LIBS) | ||||
| nDPIsrvd: nDPIsrvd.c utils.c | ||||
| 	$(CC) $(PROJECT_CFLAGS) $(CFLAGS) $^ -o $@ $(LDFLAGS) $(LIBS) | ||||
|  | ||||
| examples/c-json-stdout/c-json-stdout: | ||||
| ifneq ($(DISABLE_JSMN),yes) | ||||
|   | ||||
							
								
								
									
										97
									
								
								nDPId.c
									
									
									
									
									
								
							
							
						
						
									
										97
									
								
								nDPId.c
									
									
									
									
									
								
							| @@ -17,6 +17,7 @@ | ||||
| #include <unistd.h> | ||||
|  | ||||
| #include "config.h" | ||||
| #include "utils.h" | ||||
|  | ||||
| #if (NDPI_MAJOR == 3 && NDPI_MINOR < 3) || NDPI_MAJOR < 3 | ||||
| #error "nDPI >= 3.3.0 requiired" | ||||
| @@ -198,7 +199,6 @@ static int main_thread_shutdown = 0; | ||||
| static uint32_t global_flow_id = 0; | ||||
|  | ||||
| static char * pcap_file_or_interface = NULL; | ||||
| static int daemonize = 0; | ||||
| static int log_to_stderr = 0; | ||||
| static char pidfile[UNIX_PATH_MAX] = nDPId_PIDFILE; | ||||
| static char json_sockpath[UNIX_PATH_MAX] = COLLECTOR_UNIX_SOCKET; | ||||
| @@ -1647,28 +1647,6 @@ static int processing_threads_error_or_eof(void) | ||||
|     return 1; | ||||
| } | ||||
|  | ||||
| static int create_pidfile(char const * const pidfile) | ||||
| { | ||||
|     int pfd; | ||||
|  | ||||
|     pfd = open(pidfile, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); | ||||
|  | ||||
|     if (pfd < 0) | ||||
|     { | ||||
|         return 1; | ||||
|     } | ||||
|  | ||||
|     if (dprintf(pfd, "%d", getpid()) <= 0) | ||||
|     { | ||||
|         close(pfd); | ||||
|         return 1; | ||||
|     } | ||||
|  | ||||
|     close(pfd); | ||||
|  | ||||
|     return 0; | ||||
| } | ||||
|  | ||||
| static int start_reader_threads(void) | ||||
| { | ||||
|     sigset_t thread_signal_set, old_signal_set; | ||||
| @@ -1682,19 +1660,11 @@ static int start_reader_threads(void) | ||||
|         return 1; | ||||
|     } | ||||
|  | ||||
|     if (daemonize != 0) | ||||
|     if (daemonize_with_pidfile(pidfile) != 0) | ||||
|     { | ||||
|         if (daemon(0, 0) != 0) | ||||
|         { | ||||
|             syslog(LOG_DAEMON | LOG_ERR, "daemon: %s", strerror(errno)); | ||||
|             return 1; | ||||
|         } | ||||
|     } | ||||
|     if (create_pidfile(pidfile) != 0) | ||||
|     { | ||||
|         syslog(LOG_DAEMON | LOG_ERR, "create pidfile: %s", strerror(errno)); | ||||
|         return 1; | ||||
|     } | ||||
|     closelog(); | ||||
|     openlog("nDPId", LOG_CONS | (log_to_stderr != 0 ? LOG_PERROR : 0), LOG_DAEMON); | ||||
|  | ||||
|     for (int i = 0; i < reader_thread_count; ++i) | ||||
| @@ -1736,10 +1706,7 @@ static int stop_reader_threads(void) | ||||
|         break_pcap_loop(&reader_threads[i]); | ||||
|     } | ||||
|  | ||||
|     if (daemonize == 0) | ||||
|     { | ||||
|         printf("------------------------------------ Stopping reader threads\n"); | ||||
|     } | ||||
|     printf("------------------------------------ Stopping reader threads\n"); | ||||
|     for (int i = 0; i < reader_thread_count; ++i) | ||||
|     { | ||||
|         if (reader_threads[i].workflow == NULL) | ||||
| @@ -1753,10 +1720,7 @@ static int stop_reader_threads(void) | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     if (daemonize == 0) | ||||
|     { | ||||
|         printf("------------------------------------ Results\n"); | ||||
|     } | ||||
|     printf("------------------------------------ Results\n"); | ||||
|     for (int i = 0; i < reader_thread_count; ++i) | ||||
|     { | ||||
|         if (reader_threads[i].workflow == NULL) | ||||
| @@ -1770,29 +1734,23 @@ static int stop_reader_threads(void) | ||||
|         total_flows_idle += reader_threads[i].workflow->total_idle_flows; | ||||
|         total_flows_detected += reader_threads[i].workflow->detected_flow_protocols; | ||||
|  | ||||
|         if (daemonize == 0) | ||||
|         { | ||||
|             printf( | ||||
|                 "Stopping Thread %d, processed %10llu packets, %12llu bytes, total flows: %8llu, " | ||||
|                 "idle flows: %8llu, detected flows: %8llu\n", | ||||
|                 reader_threads[i].array_index, | ||||
|                 reader_threads[i].workflow->packets_processed, | ||||
|                 reader_threads[i].workflow->total_l4_data_len, | ||||
|                 reader_threads[i].workflow->total_active_flows, | ||||
|                 reader_threads[i].workflow->total_idle_flows, | ||||
|                 reader_threads[i].workflow->detected_flow_protocols); | ||||
|         } | ||||
|         printf( | ||||
|             "Stopping Thread %d, processed %10llu packets, %12llu bytes, total flows: %8llu, " | ||||
|             "idle flows: %8llu, detected flows: %8llu\n", | ||||
|             reader_threads[i].array_index, | ||||
|             reader_threads[i].workflow->packets_processed, | ||||
|             reader_threads[i].workflow->total_l4_data_len, | ||||
|             reader_threads[i].workflow->total_active_flows, | ||||
|             reader_threads[i].workflow->total_idle_flows, | ||||
|             reader_threads[i].workflow->detected_flow_protocols); | ||||
|     } | ||||
|     /* total packets captured: same value for all threads as packet2thread distribution happens later */ | ||||
|     if (daemonize == 0) | ||||
|     { | ||||
|         printf("Total packets captured.: %llu\n", reader_threads[0].workflow->packets_captured); | ||||
|         printf("Total packets processed: %llu\n", total_packets_processed); | ||||
|         printf("Total layer4 data size.: %llu\n", total_l4_data_len); | ||||
|         printf("Total flows captured...: %llu\n", total_flows_captured); | ||||
|         printf("Total flows timed out..: %llu\n", total_flows_idle); | ||||
|         printf("Total flows detected...: %llu\n", total_flows_detected); | ||||
|     } | ||||
|     printf("Total packets captured.: %llu\n", reader_threads[0].workflow->packets_captured); | ||||
|     printf("Total packets processed: %llu\n", total_packets_processed); | ||||
|     printf("Total layer4 data size.: %llu\n", total_l4_data_len); | ||||
|     printf("Total flows captured...: %llu\n", total_flows_captured); | ||||
|     printf("Total flows timed out..: %llu\n", total_flows_idle); | ||||
|     printf("Total flows detected...: %llu\n", total_flows_detected); | ||||
|  | ||||
|     return 0; | ||||
| } | ||||
| @@ -1849,7 +1807,7 @@ static int parse_options(int argc, char ** argv) | ||||
|                 json_sockpath[sizeof(json_sockpath) - 1] = '\0'; | ||||
|                 break; | ||||
|             case 'd': | ||||
|                 daemonize = 1; | ||||
|                 daemonize_enable(); | ||||
|                 break; | ||||
|             case 'p': | ||||
|                 strncpy(pidfile, optarg, sizeof(pidfile) - 1); | ||||
| @@ -1863,12 +1821,6 @@ static int parse_options(int argc, char ** argv) | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     if (log_to_stderr != 0 && daemonize != 0) | ||||
|     { | ||||
|         fprintf(stderr, "%s: Using -l and -d does not make sense.\n", argv[0]); | ||||
|         return 1; | ||||
|     } | ||||
|  | ||||
|     return 0; | ||||
| } | ||||
|  | ||||
| @@ -1898,13 +1850,13 @@ int main(int argc, char ** argv) | ||||
|  | ||||
|     if (setup_reader_threads(pcap_file_or_interface) != 0) | ||||
|     { | ||||
|         syslog(LOG_DAEMON | LOG_ERR, "%s: setup_reader_threads failed", argv[0]); | ||||
|         syslog(LOG_DAEMON | LOG_ERR, "setup_reader_threads failed"); | ||||
|         return 1; | ||||
|     } | ||||
|  | ||||
|     if (start_reader_threads() != 0) | ||||
|     { | ||||
|         syslog(LOG_DAEMON | LOG_ERR, "%s: start_reader_threads", argv[0]); | ||||
|         syslog(LOG_DAEMON | LOG_ERR, "start_reader_threads failed"); | ||||
|         return 1; | ||||
|     } | ||||
|  | ||||
| @@ -1917,11 +1869,12 @@ int main(int argc, char ** argv) | ||||
|  | ||||
|     if (main_thread_shutdown == 0 && stop_reader_threads() != 0) | ||||
|     { | ||||
|         syslog(LOG_DAEMON | LOG_ERR, "%s: stop_reader_threads", argv[0]); | ||||
|         syslog(LOG_DAEMON | LOG_ERR, "stop_reader_threads"); | ||||
|         return 1; | ||||
|     } | ||||
|     free_reader_threads(); | ||||
|  | ||||
|     daemonize_shutdown(pidfile); | ||||
|     syslog(LOG_DAEMON | LOG_NOTICE, "Bye."); | ||||
|     closelog(); | ||||
|  | ||||
|   | ||||
							
								
								
									
										111
									
								
								utils.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										111
									
								
								utils.c
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,111 @@ | ||||
| #include <errno.h> | ||||
| #include <fcntl.h> | ||||
| #include <stdio.h> | ||||
| #include <string.h> | ||||
| #include <syslog.h> | ||||
| #include <unistd.h> | ||||
|  | ||||
| #include "utils.h" | ||||
|  | ||||
| typedef char pid_str[16]; | ||||
|  | ||||
| static int daemonize = 0; | ||||
|  | ||||
| void daemonize_enable(void) | ||||
| { | ||||
|     daemonize = 1; | ||||
| } | ||||
|  | ||||
| static int is_daemon_running(char const * const pidfile, pid_str ps) | ||||
| { | ||||
|     int pfd = open(pidfile, O_RDONLY, 0); | ||||
|     char proc_path[32]; | ||||
|  | ||||
|     if (pfd < 0) | ||||
|     { | ||||
|         return 0; | ||||
|     } | ||||
|  | ||||
|     if (read(pfd, ps, sizeof(pid_str)) <= 0) | ||||
|     { | ||||
|         return 1; | ||||
|     } | ||||
|  | ||||
|     close(pfd); | ||||
|  | ||||
|     if (snprintf(proc_path, sizeof(pid_str), "/proc/%s", ps) <= 0) | ||||
|     { | ||||
|         return 1; | ||||
|     } | ||||
|  | ||||
|     if (access(proc_path, F_OK) == 0) | ||||
|     { | ||||
|         return 1; | ||||
|     } | ||||
|  | ||||
|     return 0; | ||||
| } | ||||
|  | ||||
| static int create_pidfile(char const * const pidfile) | ||||
| { | ||||
|     int pfd; | ||||
|  | ||||
|     pfd = open(pidfile, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); | ||||
|  | ||||
|     if (pfd < 0) | ||||
|     { | ||||
|         syslog(LOG_DAEMON | LOG_ERR, "Could open pidfile %s for writing: %s", pidfile, strerror(errno)); | ||||
|         return 1; | ||||
|     } | ||||
|  | ||||
|     if (dprintf(pfd, "%d", getpid()) <= 0) | ||||
|     { | ||||
|         close(pfd); | ||||
|         return 1; | ||||
|     } | ||||
|  | ||||
|     close(pfd); | ||||
|  | ||||
|     return 0; | ||||
| } | ||||
|  | ||||
| int daemonize_with_pidfile(char const * const pidfile) | ||||
| { | ||||
|     pid_str ps; | ||||
|  | ||||
|     if (daemonize != 0) | ||||
|     { | ||||
|         if (is_daemon_running(pidfile, ps) != 0) | ||||
|         { | ||||
|             syslog(LOG_DAEMON | LOG_ERR, "Pidfile %s found and daemon %s still running", pidfile, ps); | ||||
|             return 1; | ||||
|         } | ||||
|  | ||||
|         if (daemon(0, 0) != 0) | ||||
|         { | ||||
|             syslog(LOG_DAEMON | LOG_ERR, "daemon: %s", strerror(errno)); | ||||
|             return 1; | ||||
|         } | ||||
|  | ||||
|         if (create_pidfile(pidfile) != 0) | ||||
|         { | ||||
|             return 1; | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     return 0; | ||||
| } | ||||
|  | ||||
| int daemonize_shutdown(char const * const pidfile) | ||||
| { | ||||
|     if (daemonize != 0) | ||||
|     { | ||||
|         if (unlink(pidfile) != 0) | ||||
|         { | ||||
|             syslog(LOG_DAEMON | LOG_ERR, "Could not unlink pidfile %s: %s", pidfile, strerror(errno)); | ||||
|             return 1; | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     return 0; | ||||
| } | ||||
		Reference in New Issue
	
	Block a user
	 Toni Uhlig
					Toni Uhlig