1 /*
2  * This file is part of RTRlib.
3  *
4  * This file is subject to the terms and conditions of the MIT license.
5  * See the file LICENSE in the top level directory for more details.
6  *
7  * Website: http://rtrlib.realmv6.org/
8  */
9 
10 
11 /**
12  * @defgroup mod_rtr_h RTR socket
13  * @brief An RTR socket implements the RPKI-RTR protocol scheme.
14  * @details One rtr_socket communicates with a single RPKI-RTR server.
15  * @{
16  */
17 
18 #ifndef RTR_H
19 #define RTR_H
20 #include <pthread.h>
21 #include <stdbool.h>
22 #include <stdint.h>
23 
24 enum rtr_rtvals {
25     RTR_SUCCESS = 0,
26     RTR_ERROR = -1,
27     RTR_INVALID_PARAM = -2
28 };
29 
30 /**
31  * @brief These modes let the user configure how received intervals should be handled.
32  */
33 enum rtr_interval_mode {
34     RTR_INTERVAL_MODE_IGNORE_ANY,         /*< Ignore appliance of interval values at all. */
35     RTR_INTERVAL_MODE_ACCEPT_ANY,         /*< Accept any interval values, even if outside of range. */
36     RTR_INTERVAL_MODE_DEFAULT_MIN_MAX,    /*< If interval value is outside of range, apply min (if below range) or max (if above range). */
37     RTR_INTERVAL_MODE_IGNORE_ON_FAILURE   /*< Ignore any interval values that are outside of range. */
38 };
39 
40 /**
41  * @brief States of the RTR socket.
42  */
43 enum rtr_socket_state {
44     /** Socket is establishing the transport connection. */
45     RTR_CONNECTING,
46 
47     /** Connection is established, socket is waiting for a Serial Notify or expiration of the refresh_interval timer */
48     RTR_ESTABLISHED,
49 
50     /** Resetting RTR connection. */
51     RTR_RESET,
52 
53     /** Receiving validation records from the RTR server.  */
54     RTR_SYNC,
55 
56     /** Reconnect without any waiting period */
57     RTR_FAST_RECONNECT,
58 
59     /** No validation records are available on the RTR server. */
60     RTR_ERROR_NO_DATA_AVAIL,
61 
62     /** Server was unable to answer the last serial or reset query. */
63     RTR_ERROR_NO_INCR_UPDATE_AVAIL,
64 
65     /** Fatal protocol error occurred. */
66     RTR_ERROR_FATAL,
67 
68     /** Error on the transport socket occurred. */
69     RTR_ERROR_TRANSPORT,
70 
71     /** RTR Socket was started, but now has shut down. */
72     RTR_SHUTDOWN,
73 
74     /** RTR Socket has not been started yet. Initial state after rtr_init */
75     RTR_CLOSED,
76 };
77 
78 struct rtr_socket;
79 
80 /**
81  * @brief A function pointer that is called if the state of the rtr socket has changed.
82  */
83 typedef void (*rtr_connection_state_fp)(const struct rtr_socket *rtr_socket, const enum rtr_socket_state state, void *connection_state_fp_param_config, void *connection_state_fp_param_group);
84 
85 /**
86  * @brief A RTR socket.
87  * @param tr_socket Pointer to an initialized tr_socket that will be used to communicate with the RTR server.
88  * @param refresh_interval Time period in seconds. Tells the router how long to wait before next attempting to poll the cache, using a Serial Query or
89  * Reset Query PDU.
90  * @param last_update Timestamp of the last validation record update. Is 0 if the pfx_table doesn't stores any
91  * validation reords from this rtr_socket.
92  * @param expire_interval Time period in seconds. Received records are deleted if the client was unable to refresh data for this time period.
93  * If 0 is specified, the expire_interval is twice the refresh_interval.
94  * @param retry_interval Time period in seconds between a faild quary and the next attempt.
95  * @param iv_mode Defines handling of incoming intervals.
96  * @param state Current state of the socket.
97  * @param session_id session_id of the RTR session.
98  * @param request_session_id True, if the rtr_client have to request a new none from the server.
99  * @param serial_number Last serial number of the obtained validation records.
100  * @param pfx_table pfx_table that stores the validation records obtained from the connected rtr server.
101  * @param thread_id Handle of the thread this socket is running in.
102  * @param connection_state_fp A callback function that is executed when the state of the socket changes.
103  * @param connection_state_fp_param_config Parameter that is passed to the connection_state_fp callback. Expects a pointer to a rtr_mgr_config struct.
104  * @param connection_state_fp_param_group Parameter that is passed to the connection_state_fp callback. Expects a pointer to the rtr_mgr_group this socket belongs to.
105  * @param version Protocol version used by this socket
106  * @param has_received_pdus True, if this socket has already recieved PDUs
107  * @param spki_table spki_table that stores the router keys obtaiend from the connected rtr server
108  */
109 struct rtr_socket {
110     struct tr_socket *tr_socket;
111     unsigned int refresh_interval;
112     time_t last_update;
113     unsigned int expire_interval;
114     unsigned int retry_interval;
115     enum rtr_interval_mode iv_mode;
116     enum rtr_socket_state state;
117     uint32_t session_id;
118     bool request_session_id;
119     uint32_t serial_number;
120     struct pfx_table *pfx_table;
121     pthread_t thread_id;
122     rtr_connection_state_fp connection_state_fp;
123     void *connection_state_fp_param_config;
124     void *connection_state_fp_param_group;
125     unsigned int version;
126     bool has_received_pdus;
127     struct spki_table *spki_table;
128     bool is_resetting;
129 };
130 
131 
132 /**
133  * @brief Converts a rtr_socket_state to a String.
134  * @param[in] state state to convert to a string
135  * @return NULL If state isn't a valid rtr_socket_state
136  * @return !=NULL The rtr_socket_state as String.
137  */
138 const char *rtr_state_to_str(enum rtr_socket_state state);
139 
140 /**
141  * @brief Set the interval option to the desired one. It's either RTR_INTERVAL_MODE_IGNORE_ANY,
142  * RTR_INTERVAL_MODE_APPLY_ANY, RTR_INTERVAL_MODE_DEFAULT_MIN_MAX or RTR_INTERVAL_MODE_IGNORE_ON_FAILURE.
143  * @param[in] rtr_socket The target socket.
144  * @param[in] option The new interval option that should be applied.
145  */
146 void rtr_set_interval_mode(struct rtr_socket *rtr_socket, enum rtr_interval_mode option);
147 
148 /**
149  * @brief Get the current interval mode.
150  * @param[in] rtr_socket The target socket.
151  * @return The value of the interval_option variable.
152  */
153 enum rtr_interval_mode rtr_get_interval_mode(struct rtr_socket *rtr_socket);
154 #endif
155 /* @} */
156