blob: d8080a56da94ad561870b6c3b9bcfb05b71dad21 [file] [log] [blame]
Radek Krejci43390242015-10-08 15:34:04 +02001/**
Michal Vasko95ea9ff2021-11-09 12:29:14 +01002 * @file session.h
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief libnetconf2 session manipulation
Radek Krejci43390242015-10-08 15:34:04 +02005 *
Michal Vasko95ea9ff2021-11-09 12:29:14 +01006 * @copyright
7 * Copyright (c) 2015 - 2021 CESNET, z.s.p.o.
Radek Krejci43390242015-10-08 15:34:04 +02008 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +01009 * This source code is licensed under BSD 3-Clause License (the "License").
10 * You may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
Michal Vaskoafd416b2016-02-25 14:51:46 +010012 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +010013 * https://opensource.org/licenses/BSD-3-Clause
Radek Krejci43390242015-10-08 15:34:04 +020014 */
15
16#ifndef NC_SESSION_H_
17#define NC_SESSION_H_
18
Michal Vaskoc09730e2019-01-17 10:07:26 +010019#ifdef __cplusplus
20extern "C" {
21#endif
22
Radek Krejci6e36bfb2016-12-01 21:40:16 +010023#include "netconf.h"
24
Radek Krejci53691be2016-02-22 13:58:37 +010025#ifdef NC_ENABLED_SSH
Michal Vasko4ef14932015-12-04 11:09:10 +010026
Michal Vaskof0537d82016-01-29 14:42:38 +010027/**
28 * @brief Enumeration of NETCONF SSH authentication methods
29 */
Michal Vasko4ef14932015-12-04 11:09:10 +010030typedef enum {
Michal Vaskof0537d82016-01-29 14:42:38 +010031 NC_SSH_AUTH_PUBLICKEY = 0x01, /**< publickey SSH authentication */
32 NC_SSH_AUTH_PASSWORD = 0x02, /**< password SSH authentication */
33 NC_SSH_AUTH_INTERACTIVE = 0x04 /**< interactive SSH authentication */
Michal Vasko4ef14932015-12-04 11:09:10 +010034} NC_SSH_AUTH_TYPE;
35
Radek Krejci53691be2016-02-22 13:58:37 +010036#endif /* NC_ENABLED_SSH */
Radek Krejci695d4fa2015-10-22 13:23:54 +020037
Radek Krejci53691be2016-02-22 13:58:37 +010038#ifdef NC_ENABLED_TLS
Michal Vaskoc14e3c82016-01-11 16:14:30 +010039
Michal Vaskof0537d82016-01-29 14:42:38 +010040/**
41 * @brief Enumeration of cert-to-name mapping types
42 */
Michal Vaskoc14e3c82016-01-11 16:14:30 +010043typedef enum {
Michal Vaskof0537d82016-01-29 14:42:38 +010044 NC_TLS_CTN_UNKNOWN = 0, /**< unknown mapping */
45 NC_TLS_CTN_SPECIFIED, /**< username explicitly specified */
46 NC_TLS_CTN_SAN_RFC822_NAME, /**< email address as username */
47 NC_TLS_CTN_SAN_DNS_NAME, /**< DNS name as username */
48 NC_TLS_CTN_SAN_IP_ADDRESS, /**< IP address as username */
49 NC_TLS_CTN_SAN_ANY, /**< any certificate Subject Alternative Name as username */
50 NC_TLS_CTN_COMMON_NAME /**< common name as username */
Michal Vaskoc14e3c82016-01-11 16:14:30 +010051} NC_TLS_CTN_MAPTYPE;
52
Radek Krejci53691be2016-02-22 13:58:37 +010053#endif /* NC_ENABLED_TLS */
Michal Vaskoc14e3c82016-01-11 16:14:30 +010054
Radek Krejci43390242015-10-08 15:34:04 +020055/**
Radek Krejci695d4fa2015-10-22 13:23:54 +020056 * @brief Enumeration of possible session statuses
57 */
58typedef enum {
Radek Krejci465308c2017-05-22 14:49:10 +020059 NC_STATUS_ERR = -1, /**< error return code for function getting the session status */
Michal Vasko38a7c6c2015-12-04 12:29:20 +010060 NC_STATUS_STARTING = 0, /**< session is not yet fully initiated */
61 NC_STATUS_CLOSING, /**< session is being closed */
Michal Vasko428087d2016-01-14 16:04:28 +010062 NC_STATUS_INVALID, /**< session is not running and is supposed to be closed (nc_session_free()) */
Michal Vasko38a7c6c2015-12-04 12:29:20 +010063 NC_STATUS_RUNNING /**< up and running */
Radek Krejci695d4fa2015-10-22 13:23:54 +020064} NC_STATUS;
65
66/**
Michal Vasko38a7c6c2015-12-04 12:29:20 +010067 * @brief Enumeration of transport implementations (ways how libnetconf implements NETCONF transport protocol)
68 */
69typedef enum {
70 NC_TI_NONE = 0, /**< none - session is not connected yet */
71 NC_TI_FD, /**< file descriptors - use standard input/output, transport protocol is implemented
72 outside the current application */
Olivier Matzac7fa2f2018-10-11 10:02:04 +020073 NC_TI_UNIX, /**< unix socket */
Radek Krejci53691be2016-02-22 13:58:37 +010074#ifdef NC_ENABLED_SSH
Michal Vasko38a7c6c2015-12-04 12:29:20 +010075 NC_TI_LIBSSH, /**< libssh - use libssh library, only for NETCONF over SSH transport */
76#endif
Radek Krejci53691be2016-02-22 13:58:37 +010077#ifdef NC_ENABLED_TLS
Michal Vasko38a7c6c2015-12-04 12:29:20 +010078 NC_TI_OPENSSL /**< OpenSSL - use OpenSSL library, only for NETCONF over TLS transport */
79#endif
80} NC_TRANSPORT_IMPL;
81
82/**
Michal Vasko2e6defd2016-10-07 15:48:15 +020083 * @brief Enumeration of Call Home connection types.
84 */
85typedef enum {
86 NC_CH_CT_NOT_SET = 0,
87 NC_CH_PERSIST,
88 NC_CH_PERIOD
89} NC_CH_CONN_TYPE;
90
91/**
92 * @brief Enumeration of Call Home client priority policy.
93 */
94typedef enum {
Michal Vaskob83a3fa2021-05-26 09:53:42 +020095 NC_CH_FIRST_LISTED = 0, // default
Michal Vaskoe49a15f2019-05-27 14:18:36 +020096 NC_CH_LAST_CONNECTED,
97 NC_CH_RANDOM
Michal Vasko2e6defd2016-10-07 15:48:15 +020098} NC_CH_START_WITH;
99
100/**
Michal Vasko17dfda92016-12-01 14:06:16 +0100101 * @brief Enumeration of SSH key types.
102 */
103typedef enum {
104 NC_SSH_KEY_UNKNOWN = 0,
105 NC_SSH_KEY_DSA,
106 NC_SSH_KEY_RSA,
107 NC_SSH_KEY_ECDSA
108} NC_SSH_KEY_TYPE;
109
110/**
Radek Krejci43390242015-10-08 15:34:04 +0200111 * @brief NETCONF session object
112 */
113struct nc_session;
114
Radek Krejci695d4fa2015-10-22 13:23:54 +0200115/**
Michal Vasko8dadf782016-01-15 10:29:36 +0100116 * @brief Get session status.
117 *
118 * @param[in] session Session to get the information from.
Michal Vasko8dadf782016-01-15 10:29:36 +0100119 * @return Session status.
120 */
121NC_STATUS nc_session_get_status(const struct nc_session *session);
122
123/**
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100124 * @brief Get session termination reason.
125 *
126 * @param[in] session Session to get the information from.
127 * @return Session termination reason enum value.
128 */
Michal Vasko142cfea2017-08-07 10:12:11 +0200129NC_SESSION_TERM_REASON nc_session_get_term_reason(const struct nc_session *session);
130
131/**
132 * @brief Get session killer session ID.
133 *
134 * @param[in] session Session to get the information from.
135 * @return Session killer ID.
136 */
137uint32_t nc_session_get_killed_by(const struct nc_session *session);
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100138
139/**
Michal Vasko8dadf782016-01-15 10:29:36 +0100140 * @brief Get session ID.
141 *
142 * @param[in] session Session to get the information from.
Michal Vasko8dadf782016-01-15 10:29:36 +0100143 * @return Session ID.
144 */
145uint32_t nc_session_get_id(const struct nc_session *session);
146
147/**
Michal Vasko174fe8e2016-02-17 15:38:09 +0100148 * @brief Get session NETCONF version.
149 *
150 * @param[in] session Session to get the information from.
151 * @return 0 for version 1.0, non-zero for version 1.1.
152 */
153int nc_session_get_version(const struct nc_session *session);
154
155/**
Michal Vasko8dadf782016-01-15 10:29:36 +0100156 * @brief Get session transport used.
157 *
158 * @param[in] session Session to get the information from.
Michal Vasko8dadf782016-01-15 10:29:36 +0100159 * @return Session transport.
160 */
161NC_TRANSPORT_IMPL nc_session_get_ti(const struct nc_session *session);
162
163/**
164 * @brief Get session username.
165 *
166 * @param[in] session Session to get the information from.
Michal Vasko8dadf782016-01-15 10:29:36 +0100167 * @return Session username.
168 */
169const char *nc_session_get_username(const struct nc_session *session);
170
171/**
172 * @brief Get session host.
173 *
174 * @param[in] session Session to get the information from.
Michal Vasko8dadf782016-01-15 10:29:36 +0100175 * @return Session host.
176 */
177const char *nc_session_get_host(const struct nc_session *session);
178
179/**
180 * @brief Get session port.
181 *
182 * @param[in] session Session to get the information from.
Michal Vasko8dadf782016-01-15 10:29:36 +0100183 * @return Session port.
184 */
185uint16_t nc_session_get_port(const struct nc_session *session);
186
187/**
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200188 * @brief Get session path (unix socket only).
189 *
190 * @param[in] session Session to get the information from.
191 * @return Session unix socket path.
192 */
193const char *nc_session_get_path(const struct nc_session *session);
194
195/**
Michal Vasko9a25e932016-02-01 10:36:42 +0100196 * @brief Get session context.
197 *
198 * @param[in] session Session to get the information from.
199 * @return Session context.
200 */
Michal Vasko93224072021-11-09 12:14:28 +0100201const struct ly_ctx *nc_session_get_ctx(const struct nc_session *session);
Michal Vasko9a25e932016-02-01 10:36:42 +0100202
203/**
Michal Vasko2cc4c682016-03-01 09:16:48 +0100204 * @brief Assign arbitrary data to a session.
205 *
206 * @param[in] session Session to modify.
207 * @param[in] data Data to be stored in the session.
208 */
209void nc_session_set_data(struct nc_session *session, void *data);
210
211/**
212 * @brief Get the data assigned to a session.
213 *
214 * @param[in] session Session to get the data from.
215 * @return Session-specific data.
216 */
217void *nc_session_get_data(const struct nc_session *session);
218
219/**
Radek Krejci695d4fa2015-10-22 13:23:54 +0200220 * @brief Free the NETCONF session object.
221 *
222 * @param[in] session Object to free.
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100223 * @param[in] data_free Session user data destructor.
Radek Krejci695d4fa2015-10-22 13:23:54 +0200224 */
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100225void nc_session_free(struct nc_session *session, void (*data_free)(void *));
Radek Krejci695d4fa2015-10-22 13:23:54 +0200226
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200227#if defined (NC_ENABLED_SSH) || defined (NC_ENABLED_TLS)
Radek Krejci62aa0642017-05-25 16:33:49 +0200228
229/**
230 * @brief Free all the dynamically allocated thread-specific libssl/libcrypto
231 * resources.
232 *
233 * This function should be called only if init (nc_client_init(), respectively nc_server_init()) was called.
234 * Call it in every thread your application creates just before the thread exits. In the last thread
235 * (usually the main one) call nc_client_destroy(), respectively nc_server_destroy().
236 */
237void nc_thread_destroy(void);
238
239#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
240
Michal Vaskoc09730e2019-01-17 10:07:26 +0100241#ifdef __cplusplus
242}
243#endif
244
Radek Krejci43390242015-10-08 15:34:04 +0200245#endif /* NC_SESSION_H_ */