1 | /*
|
---|
2 | * The MIT License (MIT)
|
---|
3 | *
|
---|
4 | * Copyright (c) 2013 Mario Hock mails2013@omnifile.org
|
---|
5 | *
|
---|
6 | * Permission is hereby granted, free of charge, to any person
|
---|
7 | * obtaining a copy of this software and associated documentation
|
---|
8 | * files (the "Software"), to deal in the Software without
|
---|
9 | * restriction, including without limitation the rights to use,
|
---|
10 | * copy, modify, merge, publish, distribute, sublicense, and/or sell
|
---|
11 | * copies of the Software, and to permit persons to whom the
|
---|
12 | * Software is furnished to do so, subject to the following
|
---|
13 | * conditions:
|
---|
14 | *
|
---|
15 | * The above copyright notice and this permission notice shall be
|
---|
16 | * included in all copies or substantial portions of the Software.
|
---|
17 | *
|
---|
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
---|
19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
---|
20 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
---|
21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
---|
22 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
---|
23 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
---|
24 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
---|
25 | * OTHER DEALINGS IN THE SOFTWARE.
|
---|
26 | *
|
---|
27 | */
|
---|
28 |
|
---|
29 | #include <iostream>
|
---|
30 | #include "ariba/utility/system/StartupWrapper.h"
|
---|
31 | #include <boost/property_tree/json_parser.hpp>
|
---|
32 | #include <stdexcept>
|
---|
33 |
|
---|
34 | #include "Tunnel.h"
|
---|
35 | #include "Udp.h"
|
---|
36 |
|
---|
37 | using ariba::utility::StartupWrapper;
|
---|
38 |
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * This program tunnels an (bidirectional) UDP connection over ARIBA:
|
---|
42 | *
|
---|
43 | * /------------/ UDP /-------------/
|
---|
44 | * | UDP-Client | <-----------> | AribaTunnel |
|
---|
45 | * /------------/ /--------------/
|
---|
46 | * . ^
|
---|
47 | * . | ARIBA
|
---|
48 | * . V
|
---|
49 | * /------------/ UDP /-------------/
|
---|
50 | * | UDP-Server | <-----------> | AribaTunnel |
|
---|
51 | * /------------/ /--------------/
|
---|
52 | *
|
---|
53 | */
|
---|
54 | int main(int argc, char **argv)
|
---|
55 | {
|
---|
56 | Tunnel tunnel;
|
---|
57 | string config_file;
|
---|
58 |
|
---|
59 | // parameter: config file
|
---|
60 | if ( argc == 2 )
|
---|
61 | {
|
---|
62 | config_file = argv[1];
|
---|
63 |
|
---|
64 | std::cout << "Using config: " << config_file << std::endl;
|
---|
65 | }
|
---|
66 |
|
---|
67 | // read config
|
---|
68 | ptree config;
|
---|
69 | try
|
---|
70 | {
|
---|
71 | using boost::property_tree::json_parser::read_json;
|
---|
72 |
|
---|
73 | read_json(config_file, config);
|
---|
74 | }
|
---|
75 | catch ( std::exception& e )
|
---|
76 | {
|
---|
77 | logging_warn("ERROR: Failed to read config file »" << config_file << "«: ");
|
---|
78 | logging_warn(e.what());
|
---|
79 | logging_warn("---> Using fallback config.");
|
---|
80 |
|
---|
81 | config.put("ariba.spovnet_name", "tunnel");
|
---|
82 | }
|
---|
83 |
|
---|
84 |
|
---|
85 | // start System Queue
|
---|
86 | StartupWrapper::startSystem();
|
---|
87 |
|
---|
88 | // ARIBA
|
---|
89 | // TODO fix/update startup wrapper? -- but scheduleCall does basically the same
|
---|
90 | SystemQueue::instance().scheduleCall(
|
---|
91 | boost::bind(
|
---|
92 | &Tunnel::init_ariba,
|
---|
93 | &tunnel,
|
---|
94 | config)
|
---|
95 | );
|
---|
96 |
|
---|
97 |
|
---|
98 | // ASIO (UDP)
|
---|
99 | uint16_t listen_port = config.get<uint16_t>("tunnel.udp_listen_on_port", 0);
|
---|
100 | uint16_t connect_port = config.get<uint16_t>("tunnel.udp_connect_to_port", 0);
|
---|
101 |
|
---|
102 | boost::asio::io_service io_service;
|
---|
103 | udp_server server(io_service, tunnel);
|
---|
104 |
|
---|
105 | // client-side (use server-socket)
|
---|
106 | if ( listen_port > 0 )
|
---|
107 | {
|
---|
108 | server.listen_on(listen_port);
|
---|
109 | }
|
---|
110 | // server side (use client_socket)
|
---|
111 | else if ( connect_port > 0 )
|
---|
112 | {
|
---|
113 | server.connect_to(connect_port);
|
---|
114 | }
|
---|
115 |
|
---|
116 | tunnel.register_udp_sender(&server);
|
---|
117 |
|
---|
118 | try
|
---|
119 | {
|
---|
120 | io_service.run();
|
---|
121 | }
|
---|
122 | catch (std::exception& e)
|
---|
123 | {
|
---|
124 | std::cerr << e.what() << std::endl;
|
---|
125 | }
|
---|
126 |
|
---|
127 | // TODO stop tunnel gracefully.... fix/update startup wrapper?
|
---|
128 |
|
---|
129 | // stop System Queue
|
---|
130 | StartupWrapper::stopSystem();
|
---|
131 |
|
---|
132 | return 0;
|
---|
133 | }
|
---|