blob: 8eccd1aa47780faad5d70b3d0940227902a7684d [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/**
Radek Krejci695d4fa2015-10-22 13:23:54 +020078 * @brief type of the session
Radek Krejci206fcd62015-10-07 15:42:48 +020079 */
80typedef enum {
Radek Krejci695d4fa2015-10-22 13:23:54 +020081 NC_CLIENT, /**< client side */
82 NC_SERVER /**< server side */
Radek Krejci206fcd62015-10-07 15:42:48 +020083} NC_SIDE;
84
85/**
86 * @brief Enumeration of the supported NETCONF protocol versions
87 */
88typedef enum {
89 NC_VERSION_10 = 0, /**< NETCONV 1.0 - RFC 4741, 4742 */
90 NC_VERSION_11 = 1 /**< NETCONF 1.1 - RFC 6241, 6242 */
91} NC_VERSION;
92
93#define NC_VERSION_10_ENDTAG "]]>]]>"
94#define NC_VERSION_10_ENDTAG_LEN 6
95
96/**
Michal Vaskoad611702015-12-03 13:41:51 +010097 * @brief Container to serialize PRC messages
Radek Krejci5686ff72015-10-09 13:33:56 +020098 */
Michal Vaskoad611702015-12-03 13:41:51 +010099struct nc_msg_cont {
100 struct lyxml_elem *msg;
101 struct nc_msg_cont *next;
Radek Krejci5686ff72015-10-09 13:33:56 +0200102};
103
104/**
Radek Krejci206fcd62015-10-07 15:42:48 +0200105 * @brief NETCONF session structure
106 */
107struct nc_session {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200108 NC_STATUS status; /**< status of the session */
109 NC_SIDE side; /**< side of the session: client or server */
Radek Krejci5686ff72015-10-09 13:33:56 +0200110
111 /* NETCONF data */
Radek Krejci206fcd62015-10-07 15:42:48 +0200112 uint32_t id; /**< NETCONF session ID (session-id-type) */
113 NC_VERSION version; /**< NETCONF protocol version */
Radek Krejci695d4fa2015-10-22 13:23:54 +0200114 pthread_t *notif; /**< running notifications thread - TODO server-side only? */
Radek Krejci5686ff72015-10-09 13:33:56 +0200115
116 /* Transport implementation */
Radek Krejci206fcd62015-10-07 15:42:48 +0200117 NC_TRANSPORT_IMPL ti_type; /**< transport implementation type to select items from ti union */
Radek Krejci695d4fa2015-10-22 13:23:54 +0200118 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 +0100119 NETCONF sessions on the same SSH session (but different SSH channel) */
Radek Krejci206fcd62015-10-07 15:42:48 +0200120 union {
121 struct {
122 int in; /**< input file descriptor */
123 int out; /**< output file descriptor */
Radek Krejci206fcd62015-10-07 15:42:48 +0200124 } fd; /**< NC_TI_FD transport implementation structure */
Michal Vaskofb2fb762015-10-27 11:44:32 +0100125#ifdef ENABLE_SSH
Radek Krejci206fcd62015-10-07 15:42:48 +0200126 struct {
Radek Krejci206fcd62015-10-07 15:42:48 +0200127 ssh_channel channel;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200128 ssh_session session;
129 struct nc_session *next; /**< pointer to the next NETCONF session on the same
130 SSH session, but different SSH channel. If no such session exists, it is NULL.
131 otherwise there is a ring list of the NETCONF sessions */
Radek Krejci206fcd62015-10-07 15:42:48 +0200132 } libssh;
133#endif
134#ifdef ENABLE_TLS
135 SSL *tls;
136#endif
Radek Krejci5686ff72015-10-09 13:33:56 +0200137 } ti; /**< transport implementation data */
Radek Krejciac6d3472015-10-22 15:47:18 +0200138 const char *username;
139 const char *host;
Michal Vasko38a7c6c2015-12-04 12:29:20 +0100140 uint16_t port;
Radek Krejci5686ff72015-10-09 13:33:56 +0200141
142 /* other */
143 struct ly_ctx *ctx; /**< libyang context of the session */
Radek Krejci695d4fa2015-10-22 13:23:54 +0200144 uint8_t flags; /**< various flags of the session - TODO combine with status and/or side */
145#define NC_SESSION_SHAREDCTX 0x1
Radek Krejci5686ff72015-10-09 13:33:56 +0200146
147 /* client side only data */
Radek Krejcife0b3472015-10-12 13:43:42 +0200148 uint64_t msgid;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200149 const char **cpblts; /**< list of server's capabilities on client side */
Michal Vaskoad611702015-12-03 13:41:51 +0100150 struct nc_msg_cont *replies; /**< queue for RPC replies received instead of notifications */
151 struct nc_msg_cont *notifs; /**< queue for notifications received instead of RPC reply */
Radek Krejci206fcd62015-10-07 15:42:48 +0200152};
153
154/**
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100155 * @brief Fill libyang context in \p session. Context models are based on the stored session
156 * capabilities. If the server does not support \<get-schema\>, the models are searched
157 * for in the directory set using nc_schema_searchpath().
158 *
159 * @param[in] session Session to create the context for.
160 * @return 0 on success, non-zero on failure.
161 */
162int nc_ctx_fill(struct nc_session *session);
163
164/**
165 * @brief Check whether the libyang context in \p session is suitable for NETCONF use
166 * meaning whether the ietf-netconf model is loaded.
167 *
168 * @param[in] session Session with the capabilities to be supported if loading ietf-netconf
169 * explicitly.
170 * @return 0 on success, non-zero on failure.
171 */
172int nc_ctx_check(struct nc_session *session);
173
174/**
175 * @brief Create and connect a socket.
176 *
177 * @param[in] host Hostname to connect to.
178 * @param[in] port Port to connect on.
179 * @return Connected socket or -1 on error.
180 */
181int nc_connect_getsocket(const char *host, unsigned short port);
182
183/**
184 * @brief Perform NETCONF handshake on \p session.
185 *
186 * @param[in] session NETCONF session to use.
187 * @return 0 on success, non-zero on failure.
188 */
189int nc_handshake(struct nc_session *session);
190
191/**
Radek Krejci206fcd62015-10-07 15:42:48 +0200192 * Functions
193 * - io.c
194 */
195
196/**
197 * @brief Read message from the wire.
198 *
199 * Accepts hello, rpc, rpc-reply and notification. Received string is transformed into
200 * libyang XML tree and the message type is detected from the top level element.
201 *
202 * @param[in] session NETCONF session from which the message is being read.
203 * @param[in] timeout Timeout in milliseconds. Negative value means infinite timeout,
204 * zero value causes to return immediately.
205 * @param[out] data XML tree built from the read data.
206 * @return Type of the read message. #NC_MSG_WOULDBLOCK is returned if timeout is positive
Radek Krejci5686ff72015-10-09 13:33:56 +0200207 * (or zero) value and it passed out without any data on the wire. #NC_MSG_ERROR is
Radek Krejci206fcd62015-10-07 15:42:48 +0200208 * returned on error and #NC_MSG_NONE is never returned by this function.
209 */
210NC_MSG_TYPE nc_read_msg(struct nc_session* session, int timeout, struct lyxml_elem **data);
211
Radek Krejcife0b3472015-10-12 13:43:42 +0200212/**
213 * @brief Write message into wire.
214 *
215 * @param[in] session NETCONF session to which the message will be written.
216 * @param[in] type Type of the message to write. According to the type, the
217 * specific additional parameters are required or accepted:
218 * - #NC_MSG_RPC
219 * - `struct lyd_node *op;` - operation (content of the \<rpc/\> to be sent. Required parameter.
220 * - `const char *attrs;` - additional attributes to be added into the \<rpc/\> element.
221 * `message-id` attribute is added automatically and default namespace is set to
222 * #NC_NS_BASE. Optional parameter.
223 * - #NC_MSG_REPLY
224 * - `struct nc_rpc *rpc;` - RPC object to reply. Required parameter.
225 * - TODO: content
226 * - #NC_MSG_NOTIF
227 * - TODO: content
228 * @return 0 on success
229 */
230int nc_write_msg(struct nc_session *session, NC_MSG_TYPE type, ...);
231
Radek Krejci206fcd62015-10-07 15:42:48 +0200232#endif /* NC_SESSION_PRIVATE_H_ */