/*************************************************************************** * Copyright (C) 2001 by Rick L. Vinyard, Jr. * * rvinyard@cs.nmsu.edu * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU Lesser General Public License as * * published by the Free Software Foundation version 2.1. * * * * 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 Lesser General Public * * License along with this library; if not, write to the * * Free Software Foundation, Inc., * * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * ***************************************************************************/ #include <conexus/conexus.h> #include <iostream> using namespace Conexus; using namespace std; typedef struct datagram { uint8_t ihl:4; uint8_t version:4; uint8_t tos; uint16_t tl; uint16_t id; uint16_t fo; uint8_t ttl; uint8_t protocol; uint16_t checksum; uint32_t source; uint32_t destination; uint8_t data[64]; } datagram; int main(int argc, char* argv[]) { Conexus::init(); LL::Packet packet; LL::Address lladdr; IPv4::Address source, dest; vector<string> ifnames = interface_names(); for (vector<string>::iterator i = ifnames.begin(); i != ifnames.end(); i++) cout << *i << " : " << interface_index(*i) << endl; lladdr.set_interface("eth0"); datagram dg; source.set_address("192.168.10.11"); dest.set_address("192.168.10.12"); dg.version = 4; dg.ihl = 5; dg.tos = 0; dg.tl = htons(sizeof(datagram)); dg.fo = htons(2<<13); dg.ttl = 42; dg.protocol = 1; dg.checksum = htons(0xbb75); dg.source = htonl(source.address()); dg.destination = htonl(dest.address()); dg.data[0] = 0x08; // ICMP ping request dg.data[1] = 0x00; // ICMP code dg.data[2] = 0x38; dg.data[3] = 0x9b; // ICMP checksum dg.data[4] = 0x42; dg.data[5] = 0x53; // ICMP identifier dg.data[6] = 0x00; dg.data[7] = 0x01; // ICMP sequence uint8_t data[] = {0x45, 0x00, 0x00, 0x27, 0x00, 0x00, 0x40, 0x00, 0x40, 0x11, 0x24, 0xb5, 0x0a, 0x08, 0x01, 0x01, 0x0a, 0x08, 0x01, 0x01, 0x80, 0x40, 0x05, 0xdc, 0x00, 0x13, 0x5e, 0x90, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x00}; packet.writeto(lladdr, &dg, sizeof(datagram)); packet.set_interface(1); packet.write(data, sizeof(data)); return 0; }
/*************************************************************************** * Copyright (C) 2001 by Rick L. Vinyard, Jr. * * rvinyard@cs.nmsu.edu * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU Lesser General Public License as * * published by the Free Software Foundation version 2.1. * * * * 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 Lesser General Public * * License along with this library; if not, write to the * * Free Software Foundation, Inc., * * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * ***************************************************************************/ #include <conexus/conexus.h> #include <unistd.h> #include <iostream> #include <netinet/in.h> using namespace Conexus; using namespace std; typedef struct datagram { uint8_t ihl:4; uint8_t version:4; uint8_t tos; uint16_t tl; uint16_t id; uint16_t fragoffset:13; uint8_t flags:3; uint8_t ttl; uint8_t protocol; uint16_t checksum; uint32_t source; uint32_t destination; uint8_t data[]; } datagram; void print_data(Data d); int main() { Conexus::init(); LL::Packet packet(ETH_P_ALL); LL::Address address; address.set_interface("eth0"); packet.bind(address); // std::cout << "Listening on " << packet.get_interface_name() << std::endl; packet.signal_data().connect(sigc::ptr_fun(&print_data)); packet.start(); for (int i=1; i <= 20; i++) { if (i%5 == 0) cout << "Time: " << i << endl; sleep(1); } packet.stop(); return 0; } void print_data(Data d) { IPv4::Address addr; datagram* dg = (datagram*)(Data::Octet*)d; addr.set_address(ntohl(dg->source)); cout << "Received " << d.size() << " bytes of data from " << addr.address_string() << "\n"; cout << "version: " << (uint)dg->version << endl; cout << "ihl: " << (uint)dg->ihl << endl; cout << "tos: " << (uint)dg->tos << endl; cout << "tl: " << ntohs((uint)dg->tl) << endl; cout << "id: " << ntohs((uint)dg->id) << endl; cout << "flags: " << (uint)dg->flags << endl; cout << "fragoffset: " << ntohs((uint)dg->fragoffset) << endl; cout << "ttl: " << (uint)dg->ttl << endl; cout << "protocol: " << (uint)dg->protocol << endl; cout << "checksum: " << ntohs((uint)dg->checksum) << endl; }