]> git.meshlink.io Git - meshlink/blob - test/blackbox/common/tcpdump.c
Add meta-connection attempt callback feature
[meshlink] / test / blackbox / common / tcpdump.c
1 /*
2     tcpdump.c -- Implementation of Black Box Test Execution for meshlink
3
4     Copyright (C) 2018  Guus Sliepen <guus@meshlink.io>
5
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.
10
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.
15
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.
19 */
20 #include <unistd.h>
21 #include <sys/prctl.h>
22 #include <assert.h>
23 #include <fcntl.h>
24 #include <stdio.h>
25 #include <signal.h>
26 #include <sys/types.h>
27 #include "common_handlers.h"
28 #include "tcpdump.h"
29
30 pid_t tcpdump_start(char *interface) {
31         pid_t tcpdump_pid;
32         char *argv[] = { "tcpdump", "-i", interface, NULL };
33         // child process have a pipe to the parent process when parent process terminates SIGPIPE kills the tcpdump
34         int pipes[2];
35         assert(pipe(pipes) != -1);
36         PRINT_TEST_CASE_MSG("\x1b[32mLaunching TCP Dump ..\x1b[0m\n");
37
38         if((tcpdump_pid = fork()) == 0) {
39                 prctl(PR_SET_PDEATHSIG, SIGHUP);
40                 close(pipes[1]);
41                 // Open log file for TCP Dump
42                 int fd = open(TCPDUMP_LOG_FILE, O_WRONLY | O_CREAT | O_TRUNC, 0644);
43                 assert(fd != -1);
44                 close(STDOUT_FILENO);
45                 assert(dup2(fd, STDOUT_FILENO) != -1);
46
47                 // Launch TCPDump with port numbers of sleepy, gateway & relay
48                 int ret = execvp("/usr/sbin/tcpdump", argv);
49                 perror("execvp ");
50                 assert(ret != -1);
51         } else {
52                 close(pipes[0]);
53                 return tcpdump_pid;
54         }
55 }
56
57 void tcpdump_stop(pid_t tcpdump_pid) {
58         PRINT_TEST_CASE_MSG("\n\x1b[32mStopping TCP Dump.\x1b[0m\n");
59         assert(!kill(tcpdump_pid, SIGTERM));
60 }