X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=src%2Ftincctl.c;h=5477fd0e4b4cbb2139b93fcb1e2f39b07b9197be;hb=d772289f6d6adfb8932658b533349d43f08ec326;hp=641fb9c4edd9de90e6f0af9ff8537ab5b4274305;hpb=8de8f1d9e2c2c02d4a14a5506e7d0d914dc328da;p=meshlink diff --git a/src/tincctl.c b/src/tincctl.c index 641fb9c4..5477fd0e 100644 --- a/src/tincctl.c +++ b/src/tincctl.c @@ -30,19 +30,19 @@ #include "top.h" /* The name this program was run with. */ -char *program_name = NULL; +static char *program_name = NULL; /* If nonzero, display usage information and exit. */ -bool show_help = false; +static bool show_help = false; /* If nonzero, print the version on standard output and exit. */ -bool show_version = false; +static bool show_version = false; /* If nonzero, it will attempt to kill a running tincd and exit. */ -int kill_tincd = 0; +static int kill_tincd = 0; /* If nonzero, generate public/private keypair for this host/net. */ -int generate_keys = 0; +static int generate_keys = 0; static char *name = NULL; static char *identname = NULL; /* program name for syslog */ @@ -98,6 +98,7 @@ static void usage(bool status) { #ifdef HAVE_CURSES " top Show real-time statistics\n" #endif + " pcap Dump traffic in pcap format\n" "\n"); printf("Report bugs to tinc@tinc-vpn.org.\n"); } @@ -301,9 +302,10 @@ static void make_names(void) { } } +static char buffer[4096]; +static size_t blen = 0; + bool recvline(int fd, char *line, size_t len) { - static char buffer[4096]; - static size_t blen = 0; char *newline = NULL; while(!(newline = memchr(buffer, '\n', blen))) { @@ -328,6 +330,23 @@ bool recvline(int fd, char *line, size_t len) { return true; } +bool recvdata(int fd, char *data, size_t len) { + while(blen < len) { + int result = recv(fd, buffer + blen, sizeof buffer - blen, 0); + if(result == -1 && errno == EINTR) + continue; + else if(result <= 0) + return false; + blen += result; + } + + memcpy(data, buffer, len); + memmove(buffer, buffer + len, blen - len); + blen -= len; + + return true; +} + bool sendline(int fd, char *format, ...) { static char buffer[4096]; char *p = buffer; @@ -357,6 +376,57 @@ bool sendline(int fd, char *format, ...) { return true; } +void pcap(int fd, FILE *out) { + sendline(fd, "%d %d", CONTROL, REQ_PCAP); + char data[9018]; + + struct { + uint32_t magic; + uint16_t major; + uint16_t minor; + uint32_t tz_offset; + uint32_t tz_accuracy; + uint32_t snaplen; + uint32_t ll_type; + } header = { + 0xa1b2c3d4, + 2, 4, + 0, 0, + sizeof data, + 1, + }; + + struct { + uint32_t tv_sec; + uint32_t tv_usec; + uint32_t len; + uint32_t origlen; + } packet; + + struct timeval tv; + + fwrite(&header, sizeof header, 1, out); + fflush(out); + + char line[32]; + while(recvline(fd, line, sizeof line)) { + int code, req, len; + int n = sscanf(line, "%d %d %d", &code, &req, &len); + gettimeofday(&tv, NULL); + if(n != 3 || code != CONTROL || req != REQ_PCAP || len < 0 || len > sizeof data) + break; + if(!recvdata(fd, data, len)) + break; + packet.tv_sec = tv.tv_sec; + packet.tv_usec = tv.tv_usec; + packet.len = len; + packet.origlen = len; + fwrite(&packet, sizeof packet, 1, out); + fwrite(data, len, 1, out); + fflush(out); + } +} + int main(int argc, char *argv[], char *envp[]) { int fd; int result; @@ -636,6 +706,11 @@ int main(int argc, char *argv[], char *envp[]) { } #endif + if(!strcasecmp(argv[optind], "pcap")) { + pcap(fd, stdout); + return 0; + } + fprintf(stderr, "Unknown command `%s'.\n", argv[optind]); usage(true);