2008-08-24 13:53:29 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2007 Ole André Vadla Ravnås <ole.andre.ravnas@tandberg.com>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
2012-11-03 20:38:00 +00:00
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
2008-08-24 13:53:29 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SECTION:element-pcapparse
|
2017-03-08 18:01:13 +00:00
|
|
|
* @title: pcapparse
|
2008-08-24 13:53:29 +00:00
|
|
|
*
|
2011-07-14 14:40:14 +00:00
|
|
|
* Extracts payloads from Ethernet-encapsulated IP packets.
|
|
|
|
* Use #GstPcapParse:src-ip, #GstPcapParse:dst-ip,
|
2008-08-24 16:55:48 +00:00
|
|
|
* #GstPcapParse:src-port and #GstPcapParse:dst-port to restrict which packets
|
|
|
|
* should be included.
|
|
|
|
*
|
2019-08-23 17:56:35 +00:00
|
|
|
* The supported data format is the classical
|
|
|
|
* [libpcap file format](https://wiki.wireshark.org/Development/LibpcapFileFormat)
|
2018-04-14 18:14:35 +00:00
|
|
|
*
|
2017-03-08 18:01:13 +00:00
|
|
|
* ## Example pipelines
|
2008-08-24 16:55:48 +00:00
|
|
|
* |[
|
2015-11-19 17:24:53 +00:00
|
|
|
* gst-launch-1.0 filesrc location=h264crasher.pcap ! pcapparse ! rtph264depay
|
2008-08-24 16:55:48 +00:00
|
|
|
* ! ffdec_h264 ! fakesink
|
|
|
|
* ]| Read from a pcap dump file using filesrc, extract the raw UDP packets,
|
|
|
|
* depayload and decode them.
|
2017-03-08 18:01:13 +00:00
|
|
|
*
|
2008-08-24 13:53:29 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* TODO:
|
|
|
|
* - Implement support for timestamping the buffers.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
2010-11-03 22:02:49 +00:00
|
|
|
#include <config.h>
|
2008-08-24 13:53:29 +00:00
|
|
|
#endif
|
|
|
|
|
2008-08-26 11:01:43 +00:00
|
|
|
#include "gstpcapparse.h"
|
|
|
|
|
2008-09-15 09:39:00 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2008-12-29 15:54:44 +00:00
|
|
|
#ifndef G_OS_WIN32
|
2008-08-24 13:53:29 +00:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netinet/in.h>
|
2008-08-26 11:01:43 +00:00
|
|
|
#include <string.h>
|
2008-08-24 13:53:29 +00:00
|
|
|
#else
|
|
|
|
#include <winsock2.h>
|
|
|
|
#endif
|
|
|
|
|
2017-08-03 12:42:14 +00:00
|
|
|
|
|
|
|
const guint GST_PCAPPARSE_MAGIC_MILLISECOND_NO_SWAP_ENDIAN = 0xa1b2c3d4;
|
|
|
|
const guint GST_PCAPPARSE_MAGIC_NANOSECOND_NO_SWAP_ENDIAN = 0xa1b23c4d;
|
|
|
|
const guint GST_PCAPPARSE_MAGIC_MILLISECOND_SWAP_ENDIAN = 0xd4c3b2a1;
|
|
|
|
const guint GST_PCAPPARSE_MAGIC_NANOSECOND_SWAP_ENDIAN = 0x4d3cb2a1;
|
|
|
|
|
|
|
|
|
2008-08-24 13:53:29 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_0,
|
|
|
|
PROP_SRC_IP,
|
|
|
|
PROP_DST_IP,
|
|
|
|
PROP_SRC_PORT,
|
|
|
|
PROP_DST_PORT,
|
2010-09-22 14:41:33 +00:00
|
|
|
PROP_CAPS,
|
2015-04-24 15:48:23 +00:00
|
|
|
PROP_TS_OFFSET
|
2008-08-24 13:53:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_STATIC (gst_pcap_parse_debug);
|
|
|
|
#define GST_CAT_DEFAULT gst_pcap_parse_debug
|
|
|
|
|
|
|
|
static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
|
|
|
|
GST_PAD_SINK,
|
|
|
|
GST_PAD_ALWAYS,
|
|
|
|
GST_STATIC_CAPS ("raw/x-pcap"));
|
|
|
|
|
|
|
|
static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
|
|
|
|
GST_PAD_SRC,
|
|
|
|
GST_PAD_ALWAYS,
|
|
|
|
GST_STATIC_CAPS_ANY);
|
|
|
|
|
2010-09-22 14:41:33 +00:00
|
|
|
static void gst_pcap_parse_finalize (GObject * object);
|
2008-08-24 13:53:29 +00:00
|
|
|
static void gst_pcap_parse_get_property (GObject * object, guint prop_id,
|
|
|
|
GValue * value, GParamSpec * pspec);
|
|
|
|
static void gst_pcap_parse_set_property (GObject * object, guint prop_id,
|
|
|
|
const GValue * value, GParamSpec * pspec);
|
2015-09-17 14:10:10 +00:00
|
|
|
static GstStateChangeReturn
|
|
|
|
gst_pcap_parse_change_state (GstElement * element, GstStateChange transition);
|
2008-08-24 13:53:29 +00:00
|
|
|
|
|
|
|
static void gst_pcap_parse_reset (GstPcapParse * self);
|
|
|
|
|
2012-01-16 21:52:13 +00:00
|
|
|
static GstFlowReturn gst_pcap_parse_chain (GstPad * pad,
|
|
|
|
GstObject * parent, GstBuffer * buffer);
|
|
|
|
static gboolean gst_pcap_sink_event (GstPad * pad,
|
|
|
|
GstObject * parent, GstEvent * event);
|
2008-08-24 13:53:29 +00:00
|
|
|
|
2015-09-17 14:10:10 +00:00
|
|
|
|
2012-01-16 21:52:13 +00:00
|
|
|
#define parent_class gst_pcap_parse_parent_class
|
|
|
|
G_DEFINE_TYPE (GstPcapParse, gst_pcap_parse, GST_TYPE_ELEMENT);
|
2008-08-24 13:53:29 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
gst_pcap_parse_class_init (GstPcapParseClass * klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
|
2012-01-16 21:52:13 +00:00
|
|
|
GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
|
2008-08-24 13:53:29 +00:00
|
|
|
|
2010-09-22 14:41:33 +00:00
|
|
|
gobject_class->finalize = gst_pcap_parse_finalize;
|
2008-08-24 13:53:29 +00:00
|
|
|
gobject_class->get_property = gst_pcap_parse_get_property;
|
|
|
|
gobject_class->set_property = gst_pcap_parse_set_property;
|
|
|
|
|
|
|
|
g_object_class_install_property (gobject_class,
|
|
|
|
PROP_SRC_IP, g_param_spec_string ("src-ip", "Source IP",
|
2010-10-19 10:43:14 +00:00
|
|
|
"Source IP to restrict to", "",
|
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
2008-08-24 13:53:29 +00:00
|
|
|
|
|
|
|
g_object_class_install_property (gobject_class,
|
|
|
|
PROP_DST_IP, g_param_spec_string ("dst-ip", "Destination IP",
|
2010-10-19 10:43:14 +00:00
|
|
|
"Destination IP to restrict to", "",
|
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
2008-08-24 13:53:29 +00:00
|
|
|
|
|
|
|
g_object_class_install_property (gobject_class,
|
|
|
|
PROP_SRC_PORT, g_param_spec_int ("src-port", "Source port",
|
|
|
|
"Source port to restrict to", -1, G_MAXUINT16, -1,
|
2010-10-19 10:43:14 +00:00
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
2008-08-24 13:53:29 +00:00
|
|
|
|
|
|
|
g_object_class_install_property (gobject_class,
|
|
|
|
PROP_DST_PORT, g_param_spec_int ("dst-port", "Destination port",
|
|
|
|
"Destination port to restrict to", -1, G_MAXUINT16, -1,
|
2010-10-19 10:43:14 +00:00
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
2008-08-24 13:53:29 +00:00
|
|
|
|
2010-09-22 14:41:33 +00:00
|
|
|
g_object_class_install_property (gobject_class, PROP_CAPS,
|
|
|
|
g_param_spec_boxed ("caps", "Caps",
|
2010-10-19 10:43:14 +00:00
|
|
|
"The caps of the source pad", GST_TYPE_CAPS,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
2010-09-22 14:41:33 +00:00
|
|
|
|
2011-07-15 08:04:53 +00:00
|
|
|
g_object_class_install_property (gobject_class, PROP_TS_OFFSET,
|
|
|
|
g_param_spec_int64 ("ts-offset", "Timestamp Offset",
|
2011-07-14 14:50:29 +00:00
|
|
|
"Relative timestamp offset (ns) to apply (-1 = use absolute packet time)",
|
|
|
|
-1, G_MAXINT64, -1, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
|
|
|
|
2016-03-04 06:50:26 +00:00
|
|
|
gst_element_class_add_static_pad_template (element_class, &sink_template);
|
|
|
|
gst_element_class_add_static_pad_template (element_class, &src_template);
|
2012-01-16 21:52:13 +00:00
|
|
|
|
2015-09-17 14:10:10 +00:00
|
|
|
element_class->change_state = gst_pcap_parse_change_state;
|
|
|
|
|
2012-10-17 16:34:26 +00:00
|
|
|
gst_element_class_set_static_metadata (element_class, "PCapParse",
|
2012-01-16 21:52:13 +00:00
|
|
|
"Raw/Parser",
|
|
|
|
"Parses a raw pcap stream",
|
|
|
|
"Ole André Vadla Ravnås <ole.andre.ravnas@tandberg.com>");
|
|
|
|
|
2008-08-24 13:53:29 +00:00
|
|
|
GST_DEBUG_CATEGORY_INIT (gst_pcap_parse_debug, "pcapparse", 0, "pcap parser");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-01-16 21:52:13 +00:00
|
|
|
gst_pcap_parse_init (GstPcapParse * self)
|
2008-08-24 13:53:29 +00:00
|
|
|
{
|
|
|
|
self->sink_pad = gst_pad_new_from_static_template (&sink_template, "sink");
|
|
|
|
gst_pad_set_chain_function (self->sink_pad,
|
|
|
|
GST_DEBUG_FUNCPTR (gst_pcap_parse_chain));
|
|
|
|
gst_pad_use_fixed_caps (self->sink_pad);
|
2009-09-02 01:30:35 +00:00
|
|
|
gst_pad_set_event_function (self->sink_pad,
|
|
|
|
GST_DEBUG_FUNCPTR (gst_pcap_sink_event));
|
2008-08-24 13:53:29 +00:00
|
|
|
gst_element_add_pad (GST_ELEMENT (self), self->sink_pad);
|
|
|
|
|
|
|
|
self->src_pad = gst_pad_new_from_static_template (&src_template, "src");
|
|
|
|
gst_pad_use_fixed_caps (self->src_pad);
|
|
|
|
gst_element_add_pad (GST_ELEMENT (self), self->src_pad);
|
|
|
|
|
|
|
|
self->src_ip = -1;
|
|
|
|
self->dst_ip = -1;
|
|
|
|
self->src_port = -1;
|
|
|
|
self->dst_port = -1;
|
2011-07-14 14:50:29 +00:00
|
|
|
self->offset = -1;
|
2008-08-24 13:53:29 +00:00
|
|
|
|
|
|
|
self->adapter = gst_adapter_new ();
|
|
|
|
|
|
|
|
gst_pcap_parse_reset (self);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-09-22 14:41:33 +00:00
|
|
|
gst_pcap_parse_finalize (GObject * object)
|
2008-08-24 13:53:29 +00:00
|
|
|
{
|
|
|
|
GstPcapParse *self = GST_PCAP_PARSE (object);
|
|
|
|
|
|
|
|
g_object_unref (self->adapter);
|
2010-09-22 14:41:33 +00:00
|
|
|
if (self->caps)
|
|
|
|
gst_caps_unref (self->caps);
|
2008-08-24 13:53:29 +00:00
|
|
|
|
2010-09-22 14:41:33 +00:00
|
|
|
G_OBJECT_CLASS (parent_class)->finalize (object);
|
2008-08-24 13:53:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const gchar *
|
|
|
|
get_ip_address_as_string (gint64 ip_addr)
|
|
|
|
{
|
|
|
|
if (ip_addr >= 0) {
|
|
|
|
struct in_addr addr;
|
|
|
|
addr.s_addr = ip_addr;
|
|
|
|
return inet_ntoa (addr);
|
|
|
|
} else {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
set_ip_address_from_string (gint64 * ip_addr, const gchar * ip_str)
|
|
|
|
{
|
|
|
|
if (ip_str[0] != '\0') {
|
|
|
|
gulong addr = inet_addr (ip_str);
|
|
|
|
if (addr != INADDR_NONE)
|
|
|
|
*ip_addr = addr;
|
|
|
|
} else {
|
|
|
|
*ip_addr = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_pcap_parse_get_property (GObject * object, guint prop_id,
|
|
|
|
GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
GstPcapParse *self = GST_PCAP_PARSE (object);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case PROP_SRC_IP:
|
|
|
|
g_value_set_string (value, get_ip_address_as_string (self->src_ip));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_DST_IP:
|
|
|
|
g_value_set_string (value, get_ip_address_as_string (self->dst_ip));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_SRC_PORT:
|
|
|
|
g_value_set_int (value, self->src_port);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_DST_PORT:
|
|
|
|
g_value_set_int (value, self->dst_port);
|
|
|
|
break;
|
|
|
|
|
2010-09-22 14:41:33 +00:00
|
|
|
case PROP_CAPS:
|
|
|
|
gst_value_set_caps (value, self->caps);
|
|
|
|
break;
|
|
|
|
|
2011-07-15 08:04:53 +00:00
|
|
|
case PROP_TS_OFFSET:
|
2011-07-14 14:50:29 +00:00
|
|
|
g_value_set_int64 (value, self->offset);
|
|
|
|
break;
|
|
|
|
|
2008-08-24 13:53:29 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_pcap_parse_set_property (GObject * object, guint prop_id,
|
|
|
|
const GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
GstPcapParse *self = GST_PCAP_PARSE (object);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case PROP_SRC_IP:
|
|
|
|
set_ip_address_from_string (&self->src_ip, g_value_get_string (value));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_DST_IP:
|
|
|
|
set_ip_address_from_string (&self->dst_ip, g_value_get_string (value));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_SRC_PORT:
|
|
|
|
self->src_port = g_value_get_int (value);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_DST_PORT:
|
|
|
|
self->dst_port = g_value_get_int (value);
|
|
|
|
break;
|
|
|
|
|
2010-09-22 14:41:33 +00:00
|
|
|
case PROP_CAPS:
|
|
|
|
{
|
|
|
|
const GstCaps *new_caps_val;
|
|
|
|
GstCaps *new_caps, *old_caps;
|
|
|
|
|
|
|
|
new_caps_val = gst_value_get_caps (value);
|
|
|
|
if (new_caps_val == NULL) {
|
|
|
|
new_caps = gst_caps_new_any ();
|
|
|
|
} else {
|
|
|
|
new_caps = gst_caps_copy (new_caps_val);
|
|
|
|
}
|
|
|
|
|
|
|
|
old_caps = self->caps;
|
|
|
|
self->caps = new_caps;
|
|
|
|
if (old_caps)
|
|
|
|
gst_caps_unref (old_caps);
|
|
|
|
|
|
|
|
gst_pad_set_caps (self->src_pad, new_caps);
|
|
|
|
break;
|
|
|
|
}
|
2011-07-14 14:50:29 +00:00
|
|
|
|
2011-07-15 08:04:53 +00:00
|
|
|
case PROP_TS_OFFSET:
|
2011-07-14 14:50:29 +00:00
|
|
|
self->offset = g_value_get_int64 (value);
|
|
|
|
break;
|
|
|
|
|
2008-08-24 13:53:29 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_pcap_parse_reset (GstPcapParse * self)
|
|
|
|
{
|
|
|
|
self->initialized = FALSE;
|
|
|
|
self->swap_endian = FALSE;
|
2017-08-03 12:42:14 +00:00
|
|
|
self->nanosecond_timestamp = FALSE;
|
2008-08-24 13:53:29 +00:00
|
|
|
self->cur_packet_size = -1;
|
2009-09-02 01:30:35 +00:00
|
|
|
self->cur_ts = GST_CLOCK_TIME_NONE;
|
2011-07-14 14:50:29 +00:00
|
|
|
self->base_ts = GST_CLOCK_TIME_NONE;
|
2009-09-02 01:30:35 +00:00
|
|
|
self->newsegment_sent = FALSE;
|
2019-08-13 14:50:33 +00:00
|
|
|
self->first_packet = TRUE;
|
2008-08-24 13:53:29 +00:00
|
|
|
|
|
|
|
gst_adapter_clear (self->adapter);
|
|
|
|
}
|
|
|
|
|
|
|
|
static guint32
|
|
|
|
gst_pcap_parse_read_uint32 (GstPcapParse * self, const guint8 * p)
|
|
|
|
{
|
|
|
|
guint32 val = *((guint32 *) p);
|
|
|
|
|
|
|
|
if (self->swap_endian) {
|
|
|
|
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
|
|
|
|
return GUINT32_FROM_BE (val);
|
|
|
|
#else
|
|
|
|
return GUINT32_FROM_LE (val);
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-03 15:16:20 +00:00
|
|
|
#define ETH_MAC_ADDRESSES_LEN 12
|
2008-08-24 13:53:29 +00:00
|
|
|
#define ETH_HEADER_LEN 14
|
2017-08-03 15:16:20 +00:00
|
|
|
#define ETH_VLAN_HEADER_LEN 4
|
2010-11-03 22:34:28 +00:00
|
|
|
#define SLL_HEADER_LEN 16
|
2008-08-24 13:53:29 +00:00
|
|
|
#define IP_HEADER_MIN_LEN 20
|
|
|
|
#define UDP_HEADER_LEN 8
|
|
|
|
|
|
|
|
#define IP_PROTO_UDP 17
|
2011-07-14 14:40:14 +00:00
|
|
|
#define IP_PROTO_TCP 6
|
|
|
|
|
2008-08-24 13:53:29 +00:00
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_pcap_parse_scan_frame (GstPcapParse * self,
|
|
|
|
const guint8 * buf,
|
|
|
|
gint buf_size, const guint8 ** payload, gint * payload_size)
|
|
|
|
{
|
2010-12-01 22:20:36 +00:00
|
|
|
const guint8 *buf_ip = 0;
|
2011-07-14 14:40:14 +00:00
|
|
|
const guint8 *buf_proto;
|
2008-08-24 13:53:29 +00:00
|
|
|
guint16 eth_type;
|
|
|
|
guint8 b;
|
|
|
|
guint8 ip_header_size;
|
2018-04-15 08:06:46 +00:00
|
|
|
guint8 flags;
|
|
|
|
guint16 fragment_offset;
|
2008-08-24 13:53:29 +00:00
|
|
|
guint8 ip_protocol;
|
|
|
|
guint32 ip_src_addr;
|
|
|
|
guint32 ip_dst_addr;
|
2011-07-14 14:40:14 +00:00
|
|
|
guint16 src_port;
|
|
|
|
guint16 dst_port;
|
|
|
|
guint16 len;
|
2019-06-24 15:39:35 +00:00
|
|
|
guint16 ip_packet_len;
|
2008-08-24 13:53:29 +00:00
|
|
|
|
2010-11-03 22:34:28 +00:00
|
|
|
switch (self->linktype) {
|
2014-10-09 08:11:00 +00:00
|
|
|
case LINKTYPE_ETHER:
|
2010-11-03 22:34:28 +00:00
|
|
|
if (buf_size < ETH_HEADER_LEN + IP_HEADER_MIN_LEN + UDP_HEADER_LEN)
|
|
|
|
return FALSE;
|
2017-08-03 15:16:20 +00:00
|
|
|
eth_type = GUINT16_FROM_BE (*((guint16 *) (buf + ETH_MAC_ADDRESSES_LEN)));
|
|
|
|
/* check for vlan 802.1q header (4 bytes, with first two bytes equal to 0x8100) */
|
|
|
|
if (eth_type == 0x8100) {
|
|
|
|
if (buf_size <
|
|
|
|
ETH_HEADER_LEN + ETH_VLAN_HEADER_LEN + IP_HEADER_MIN_LEN +
|
|
|
|
UDP_HEADER_LEN)
|
|
|
|
return FALSE;
|
|
|
|
eth_type =
|
|
|
|
GUINT16_FROM_BE (*((guint16 *) (buf + ETH_MAC_ADDRESSES_LEN +
|
|
|
|
ETH_VLAN_HEADER_LEN)));
|
|
|
|
buf_ip = buf + ETH_HEADER_LEN + ETH_VLAN_HEADER_LEN;
|
|
|
|
} else {
|
|
|
|
buf_ip = buf + ETH_HEADER_LEN;
|
|
|
|
}
|
2010-11-03 22:34:28 +00:00
|
|
|
break;
|
2014-10-09 08:11:00 +00:00
|
|
|
case LINKTYPE_SLL:
|
2010-11-03 22:34:28 +00:00
|
|
|
if (buf_size < SLL_HEADER_LEN + IP_HEADER_MIN_LEN + UDP_HEADER_LEN)
|
|
|
|
return FALSE;
|
|
|
|
|
2011-07-26 16:11:16 +00:00
|
|
|
eth_type = GUINT16_FROM_BE (*((guint16 *) (buf + 14)));
|
2010-11-03 22:34:28 +00:00
|
|
|
buf_ip = buf + SLL_HEADER_LEN;
|
|
|
|
break;
|
2014-10-09 08:11:00 +00:00
|
|
|
case LINKTYPE_RAW:
|
|
|
|
if (buf_size < IP_HEADER_MIN_LEN + UDP_HEADER_LEN)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
eth_type = 0x800; /* This is fine since IPv4/IPv6 is parse elsewhere */
|
|
|
|
buf_ip = buf;
|
|
|
|
break;
|
|
|
|
|
2011-07-27 09:56:15 +00:00
|
|
|
default:
|
|
|
|
return FALSE;
|
2010-11-03 22:34:28 +00:00
|
|
|
}
|
2008-08-24 13:53:29 +00:00
|
|
|
|
2017-08-03 15:16:20 +00:00
|
|
|
if (eth_type != 0x800) {
|
|
|
|
GST_ERROR_OBJECT (self,
|
|
|
|
"Link type %d: Ethernet type %d is not supported; only type 0x800",
|
|
|
|
(gint) self->linktype, (gint) eth_type);
|
2011-07-26 16:11:16 +00:00
|
|
|
return FALSE;
|
2017-08-03 15:16:20 +00:00
|
|
|
}
|
2011-07-26 16:11:16 +00:00
|
|
|
|
2008-08-24 13:53:29 +00:00
|
|
|
b = *buf_ip;
|
2018-04-14 18:14:35 +00:00
|
|
|
|
|
|
|
/* Check that the packet is IPv4 */
|
2008-08-24 13:53:29 +00:00
|
|
|
if (((b >> 4) & 0x0f) != 4)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
ip_header_size = (b & 0x0f) * 4;
|
|
|
|
if (buf_ip + ip_header_size > buf + buf_size)
|
|
|
|
return FALSE;
|
|
|
|
|
2018-04-15 08:06:46 +00:00
|
|
|
flags = buf_ip[6] >> 5;
|
|
|
|
fragment_offset =
|
|
|
|
(GUINT16_FROM_BE (*((guint16 *) (buf_ip + 6))) & 0x1fff) * 8;
|
|
|
|
if (flags & 0x1 || fragment_offset > 0) {
|
|
|
|
GST_ERROR_OBJECT (self, "Fragmented packets are not supported");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2008-08-24 13:53:29 +00:00
|
|
|
ip_protocol = *(buf_ip + 9);
|
2011-07-14 14:40:14 +00:00
|
|
|
GST_LOG_OBJECT (self, "ip proto %d", (gint) ip_protocol);
|
2008-08-24 13:53:29 +00:00
|
|
|
|
2011-07-14 14:40:14 +00:00
|
|
|
if (ip_protocol != IP_PROTO_UDP && ip_protocol != IP_PROTO_TCP)
|
2008-08-24 13:53:29 +00:00
|
|
|
return FALSE;
|
|
|
|
|
2011-07-14 14:40:14 +00:00
|
|
|
/* ip info */
|
|
|
|
ip_src_addr = *((guint32 *) (buf_ip + 12));
|
2008-08-24 13:53:29 +00:00
|
|
|
ip_dst_addr = *((guint32 *) (buf_ip + 16));
|
2011-07-14 14:40:14 +00:00
|
|
|
buf_proto = buf_ip + ip_header_size;
|
2019-06-24 15:39:35 +00:00
|
|
|
ip_packet_len = GUINT16_FROM_BE (*(guint16 *) (buf_ip + 2));
|
2008-08-24 13:53:29 +00:00
|
|
|
|
2011-07-14 14:40:14 +00:00
|
|
|
/* ok for tcp and udp */
|
|
|
|
src_port = GUINT16_FROM_BE (*((guint16 *) (buf_proto + 0)));
|
|
|
|
dst_port = GUINT16_FROM_BE (*((guint16 *) (buf_proto + 2)));
|
2008-08-24 13:53:29 +00:00
|
|
|
|
2011-07-14 14:40:14 +00:00
|
|
|
/* extract some params and data according to protocol */
|
|
|
|
if (ip_protocol == IP_PROTO_UDP) {
|
|
|
|
len = GUINT16_FROM_BE (*((guint16 *) (buf_proto + 4)));
|
|
|
|
if (len < UDP_HEADER_LEN || buf_proto + len > buf + buf_size)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
*payload = buf_proto + UDP_HEADER_LEN;
|
|
|
|
*payload_size = len - UDP_HEADER_LEN;
|
|
|
|
} else {
|
|
|
|
if (buf_proto + 12 >= buf + buf_size)
|
|
|
|
return FALSE;
|
|
|
|
len = (buf_proto[12] >> 4) * 4;
|
|
|
|
if (buf_proto + len > buf + buf_size)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
/* all remaining data following tcp header is payload */
|
|
|
|
*payload = buf_proto + len;
|
2019-06-24 15:39:35 +00:00
|
|
|
*payload_size = ip_packet_len - ip_header_size - len;
|
2011-07-14 14:40:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* but still filter as configured */
|
|
|
|
if (self->src_ip >= 0 && ip_src_addr != self->src_ip)
|
2008-08-24 13:53:29 +00:00
|
|
|
return FALSE;
|
|
|
|
|
2011-07-14 14:40:14 +00:00
|
|
|
if (self->dst_ip >= 0 && ip_dst_addr != self->dst_ip)
|
2008-08-24 13:53:29 +00:00
|
|
|
return FALSE;
|
|
|
|
|
2011-07-14 14:40:14 +00:00
|
|
|
if (self->src_port >= 0 && src_port != self->src_port)
|
2008-08-24 13:53:29 +00:00
|
|
|
return FALSE;
|
|
|
|
|
2011-07-14 14:40:14 +00:00
|
|
|
if (self->dst_port >= 0 && dst_port != self->dst_port)
|
|
|
|
return FALSE;
|
2008-08-24 13:53:29 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstFlowReturn
|
2012-01-16 21:52:13 +00:00
|
|
|
gst_pcap_parse_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
|
2008-08-24 13:53:29 +00:00
|
|
|
{
|
2012-01-16 21:52:13 +00:00
|
|
|
GstPcapParse *self = GST_PCAP_PARSE (parent);
|
2008-08-24 13:53:29 +00:00
|
|
|
GstFlowReturn ret = GST_FLOW_OK;
|
2015-04-11 00:15:52 +00:00
|
|
|
GstBufferList *list = NULL;
|
2008-08-24 13:53:29 +00:00
|
|
|
|
|
|
|
gst_adapter_push (self->adapter, buffer);
|
|
|
|
|
|
|
|
while (ret == GST_FLOW_OK) {
|
|
|
|
gint avail;
|
|
|
|
const guint8 *data;
|
|
|
|
|
|
|
|
avail = gst_adapter_available (self->adapter);
|
|
|
|
|
|
|
|
if (self->initialized) {
|
|
|
|
if (self->cur_packet_size >= 0) {
|
2018-04-14 18:14:35 +00:00
|
|
|
/* Parse the Packet Data */
|
2008-08-24 13:53:29 +00:00
|
|
|
if (avail < self->cur_packet_size)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (self->cur_packet_size > 0) {
|
|
|
|
const guint8 *payload_data;
|
|
|
|
gint payload_size;
|
|
|
|
|
2012-01-16 21:52:13 +00:00
|
|
|
data = gst_adapter_map (self->adapter, self->cur_packet_size);
|
2008-08-24 13:53:29 +00:00
|
|
|
|
2011-07-19 06:37:24 +00:00
|
|
|
GST_LOG_OBJECT (self, "examining packet size %" G_GINT64_FORMAT,
|
2011-07-15 08:04:53 +00:00
|
|
|
self->cur_packet_size);
|
|
|
|
|
2008-08-24 13:53:29 +00:00
|
|
|
if (gst_pcap_parse_scan_frame (self, data, self->cur_packet_size,
|
|
|
|
&payload_data, &payload_size)) {
|
|
|
|
GstBuffer *out_buf;
|
2015-04-11 00:25:49 +00:00
|
|
|
guintptr offset = payload_data - data;
|
|
|
|
|
|
|
|
gst_adapter_unmap (self->adapter);
|
|
|
|
gst_adapter_flush (self->adapter, offset);
|
2015-06-15 22:40:44 +00:00
|
|
|
/* we don't use _take_buffer_fast() on purpose here, we need a
|
|
|
|
* buffer with a single memory, since the RTP depayloaders expect
|
|
|
|
* the complete RTP header to be in the first memory if there are
|
|
|
|
* multiple ones and we can't guarantee that with _fast() */
|
2015-12-23 20:24:46 +00:00
|
|
|
if (payload_size > 0) {
|
|
|
|
out_buf = gst_adapter_take_buffer (self->adapter, payload_size);
|
|
|
|
} else {
|
|
|
|
out_buf = gst_buffer_new ();
|
|
|
|
}
|
2019-08-13 14:50:33 +00:00
|
|
|
|
|
|
|
/* only first packet should have DISCONT flag */
|
|
|
|
if (G_LIKELY (!self->first_packet)) {
|
|
|
|
GST_BUFFER_FLAG_UNSET (out_buf, GST_BUFFER_FLAG_DISCONT);
|
|
|
|
} else {
|
|
|
|
GST_BUFFER_FLAG_SET (out_buf, GST_BUFFER_FLAG_DISCONT);
|
|
|
|
self->first_packet = FALSE;
|
|
|
|
}
|
|
|
|
|
2015-06-15 11:55:39 +00:00
|
|
|
gst_adapter_flush (self->adapter,
|
|
|
|
self->cur_packet_size - offset - payload_size);
|
2015-04-11 00:25:49 +00:00
|
|
|
|
|
|
|
if (GST_CLOCK_TIME_IS_VALID (self->cur_ts)) {
|
|
|
|
if (!GST_CLOCK_TIME_IS_VALID (self->base_ts))
|
|
|
|
self->base_ts = self->cur_ts;
|
|
|
|
if (self->offset >= 0) {
|
|
|
|
self->cur_ts -= self->base_ts;
|
|
|
|
self->cur_ts += self->offset;
|
2011-07-14 14:50:29 +00:00
|
|
|
}
|
2015-04-11 00:25:49 +00:00
|
|
|
}
|
|
|
|
GST_BUFFER_TIMESTAMP (out_buf) = self->cur_ts;
|
2011-07-14 14:50:29 +00:00
|
|
|
|
2008-08-24 13:53:29 +00:00
|
|
|
|
2015-04-11 00:25:49 +00:00
|
|
|
if (list == NULL)
|
|
|
|
list = gst_buffer_list_new ();
|
|
|
|
gst_buffer_list_add (list, out_buf);
|
|
|
|
} else {
|
|
|
|
gst_adapter_unmap (self->adapter);
|
|
|
|
gst_adapter_flush (self->adapter, self->cur_packet_size);
|
2008-08-24 13:53:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self->cur_packet_size = -1;
|
|
|
|
} else {
|
2018-04-14 18:14:35 +00:00
|
|
|
/* Parse the Record (Packet) Header */
|
2008-08-24 13:53:29 +00:00
|
|
|
guint32 ts_sec;
|
|
|
|
guint32 ts_usec;
|
|
|
|
guint32 incl_len;
|
|
|
|
|
2018-04-14 18:14:35 +00:00
|
|
|
/* sizeof(pcaprec_hdr_t) == 16 */
|
2008-08-24 13:53:29 +00:00
|
|
|
if (avail < 16)
|
|
|
|
break;
|
|
|
|
|
2012-01-16 21:52:13 +00:00
|
|
|
data = gst_adapter_map (self->adapter, 16);
|
2008-08-24 13:53:29 +00:00
|
|
|
|
|
|
|
ts_sec = gst_pcap_parse_read_uint32 (self, data + 0);
|
|
|
|
ts_usec = gst_pcap_parse_read_uint32 (self, data + 4);
|
|
|
|
incl_len = gst_pcap_parse_read_uint32 (self, data + 8);
|
2011-07-12 21:57:45 +00:00
|
|
|
/* orig_len = gst_pcap_parse_read_uint32 (self, data + 12); */
|
2008-08-24 13:53:29 +00:00
|
|
|
|
2012-01-16 21:52:13 +00:00
|
|
|
gst_adapter_unmap (self->adapter);
|
2008-08-24 13:53:29 +00:00
|
|
|
gst_adapter_flush (self->adapter, 16);
|
|
|
|
|
2017-08-03 12:42:14 +00:00
|
|
|
self->cur_ts =
|
|
|
|
ts_sec * GST_SECOND +
|
|
|
|
ts_usec * (self->nanosecond_timestamp ? 1 : GST_USECOND);
|
2008-08-24 13:53:29 +00:00
|
|
|
self->cur_packet_size = incl_len;
|
|
|
|
}
|
|
|
|
} else {
|
2018-04-14 18:14:35 +00:00
|
|
|
/* Parse the Global Header */
|
2010-11-03 22:23:27 +00:00
|
|
|
guint32 magic;
|
|
|
|
guint32 linktype;
|
|
|
|
guint16 major_version;
|
2008-08-24 13:53:29 +00:00
|
|
|
|
2018-04-14 18:14:35 +00:00
|
|
|
/* sizeof(pcap_hdr_t) == 24 */
|
2008-08-24 13:53:29 +00:00
|
|
|
if (avail < 24)
|
|
|
|
break;
|
|
|
|
|
2012-01-16 21:52:13 +00:00
|
|
|
data = gst_adapter_map (self->adapter, 24);
|
2008-08-24 13:53:29 +00:00
|
|
|
|
|
|
|
magic = *((guint32 *) data);
|
2010-11-03 22:23:27 +00:00
|
|
|
major_version = *((guint16 *) (data + 4));
|
2017-06-01 14:00:50 +00:00
|
|
|
linktype = *((guint32 *) (data + 20));
|
2012-01-16 21:52:13 +00:00
|
|
|
gst_adapter_unmap (self->adapter);
|
2008-08-24 13:53:29 +00:00
|
|
|
|
2017-08-03 12:42:14 +00:00
|
|
|
if (magic == GST_PCAPPARSE_MAGIC_MILLISECOND_NO_SWAP_ENDIAN ||
|
|
|
|
magic == GST_PCAPPARSE_MAGIC_NANOSECOND_NO_SWAP_ENDIAN) {
|
2008-08-24 13:53:29 +00:00
|
|
|
self->swap_endian = FALSE;
|
2017-08-03 12:42:14 +00:00
|
|
|
if (magic == GST_PCAPPARSE_MAGIC_NANOSECOND_NO_SWAP_ENDIAN)
|
|
|
|
self->nanosecond_timestamp = TRUE;
|
|
|
|
} else if (magic == GST_PCAPPARSE_MAGIC_MILLISECOND_SWAP_ENDIAN ||
|
|
|
|
magic == GST_PCAPPARSE_MAGIC_NANOSECOND_SWAP_ENDIAN) {
|
2008-08-24 13:53:29 +00:00
|
|
|
self->swap_endian = TRUE;
|
2017-08-03 12:42:14 +00:00
|
|
|
if (magic == GST_PCAPPARSE_MAGIC_NANOSECOND_SWAP_ENDIAN)
|
|
|
|
self->nanosecond_timestamp = TRUE;
|
2017-06-01 14:00:50 +00:00
|
|
|
major_version = GUINT16_SWAP_LE_BE (major_version);
|
|
|
|
linktype = GUINT32_SWAP_LE_BE (linktype);
|
2010-11-03 22:23:27 +00:00
|
|
|
} else {
|
|
|
|
GST_ELEMENT_ERROR (self, STREAM, WRONG_TYPE, (NULL),
|
|
|
|
("File is not a libpcap file, magic is %X", magic));
|
|
|
|
ret = GST_FLOW_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (major_version != 2) {
|
2010-11-03 22:02:49 +00:00
|
|
|
GST_ELEMENT_ERROR (self, STREAM, WRONG_TYPE, (NULL),
|
2010-11-03 22:23:27 +00:00
|
|
|
("File is not a libpcap major version 2, but %u", major_version));
|
2008-08-24 13:53:29 +00:00
|
|
|
ret = GST_FLOW_ERROR;
|
2010-11-03 22:23:27 +00:00
|
|
|
goto out;
|
2010-11-03 22:02:49 +00:00
|
|
|
}
|
2008-08-24 13:53:29 +00:00
|
|
|
|
2014-10-09 08:11:00 +00:00
|
|
|
if (linktype != LINKTYPE_ETHER && linktype != LINKTYPE_SLL &&
|
|
|
|
linktype != LINKTYPE_RAW) {
|
2010-11-03 22:23:27 +00:00
|
|
|
GST_ELEMENT_ERROR (self, STREAM, WRONG_TYPE, (NULL),
|
2014-10-09 08:11:00 +00:00
|
|
|
("Only dumps of type Ethernet, raw IP or Linux Cooked (SLL) "
|
|
|
|
"understood; type %d unknown", linktype));
|
2010-11-03 22:23:27 +00:00
|
|
|
ret = GST_FLOW_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2011-07-15 08:04:53 +00:00
|
|
|
GST_DEBUG_OBJECT (self, "linktype %u", linktype);
|
2010-11-03 22:34:28 +00:00
|
|
|
self->linktype = linktype;
|
|
|
|
|
2010-11-03 22:23:27 +00:00
|
|
|
gst_adapter_flush (self->adapter, 24);
|
|
|
|
self->initialized = TRUE;
|
2008-08-24 13:53:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-11 00:15:52 +00:00
|
|
|
if (list) {
|
|
|
|
if (!self->newsegment_sent && GST_CLOCK_TIME_IS_VALID (self->cur_ts)) {
|
|
|
|
GstSegment segment;
|
|
|
|
|
|
|
|
if (self->caps)
|
|
|
|
gst_pad_set_caps (self->src_pad, self->caps);
|
|
|
|
gst_segment_init (&segment, GST_FORMAT_TIME);
|
2015-08-05 18:52:12 +00:00
|
|
|
segment.start = self->base_ts;
|
2015-04-11 00:15:52 +00:00
|
|
|
gst_pad_push_event (self->src_pad, gst_event_new_segment (&segment));
|
|
|
|
self->newsegment_sent = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = gst_pad_push_list (self->src_pad, list);
|
|
|
|
list = NULL;
|
|
|
|
}
|
|
|
|
|
2010-11-03 22:23:27 +00:00
|
|
|
out:
|
2015-04-11 00:15:52 +00:00
|
|
|
|
|
|
|
if (list)
|
|
|
|
gst_buffer_list_unref (list);
|
|
|
|
|
2008-08-24 13:53:29 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-09-02 01:30:35 +00:00
|
|
|
static gboolean
|
2012-01-16 21:52:13 +00:00
|
|
|
gst_pcap_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
|
2009-09-02 01:30:35 +00:00
|
|
|
{
|
|
|
|
gboolean ret = TRUE;
|
2012-01-16 21:52:13 +00:00
|
|
|
GstPcapParse *self = GST_PCAP_PARSE (parent);
|
2009-09-02 01:30:35 +00:00
|
|
|
|
|
|
|
switch (GST_EVENT_TYPE (event)) {
|
2012-01-16 21:52:13 +00:00
|
|
|
case GST_EVENT_SEGMENT:
|
2009-09-02 01:30:35 +00:00
|
|
|
/* Drop it, we'll replace it with our own */
|
2010-09-22 14:41:33 +00:00
|
|
|
gst_event_unref (event);
|
2009-09-02 01:30:35 +00:00
|
|
|
break;
|
2015-08-29 00:33:40 +00:00
|
|
|
case GST_EVENT_FLUSH_STOP:
|
|
|
|
gst_pcap_parse_reset (self);
|
2015-12-01 17:52:03 +00:00
|
|
|
/* Push event down the pipeline so that other elements stop flushing */
|
|
|
|
/* fall through */
|
2009-09-02 01:30:35 +00:00
|
|
|
default:
|
|
|
|
ret = gst_pad_push_event (self->src_pad, event);
|
2010-09-22 14:41:33 +00:00
|
|
|
break;
|
2009-09-02 01:30:35 +00:00
|
|
|
}
|
|
|
|
|
2015-08-29 00:33:40 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstStateChangeReturn
|
|
|
|
gst_pcap_parse_change_state (GstElement * element, GstStateChange transition)
|
|
|
|
{
|
|
|
|
GstPcapParse *self = GST_PCAP_PARSE (element);
|
|
|
|
GstStateChangeReturn ret;
|
|
|
|
|
|
|
|
ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
|
|
|
|
|
|
|
|
switch (transition) {
|
|
|
|
case GST_STATE_CHANGE_PAUSED_TO_READY:
|
2015-09-17 14:10:10 +00:00
|
|
|
gst_pcap_parse_reset (self);
|
2015-08-29 00:33:40 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-02 01:30:35 +00:00
|
|
|
return ret;
|
|
|
|
}
|