2016-02-09 12:39:49 +00:00
|
|
|
/*
|
|
|
|
This file is part of NetGuard.
|
|
|
|
|
|
|
|
NetGuard is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
NetGuard is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with NetGuard. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2016-12-29 08:27:58 +00:00
|
|
|
Copyright 2015-2017 by Marcel Bokhorst (M66B)
|
2016-02-09 12:39:49 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "netguard.h"
|
|
|
|
|
|
|
|
FILE *pcap_file = NULL;
|
2016-03-11 10:32:15 +00:00
|
|
|
size_t pcap_record_size = 64;
|
2016-02-13 11:28:59 +00:00
|
|
|
long pcap_file_size = 2 * 1024 * 1024;
|
2016-02-09 12:39:49 +00:00
|
|
|
|
|
|
|
void write_pcap_hdr() {
|
|
|
|
struct pcap_hdr_s pcap_hdr;
|
|
|
|
pcap_hdr.magic_number = 0xa1b2c3d4;
|
|
|
|
pcap_hdr.version_major = 2;
|
|
|
|
pcap_hdr.version_minor = 4;
|
|
|
|
pcap_hdr.thiszone = 0;
|
|
|
|
pcap_hdr.sigfigs = 0;
|
2016-02-13 11:28:59 +00:00
|
|
|
pcap_hdr.snaplen = pcap_record_size;
|
2016-02-09 12:39:49 +00:00
|
|
|
pcap_hdr.network = LINKTYPE_RAW;
|
|
|
|
write_pcap(&pcap_hdr, sizeof(struct pcap_hdr_s));
|
|
|
|
}
|
|
|
|
|
|
|
|
void write_pcap_rec(const uint8_t *buffer, size_t length) {
|
|
|
|
struct timespec ts;
|
|
|
|
if (clock_gettime(CLOCK_REALTIME, &ts))
|
|
|
|
log_android(ANDROID_LOG_ERROR, "clock_gettime error %d: %s", errno, strerror(errno));
|
|
|
|
|
2016-02-13 11:28:59 +00:00
|
|
|
size_t plen = (length < pcap_record_size ? length : pcap_record_size);
|
2016-02-09 12:39:49 +00:00
|
|
|
struct pcaprec_hdr_s pcap_rec;
|
|
|
|
|
|
|
|
pcap_rec.ts_sec = (guint32_t) ts.tv_sec;
|
|
|
|
pcap_rec.ts_usec = (guint32_t) (ts.tv_nsec / 1000);
|
|
|
|
pcap_rec.incl_len = (guint32_t) plen;
|
|
|
|
pcap_rec.orig_len = (guint32_t) length;
|
|
|
|
|
|
|
|
write_pcap(&pcap_rec, sizeof(struct pcaprec_hdr_s));
|
|
|
|
write_pcap(buffer, plen);
|
|
|
|
}
|
|
|
|
|
|
|
|
void write_pcap(const void *ptr, size_t len) {
|
|
|
|
if (fwrite(ptr, len, 1, pcap_file) < 1)
|
|
|
|
log_android(ANDROID_LOG_ERROR, "PCAP fwrite error %d: %s", errno, strerror(errno));
|
|
|
|
else {
|
|
|
|
long fsize = ftell(pcap_file);
|
|
|
|
log_android(ANDROID_LOG_VERBOSE, "PCAP wrote %d @%ld", len, fsize);
|
|
|
|
|
2016-02-13 11:28:59 +00:00
|
|
|
if (fsize > pcap_file_size) {
|
2016-02-09 12:39:49 +00:00
|
|
|
log_android(ANDROID_LOG_WARN, "PCAP truncate @%ld", fsize);
|
|
|
|
if (ftruncate(fileno(pcap_file), sizeof(struct pcap_hdr_s)))
|
|
|
|
log_android(ANDROID_LOG_ERROR, "PCAP ftruncate error %d: %s",
|
|
|
|
errno, strerror(errno));
|
|
|
|
else {
|
|
|
|
if (!lseek(fileno(pcap_file), sizeof(struct pcap_hdr_s), SEEK_SET))
|
|
|
|
log_android(ANDROID_LOG_ERROR, "PCAP ftruncate error %d: %s",
|
|
|
|
errno, strerror(errno));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|