conexus logo

ll_server.cpp

Conexus::LL::Packet link layer server example

/***************************************************************************
 *   Copyright (C) 2001 by Rick L. Vinyard, Jr.                            *
 *   rvinyard@cs.nmsu.edu                                                  *
 *                                                                         *
 *   This file is part of the conexus library.                             *
 *                                                                         *
 *   The conexus library is free software; you can redistribute it and/or  *
 *   modify it under the terms of the GNU General Public License version 3 *
 *   as published by the Free Software Foundation.                         *
 *                                                                         *
 *   The conexus 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   *
 *   General Public License for more details.                              *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this software. If not see <http://www.gnu.org/licenses/>.  *
 ***************************************************************************/
#include <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(const Data d);

int main() {
  Conexus::init();

  LL::Packet::pointer packet = LL::Packet::create(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(const Data d) {
  IPv4::Address addr;
  datagram* dg = (datagram*)(d.data());
  addr.set_host(ntohl(dg->source));
  cout << "Received " << d.size() << " bytes of data from " << addr.host_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;
}

Generated on Wed Jul 8 15:50:07 2009 for conexus by doxygen 1.5.8