blob: 27398cdba4bc60459adf5e4a1621a0e59416c378 [file] [log] [blame]
Radek Krejci206fcd62015-10-07 15:42:48 +02001/**
2 * \file session_p.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_PRIVATE_H_
24#define NC_SESSION_PRIVATE_H_
25
Radek Krejcife0b3472015-10-12 13:43:42 +020026#include <stdint.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020027#include <pthread.h>
28
Michal Vasko4ef14932015-12-04 11:09:10 +010029#include <libyang/libyang.h>
30#include "libnetconf.h"
31#include "session.h"
32
Michal Vaskofb2fb762015-10-27 11:44:32 +010033#ifdef ENABLE_SSH
Michal Vasko4ef14932015-12-04 11:09:10 +010034
Radek Krejci206fcd62015-10-07 15:42:48 +020035# include <libssh/libssh.h>
36# include <libssh/callbacks.h>
Michal Vasko7b62fed2015-10-26 15:39:46 +010037
38/* seconds */
39# define SSH_TIMEOUT 10
40# define SSH_AUTH_COUNT 3
41
Michal Vasko7b62fed2015-10-26 15:39:46 +010042struct nc_ssh_auth_opts {
43 /* SSH authentication method preferences */
44 struct {
45 NC_SSH_AUTH_TYPE type;
46 short int value;
47 } auth_pref[SSH_AUTH_COUNT];
48
49 /* SSH key pairs */
50 struct {
51 char *pubkey_path;
52 char *privkey_path;
53 int privkey_crypt;
54 } *keys;
55 int key_count;
56};
57
Michal Vasko4ef14932015-12-04 11:09:10 +010058#endif /* ENABLE_SSH */
Radek Krejci206fcd62015-10-07 15:42:48 +020059
60#ifdef ENABLE_TLS
Michal Vasko4ef14932015-12-04 11:09:10 +010061
62#include <openssl/bio.h>
63#include <openssl/ssl.h>
Michal Vasko11d4cdb2015-10-29 11:42:52 +010064
65struct nc_tls_auth_opts {
66 SSL_CTX *tls_ctx;
67 X509_STORE *tls_store;
68};
69
Michal Vasko4ef14932015-12-04 11:09:10 +010070#endif /* ENABLE_TLS */
Radek Krejci43390242015-10-08 15:34:04 +020071
Radek Krejci206fcd62015-10-07 15:42:48 +020072/**
73 * Sleep time in microseconds to wait between unsuccessful reading due to EAGAIN or EWOULDBLOCK
74 */
75#define NC_READ_SLEEP 100
76
77/**
78 * @brief Enumeration of transport implementations (ways how libnetconf implements NETCONF transport protocol)
79 */
80typedef enum {
81 NC_TI_FD, /**< file descriptors - use standard input/output, transport protocol is implemented
82 outside the current application (only for NETCONF over SSH transport) */
Michal Vaskofb2fb762015-10-27 11:44:32 +010083#ifdef ENABLE_SSH
Radek Krejci206fcd62015-10-07 15:42:48 +020084 NC_TI_LIBSSH, /**< libssh - use libssh library, only for NETCONF over SSH transport */
85#endif
86#ifdef ENABLE_TLS
Michal Vaskob7ea2432015-10-26 15:38:40 +010087 NC_TI_OPENSSL /**< OpenSSL - use OpenSSL library, only for NETCONF over TLS transport */
Radek Krejci206fcd62015-10-07 15:42:48 +020088#endif
89} NC_TRANSPORT_IMPL;
90
91/**
Radek Krejci695d4fa2015-10-22 13:23:54 +020092 * @brief type of the session
Radek Krejci206fcd62015-10-07 15:42:48 +020093 */
94typedef enum {
Radek Krejci695d4fa2015-10-22 13:23:54 +020095 NC_CLIENT, /**< client side */
96 NC_SERVER /**< server side */
Radek Krejci206fcd62015-10-07 15:42:48 +020097} NC_SIDE;
98
99/**
100 * @brief Enumeration of the supported NETCONF protocol versions
101 */
102typedef enum {
103 NC_VERSION_10 = 0, /**< NETCONV 1.0 - RFC 4741, 4742 */
104 NC_VERSION_11 = 1 /**< NETCONF 1.1 - RFC 6241, 6242 */
105} NC_VERSION;
106
107#define NC_VERSION_10_ENDTAG "]]>]]>"
108#define NC_VERSION_10_ENDTAG_LEN 6
109
110/**
Michal Vaskoad611702015-12-03 13:41:51 +0100111 * @brief Container to serialize PRC messages
Radek Krejci5686ff72015-10-09 13:33:56 +0200112 */
Michal Vaskoad611702015-12-03 13:41:51 +0100113struct nc_msg_cont {
114 struct lyxml_elem *msg;
115 struct nc_msg_cont *next;
Radek Krejci5686ff72015-10-09 13:33:56 +0200116};
117
118/**
Radek Krejci206fcd62015-10-07 15:42:48 +0200119 * @brief NETCONF session structure
120 */
121struct nc_session {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200122 NC_STATUS status; /**< status of the session */
123 NC_SIDE side; /**< side of the session: client or server */
Radek Krejci5686ff72015-10-09 13:33:56 +0200124
125 /* NETCONF data */
Radek Krejci206fcd62015-10-07 15:42:48 +0200126 uint32_t id; /**< NETCONF session ID (session-id-type) */
127 NC_VERSION version; /**< NETCONF protocol version */
Radek Krejci695d4fa2015-10-22 13:23:54 +0200128 pthread_t *notif; /**< running notifications thread - TODO server-side only? */
Radek Krejci5686ff72015-10-09 13:33:56 +0200129
130 /* Transport implementation */
Radek Krejci206fcd62015-10-07 15:42:48 +0200131 NC_TRANSPORT_IMPL ti_type; /**< transport implementation type to select items from ti union */
Radek Krejci695d4fa2015-10-22 13:23:54 +0200132 pthread_mutex_t *ti_lock; /**< lock to access ti. Note that in case of libssh TI, it can be shared with other
Michal Vaskob7ea2432015-10-26 15:38:40 +0100133 NETCONF sessions on the same SSH session (but different SSH channel) */
Radek Krejci206fcd62015-10-07 15:42:48 +0200134 union {
135 struct {
136 int in; /**< input file descriptor */
137 int out; /**< output file descriptor */
Radek Krejci206fcd62015-10-07 15:42:48 +0200138 } fd; /**< NC_TI_FD transport implementation structure */
Michal Vaskofb2fb762015-10-27 11:44:32 +0100139#ifdef ENABLE_SSH
Radek Krejci206fcd62015-10-07 15:42:48 +0200140 struct {
Radek Krejci206fcd62015-10-07 15:42:48 +0200141 ssh_channel channel;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200142 ssh_session session;
143 struct nc_session *next; /**< pointer to the next NETCONF session on the same
144 SSH session, but different SSH channel. If no such session exists, it is NULL.
145 otherwise there is a ring list of the NETCONF sessions */
Radek Krejci206fcd62015-10-07 15:42:48 +0200146 } libssh;
147#endif
148#ifdef ENABLE_TLS
149 SSL *tls;
150#endif
Radek Krejci5686ff72015-10-09 13:33:56 +0200151 } ti; /**< transport implementation data */
Radek Krejciac6d3472015-10-22 15:47:18 +0200152 const char *username;
153 const char *host;
154 unsigned short port;
Radek Krejci5686ff72015-10-09 13:33:56 +0200155
156 /* other */
157 struct ly_ctx *ctx; /**< libyang context of the session */
Radek Krejci695d4fa2015-10-22 13:23:54 +0200158 uint8_t flags; /**< various flags of the session - TODO combine with status and/or side */
159#define NC_SESSION_SHAREDCTX 0x1
Radek Krejci5686ff72015-10-09 13:33:56 +0200160
161 /* client side only data */
Radek Krejcife0b3472015-10-12 13:43:42 +0200162 uint64_t msgid;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200163 const char **cpblts; /**< list of server's capabilities on client side */
Michal Vaskoad611702015-12-03 13:41:51 +0100164 struct nc_msg_cont *replies; /**< queue for RPC replies received instead of notifications */
165 struct nc_msg_cont *notifs; /**< queue for notifications received instead of RPC reply */
Radek Krejci206fcd62015-10-07 15:42:48 +0200166};
167
168/**
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100169 * @brief Fill libyang context in \p session. Context models are based on the stored session
170 * capabilities. If the server does not support \<get-schema\>, the models are searched
171 * for in the directory set using nc_schema_searchpath().
172 *
173 * @param[in] session Session to create the context for.
174 * @return 0 on success, non-zero on failure.
175 */
176int nc_ctx_fill(struct nc_session *session);
177
178/**
179 * @brief Check whether the libyang context in \p session is suitable for NETCONF use
180 * meaning whether the ietf-netconf model is loaded.
181 *
182 * @param[in] session Session with the capabilities to be supported if loading ietf-netconf
183 * explicitly.
184 * @return 0 on success, non-zero on failure.
185 */
186int nc_ctx_check(struct nc_session *session);
187
188/**
189 * @brief Create and connect a socket.
190 *
191 * @param[in] host Hostname to connect to.
192 * @param[in] port Port to connect on.
193 * @return Connected socket or -1 on error.
194 */
195int nc_connect_getsocket(const char *host, unsigned short port);
196
197/**
198 * @brief Perform NETCONF handshake on \p session.
199 *
200 * @param[in] session NETCONF session to use.
201 * @return 0 on success, non-zero on failure.
202 */
203int nc_handshake(struct nc_session *session);
204
205/**
Radek Krejci206fcd62015-10-07 15:42:48 +0200206 * Functions
207 * - io.c
208 */
209
210/**
211 * @brief Read message from the wire.
212 *
213 * Accepts hello, rpc, rpc-reply and notification. Received string is transformed into
214 * libyang XML tree and the message type is detected from the top level element.
215 *
216 * @param[in] session NETCONF session from which the message is being read.
217 * @param[in] timeout Timeout in milliseconds. Negative value means infinite timeout,
218 * zero value causes to return immediately.
219 * @param[out] data XML tree built from the read data.
220 * @return Type of the read message. #NC_MSG_WOULDBLOCK is returned if timeout is positive
Radek Krejci5686ff72015-10-09 13:33:56 +0200221 * (or zero) value and it passed out without any data on the wire. #NC_MSG_ERROR is
Radek Krejci206fcd62015-10-07 15:42:48 +0200222 * returned on error and #NC_MSG_NONE is never returned by this function.
223 */
224NC_MSG_TYPE nc_read_msg(struct nc_session* session, int timeout, struct lyxml_elem **data);
225
Radek Krejcife0b3472015-10-12 13:43:42 +0200226/**
227 * @brief Write message into wire.
228 *
229 * @param[in] session NETCONF session to which the message will be written.
230 * @param[in] type Type of the message to write. According to the type, the
231 * specific additional parameters are required or accepted:
232 * - #NC_MSG_RPC
233 * - `struct lyd_node *op;` - operation (content of the \<rpc/\> to be sent. Required parameter.
234 * - `const char *attrs;` - additional attributes to be added into the \<rpc/\> element.
235 * `message-id` attribute is added automatically and default namespace is set to
236 * #NC_NS_BASE. Optional parameter.
237 * - #NC_MSG_REPLY
238 * - `struct nc_rpc *rpc;` - RPC object to reply. Required parameter.
239 * - TODO: content
240 * - #NC_MSG_NOTIF
241 * - TODO: content
242 * @return 0 on success
243 */
244int nc_write_msg(struct nc_session *session, NC_MSG_TYPE type, ...);
245
Radek Krejci206fcd62015-10-07 15:42:48 +0200246#endif /* NC_SESSION_PRIVATE_H_ */