Hybrid ICN (hICN) plugin  v21.06-rc0-4-g18fa668
forwarder_config.h
1 /*
2  * Copyright (c) 2020 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #pragma once
17 
18 #include <hicn/transport/portability/c_portability.h>
19 #include <hicn/transport/utils/branch_prediction.h>
20 #include <hicn/transport/utils/log.h>
21 #include <hicn/transport/utils/string_utils.h>
22 
23 #include <asio.hpp>
24 #include <chrono>
25 #include <sstream>
26 #include <string>
27 
28 #include "forwarder_interface.h"
29 
30 #define RETRY_INTERVAL 300
31 
32 namespace transport {
33 
34 static constexpr char server_header[] = "server";
35 static constexpr char prefix_header[] = "prefix";
36 static constexpr char port_header[] = "port";
37 
38 using OnForwarderConfiguredCallback = std::function<void(bool)>;
39 
41  public:
42  using ListenerRetrievedCallback = std::function<void(std::error_code)>;
43 
44  template <typename Callback>
45  ForwarderConfig(asio::io_service& io_service, Callback&& callback)
46  : forwarder_interface_(io_service),
47  resolver_(io_service),
48  retx_count_(0),
49  timer_(io_service),
50  hicn_listen_port_(~0),
51  listener_retrieved_callback_(std::forward<Callback>(callback)) {}
52 
53  void close() {
54  timer_.cancel();
55  resolver_.cancel();
56  forwarder_interface_.close();
57  }
58 
59  void tryToConnectToForwarder() {
60  doTryToConnectToForwarder(std::make_error_code(std::errc(0)));
61  }
62 
63  void doTryToConnectToForwarder(std::error_code ec) {
64  if (!ec) {
65  // ec == 0 --> timer expired
66  int ret = forwarder_interface_.connectToForwarder();
67  if (ret < 0) {
68  // We were not able to connect to the local forwarder. Do not give up
69  // and retry.
70  TRANSPORT_LOG_ERROR
71  << "Could not connect to local forwarder. Retrying.";
72 
73  timer_.expires_from_now(std::chrono::milliseconds(RETRY_INTERVAL));
74  timer_.async_wait(std::bind(&ForwarderConfig::doTryToConnectToForwarder,
75  this, std::placeholders::_1));
76  } else {
77  timer_.cancel();
78  retx_count_ = 0;
79  doGetMainListener(std::make_error_code(std::errc(0)));
80  }
81  } else {
82  TRANSPORT_LOG_ERROR
83  << "Timer for re-trying forwarder connection canceled.";
84  }
85  }
86 
87  void doGetMainListener(std::error_code ec) {
88  if (!ec) {
89  // ec == 0 --> timer expired
90  int ret = forwarder_interface_.getMainListenerPort();
91  if (ret <= 0) {
92  // Since without the main listener of the forwarder the proxy cannot
93  // work, we can stop the program here until we get the listener port.
94  TRANSPORT_LOG_ERROR
95  << "Could not retrieve main listener port from the forwarder. "
96  "Retrying.";
97 
98  timer_.expires_from_now(std::chrono::milliseconds(RETRY_INTERVAL));
99  timer_.async_wait(std::bind(&ForwarderConfig::doGetMainListener, this,
100  std::placeholders::_1));
101  } else {
102  timer_.cancel();
103  retx_count_ = 0;
104  hicn_listen_port_ = uint16_t(ret);
105  listener_retrieved_callback_(std::make_error_code(std::errc(0)));
106  }
107  } else {
108  TRANSPORT_LOG_ERROR
109  << "Timer for retrieving main hicn listener canceled.";
110  }
111  }
112 
113  template <typename Callback>
114  TRANSPORT_ALWAYS_INLINE bool parseHicnHeader(std::string& header,
115  Callback&& callback) {
116  std::stringstream ss(header);
117  route_info_t* ret = new route_info_t();
118  std::string port_string;
119 
120  while (ss.good()) {
121  std::string substr;
122  getline(ss, substr, ',');
123 
124  if (TRANSPORT_EXPECT_FALSE(substr.empty())) {
125  continue;
126  }
127 
128  utils::trim(substr);
129  auto it = std::find_if(substr.begin(), substr.end(),
130  [](int ch) { return ch == '='; });
131  if (it != std::end(substr)) {
132  auto key = std::string(substr.begin(), it);
133  auto value = std::string(it + 1, substr.end());
134 
135  if (key == server_header) {
136  ret->remote_addr = value;
137  } else if (key == prefix_header) {
138  auto it = std::find_if(value.begin(), value.end(),
139  [](int ch) { return ch == '/'; });
140 
141  if (it != std::end(value)) {
142  ret->route_addr = std::string(value.begin(), it);
143  ret->route_len = std::stoul(std::string(it + 1, value.end()));
144  } else {
145  return false;
146  }
147  } else if (key == port_header) {
148  ret->remote_port = std::stoul(value);
149  port_string = value;
150  } else {
151  // Header not recognized
152  return false;
153  }
154  }
155  }
156 
157  /*
158  * Resolve server address
159  */
160  auto results =
161  resolver_.resolve({ret->remote_addr, port_string,
162  asio::ip::resolver_query_base::numeric_service});
163 
164 #if ((ASIO_VERSION / 100 % 1000) < 12)
165  asio::ip::udp::resolver::iterator end;
166  auto& it = results;
167  while (it != end) {
168 #else
169  for (auto it = results.begin(); it != results.end(); it++) {
170 #endif
171  if (it->endpoint().address().is_v4()) {
172  // Use this v4 address to configure the forwarder.
173  ret->remote_addr = it->endpoint().address().to_string();
174  ret->family = AF_INET;
175  std::string _prefix = ret->route_addr;
176  forwarder_interface_.createFaceAndRoute(
177  RouteInfoPtr(ret), [callback = std::forward<Callback>(callback),
178  configured_prefix = std::move(_prefix)](
179  uint32_t route_id, bool result) {
180  callback(result, configured_prefix);
181  });
182 
183  return true;
184  }
185 #if ((ASIO_VERSION / 100 % 1000) < 12)
186  it++;
187 #endif
188  }
189 
190  return false;
191  }
192 
193  private:
194  ForwarderInterface forwarder_interface_;
195  asio::ip::udp::resolver resolver_;
196  std::uint32_t retx_count_;
197  asio::steady_timer timer_;
198  uint16_t hicn_listen_port_;
199  ListenerRetrievedCallback listener_retrieved_callback_;
200 }; // namespace transport
201 
202 } // namespace transport
transport::ForwarderInterface
Definition: forwarder_interface.h:46
transport::route_info_t
Definition: forwarder_interface.h:36
transport
Definition: forwarder_config.h:32
transport::ForwarderConfig
Definition: forwarder_config.h:40