aboutsummaryrefslogtreecommitdiffstats
path: root/libtransport/src/hicn/transport/core/hicn_forwarder_interface.cc
diff options
context:
space:
mode:
authorMauro Sardara <msardara@cisco.com>2019-02-11 10:44:29 +0100
committerMauro Sardara <msardara@cisco.com>2019-02-18 10:48:38 +0000
commit79e0d4f89c4d532189aae06cc5dfbc14e3269703 (patch)
treeadc83eccb824c019c3c43cc48bcd4de6656eea8d /libtransport/src/hicn/transport/core/hicn_forwarder_interface.cc
parent7d2b217bd01a8da1a2ac57aaad59b3179c7af916 (diff)
[HICN-50] Added udp application connector.
Change-Id: I0c5afad4b404ec485f50b1342b81e70ef85a5163 Signed-off-by: Mauro Sardara <msardara@cisco.com> Signed-off-by: michele papalini <micpapal@cisco.com>
Diffstat (limited to 'libtransport/src/hicn/transport/core/hicn_forwarder_interface.cc')
-rw-r--r--libtransport/src/hicn/transport/core/hicn_forwarder_interface.cc121
1 files changed, 88 insertions, 33 deletions
diff --git a/libtransport/src/hicn/transport/core/hicn_forwarder_interface.cc b/libtransport/src/hicn/transport/core/hicn_forwarder_interface.cc
index 9dc3b63bb..1c8060906 100644
--- a/libtransport/src/hicn/transport/core/hicn_forwarder_interface.cc
+++ b/libtransport/src/hicn/transport/core/hicn_forwarder_interface.cc
@@ -15,11 +15,6 @@
#include <hicn/transport/core/hicn_forwarder_interface.h>
-#define ADDR_INET 1
-#define ADDR_INET6 2
-#define ADD_ROUTE 3
-#define REQUEST_LIGHT 100
-
union AddressLight {
uint32_t ipv4;
struct in6_addr ipv6;
@@ -30,6 +25,13 @@ typedef struct {
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;
@@ -37,51 +39,104 @@ typedef struct {
uint8_t len;
} RouteToSelfCommand;
-namespace transport {
-
-namespace core {
-
-HicnForwarderInterface::HicnForwarderInterface(SocketConnector &connector)
- : ForwarderInterface<HicnForwarderInterface, SocketConnector>(connector) {}
-
-HicnForwarderInterface::~HicnForwarderInterface() {}
+typedef struct {
+ uint8_t message_type;
+ uint8_t command_id;
+ uint16_t length;
+ uint32_t seq_num;
+ char symbolic_or_connid[16];
+} DeleteSelfConnectionCommand;
-void HicnForwarderInterface::connect(bool is_consumer) { connector_.connect(); }
+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 HicnForwarderInterface::registerRoute(Prefix &prefix) {
- auto addr = prefix.toSockaddr();
- const char *identifier = {"SELF_ROUTE"};
+void fillCommandHeader(CommandHeader *header) {
+ // Allocate and fill the header
+ header->message_type = request_light;
+ header->length = 1;
+}
- // allocate command payload
- RouteToSelfCommand *route_to_self = new RouteToSelfCommand();
+std::unique_ptr<RouteToSelfCommand> createCommandRoute(
+ std::unique_ptr<sockaddr> &&addr, uint8_t prefix_length) {
+ auto command = std::make_unique<RouteToSelfCommand>();
// check and set IP address
if (addr->sa_family == AF_INET) {
- route_to_self->address_type = ADDR_INET;
- route_to_self->address.ipv4 = ((Sockaddr4 *)addr.get())->sin_addr.s_addr;
+ command->address_type = addr_inet;
+ command->address.ipv4 = ((sockaddr_in *)addr.get())->sin_addr.s_addr;
} else if (addr->sa_family == AF_INET6) {
- route_to_self->address_type = ADDR_INET6;
- route_to_self->address.ipv6 = ((Sockaddr6 *)addr.get())->sin6_addr;
+ command->address_type = addr_inet6;
+ command->address.ipv6 = ((sockaddr_in6 *)addr.get())->sin6_addr;
}
// Fill remaining payload fields
#ifndef _WIN32
- strcpy(route_to_self->symbolic_or_connid, identifier);
+ strcpy(command->symbolic_or_connid, identifier);
#else
- strcpy_s(route_to_self->symbolic_or_connid, strlen(route_to_self->symbolic_or_connid), identifier);
+ strcpy_s(route_to_self->symbolic_or_connid,
+ strlen(route_to_self->symbolic_or_connid), identifier);
#endif
- route_to_self->cost = 1;
- route_to_self->len = (uint8_t) prefix.getPrefixLength();
+ command->cost = 1;
+ command->len = (uint8_t)prefix_length;
// Allocate and fill the header
- route_to_self->command_id = ADD_ROUTE;
- route_to_self->message_type = REQUEST_LIGHT;
- route_to_self->length = 1;
+ command->command_id = add_route_command;
+ fillCommandHeader((CommandHeader *)command.get());
+
+ return command;
+}
+
+std::unique_ptr<DeleteSelfConnectionCommand> createCommandDeleteConnection() {
+ auto command = std::make_unique<DeleteSelfConnectionCommand>();
+ fillCommandHeader((CommandHeader *)command.get());
+ command->command_id = delete_connection_command;
+
+#ifndef _WIN32
+ strcpy(command->symbolic_or_connid, identifier);
+#else
+ strcpy_s(route_to_self->symbolic_or_connid,
+ strlen(route_to_self->symbolic_or_connid), identifier);
+#endif
+
+ return command;
+}
+
+} // namespace
+
+namespace transport {
+
+namespace core {
+
+HicnForwarderInterface::HicnForwarderInterface(UdpSocketConnector &connector)
+ : ForwarderInterface<HicnForwarderInterface, UdpSocketConnector>(
+ connector) {}
+
+HicnForwarderInterface::~HicnForwarderInterface() {}
+
+void HicnForwarderInterface::connect(bool is_consumer) { connector_.connect(); }
+
+void HicnForwarderInterface::registerRoute(Prefix &prefix) {
+ auto command =
+ createCommandRoute(prefix.toSockaddr(), prefix.getPrefixLength())
+ .release();
+ send((uint8_t *)command, sizeof(RouteToSelfCommand),
+ [command]() { delete command; });
+}
- send((uint8_t *)route_to_self, sizeof(RouteToSelfCommand),
- [route_to_self]() { delete route_to_self; });
+void HicnForwarderInterface::closeConnection() {
+ auto command = createCommandDeleteConnection().release();
+ send((uint8_t *)command, sizeof(DeleteSelfConnectionCommand),
+ [this, command]() {
+ delete command;
+ connector_.close();
+ });
}
} // namespace core
-} // namespace transport \ No newline at end of file
+} // namespace transport