packets_replayer.cpp
Go to the documentation of this file.
1 #include <boost/asio.hpp>
2 #include <boost/program_options.hpp>
3 #include <iostream>
4 #include <memory>
5 #include <net/ethernet.h>
6 #include <netinet/ip.h>
7 #include <netinet/udp.h>
8 #include <pcap.h>
9 
10 using namespace boost::asio;
11 namespace po = boost::program_options;
12 
13 pcap_t* pcapDescriptor = nullptr;
14 bool shouldRepeat = false;
15 // std::shared_ptr<ip::udp::socket> UDPSocket;
16 io_service service;
17 ip::udp::socket UDPSocket(service);
18 ip::udp::endpoint endPoint;
19 deadline_timer timer(service);
20 long period_ms;
21 std::string pcapFile;
22 
23 void processNextDataInPcapFile(const boost::system::error_code& errocode)
24 {
25  if(errocode != boost::asio::error::operation_aborted && errocode)
26  {
27  std::cout << "Unexpected timer error : " << errocode.message();
28  exit(-1);
29  }
30  char errbuf[PCAP_ERRBUF_SIZE];
31  if(!pcapDescriptor)
32  {
33  pcapDescriptor = pcap_open_offline(pcapFile.c_str(), errbuf);
34  if(pcapDescriptor == NULL)
35  {
36  std::cout << "pcap_open() failed: " << errbuf << std::endl;
37  exit(1);
38  }
39  }
40 
41  struct pcap_pkthdr* header;
42  const u_char* pkt_data;
43  // Here we read the next packet in the PCAP File
44  auto res = pcap_next_ex(pcapDescriptor, &header, &pkt_data);
45  if(res < 0)
46  {
47  // We are at end of file,
48  pcap_close(pcapDescriptor);
49  if(shouldRepeat)
50  {
51  // if we must repeat, then we close the file and recall this method.
52  pcapDescriptor = nullptr;
53  processNextDataInPcapFile(errocode);
54  }
55  else
56  {
57  exit(0);
58  }
59  }
60  boost::system::error_code ec;
61  const u_char* startOfData = pkt_data + sizeof(struct ether_header) +
62  sizeof(struct ip) + sizeof(struct udphdr);
63  size_t dataLength = header->len - (sizeof(struct ether_header) + sizeof(struct ip) +
64  sizeof(struct udphdr));
65  if(*startOfData != 'I' && *(startOfData + 1) != 'X')
66  {
67  std::cout << "Can't send this packet, its not STDBin packet" << std::endl;
68  }
69  else
70  {
71  UDPSocket.send_to(boost::asio::buffer(startOfData, dataLength), endPoint, 0, ec);
72  if(ec)
73  {
74  std::cout << "Problem when sending data on UDP " << ec.message() << std::endl;
75  }
76  }
77  // We restart the timer.
78  timer.expires_from_now(boost::posix_time::milliseconds(period_ms));
79  timer.async_wait(processNextDataInPcapFile);
80 }
81 
82 int main(int argc, char* argv[])
83 {
84  po::options_description desc("Allowed options");
85  // clang-format off
86  desc.add_options()
87  ("help", "produce help message")
88  ("period", po::value<long>()->default_value(10), "Publication period in ms.")
89  ("repeat", "Will repeat the file infinitely")
90  ("ip", po::value<std::string>()->default_value(std::string("127.0.0.1")), "Interface on which UDP Frames will be sent")
91  ("port", po::value<uint16_t>()->default_value(8200), "Port on which UDP Frames will be sent")
92  ("file", po::value<std::string>(), "File to replay")
93  ;
94  // clang-format on
95 
96  po::variables_map vm;
97  po::store(po::parse_command_line(argc, argv, desc), vm);
98  po::notify(vm);
99 
100  if(vm.count("help"))
101  {
102  std::cout << desc << "\n";
103  return 1;
104  }
105 
106  if(vm.count("file") == 0)
107  {
108  std::cout << desc << "\nFile parameter is REQUIRED\n";
109  return 1;
110  }
111  pcapFile = vm["file"].as<std::string>();
112  shouldRepeat = vm.count("repeat") > 0;
113  period_ms = vm["period"].as<long>();
114 
115  endPoint = ip::udp::endpoint(ip::address::from_string(vm["ip"].as<std::string>()),
116  vm["port"].as<uint16_t>());
117  UDPSocket.open(ip::udp::v4());
118  socket_base::reuse_address option(true);
119  UDPSocket.set_option(option);
120 
121  timer.expires_from_now(boost::posix_time::milliseconds(period_ms));
122  timer.async_wait(processNextDataInPcapFile);
123  service.run();
124  return 0;
125 }
void processNextDataInPcapFile(const boost::system::error_code &errocode)
int main(int argc, char *argv[])
io_service service
std_msgs::Header * header(M &m)
bool shouldRepeat
def default_value(type_)
ip::udp::endpoint endPoint
pcap_t * pcapDescriptor
long period_ms
ip::udp::socket UDPSocket(service)
std::string pcapFile
deadline_timer timer(service)


ixblue_ins_driver
Author(s): Adrien BARRAL , Laure LE BRETON
autogenerated on Wed Jan 27 2021 03:37:01