blob: 21f61690033d5fea6a2f22843cfa4b5013a67703 [file] [log] [blame]
Radek Krejci43390242015-10-08 15:34:04 +02001/**
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 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of the Company nor the names of its contributors
18 * may be used to endorse or promote products derived from this
19 * software without specific prior written permission.
20 *
21 */
22
23#ifndef NC_SESSION_H_
24#define NC_SESSION_H_
25
Radek Krejcife0b3472015-10-12 13:43:42 +020026#include <stdint.h>
27
Michal Vaskofb2fb762015-10-27 11:44:32 +010028#ifdef ENABLE_SSH
Radek Krejci695d4fa2015-10-22 13:23:54 +020029# include <libssh/libssh.h>
Michal Vaskofb2fb762015-10-27 11:44:32 +010030#endif /* ENABLE_SSH */
Radek Krejci695d4fa2015-10-22 13:23:54 +020031
32#ifdef ENABLE_TLS
33# include <openssl/ssl.h>
34#endif /* ENABLE_TLS */
35
Radek Krejci43390242015-10-08 15:34:04 +020036#include "messages.h"
37
38/**
Radek Krejci695d4fa2015-10-22 13:23:54 +020039 * @brief Enumeration of possible session statuses
40 */
41typedef enum {
42 NC_STATUS_STARTING, /**< session is not yet fully initiated */
43 NC_STATUS_CLOSING, /**< session is being closed */
44 NC_STATUS_INVALID, /**< session is corrupted and it is supposed to be closed (nc_session_free()) */
45 NC_STATUS_RUNNING /**< up and running */
46} NC_STATUS;
47
48/**
Radek Krejci43390242015-10-08 15:34:04 +020049 * @brief NETCONF session object
50 */
51struct nc_session;
52
Radek Krejci695d4fa2015-10-22 13:23:54 +020053#ifdef NC_CLIENT_H_
54
55/**
56 * @brief Set location where libnetconf tries to search for YANG/YIN schemas.
57 *
58 * The location is search when connecting to a NETCONF server and building
59 * YANG context for further processing of the NETCONF messages and data.
60 *
61 * Function is provided only via nc_client.h header file.
62 *
63 * @param[in] path Directory where to search for YANG/YIN schemas.
64 * @return 0 on success, 1 on (memory allocation) failure.
65 */
66int nc_schema_searchpath(const char *path);
67
68/**
69 * @brief Connect to the NETCONF server via proviaded input/output file descriptors.
70 *
71 * Transport layer is supposed to be already set. Function do not cover authentication
72 * or any other manipulation with the transport layer, it only establish NETCONF session
73 * by sending and processing NETCONF \<hello\> messages.
74 *
75 * Function is provided only via nc_client.h header file.
76 *
77 * @param[in] fdin Input file descriptor for reading (clear) data from NETCONF server.
78 * @param[in] fdout Output file descriptor for writing (clear) data for NETCONF server.
79 * @param[in] ctx Optional parameter. If set, provides strict YANG context for the session
80 * (ignoring what is actually supported by the server side). If not set,
81 * YANG context is created for the session using \<get-schema\> (if supported
82 * by the server side) or/and by searching for YANG schemas in the searchpath
83 * (see nc_schema_searchpath()).
84 * @return Created NETCONF session object or NULL in case of error.
85 */
Radek Krejci1d06db42015-10-22 13:39:49 +020086struct nc_session *nc_connect_inout(int fdin, int fdout, struct ly_ctx *ctx);
Radek Krejci695d4fa2015-10-22 13:23:54 +020087
Michal Vaskofb2fb762015-10-27 11:44:32 +010088#ifdef ENABLE_SSH
Radek Krejci695d4fa2015-10-22 13:23:54 +020089
90/**
91 * @brief Connect to the NETCONF server using SSH transport (via libssh).
92 *
93 * SSH session is created with default options. If a caller need to change SSH session properties,
Radek Krejci1d06db42015-10-22 13:39:49 +020094 * it is supposed to use nc_connect_libssh().
Radek Krejci695d4fa2015-10-22 13:23:54 +020095 *
96 * Function is provided only via nc_client.h header file and only when libnetconf2 is compiled with libssh support.
97 *
98 * @param[in] host Hostname or address (both Ipv4 and IPv6 are accepted) of the target server.
99 * 'localhost' is used by default if NULL is specified.
100 * @param[in] port Port number of the target server. Default value 830 is used if 0 is specified.
101 * @param[in] username Name of the user to login to the server. The user running the application (detected from the
102 * effective UID) is used if NULL is specified.
103 * @param[in] ctx Optional parameter. If set, provides strict YANG context for the session
104 * (ignoring what is actually supported by the server side). If not set,
105 * YANG context is created for the session using \<get-schema\> (if supported
106 * by the server side) or/and by searching for YANG schemas in the searchpath
107 * (see nc_schema_searchpath()).
108 * @return Created NETCONF session object or NULL in case of error.
109 */
Radek Krejci1d06db42015-10-22 13:39:49 +0200110struct nc_session *nc_connect_ssh(const char *host, unsigned short port, const char* username, struct ly_ctx *ctx);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200111
112/**
113 * @brief Connect to the NETCONF server using the provided SSH (libssh) session.
114 *
Michal Vasko7b62fed2015-10-26 15:39:46 +0100115 * SSH session can have any options set, they will not be modified. If no options were set,
116 * host 'localhost', port 22, and the username detected from the EUID is used. If socket is
117 * set and connected only username must be set/is detected. Or the \p ssh_session can already
118 * be authenticated in which case it is used directly.
119 *
Radek Krejci695d4fa2015-10-22 13:23:54 +0200120 * Function is provided only via nc_client.h header file and only when libnetconf2 is compiled with libssh support.
121 *
Michal Vasko7b62fed2015-10-26 15:39:46 +0100122 * @param[in] ssh_session libssh structure representing SSH session object. After passing it
123 * to libnetconf2 this way, it is fully managed by it (including freeing!).
Radek Krejci695d4fa2015-10-22 13:23:54 +0200124 * @param[in] ctx Optional parameter. If set, provides strict YANG context for the session
125 * (ignoring what is actually supported by the server side). If not set,
126 * YANG context is created for the session using \<get-schema\> (if supported
127 * by the server side) or/and by searching for YANG schemas in the searchpath
128 * (see nc_schema_searchpath()).
129 * @return Created NETCONF session object or NULL in case of error.
130 */
Michal Vasko7b62fed2015-10-26 15:39:46 +0100131struct nc_session *nc_connect_libssh(ssh_session ssh_session, struct ly_ctx *ctx);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200132
133/**
134 * @brief Create another NETCONF session on existing SSH session using separated SSH channel.
135 *
136 * Function is provided only via nc_client.h header file and only when libnetconf2 is compiled with libssh support.
137 *
138 * @param[in] session Existing NETCONF session. The session has to be created on SSH transport layer using libssh -
Radek Krejci1d06db42015-10-22 13:39:49 +0200139 * it has to be created by nc_connect_ssh(), nc_connect_libssh() or nc_connect_ssh_channel().
Michal Vasko7b62fed2015-10-26 15:39:46 +0100140 * @param[in] ctx Optional parameter. If set, provides strict YANG context for the session
141 * (ignoring what is actually supported by the server side). If not set,
142 * YANG context is created for the session using \<get-schema\> (if supported
143 * by the server side) or/and by searching for YANG schemas in the searchpath
144 * (see nc_schema_searchpath()).
Radek Krejci695d4fa2015-10-22 13:23:54 +0200145 * @return Created NETCONF session object or NULL in case of error.
146 */
Michal Vasko7b62fed2015-10-26 15:39:46 +0100147struct nc_session *nc_connect_ssh_channel(struct nc_session *session, struct ly_ctx *ctx);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200148
Michal Vaskofb2fb762015-10-27 11:44:32 +0100149#endif /* ENABLE_SSH */
Radek Krejci695d4fa2015-10-22 13:23:54 +0200150
151#ifdef ENABLE_TLS
152
153/**
154 * @brief Connect to the NETCONF server using TLS transport (via libssl)
155 *
156 * TLS session is created with default options. If a caller need to change TLS session properties,
Radek Krejci1d06db42015-10-22 13:39:49 +0200157 * it is supposed to use nc_connect_libssl().
Radek Krejci695d4fa2015-10-22 13:23:54 +0200158 *
159 * Function is provided only via nc_client.h header file and only when libnetconf2 is compiled with TLS support.
160 *
161 * @param[in] host Hostname or address (both Ipv4 and IPv6 are accepted) of the target server.
162 * 'localhost' is used by default if NULL is specified.
163 * @param[in] port Port number of the target server. Default value 6513 is used if 0 is specified.
164 * @param[in] username Name of the user to login to the server. The user running the application (detected from the
165 * effective UID) is used if NULL is specified.
166 * @param[in] ctx Optional parameter. If set, provides strict YANG context for the session
167 * (ignoring what is actually supported by the server side). If not set,
168 * YANG context is created for the session using \<get-schema\> (if supported
169 * by the server side) or/and by searching for YANG schemas in the searchpath
170 * (see nc_schema_searchpath()).
171 * @return Created NETCONF session object or NULL in case of error.
172 */
Radek Krejci1d06db42015-10-22 13:39:49 +0200173struct nc_session *nc_connect_tls(const char *host, unsigned short port, const char *username, struct ly_ctx *ctx);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200174
175/**
176 * @brief Connect to the NETCONF server using the provided TLS (libssl) session.
177 *
178 * Function is provided only via nc_client.h header file and only when libnetconf2 is compiled with TLS support.
179 *
180 * @param[in] tls libssl structure representing TLS session object.
181 * @param[in] ctx Optional parameter. If set, provides strict YANG context for the session
182 * (ignoring what is actually supported by the server side). If not set,
183 * YANG context is created for the session using \<get-schema\> (if supported
184 * by the server side) or/and by searching for YANG schemas in the searchpath
185 * (see nc_schema_searchpath()).
186 * @return Created NETCONF session object or NULL in case of error.
187 */
Radek Krejci1d06db42015-10-22 13:39:49 +0200188struct nc_session *nc_connect_libssl(SSL *tls, struct ly_ctx *ctx);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200189
190#endif /* ENABLE_TLS */
191
192#endif /* NC_CLIENT_H_ */
193
194/**
195 * @brief Free the NETCONF session object.
196 *
197 * @param[in] session Object to free.
198 */
199void nc_session_free(struct nc_session *session);
200
Radek Krejci43390242015-10-08 15:34:04 +0200201/**
Radek Krejcife0b3472015-10-12 13:43:42 +0200202 * @brief Receive NETCONF RPC.
203 *
204 * @param[in] session NETCONF session from which the function gets data. It must be the
205 * server side session object.
206 * @param[in] timeout Timeout for reading in milliseconds. Use negative value for infinite
207 * waiting and 0 for immediate return if data are not available on wire.
208 * @param[out] notif Resulting object of NETCONF RPC.
209 * @return NC_MSG_RPC for success, NC_MSG_WOULDBLOCK if timeout reached and NC_MSG_ERROR
210 * when reading has failed.
Radek Krejci43390242015-10-08 15:34:04 +0200211 */
Radek Krejci695d4fa2015-10-22 13:23:54 +0200212NC_MSG_TYPE nc_recv_rpc(struct nc_session* session, int timeout, struct nc_rpc_server **rpc);
Radek Krejci43390242015-10-08 15:34:04 +0200213
Radek Krejci5686ff72015-10-09 13:33:56 +0200214/**
Radek Krejcife0b3472015-10-12 13:43:42 +0200215 * @brief Receive NETCONF RPC reply.
216 *
217 * @param[in] session NETCONF session from which the function gets data. It must be the
218 * client side session object.
219 * @param[in] timeout Timeout for reading in milliseconds. Use negative value for infinite
220 * waiting and 0 for immediate return if data are not available on wire.
221 * @param[out] reply Resulting object of NETCONF RPC reply.
222 * @return NC_MSG_REPLY for success, NC_MSG_WOULDBLOCK if timeout reached and NC_MSG_ERROR
223 * when reading has failed.
Radek Krejci5686ff72015-10-09 13:33:56 +0200224 */
225NC_MSG_TYPE nc_recv_reply(struct nc_session* session, int timeout, struct nc_reply **reply);
226
227/**
Radek Krejcife0b3472015-10-12 13:43:42 +0200228 * @brief Receive NETCONF Notification.
229 *
230 * @param[in] session NETCONF session from which the function gets data. It must be the
231 * client side session object.
232 * @param[in] timeout Timeout for reading in milliseconds. Use negative value for infinite
233 * waiting and 0 for immediate return if data are not available on wire.
234 * @param[out] notif Resulting object of NETCONF Notification.
235 * @return NC_MSG_NOTIF for success, NC_MSG_WOULDBLOCK if timeout reached and NC_MSG_ERROR
236 * when reading has failed.
Radek Krejci5686ff72015-10-09 13:33:56 +0200237 */
238NC_MSG_TYPE nc_recv_notif(struct nc_session* session, int timeout, struct nc_notif **notif);
239
Radek Krejcife0b3472015-10-12 13:43:42 +0200240/**
241 * @brief Send NETCONF RPC message via the session.
242 *
243 * @param[in] session NETCONF session where the RPC will be written.
Radek Krejci695d4fa2015-10-22 13:23:54 +0200244 * @param[in] rpc NETCOFN RPC object to send via specified session. Object can be created by
245 nc_rpc_lock(), nc_rpc_unlock() and nc_rpc_generic() functions.
Radek Krejcife0b3472015-10-12 13:43:42 +0200246 * @return #NC_MSG_RPC on success, #NC_MSG_WOULDBLOCK in case of busy session
247 * (try to repeat the function call) and #NC_MSG_ERROR in case of error.
248 */
Radek Krejci695d4fa2015-10-22 13:23:54 +0200249NC_MSG_TYPE nc_send_rpc(struct nc_session *session, struct nc_rpc *rpc);
Radek Krejcife0b3472015-10-12 13:43:42 +0200250
Radek Krejci43390242015-10-08 15:34:04 +0200251#endif /* NC_SESSION_H_ */