Hybrid ICN (hICN) plugin  v21.06-rc0-4-g18fa668
io_module.h
1 /*
2  * Copyright (c) 2021 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/core/asio_wrapper.h>
19 #include <hicn/transport/core/connector.h>
20 #include <hicn/transport/core/packet.h>
21 #include <hicn/transport/core/prefix.h>
22 #include <hicn/transport/portability/portability.h>
23 #include <hicn/transport/utils/chrono_typedefs.h>
24 #include <hicn/transport/utils/membuf.h>
25 
26 #include <deque>
27 
28 namespace transport {
29 
30 namespace core {
31 
32 typedef struct {
33  uint64_t rx_packets;
34  uint64_t tx_packets;
35  uint64_t rx_bytes;
36  uint64_t tx_bytes;
37  uint64_t rx_errors;
38  uint64_t tx_errors;
39 } Counters;
40 
41 class Connector;
42 
43 class IoModule {
44  protected:
45  IoModule()
46  : inet_address_({}),
47  inet6_address_({}),
48  mtu_(1500),
49  output_interface_(""),
50  content_store_reserved_(5000) {
51  inet_address_.v4.as_u32 = htonl(0x7f00001);
52  inet6_address_.v6.as_u8[15] = 0x01;
53  }
54 
55  public:
56  static IoModule *load(const char *);
57  static bool unload(IoModule *);
58 
59  public:
60  virtual ~IoModule();
61 
62  virtual void connect(bool is_consumer = true) = 0;
63 
64  virtual bool isConnected() = 0;
65 
66  virtual void init(Connector::PacketReceivedCallback &&receive_callback,
67  Connector::OnReconnectCallback &&reconnect_callback,
68  asio::io_service &io_service,
69  const std::string &app_name = "Libtransport") = 0;
70 
71  virtual void registerRoute(const Prefix &prefix) = 0;
72 
73  virtual std::uint32_t getMtu() = 0;
74 
75  virtual bool isControlMessage(const uint8_t *message) = 0;
76 
77  virtual void processControlMessageReply(utils::MemBuf &packet_buffer) = 0;
78 
79  virtual void closeConnection() = 0;
80 
81  virtual void send(Packet &packet) {
82  counters_.tx_packets++;
83  counters_.tx_bytes += packet.payloadSize() + packet.headerSize();
84 
85  if (_is_ipv4(packet.getFormat())) {
86  packet.setLocator(inet_address_);
87  } else {
88  packet.setLocator(inet6_address_);
89  }
90  }
91 
92  virtual void send(const uint8_t *packet, std::size_t len) = 0;
93 
94  void setContentStoreSize(uint32_t cs_size) {
95  content_store_reserved_ = cs_size;
96  }
97 
98  uint32_t getContentStoreSize() const { return content_store_reserved_; }
99 
100  void setOutputInterface(const std::string &interface) {
101  output_interface_ = interface;
102  }
103 
104  const std::string &getOutputInterface() { return output_interface_; }
105 
106 #ifndef ANDROID
107  private:
108  void *handle_;
109 #endif
110 
111  protected:
112  ip_address_t inet_address_;
113  ip_address_t inet6_address_;
114  uint16_t mtu_;
115  std::string output_interface_;
116  uint32_t content_store_reserved_;
117  Counters counters_;
118 };
119 
120 extern "C" IoModule *createModule();
121 
122 } // namespace core
123 } // namespace transport
transport::core::Packet
Definition: packet.h:51
transport::core::IoModule
Definition: io_module.h:43
ip_address_t
Definition: ip_address.h:68
transport::core::Connector
Definition: connector.h:37
transport
Definition: forwarder_config.h:32
transport::core::Counters
Definition: io_module.h:32
transport::core::Prefix
Definition: prefix.h:24
utils::MemBuf
Definition: membuf.h:45