aboutsummaryrefslogtreecommitdiffstats
path: root/libtransport/src/io_modules/udp/hicn_forwarder_module.cc
blob: ba08dd8c057a0e12f081d78e0c48e50eb369023f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
 * Copyright (c) 2017-2020 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <io_modules/udp/hicn_forwarder_module.h>
#include <io_modules/udp/udp_socket_connector.h>

union AddressLight {
  uint32_t ipv4;
  struct in6_addr ipv6;
};

typedef struct {
  uint8_t message_type;
  uint8_t command_id;
  uint16_t length;
  uint32_t seq_num;
} CommandHeader;

typedef struct {
  uint8_t message_type;
  uint8_t command_id;
  uint16_t length;
  uint32_t seq_num;
  char symbolic_or_connid[16];
  union AddressLight address;
  uint16_t cost;
  uint8_t address_type;
  uint8_t len;
} RouteToSelfCommand;

typedef struct {
  uint8_t message_type;
  uint8_t command_id;
  uint16_t length;
  uint32_t seq_num;
  char symbolic_or_connid[16];
} DeleteSelfConnectionCommand;

namespace {
static constexpr uint8_t addr_inet = 1;
static constexpr uint8_t addr_inet6 = 2;
static constexpr uint8_t add_route_command = 3;
static constexpr uint8_t delete_connection_command = 5;
static constexpr uint8_t request_light = 0xc0;
static constexpr char identifier[] = "SELF";

void fillCommandHeader(CommandHeader *header) {
  // Allocate and fill the header
  header->message_type = request_light;
  header->length = 1;
}

RouteToSelfCommand createCommandRoute(std::unique_ptr<sockaddr> &&addr,
                                      uint8_t prefix_length) {
  RouteToSelfCommand command = {0};

  // check and set IP address
  if (addr->sa_family == AF_INET) {
    command.address_type = addr_inet;
    command.address.ipv4 = ((sockaddr_in *)addr.get())->sin_addr.s_addr;
  } else if (addr->sa_family == AF_INET6) {
    command.address_type = addr_inet6;
    command.address.ipv6 = ((sockaddr_in6 *)addr.get())->sin6_addr;
  }

  // Fill remaining payload fields
#ifndef _WIN32
  strcpy(command.symbolic_or_connid, identifier);
#else
  strcpy_s(command.symbolic_or_connid, 16, identifier);
#endif
  command.cost = 1;
  command.len = (uint8_t)prefix_length;

  // Allocate and fill the header
  command.command_id = add_route_command;
  fillCommandHeader((CommandHeader *)&command);

  return command;
}

DeleteSelfConnectionCommand createCommandDeleteConnection() {
  DeleteSelfConnectionCommand command = {0};
  fillCommandHeader((CommandHeader *)&command);
  command.command_id = delete_connection_command;

#ifndef _WIN32
  strcpy(command.symbolic_or_connid, identifier);
#else
  strcpy_s(command.symbolic_or_connid, 16, identifier);
#endif

  return command;
}

}  // namespace

namespace transport {

namespace core {

HicnForwarderModule::HicnForwarderModule() : IoModule(), connector_(nullptr) {}

HicnForwarderModule::~HicnForwarderModule() {}

void HicnForwarderModule::connect(bool is_consumer) {
  connector_->connect();
  connector_->setRole(is_consumer ? Connector::Role::CONSUMER
                                  : Connector::Role::PRODUCER);
}

bool HicnForwarderModule::isConnected() { return connector_->isConnected(); }

void HicnForwarderModule::send(Packet &packet) {
  IoModule::send(packet);
  packet.setChecksum();
  connector_->send(packet);
}

void HicnForwarderModule::send(const uint8_t *packet, std::size_t len) {
  counters_.tx_packets++;
  counters_.tx_bytes += len;

  // Perfect forwarding
  connector_->send(packet, len);
}

void HicnForwarderModule::registerRoute(const Prefix &prefix) {
  auto command = createCommandRoute(prefix.toSockaddr(),
                                    (uint8_t)prefix.getPrefixLength());
  send((uint8_t *)&command, sizeof(RouteToSelfCommand));
}

void HicnForwarderModule::closeConnection() {
  auto command = createCommandDeleteConnection();
  send((uint8_t *)&command, sizeof(DeleteSelfConnectionCommand));
  connector_->close();
}

void HicnForwarderModule::init(
    Connector::PacketReceivedCallback &&receive_callback,
    Connector::OnReconnectCallback &&reconnect_callback,
    asio::io_service &io_service, const std::string &app_name) {
  if (!connector_) {
    connector_ = new UdpSocketConnector(std::move(receive_callback), nullptr,
                                        nullptr, std::move(reconnect_callback),
                                        io_service, app_name);
  }
}

void HicnForwarderModule::processControlMessageReply(
    utils::MemBuf &packet_buffer) {
  if (packet_buffer.data()[0] == nack_code) {
    throw errors::RuntimeException(
        "Received Nack message from hicn light forwarder.");
  }
}

std::uint32_t HicnForwarderModule::getMtu() { return interface_mtu; }

bool HicnForwarderModule::isControlMessage(const uint8_t *message) {
  return message[0] == ack_code || message[0] == nack_code;
}

extern "C" IoModule *create_module(void) { return new HicnForwarderModule(); }

}  // namespace core

}  // namespace transport