add wireshark connector and packet dropper

This commit is contained in:
Bastian Bloessl
2013-06-01 18:28:31 +02:00
parent 0ee514b41d
commit 39d28c8c3a
12 changed files with 419 additions and 6 deletions
+3 -1
View File
@@ -18,5 +18,7 @@
# Boston, MA 02110-1301, USA. # Boston, MA 02110-1301, USA.
install(FILES install(FILES
DESTINATION share/gnuradio/grc/blocks ccs_packet_dropper.xml
ccs_wireshark_connector.xml
DESTINATION share/gnuradio/grc/blocks
) )
+30
View File
@@ -0,0 +1,30 @@
<?xml version="1.0"?>
<block>
<name>Packet Dropper</name>
<key>ccs_packet_dropper</key>
<category>CCS</category>
<import>import ccs</import>
<make>ccs.packet_dropper($drop_rate, $seed)</make>
<param>
<name>Packet drop rate</name>
<key>drop_rate</key>
<type>real</type>
</param>
<param>
<name>Random seed</name>
<key>seed</key>
<type>int</type>
</param>
<sink>
<name>in</name>
<type>message</type>
</sink>
<source>
<name>out</name>
<type>message</type>
</source>
</block>
+34
View File
@@ -0,0 +1,34 @@
<?xml version="1.0"?>
<block>
<name>Wireshark Connector</name>
<key>ccs_wireshark_connector</key>
<category>CCS</category>
<import>import ccs</import>
<make>ccs.wireshark_connector($debug)</make>
<param>
<name>Debug</name>
<key>debug</key>
<value>False</value>
<type>bool</type>
<option>
<name>Enable</name>
<key>True</key>
</option>
<option>
<name>Disable</name>
<key>False</key>
</option>
</param>
<sink>
<name>in</name>
<type>message</type>
</sink>
<source>
<name>out</name>
<type>byte</type>
<nports>1</nports>
</source>
</block>
+4 -2
View File
@@ -21,6 +21,8 @@
# Install public header files # Install public header files
######################################################################## ########################################################################
install(FILES install(FILES
api.h api.h
DESTINATION include/ccs packet_dropper.h
wireshark_connector.h
DESTINATION include/ccs
) )
+35
View File
@@ -0,0 +1,35 @@
/*
* Copyright (C) 2013 Christoph Leitner <c.leitner@student.uibk.ac.at>
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef INCLUDED_GR_CCS_PACKET_DROPPER_H
#define INCLUDED_GR_CCS_PACKET_DROPPER_H
#include <ccs/api.h>
#include <gnuradio/block.h>
namespace gr {
namespace ccs {
class CCS_API packet_dropper : virtual public gr::block
{
public:
typedef boost::shared_ptr<packet_dropper> sptr;
static sptr make(double drop_rate, unsigned long seed);
};
} // namespace ccs
} // namespace gr
#endif /* INCLUDED_GR_CCS_PACKET_DROPPER_H */
+37
View File
@@ -0,0 +1,37 @@
/*
* Copyright (C) 2013 Bastian Bloessl <bloessl@ccs-labs.org>
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef INCLUDED_GR_CCS_WIRESHARK_CONNECTOR_H
#define INCLUDED_GR_CCS_WIRESHARK_CONNECTOR_H
#include <ccs/api.h>
#include <gnuradio/block.h>
namespace gr {
namespace ccs {
class CCS_API wireshark_connector : virtual public gr::block
{
public:
typedef boost::shared_ptr<wireshark_connector> sptr;
static sptr make(bool debug= false);
};
} // namespace ccs
} // namespace gr
#endif /* INCLUDED_GR_CCS_WIRESHARK_CONNECTOR_H */
+2
View File
@@ -26,6 +26,8 @@ include_directories(${Boost_INCLUDE_DIR})
link_directories(${Boost_LIBRARY_DIRS}) link_directories(${Boost_LIBRARY_DIRS})
list(APPEND ccs_sources list(APPEND ccs_sources
packet_dropper_impl.cc
wireshark_connector_impl.cc
) )
add_library(gnuradio-ccs SHARED ${ccs_sources}) add_library(gnuradio-ccs SHARED ${ccs_sources})
+52
View File
@@ -0,0 +1,52 @@
/*
* Copyright (C) 2013 Christoph Leitner <c.leitner@student.uibk.ac.at>
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <ccs/packet_dropper.h>
#include <gnuradio/io_signature.h>
#include <gnuradio/block_detail.h>
#include "packet_dropper_impl.h"
using namespace gr::ccs;
packet_dropper_impl::packet_dropper_impl(double drop_rate, unsigned long seed)
: d_drop_rate(drop_rate), d_generator(seed), d_distribution(0.0,1.0),
block("rime_stack", gr::io_signature::make(0, 0, 0),
gr::io_signature::make(0, 0, 0))
{
if(d_drop_rate >= 1){
throw std::out_of_range("drop rate has to be < 1");
}
message_port_register_in(pmt::mp("in"));
set_msg_handler(pmt::mp("in"), boost::bind(&packet_dropper_impl::msg_handler, this, _1));
message_port_register_out(pmt::mp("out"));
}
void
packet_dropper_impl::msg_handler(pmt::pmt_t msg)
{
if(d_distribution(d_generator) <= d_drop_rate){
return;
}
message_port_pub(pmt::mp("out"), msg);
}
packet_dropper::sptr
packet_dropper::make(double drop_rate, unsigned long seed)
{
return gnuradio::get_initial_sptr(new packet_dropper_impl(drop_rate, seed));
}
+37
View File
@@ -0,0 +1,37 @@
/*
* Copyright (C) 2013 Christoph Leitner <c.leitner@student.uibk.ac.at>
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef INCLUDED_GR_CCS_PACKET_DROPPER_IMPL_H
#define INCLUDED_GR_CCS_PACKET_DROPPER_IMPL_H
#include <ccs/packet_dropper.h>
namespace gr {
namespace ccs {
class CCS_API packet_dropper_impl : public packet_dropper {
private:
double d_drop_rate;
std::default_random_engine d_generator;
std::uniform_real_distribution<double> d_distribution;
void msg_handler(pmt::pmt_t msg);
public:
packet_dropper_impl(double drop_rate, unsigned long seed);
};
}
}
#endif /* INCLUDED_GR_CCS_PACKET_DROPPER_IMPL_H */
+128
View File
@@ -0,0 +1,128 @@
/*
* Copyright (C) 2013 Bastian Bloessl <bloessl@ccs-labs.org>
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "wireshark_connector_impl.h"
#include <ccs/wireshark_connector.h>
#include <gnuradio/io_signature.h>
#include <iostream>
#include <iomanip>
#include <sys/time.h>
using namespace gr::ccs;
class wireshark_connector_impl : public wireshark_connector {
public:
#define dout d_debug && std::cout
wireshark_connector_impl(bool debug) :
block ("wireshark_connector",
gr::io_signature::make(0, 0, 0),
gr::io_signature::make(1, 1, sizeof(uint8_t))),
d_msg_offset(0),
d_debug(debug) {
message_port_register_in(pmt::mp("in"));
pcap_global *hdr = (pcap_global*)d_msg;
hdr->magic_number = 0xa1b2c3d4;
hdr->version_major = 2;
hdr->version_minor = 4;
hdr->thiszone = 0;
hdr->sigfigs = 0;
hdr->snaplen = 65535;
hdr->network = ZIGBEE;
d_msg_len = sizeof(pcap_global);
}
~wireshark_connector_impl(void) {
}
void copy_message(const char *buf, int len) {
struct timeval t;
gettimeofday(&t, NULL);
pcap_pkt hdr;
hdr.ts_sec = t.tv_sec;
hdr.ts_usec = t.tv_usec;
hdr.incl_len = len;
hdr.orig_len = len;
memcpy(d_msg, &hdr, sizeof(pcap_pkt));
memcpy(d_msg + sizeof(pcap_pkt), buf, len);
d_msg_len = sizeof(pcap_pkt) + len;
}
int general_work(int noutput, gr_vector_int& ninput_items,
gr_vector_const_void_star& input_items,
gr_vector_void_star& output_items ) {
gr_complex *out = (gr_complex*)output_items[0];
while(!d_msg_len) {
pmt::pmt_t msg(delete_head_blocking(pmt::mp("in")));
if(pmt::is_eof_object(msg)) {
dout << "WIRESHARK: exiting" << std::endl;
return -1;
} else if(pmt::is_blob(msg)) {
dout << "WIRESHARK: received new message" << std::endl;
dout << "message length " << pmt::blob_length(msg) << std::endl;
copy_message((const char*)pmt::blob_data(msg), pmt::blob_length(msg));
break;
} else if(pmt::is_pair(msg)) {
pmt::pmt_t blob = pmt::cdr(msg);
dout << "WIRESHARK: received new message" << std::endl;
dout << "message length " << pmt::blob_length(blob) << std::endl;
copy_message((const char*)pmt::blob_data(blob), pmt::blob_length(blob));
break;
}
}
int to_copy = std::min((d_msg_len - d_msg_offset), noutput);
memcpy(out, d_msg + d_msg_offset, to_copy);
dout << "WIRESHARK: d_msg_offset: " << d_msg_offset <<
" to_copy: " << to_copy <<
" d_msg_len " << d_msg_len << std::endl;
d_msg_offset += to_copy;
if(d_msg_offset == d_msg_len) {
d_msg_offset = 0;
d_msg_len = 0;
}
dout << "WIRESHARK: output size: " << noutput <<
" produced items: " << to_copy << std::endl;
return to_copy;
}
private:
bool d_debug;
int d_msg_offset;
int d_msg_len;
char d_msg[256];
};
wireshark_connector::sptr
wireshark_connector::make(bool debug) {
return gnuradio::get_initial_sptr(new wireshark_connector_impl(debug));
}
+49
View File
@@ -0,0 +1,49 @@
/*
* Copyright (C) 2013 Bastian Bloessl <bloessl@ccs-labs.org>
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef INCLUDED_GR_CCS_WIRESHARK_CONNECTOR_IMPL_H
#define INCLUDED_GR_CCS_WIRESHARK_CONNECTOR_IMPL_H
#include <boost/cstdint.hpp>
namespace gr {
namespace ccs {
enum LINK_LAYER_PROTOCOL {
ZIGBEE = 195
};
struct pcap_global {
uint32_t magic_number; /* magic number */
uint16_t version_major; /* major version number */
uint16_t version_minor; /* minor version number */
int32_t thiszone; /* GMT to local correction */
uint32_t sigfigs; /* accuracy of timestamps */
uint32_t snaplen; /* max length of captured packets, in octets */
uint32_t network; /* data link type */
}__attribute__((packed));
struct pcap_pkt {
uint32_t ts_sec; /* timestamp seconds */
uint32_t ts_usec; /* timestamp microseconds */
uint32_t incl_len; /* number of octets of packet saved in file */
uint32_t orig_len; /* actual length of packet */
}__attribute__((packed));
} // namespace ccs
} // namespace gr
#endif /* INCLUDED_GR_CCS_WIRESHARK_CONNECTOR_IMPL_H */
+8 -3
View File
@@ -1,13 +1,18 @@
/* -*- c++ -*- */
#define CCS_API #define CCS_API
%include "gnuradio.i" // the common stuff %include "gnuradio.i"
//load generated python docstrings //load generated python docstrings
%include "ccs_swig_doc.i" %include "ccs_swig_doc.i"
%{ %{
#include "ccs/packet_dropper.h"
#include "ccs/wireshark_connector.h"
%} %}
%include "ccs/packet_dropper.h"
%include "ccs/wireshark_connector.h"
GR_SWIG_BLOCK_MAGIC2(ccs, packet_dropper);
GR_SWIG_BLOCK_MAGIC2(ccs, wireshark_connector);