Radek Krejci | 4339024 | 2015-10-08 15:34:04 +0200 | [diff] [blame] | 1 | /** |
| 2 | * \file session.h |
| 3 | * \author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * \brief libnetconf2 session manipulation |
| 5 | * |
| 6 | * Copyright (c) 2015 CESNET, z.s.p.o. |
| 7 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
Michal Vasko | afd416b | 2016-02-25 14:51:46 +0100 | [diff] [blame] | 11 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 12 | * https://opensource.org/licenses/BSD-3-Clause |
Radek Krejci | 4339024 | 2015-10-08 15:34:04 +0200 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #ifndef NC_SESSION_H_ |
| 16 | #define NC_SESSION_H_ |
| 17 | |
Michal Vasko | c09730e | 2019-01-17 10:07:26 +0100 | [diff] [blame^] | 18 | #ifdef __cplusplus |
| 19 | extern "C" { |
| 20 | #endif |
| 21 | |
Radek Krejci | 6e36bfb | 2016-12-01 21:40:16 +0100 | [diff] [blame] | 22 | #include "netconf.h" |
| 23 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 24 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 4ef1493 | 2015-12-04 11:09:10 +0100 | [diff] [blame] | 25 | |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 26 | /** |
| 27 | * @brief Enumeration of NETCONF SSH authentication methods |
| 28 | */ |
Michal Vasko | 4ef1493 | 2015-12-04 11:09:10 +0100 | [diff] [blame] | 29 | typedef enum { |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 30 | NC_SSH_AUTH_PUBLICKEY = 0x01, /**< publickey SSH authentication */ |
| 31 | NC_SSH_AUTH_PASSWORD = 0x02, /**< password SSH authentication */ |
| 32 | NC_SSH_AUTH_INTERACTIVE = 0x04 /**< interactive SSH authentication */ |
Michal Vasko | 4ef1493 | 2015-12-04 11:09:10 +0100 | [diff] [blame] | 33 | } NC_SSH_AUTH_TYPE; |
| 34 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 35 | #endif /* NC_ENABLED_SSH */ |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 36 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 37 | #ifdef NC_ENABLED_TLS |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 38 | |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 39 | /** |
| 40 | * @brief Enumeration of cert-to-name mapping types |
| 41 | */ |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 42 | typedef enum { |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 43 | NC_TLS_CTN_UNKNOWN = 0, /**< unknown mapping */ |
| 44 | NC_TLS_CTN_SPECIFIED, /**< username explicitly specified */ |
| 45 | NC_TLS_CTN_SAN_RFC822_NAME, /**< email address as username */ |
| 46 | NC_TLS_CTN_SAN_DNS_NAME, /**< DNS name as username */ |
| 47 | NC_TLS_CTN_SAN_IP_ADDRESS, /**< IP address as username */ |
| 48 | NC_TLS_CTN_SAN_ANY, /**< any certificate Subject Alternative Name as username */ |
| 49 | NC_TLS_CTN_COMMON_NAME /**< common name as username */ |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 50 | } NC_TLS_CTN_MAPTYPE; |
| 51 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 52 | #endif /* NC_ENABLED_TLS */ |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 53 | |
Radek Krejci | 4339024 | 2015-10-08 15:34:04 +0200 | [diff] [blame] | 54 | /** |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 55 | * @brief Enumeration of possible session statuses |
| 56 | */ |
| 57 | typedef enum { |
Radek Krejci | 465308c | 2017-05-22 14:49:10 +0200 | [diff] [blame] | 58 | NC_STATUS_ERR = -1, /**< error return code for function getting the session status */ |
Michal Vasko | 38a7c6c | 2015-12-04 12:29:20 +0100 | [diff] [blame] | 59 | NC_STATUS_STARTING = 0, /**< session is not yet fully initiated */ |
| 60 | NC_STATUS_CLOSING, /**< session is being closed */ |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 61 | NC_STATUS_INVALID, /**< session is not running and is supposed to be closed (nc_session_free()) */ |
Michal Vasko | 38a7c6c | 2015-12-04 12:29:20 +0100 | [diff] [blame] | 62 | NC_STATUS_RUNNING /**< up and running */ |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 63 | } NC_STATUS; |
| 64 | |
| 65 | /** |
Michal Vasko | 38a7c6c | 2015-12-04 12:29:20 +0100 | [diff] [blame] | 66 | * @brief Enumeration of transport implementations (ways how libnetconf implements NETCONF transport protocol) |
| 67 | */ |
| 68 | typedef enum { |
| 69 | NC_TI_NONE = 0, /**< none - session is not connected yet */ |
| 70 | NC_TI_FD, /**< file descriptors - use standard input/output, transport protocol is implemented |
| 71 | outside the current application */ |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 72 | NC_TI_UNIX, /**< unix socket */ |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 73 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 38a7c6c | 2015-12-04 12:29:20 +0100 | [diff] [blame] | 74 | NC_TI_LIBSSH, /**< libssh - use libssh library, only for NETCONF over SSH transport */ |
| 75 | #endif |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 76 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 38a7c6c | 2015-12-04 12:29:20 +0100 | [diff] [blame] | 77 | NC_TI_OPENSSL /**< OpenSSL - use OpenSSL library, only for NETCONF over TLS transport */ |
| 78 | #endif |
| 79 | } NC_TRANSPORT_IMPL; |
| 80 | |
| 81 | /** |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 82 | * @brief Enumeration of Call Home connection types. |
| 83 | */ |
| 84 | typedef enum { |
| 85 | NC_CH_CT_NOT_SET = 0, |
| 86 | NC_CH_PERSIST, |
| 87 | NC_CH_PERIOD |
| 88 | } NC_CH_CONN_TYPE; |
| 89 | |
| 90 | /** |
| 91 | * @brief Enumeration of Call Home client priority policy. |
| 92 | */ |
| 93 | typedef enum { |
| 94 | NC_CH_FIRST_LISTED = 0, //default |
| 95 | NC_CH_LAST_CONNECTED |
| 96 | } NC_CH_START_WITH; |
| 97 | |
| 98 | /** |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 99 | * @brief Enumeration of SSH key types. |
| 100 | */ |
| 101 | typedef enum { |
| 102 | NC_SSH_KEY_UNKNOWN = 0, |
| 103 | NC_SSH_KEY_DSA, |
| 104 | NC_SSH_KEY_RSA, |
| 105 | NC_SSH_KEY_ECDSA |
| 106 | } NC_SSH_KEY_TYPE; |
| 107 | |
| 108 | /** |
Radek Krejci | 4339024 | 2015-10-08 15:34:04 +0200 | [diff] [blame] | 109 | * @brief NETCONF session object |
| 110 | */ |
| 111 | struct nc_session; |
| 112 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 113 | /** |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 114 | * @brief Get session status. |
| 115 | * |
| 116 | * @param[in] session Session to get the information from. |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 117 | * @return Session status. |
| 118 | */ |
| 119 | NC_STATUS nc_session_get_status(const struct nc_session *session); |
| 120 | |
| 121 | /** |
Radek Krejci | 6e36bfb | 2016-12-01 21:40:16 +0100 | [diff] [blame] | 122 | * @brief Get session termination reason. |
| 123 | * |
| 124 | * @param[in] session Session to get the information from. |
| 125 | * @return Session termination reason enum value. |
| 126 | */ |
Michal Vasko | 142cfea | 2017-08-07 10:12:11 +0200 | [diff] [blame] | 127 | NC_SESSION_TERM_REASON nc_session_get_term_reason(const struct nc_session *session); |
| 128 | |
| 129 | /** |
| 130 | * @brief Get session killer session ID. |
| 131 | * |
| 132 | * @param[in] session Session to get the information from. |
| 133 | * @return Session killer ID. |
| 134 | */ |
| 135 | uint32_t nc_session_get_killed_by(const struct nc_session *session); |
Radek Krejci | 6e36bfb | 2016-12-01 21:40:16 +0100 | [diff] [blame] | 136 | |
| 137 | /** |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 138 | * @brief Get session ID. |
| 139 | * |
| 140 | * @param[in] session Session to get the information from. |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 141 | * @return Session ID. |
| 142 | */ |
| 143 | uint32_t nc_session_get_id(const struct nc_session *session); |
| 144 | |
| 145 | /** |
Michal Vasko | 174fe8e | 2016-02-17 15:38:09 +0100 | [diff] [blame] | 146 | * @brief Get session NETCONF version. |
| 147 | * |
| 148 | * @param[in] session Session to get the information from. |
| 149 | * @return 0 for version 1.0, non-zero for version 1.1. |
| 150 | */ |
| 151 | int nc_session_get_version(const struct nc_session *session); |
| 152 | |
| 153 | /** |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 154 | * @brief Get session transport used. |
| 155 | * |
| 156 | * @param[in] session Session to get the information from. |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 157 | * @return Session transport. |
| 158 | */ |
| 159 | NC_TRANSPORT_IMPL nc_session_get_ti(const struct nc_session *session); |
| 160 | |
| 161 | /** |
| 162 | * @brief Get session username. |
| 163 | * |
| 164 | * @param[in] session Session to get the information from. |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 165 | * @return Session username. |
| 166 | */ |
| 167 | const char *nc_session_get_username(const struct nc_session *session); |
| 168 | |
| 169 | /** |
| 170 | * @brief Get session host. |
| 171 | * |
| 172 | * @param[in] session Session to get the information from. |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 173 | * @return Session host. |
| 174 | */ |
| 175 | const char *nc_session_get_host(const struct nc_session *session); |
| 176 | |
| 177 | /** |
| 178 | * @brief Get session port. |
| 179 | * |
| 180 | * @param[in] session Session to get the information from. |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 181 | * @return Session port. |
| 182 | */ |
| 183 | uint16_t nc_session_get_port(const struct nc_session *session); |
| 184 | |
| 185 | /** |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 186 | * @brief Get session path (unix socket only). |
| 187 | * |
| 188 | * @param[in] session Session to get the information from. |
| 189 | * @return Session unix socket path. |
| 190 | */ |
| 191 | const char *nc_session_get_path(const struct nc_session *session); |
| 192 | |
| 193 | /** |
Michal Vasko | 9a25e93 | 2016-02-01 10:36:42 +0100 | [diff] [blame] | 194 | * @brief Get session context. |
| 195 | * |
| 196 | * @param[in] session Session to get the information from. |
| 197 | * @return Session context. |
| 198 | */ |
| 199 | struct ly_ctx *nc_session_get_ctx(const struct nc_session *session); |
| 200 | |
| 201 | /** |
Michal Vasko | 2cc4c68 | 2016-03-01 09:16:48 +0100 | [diff] [blame] | 202 | * @brief Assign arbitrary data to a session. |
| 203 | * |
| 204 | * @param[in] session Session to modify. |
| 205 | * @param[in] data Data to be stored in the session. |
| 206 | */ |
| 207 | void nc_session_set_data(struct nc_session *session, void *data); |
| 208 | |
| 209 | /** |
| 210 | * @brief Get the data assigned to a session. |
| 211 | * |
| 212 | * @param[in] session Session to get the data from. |
| 213 | * @return Session-specific data. |
| 214 | */ |
| 215 | void *nc_session_get_data(const struct nc_session *session); |
| 216 | |
| 217 | /** |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 218 | * @brief Free the NETCONF session object. |
| 219 | * |
| 220 | * @param[in] session Object to free. |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 221 | * @param[in] data_free Session user data destructor. |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 222 | */ |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 223 | void nc_session_free(struct nc_session *session, void (*data_free)(void *)); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 224 | |
Radek Krejci | 62aa064 | 2017-05-25 16:33:49 +0200 | [diff] [blame] | 225 | #if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS) |
| 226 | |
| 227 | /** |
| 228 | * @brief Free all the dynamically allocated thread-specific libssl/libcrypto |
| 229 | * resources. |
| 230 | * |
| 231 | * This function should be called only if init (nc_client_init(), respectively nc_server_init()) was called. |
| 232 | * Call it in every thread your application creates just before the thread exits. In the last thread |
| 233 | * (usually the main one) call nc_client_destroy(), respectively nc_server_destroy(). |
| 234 | */ |
| 235 | void nc_thread_destroy(void); |
| 236 | |
| 237 | #endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */ |
| 238 | |
Michal Vasko | c09730e | 2019-01-17 10:07:26 +0100 | [diff] [blame^] | 239 | #ifdef __cplusplus |
| 240 | } |
| 241 | #endif |
| 242 | |
Radek Krejci | 4339024 | 2015-10-08 15:34:04 +0200 | [diff] [blame] | 243 | #endif /* NC_SESSION_H_ */ |