From 79e0d4f89c4d532189aae06cc5dfbc14e3269703 Mon Sep 17 00:00:00 2001 From: Mauro Sardara Date: Mon, 11 Feb 2019 10:44:29 +0100 Subject: [HICN-50] Added udp application connector. Change-Id: I0c5afad4b404ec485f50b1342b81e70ef85a5163 Signed-off-by: Mauro Sardara Signed-off-by: michele papalini --- .../transport/core/hicn_forwarder_interface.cc | 121 +++++++++++++++------ 1 file changed, 88 insertions(+), 33 deletions(-) (limited to 'libtransport/src/hicn/transport/core/hicn_forwarder_interface.cc') 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,16 +15,18 @@ #include -#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; }; +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; @@ -37,51 +39,104 @@ typedef struct { uint8_t len; } RouteToSelfCommand; -namespace transport { - -namespace core { - -HicnForwarderInterface::HicnForwarderInterface(SocketConnector &connector) - : ForwarderInterface(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 createCommandRoute( + std::unique_ptr &&addr, uint8_t prefix_length) { + auto command = std::make_unique(); // 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 createCommandDeleteConnection() { + auto command = std::make_unique(); + 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( + 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 -- cgit 1.2.3-korg