hmbdc
simplify-high-performance-messaging-programming
udpcast-sniff.cpp
//this is to show how to use hmbdc-udpcast to write a simple multicast udp packet sniffer app
//packet content is print out on stdout
//to run:
//./msnf <local-ip> <multicast-group-addr> <multicast-group-port>
//for example, the following listen to the default udpcast address 232.43.211.234:4321 using interface 192.168.0.100
//./msnf 192.168.0.100 232.43.211.234 4321
//to build: (newer compilers might require -faligned-new compile flag)
//g++ example/udpcast-sniff.cpp -g -std=c++1z -Wall -Werror -pthread -I path-to-boost -Ipath-to-hmbdc-lib-include path-to-hmbdc-lib/libhmbdc.a /usr/local/lib/libboost_system.a /usr/local/lib/libboost_iostreams.a -lpthread -lrt -o /tmp/udp-snf
//
#include <hmbdc/app/udpcast/NetContext.hpp>
#include <hmbdc/os/Signals.hpp>
#include <boost/format.hpp>
#include <iostream>
using namespace std;
using namespace hmbdc::app;
int main(int argc, char** argv) {
if (argc != 3) {
cerr << argv[0] << " <local-ip> <multicast-group-addr:multicast-group-port>" << endl;
exit(1);
}
Config config;
config.put("ifaceAddr", argv[1]);
config.put("udpcastDests", argv[2]);
SingletonGuardian<udpcast::NetContext> g; //RAII for udpcast::NetContext resources
Context<> ctx;
//! [define packet arb]
auto eng = udpcast::NetContext::instance().createRecvTransportEngine(config
, ctx.buffer()
//the lambda below provides the 'filter' before the packet gets to hmbdc processing
, [](void* packet, size_t len) {
auto buf = (uint8_t*)(packet);
size_t l = 0;
cout << "packet size=" << len;
while (l != len) {
cout << ' ' << boost::format("%02x") % (uint16_t)buf[l++];
}
cout << endl;
return -1; //-1 - drops the packet
//1 - keep
//0 - cannot decide, ask me later
//we already dumped it out - drop it now, no further processing
}
);
//! [define packet arb]
ctx.start(*eng, 0);
cout << "waiting for packets, ctrl-c to exit" << endl;
[&ctx] {
ctx.stop();
}
);
ctx.join();
}