diff options
Diffstat (limited to 'hicn-light/src/hicn/core/connection.h')
-rw-r--r-- | hicn-light/src/hicn/core/connection.h | 328 |
1 files changed, 187 insertions, 141 deletions
diff --git a/hicn-light/src/hicn/core/connection.h b/hicn-light/src/hicn/core/connection.h index b6513ea1a..e177e2039 100644 --- a/hicn-light/src/hicn/core/connection.h +++ b/hicn-light/src/hicn/core/connection.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021-2023 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: @@ -15,184 +15,230 @@ /** * @file connection.h - * @brief Wrapper for different types of connections - * - * A connection wraps a specific set of {@link IoOperations}. Those operations - * allow for input and output. Connections get stored in the Connection Table. - * + * @brief hICN connections */ -#ifndef connection_h -#define connection_h -#include <hicn/hicn-light/config.h> -#include <hicn/core/connectionState.h> -#include <hicn/io/ioOperations.h> -#include <hicn/utils/address.h> +#ifndef HICNLIGHT_CONNECTION_H +#define HICNLIGHT_CONNECTION_H -#ifdef WITH_MAPME -typedef enum { - CONNECTION_EVENT_CREATE, - CONNECTION_EVENT_DELETE, - CONNECTION_EVENT_UPDATE, - CONNECTION_EVENT_SET_UP, - CONNECTION_EVENT_SET_DOWN, - CONNECTION_EVENT_PRIORITY_CHANGED, - CONNECTION_EVENT_TAGS_CHANGED, -} connection_event_t; +#include <hicn/face.h> -#endif /* WITH_MAPME */ +#include "address_pair.h" +#include "listener.h" +#include "msgbuf.h" #ifdef WITH_POLICY #include <hicn/policy.h> #endif /* WITH_POLICY */ -struct connection; -typedef struct connection Connection; +#define CONNECTION_ID_UNDEFINED INVALID_FACE_ID -/** - * Creates a connection object. - */ -Connection *connection_Create(IoOperations *ops); +#define foreach_connection_event \ + _(UNDEFINED) \ + _(CREATE) \ + _(DELETE) \ + _(UPDATE) \ + _(SET_UP) \ + _(SET_DOWN) \ + _(PRIORITY_CHANGED) \ + _(TAGS_CHANGED) \ + _(N) -/** - * @function connection_Release - * @abstract Releases a reference count, destroying on last release - * @discussion - * Only frees the memory on the final reference count. The pointer will - * always be NULL'd. - */ -void connection_Release(Connection **connectionPtr); +typedef enum { +#define _(x) CONNECTION_EVENT_##x, + foreach_connection_event +#undef _ +} connection_event_t; -/** - * @function connection_Acquire - * @abstract A reference counted copy. - * @discussion - * A shallow copy, they share the same memory. - */ -Connection *connection_Acquire(Connection *connection); +struct wldr_s; + +typedef struct { + unsigned id; + char* name; + char* interface_name; + netdevice_type_t interface_type; + face_type_t type; + address_pair_t pair; + // bool up; + bool local; + face_state_t state; + face_state_t admin_state; +#ifdef WITH_POLICY + policy_tags_t tags; + uint32_t priority; +#endif /* WITH_POLICY */ -/** - * @function connection_Send - * @abstract Sends the message on the connection - * @return true if message sent, false if connection not up - */ -bool connection_Send(const Connection *conn, Message *message); + int fd; + bool connected; // true if the connection is connected and has its own fd + event_t* event_data; + + void* data; + + listener_t* listener; + // struct forwarder_s * forwarder; // recv only + bool closed; + + /* WLDR */ + + bool wldr_autostart; + /* + * if true, wldr can be set automatically by default this value is set to + * true. if wldr is activated using a command (config file/hicnLightControl) + * this value is set to false so that a base station can not disable wldr at + * the client. + */ + struct wldr_s* wldr; + + connection_stats_t stats; +} connection_t; + +#if 1 +#define connection_get_id(C) ((C)->id) +#define connection_id_is_valid(ID) (ID != CONNECTION_ID_UNDEFINED) +#define connection_get_name(C) ((C)->name) +#define connection_get_type(C) ((C)->type) +#define connection_has_valid_id(C) \ + (connection_id_is_valid(connection_get_id(C))) +#define connection_has_valid_type(C) \ + (face_type_is_valid(connection_get_type(C))) +#define connection_get_pair(C) (&(C)->pair) +#define connection_get_local(C) (address_pair_get_local(connection_get_pair(C))) +#define connection_get_remote(C) \ + (address_pair_get_remote(connection_get_pair(C))) +#define connection_get_local(C) (address_pair_get_local(connection_get_pair(C))) +#define connection_get_remote(C) \ + (address_pair_get_remote(connection_get_pair(C))) +#define connection_is_up(C) ((C)->state == FACE_STATE_UP) +#define connection_is_closed(C) ((C)->closed == true) +#define connection_is_local(C) ((C)->local) +#define connection_get_state(C) ((C)->state) +#define connection_set_state(C, STATE) (C)->state = STATE +#define connection_get_admin_state(C) ((C)->admin_state) +#define connection_set_admin_state(C, STATE) (C)->admin_state = STATE +#define connection_get_interface_name(C) ((C)->interface_name) +#define connection_get_interface_type(C) ((C)->interface_type) -/** - * @function connection_SendIOVBuffer - * @abstract Sends an IOV buffer - */ -bool connection_SendIOVBuffer(const Connection *conn, struct iovec *msg, - size_t size); +#ifdef WITH_POLICY +#define connection_get_priority(C) ((C)->priority) +#define connection_set_priority(C, PRIORITY) (C)->priority = PRIORITY +#define connection_get_tags(C) ((C)->tags) +#define connection_set_tags(C, TAGS) (C)->tags = TAGS +#define connection_has_tag(C, TAG) policy_tags_has(connection_get_tags(C), TAG) +#define connection_add_tag(C, TAG) policy_tags_add(&connection_get_tags(C), TAG) +#define connection_remove_tag(C, TAG) \ + do { \ + policy_tags_t _conn_var(tags); \ + _conn_var(tags) = connection_get_tags(C); \ + policy_tags_remove(_conn_var(tags), (TAG)); \ + connection_set_tags((C), _conn_var(tags)); \ + } while (0) +#define connection_clear_tags(C) connection_set_tags(C, POLICY_TAGS_EMPTY) -/** - * @function connection_SendBuffer - * @abstract Sends a buffer - */ -bool connection_SendBuffer(const Connection *conn, u8 * buffer, size_t length); +#endif /* WITH_POLICY */ -/** - * Return the `IoOperations` instance associated with the specified `Connection` - * instance. - * @param [in] connection The allocated connection - * @return a pointer to the IoOperations instance associated by th specified - * connection. - */ -IoOperations *connection_GetIoOperations(const Connection *conn); +#else -/** - * Returns the unique identifier of the connection - * Calls the underlying IoOperations to fetch the connection id - * @param [in] connection The allocated connection - * @return unsigned The unique connection id - */ -unsigned connection_GetConnectionId(const Connection *conn); +/* Accessors */ +static inline unsigned connection_get_id(const connection_t* connection); -/** - * Returns the (remote, local) address pair that describes the connection - * @param [in] connection The allocated connection - * @return non-null The connection's remote and local address - * @return null Should never return NULL - */ -const AddressPair *connection_GetAddressPair(const Connection *conn); +#define connection_id_is_valid(id) (id != CONNECTION_ID_UNDEFINED) +#define connection_has_valid_id(C) (connection_id_is_valid(connection_get_id(C)) -/** - * Checks if the connection is in the "up" state - * @param [in] connection The allocated connection - * @return true The connection is in the "up" state - * @return false The connection is not in the "up" state - */ -bool connection_IsUp(const Connection *conn); +static inline char* connection_get_name(const connection_t* connection); -/** - * Checks if the connection is to a Local/Loopback address - * - * A local connection is PF_LOCAL (PF_UNIX) and a loopback connection is - * 127.0.0.0/8 or ::1 for IPv6. - * - * @param [in] connection The allocated connection - * - * @retval true The connection is local or loopback - * @retval false The connection is not local or loopback - */ -bool connection_IsLocal(const Connection *conn); +static inline face_type_t connection_get_type(const connection_t* connection); -/** - * Returns an opaque pointer representing the class of the Io Operations - * - * Returns an opaque pointer that an implementation can use to detect if - * the connection is based on that class. - * - * @param [in] conn The Connection to analyze - * - * @return non-null An opaque pointer for each concrete implementation - */ -const void *connection_Class(const Connection *conn); +static inline address_pair_t* connection_get_pair( + const connection_t* connection); -bool connection_ReSend(const Connection *conn, Message *message, - bool notification); +#define connection_get_local(C) (address_pair_get_local(connection_get_pair(C))) +#define connection_get_remote(C) (address_pair_remote(connection_get_pair(C))) -void connection_Probe(Connection *conn, uint8_t *probe); +static inline bool connection_is_up(const connection_t* connection); -void connection_HandleProbe(Connection *conn, uint8_t *message); +static inline bool connection_is_local(const connection_t* connection); -void connection_AllowWldrAutoStart(Connection *conn, bool allow); +static inline face_state_t connection_get_state(const connection_t* connection); -void connection_EnableWldr(Connection *conn); +static inline void connection_set_state(connection_t* connection, + face_state_t state); -void connection_DisableWldr(Connection *conn); +static inline face_state_t connection_get_admin_state( + const connection_t* connection); -bool connection_HasWldr(const Connection *conn); +static inline void connection_set_admin_state(connection_t* connection, + face_state_t state); -bool connection_WldrAutoStartAllowed(const Connection *conn); +static inline const char* connection_get_interface_name( + const connection_t* connection); -void connection_DetectLosses(Connection *conn, Message *message); +#ifdef WITH_POLICY -void connection_HandleWldrNotification(Connection *conn, Message *message); +static inline uint32_t connection_get_priority(const connection_t* connection); -connection_state_t connection_GetState(const Connection *conn); +static inline void connection_set_priority(connection_t* connection, + uint32_t priority); -void connection_SetState(Connection *conn, connection_state_t state); +static inline policy_tags_t connection_get_tags(const connection_t* connection); -connection_state_t connection_GetAdminState(const Connection *conn); +static inline void connection_set_tags(connection_t* connection, + policy_tags_t tags); -void connection_SetAdminState(Connection *conn, connection_state_t admin_state); +#define connection_has_tag(C, TAG) policy_tags_has(connection_get_tags(C), TAG) -#ifdef WITH_POLICY -uint32_t connection_GetPriority(const Connection *conn); +#define connection_add_tag(C, TAG) policy_tags_add(connection_get_tags(X), TAG) -void connection_SetPriority(Connection *conn, uint32_t priority); -#endif /* WITH_POLICY */ +#define connection_remove_tag(C, TAG) \ + do { \ + policy_tags_t _conn_var(tags); \ + _conn_var(tags) = connection_get_tags(C); \ + policy_tags_remove(_conn_var(tags), (TAG)); \ + connection_set_tags((C), _conn_var(tags)); \ + } while (0) -const char * connection_GetInterfaceName(const Connection * conn); +#define connection_clear_tags(C) connection_set_tags(C, POLICY_TAGS_EMPTY) -#ifdef WITH_POLICY -void connection_AddTag(Connection *conn, policy_tag_t tag); -void connection_RemoveTag(Connection *conn, policy_tag_t tag); -policy_tags_t connection_GetTags(const Connection *conn); -void connection_SetTags(Connection *conn, policy_tags_t tags); -void connection_ClearTags(Connection *conn); -int connection_HasTag(const Connection *conn, policy_tag_t tag); #endif /* WITH_POLICY */ -#endif // connection_h +#endif + +connection_t* connection_create(face_type_t type, const char* name, + const address_pair_t* pair, + const struct forwarder_s* forwarder); + +int connection_initialize(connection_t* connection, face_type_t type, + const char* name, const char* interface_name, int fd, + const address_pair_t* pair, bool local, + unsigned connection_id, listener_t* listener); + +int connection_finalize(connection_t* connection); + +bool connection_send_packet(const connection_t* connection, + const uint8_t* packet, size_t size); + +bool connection_flush(connection_t* connection); + +bool connection_send(connection_t* connection, off_t msgbuf_id, bool queue); + +size_t connection_process_buffer(connection_t* connection, + const uint8_t* buffer, size_t size); + +/* WLDR */ + +void connection_wldr_allow_autostart(connection_t* connection, bool value); + +bool connection_wldr_autostart_is_allowed(const connection_t* connection); + +void connection_wldr_enable(connection_t* connection, bool value); + +bool connection_has_wldr(const connection_t* connection); + +void connection_wldr_detect_losses(const connection_t* connection, + const msgbuf_t* msgbuf); + +void connection_wldr_handle_notification(const connection_t* connection, + const msgbuf_t* msgbuf); + +#define connection_get_listener(connection) (connection->listener) + +#endif /* HICNLIGHT_CONNECTION_H */ |