/* * Copyright (C) 2013 Bastian Bloessl * * 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 . */ #include "wireshark_connector_impl.h" #include #include #include #include #include using namespace gr::foo; #define dout d_debug && std::cout wireshark_connector_impl::wireshark_connector_impl(LinkType type, 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), d_link(type) { 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 = d_link; d_msg_len = sizeof(pcap_global); } wireshark_connector_impl::~wireshark_connector_impl() { } void wireshark_connector_impl::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 wireshark_connector_impl::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; } wireshark_connector::sptr wireshark_connector::make(LinkType type, bool debug) { return gnuradio::get_initial_sptr(new wireshark_connector_impl(type, debug)); }