2 tcpdump.c -- Implementation of Black Box Test Execution for meshlink
4 Copyright (C) 2018 Guus Sliepen <guus@meshlink.io>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include <sys/prctl.h>
32 #include <sys/types.h>
33 #include "common_handlers.h"
36 pid_t tcpdump_start(char *interface) {
37 char *argv[] = { "tcpdump", "-i", interface, NULL };
38 // child process have a pipe to the parent process when parent process terminates SIGPIPE kills the tcpdump
40 assert(pipe(pipes) != -1);
41 PRINT_TEST_CASE_MSG("\x1b[32mLaunching TCP Dump ..\x1b[0m\n");
43 pid_t tcpdump_pid = fork();
45 if(tcpdump_pid == 0) {
46 prctl(PR_SET_PDEATHSIG, SIGHUP);
48 // Open log file for TCP Dump
49 int fd = open(TCPDUMP_LOG_FILE, O_WRONLY | O_CREAT | O_TRUNC, 0644);
52 assert(dup2(fd, STDOUT_FILENO) != -1);
54 // Launch TCPDump with port numbers of sleepy, gateway & relay
55 execvp("/usr/sbin/tcpdump", argv);
65 void tcpdump_stop(pid_t tcpdump_pid) {
66 PRINT_TEST_CASE_MSG("\n\x1b[32mStopping TCP Dump.\x1b[0m\n");
67 assert(!kill(tcpdump_pid, SIGTERM));