summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWill Thompson <will@willthompson.co.uk>2020-05-26 07:34:39 +0100
committerWill Thompson <will@willthompson.co.uk>2020-06-05 09:24:02 +0100
commit06a34ec11448366565b16588dc72aecc41682e2c (patch)
tree2dc9bac980181afd0b3b4444c528ae5744ac771c
parentdc19033ac72389f21f16d09445de266689a4af4c (diff)
reader: Encapsulate libpcap
-rw-r--r--c-sources/pcap-monitor.c9
-rw-r--r--c-sources/pcap-reader.c42
-rw-r--r--c-sources/pcap-reader.h12
3 files changed, 38 insertions, 25 deletions
diff --git a/c-sources/pcap-monitor.c b/c-sources/pcap-monitor.c
index 9ecba12..2a5c022 100644
--- a/c-sources/pcap-monitor.c
+++ b/c-sources/pcap-monitor.c
@@ -31,8 +31,6 @@
#include <sys/stat.h>
#include <sys/types.h>
-#include <pcap/pcap.h>
-
#include <glib.h>
#include <glib/gstdio.h>
#include <gio/gunixinputstream.h>
@@ -685,11 +683,12 @@ read_one (
BustlePcapMonitor *self,
GError **error)
{
- struct pcap_pkthdr *hdr;
+ glong sec, usec;
const guchar *blob;
+ guint length;
g_autoptr(GDBusMessage) message = NULL;
- if (!bustle_pcap_reader_read_one (self->reader, &hdr, &blob, &message, error))
+ if (!bustle_pcap_reader_read_one (self->reader, &sec, &usec, &blob, &length, &message, error))
{
return FALSE;
}
@@ -703,7 +702,7 @@ read_one (
else
{
g_signal_emit (self, signals[SIG_MESSAGE_LOGGED], 0,
- hdr->ts.tv_sec, hdr->ts.tv_usec, blob, hdr->caplen, message);
+ sec, usec, blob, length, message);
return TRUE;
}
diff --git a/c-sources/pcap-reader.c b/c-sources/pcap-reader.c
index 7d3dd5f..8d03245 100644
--- a/c-sources/pcap-reader.c
+++ b/c-sources/pcap-reader.c
@@ -139,18 +139,23 @@ bustle_pcap_reader_class_init (BustlePcapReaderClass *klass)
* Returns: %FALSE on error; %TRUE on success or end-of-file.
*/
gboolean
-bustle_pcap_reader_read_one (BustlePcapReader *self,
- struct pcap_pkthdr **hdr,
- const guchar **blob,
- GDBusMessage **message,
- GError **error)
+bustle_pcap_reader_read_one (BustlePcapReader *self,
+ glong *sec,
+ glong *usec,
+ const guchar **blob,
+ guint *length,
+ GDBusMessage **message,
+ GError **error)
{
+ struct pcap_pkthdr *hdr;
int ret;
g_return_val_if_fail (BUSTLE_IS_PCAP_READER (self), FALSE);
- g_return_val_if_fail (hdr != NULL, FALSE);
+ g_return_val_if_fail (sec != NULL, FALSE);
+ g_return_val_if_fail (usec != NULL, FALSE);
g_return_val_if_fail (blob != NULL, FALSE);
- g_return_val_if_fail (message != NULL && *message == NULL, FALSE);
+ g_return_val_if_fail (length != NULL, FALSE);
+ g_return_val_if_fail (message == NULL || *message == NULL, FALSE);
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
if (self->pcap_in == NULL)
@@ -159,24 +164,33 @@ bustle_pcap_reader_read_one (BustlePcapReader *self,
return FALSE;
}
- ret = pcap_next_ex (self->pcap_in, hdr, blob);
+ ret = pcap_next_ex (self->pcap_in, &hdr, blob);
switch (ret)
{
case 1:
- *message = g_dbus_message_new_from_blob ((guchar *) *blob, (*hdr)->caplen, G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING, error);
- if (*message == NULL)
+ if (message != NULL)
{
- g_prefix_error (error, "Error while parsing message from dbus-monitor: ");
- return FALSE;
+ *message = g_dbus_message_new_from_blob ((guchar *) *blob, hdr->caplen, G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING, error);
+ if (*message == NULL)
+ {
+ g_prefix_error (error, "Error while parsing message from dbus-monitor: ");
+ return FALSE;
+ }
}
+ *sec = hdr->ts.tv_sec;
+ *usec = hdr->ts.tv_usec;
+ *length = hdr->caplen;
return TRUE;
case -2:
/* EOF */
- *hdr = NULL;
+ *sec = 0;
+ *usec = 0;
*blob = NULL;
- *message = NULL;
+ *length = 0;
+ if (message != NULL)
+ *message = NULL;
return TRUE;
default:
diff --git a/c-sources/pcap-reader.h b/c-sources/pcap-reader.h
index e8c1a16..7485427 100644
--- a/c-sources/pcap-reader.h
+++ b/c-sources/pcap-reader.h
@@ -32,12 +32,12 @@ BustlePcapReader *bustle_pcap_reader_open (const gchar *filename,
GError **error);
BustlePcapReader *bustle_pcap_reader_fopen (FILE *filep,
GError **error);
-BustlePcapReader *bustle_pcap_reader_new (const gchar *filename,
+gboolean bustle_pcap_reader_read_one (BustlePcapReader *self,
+ glong *sec,
+ glong *usec,
+ const guchar **blob,
+ guint *length,
+ GDBusMessage **message,
GError **error);
-gboolean bustle_pcap_reader_read_one (BustlePcapReader *self,
- struct pcap_pkthdr **hdr,
- const guchar **blob,
- GDBusMessage **message,
- GError **error);
void bustle_pcap_reader_close (BustlePcapReader *self);