26 #include <boost/asio.hpp> 27 #include <boost/program_options.hpp> 28 #include <boost/regex.hpp> 39 m_StopperCallbackList.push_back(a_StopperCallback);
43 while (!m_StopperCallbackList.empty()) {
44 auto l_StopperCallback = m_StopperCallbackList.front();
45 m_StopperCallbackList.pop_front();
51 std::list<std::function<void()>> m_StopperCallbackList;
54 int main(
int argc,
char* argv[]) {
57 boost::program_options::options_description l_Description(
"Allowed options");
58 l_Description.add_options()
59 (
"help,h",
"produce this help message")
60 (
"version,v",
"show version information")
61 (
"connect,c", boost::program_options::value<std::string>(),
62 "connect to a single device via the HDLCd\n" 63 "syntax: SerialPort@IPAddess:PortNbr\n" 64 " linux: /dev/ttyUSB0@localhost:5001\n" 65 " windows: //./COM1@example.com:5001")
66 (
"payload,p", boost::program_options::value<std::string>(),
67 "quoted payload to be sent as hex dump")
71 boost::program_options::variables_map l_VariablesMap;
72 boost::program_options::store(boost::program_options::parse_command_line(argc, argv, l_Description), l_VariablesMap);
73 boost::program_options::notify(l_VariablesMap);
74 if (l_VariablesMap.count(
"version")) {
75 std::cerr <<
"HDLCd payload injector (single packet as hexdump via command line) version " << HDLCD_TOOLS_VERSION_MAJOR <<
"." << HDLCD_TOOLS_VERSION_MINOR
79 if (l_VariablesMap.count(
"help")) {
80 std::cout << l_Description << std::endl;
81 std::cout <<
"The HDLC hex injector is Copyright (C) 2016, and GNU GPL'd, by Florian Evers." << std::endl;
82 std::cout <<
"Bug reports, feedback, admiration, abuse, etc, to: https://github.com/Strunzdesign/hdlcd-tools" << std::endl;
86 if (!l_VariablesMap.count(
"connect")) {
87 std::cout <<
"hdlcd-hexinjector: you have to specify one device to connect to" << std::endl;
88 std::cout <<
"hdlcd-hexinjector: Use --help for more information." << std::endl;
92 if (!l_VariablesMap.count(
"payload")) {
93 std::cout <<
"hdlcd-hexinjector: you have to provide a payload to be transmitted" << std::endl;
94 std::cout <<
"hdlcd-hexinjector: Use --help for more information." << std::endl;
99 boost::asio::io_service l_IoService;
103 boost::asio::signal_set l_Signals(l_IoService);
104 l_Signals.add(SIGINT);
105 l_Signals.add(SIGTERM);
106 l_Signals.async_wait([&l_SystemStopper](boost::system::error_code,
int){ l_SystemStopper.
Stop(); });
110 static boost::regex s_RegEx(
"^(.*?)@(.*?):(.*?)$");
111 boost::smatch l_Match;
112 if (boost::regex_match(l_VariablesMap[
"connect"].as<std::string>(), l_Match, s_RegEx)) {
114 boost::asio::ip::tcp::resolver l_Resolver(l_IoService);
115 auto l_EndpointIterator = l_Resolver.resolve({ l_Match[2], l_Match[3] });
121 l_HdlcdClient.
AsyncConnect(l_EndpointIterator, [&l_VariablesMap, &l_HdlcdClient, &l_SystemStopper](
bool a_bSuccess) {
124 std::istringstream l_InputStream(l_VariablesMap[
"payload"].as<std::string>());
125 l_InputStream >> std::hex;
126 std::vector<unsigned char> l_Buffer;
127 l_Buffer.reserve(65536);
128 l_Buffer.insert(l_Buffer.end(),std::istream_iterator<unsigned int>(l_InputStream), {});
132 std::cout <<
"Failed to connect to the HDLC Daemon!" << std::endl;
133 l_SystemStopper.
Stop();
140 throw boost::program_options::validation_error(boost::program_options::validation_error::invalid_option_value);
142 }
catch (std::exception& a_Error) {
143 std::cerr <<
"Exception: " << a_Error.what() <<
"\n";
#define HDLCD_DEVEL_VERSION_MINOR
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.
bool Send(const HdlcdPacketData &a_PacketData, std::function< void()> a_OnSendDoneCallback=nullptr)
Send a single data packet to the peer entity.
#define HDLCD_DEVEL_VERSION_MAJOR
static HdlcdPacketData CreatePacket(const std::vector< unsigned char > a_Payload, bool a_bReliable, bool a_bInvalid=false, bool a_bWasSent=false)
void Shutdown()
Shuts all TCP connections down.
void RegisterStopperCallback(std::function< void()> a_StopperCallback)
void SetOnClosedCallback(std::function< void()> a_OnClosedCallback)
Provide a callback method to be called if this client entity is closing.
Copyright (c) 2016, Florian Evers, florian-evers@gmx.de All rights reserved.
int main(int argc, char *argv[])
void Close()
Close the client entity.