s-net-Tools
main-snet-flooder.cpp
Go to the documentation of this file.
1 
22 #include "Config.h"
23 #include <iostream>
24 #include <boost/asio.hpp>
25 #include <boost/program_options.hpp>
26 #include <boost/regex.hpp>
27 #include "HdlcdClient.h"
28 #include "FloodingPacketSource.h"
29 
30 int main(int argc, char* argv[]) {
31  try {
32  // Declare the supported options.
33  boost::program_options::options_description l_Description("Allowed options");
34  l_Description.add_options()
35  ("help,h", "produce this help message")
36  ("version,v", "show version information")
37  ("connect,c", boost::program_options::value<std::string>(),
38  "syntax: SerialPort@IPAddess:PortNbr\n"
39  " linux: /dev/ttyUSB0@localhost:5001\n"
40  " windows: //./COM1@example.com:5001")
41  ("destination,d", boost::program_options::value<std::string>(),
42  "the destination address (SSA) as a 16bit hex value, e.g., 3FF4")
43  ;
44 
45  // Parse the command line
46  boost::program_options::variables_map l_VariablesMap;
47  boost::program_options::store(boost::program_options::parse_command_line(argc, argv, l_Description), l_VariablesMap);
48  boost::program_options::notify(l_VariablesMap);
49  if (l_VariablesMap.count("version")) {
50  std::cerr << "s-net(r) flooder tool version " << SNET_TOOLS_VERSION_MAJOR << "." << SNET_TOOLS_VERSION_MINOR
51  << " built with hdlcd-devel version " << HDLCD_DEVEL_VERSION_MAJOR << "." << HDLCD_DEVEL_VERSION_MINOR
52  << " and snet-devel version " << SNET_DEVEL_VERSION_MAJOR << "." << SNET_DEVEL_VERSION_MINOR << std::endl;
53  } // if
54 
55  if (l_VariablesMap.count("help")) {
56  std::cout << l_Description << std::endl;
57  std::cout << "The s-net(r) packet flooder for the HDLC Daemon is Copyright (C) 2016, and GNU GPL'd, by Florian Evers." << std::endl;
58  std::cout << "Bug reports, feedback, admiration, abuse, etc, to: https://github.com/Strunzdesign/snet-tools" << std::endl;
59  return 1;
60  } // if
61 
62  if (!l_VariablesMap.count("connect")) {
63  std::cout << "snet-flooder: you have to specify one device to connect to" << std::endl;
64  std::cout << "snet-flooder: Use --help for more information." << std::endl;
65  return 1;
66  } // if
67 
68  uint16_t l_UnicastSSA = 0x3FF4;
69  if (!l_VariablesMap.count("destination")) {
70  std::cout << "snet-flooder: you have to specify the destination address (SSA)" << std::endl;
71  std::cout << "snet-flooder: Use --help for more information." << std::endl;
72  return 1;
73  } else {
74  // Convert the provided hexadecimal SSA
75  std::istringstream l_Converter(l_VariablesMap["destination"].as<std::string>());
76  l_Converter >> std::hex >> l_UnicastSSA;
77  } // else
78 
79  // Install signal handlers
80  boost::asio::io_service l_IoService;
81  boost::asio::signal_set l_Signals(l_IoService);
82  l_Signals.add(SIGINT);
83  l_Signals.add(SIGTERM);
84  l_Signals.async_wait([&l_IoService](boost::system::error_code, int){ l_IoService.stop(); });
85 
86  // Parse the destination specifier
87  static boost::regex s_RegEx("^(.*?)@(.*?):(.*?)$");
88  boost::smatch l_Match;
89  if (boost::regex_match(l_VariablesMap["connect"].as<std::string>(), l_Match, s_RegEx)) {
90  // Resolve destination
91  boost::asio::ip::tcp::resolver l_Resolver(l_IoService);
92  auto l_EndpointIterator = l_Resolver.resolve({ l_Match[2], l_Match[3] });
93 
94  // Prepare the HDLCd client entity
95  HdlcdClient l_HdlcdClient(l_IoService, l_Match[1], HdlcdSessionDescriptor(SESSION_TYPE_TRX_ALL, SESSION_FLAGS_NONE));
96  l_HdlcdClient.SetOnClosedCallback([&l_IoService](){ l_IoService.stop(); });
97 
98  // Prepare input then connect
99  FloodingPacketSource l_FloodingPacketSource(l_HdlcdClient, l_UnicastSSA);
100  l_HdlcdClient.AsyncConnect(l_EndpointIterator, [&l_FloodingPacketSource, &l_Signals](bool a_bSuccess) {
101  if (a_bSuccess) {
102  l_FloodingPacketSource.Start();
103  } else {
104  std::cout << "Failed to connect to the HDLC Daemon!" << std::endl;
105  l_Signals.cancel();
106  } // else
107  }); // AsyncConnect
108 
109  // Start event processing
110  l_IoService.run();
111  } else {
112  throw boost::program_options::validation_error(boost::program_options::validation_error::invalid_option_value);
113  } // else
114  } catch (std::exception& a_Error) {
115  std::cerr << "Exception: " << a_Error.what() << "\n";
116  } // catch
117 
118  return 0;
119 }
120 
#define HDLCD_DEVEL_VERSION_MINOR
Definition: HdlcdConfig.h:3
#define SNET_DEVEL_VERSION_MAJOR
Definition: SnetConfig.h:2
#define SNET_DEVEL_VERSION_MINOR
Definition: SnetConfig.h:3
void AsyncConnect(boost::asio::ip::tcp::resolver::iterator a_EndpointIterator, std::function< void(bool a_bSuccess)> a_OnConnectedCallback)
Perform an asynchronous connect procedure regarding both TCP sockets.
Definition: HdlcdClient.h:82
#define HDLCD_DEVEL_VERSION_MAJOR
Definition: HdlcdConfig.h:2
Class HdlcdClient.
Definition: HdlcdClient.h:54
void SetOnClosedCallback(std::function< void()> a_OnClosedCallback)
Provide a callback method to be called if this client entity is closing.
Definition: HdlcdClient.h:192
int main(int argc, char *argv[])
A set of tools to exchange and handle packets of s-net(r) devices via the HDLC Daemon. Copyright (C) 2016 Florian Evers, florian-evers@gmx.de.
Copyright (c) 2016, Florian Evers, florian-evers@gmx.de All rights reserved.