Files
gr-foo/lib/wireshark_connector_impl.cc
T
2013-06-05 06:44:42 +02:00

129 lines
3.4 KiB
C++

/*
* 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 <foo/wireshark_connector.h>
#include <gnuradio/io_signature.h>
#include <iostream>
#include <iomanip>
#include <sys/time.h>
using namespace gr::foo;
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));
}