Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 1 | /** |
| 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 Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 26 | #include <stdint.h> |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 27 | #include <pthread.h> |
| 28 | |
Michal Vasko | 4ef1493 | 2015-12-04 11:09:10 +0100 | [diff] [blame] | 29 | #include <libyang/libyang.h> |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 30 | |
Michal Vasko | 4ef1493 | 2015-12-04 11:09:10 +0100 | [diff] [blame] | 31 | #include "libnetconf.h" |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 32 | #include "netconf.h" |
Michal Vasko | 4ef1493 | 2015-12-04 11:09:10 +0100 | [diff] [blame] | 33 | #include "session.h" |
| 34 | |
Michal Vasko | fb2fb76 | 2015-10-27 11:44:32 +0100 | [diff] [blame] | 35 | #ifdef ENABLE_SSH |
Michal Vasko | 4ef1493 | 2015-12-04 11:09:10 +0100 | [diff] [blame] | 36 | |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 37 | # include <libssh/libssh.h> |
| 38 | # include <libssh/callbacks.h> |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 39 | # include <libssh/server.h> |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 40 | |
| 41 | /* seconds */ |
Michal Vasko | c111ac5 | 2015-12-08 14:36:36 +0100 | [diff] [blame] | 42 | # define NC_SSH_TIMEOUT 10 |
| 43 | # define NC_SSH_AUTH_COUNT 3 |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 44 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 45 | struct nc_ssh_client_opts { |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 46 | /* SSH authentication method preferences */ |
| 47 | struct { |
| 48 | NC_SSH_AUTH_TYPE type; |
| 49 | short int value; |
Michal Vasko | c111ac5 | 2015-12-08 14:36:36 +0100 | [diff] [blame] | 50 | } auth_pref[NC_SSH_AUTH_COUNT]; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 51 | |
| 52 | /* SSH key pairs */ |
| 53 | struct { |
| 54 | char *pubkey_path; |
| 55 | char *privkey_path; |
| 56 | int privkey_crypt; |
| 57 | } *keys; |
| 58 | int key_count; |
| 59 | }; |
| 60 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 61 | struct nc_ssh_server_opts { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 62 | ssh_bind sshbind; |
| 63 | |
| 64 | struct { |
| 65 | char *path; |
| 66 | char *username; |
| 67 | } *authkeys; |
| 68 | uint16_t authkey_count; |
| 69 | |
| 70 | int auth_methods; |
| 71 | uint16_t auth_attempts; |
| 72 | uint16_t auth_timeout; |
| 73 | }; |
| 74 | |
Michal Vasko | 4ef1493 | 2015-12-04 11:09:10 +0100 | [diff] [blame] | 75 | #endif /* ENABLE_SSH */ |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 76 | |
| 77 | #ifdef ENABLE_TLS |
Michal Vasko | 4ef1493 | 2015-12-04 11:09:10 +0100 | [diff] [blame] | 78 | |
Michal Vasko | c111ac5 | 2015-12-08 14:36:36 +0100 | [diff] [blame] | 79 | # include <openssl/bio.h> |
| 80 | # include <openssl/ssl.h> |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 81 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 82 | struct nc_tls_client_opts { |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 83 | SSL_CTX *tls_ctx; |
| 84 | X509_STORE *tls_store; |
| 85 | }; |
| 86 | |
Michal Vasko | 0457bb4 | 2016-01-08 15:49:13 +0100 | [diff] [blame] | 87 | struct nc_tls_server_opts { |
| 88 | SSL_CTX *tls_ctx; |
| 89 | X509_STORE *crl_store; |
| 90 | |
| 91 | struct { |
| 92 | uint32_t id; |
| 93 | const char *fingerprint; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 94 | NC_TLS_CTN_MAPTYPE map_type; |
Michal Vasko | 0457bb4 | 2016-01-08 15:49:13 +0100 | [diff] [blame] | 95 | const char *name; |
| 96 | } *ctn; |
| 97 | uint16_t ctn_count; |
| 98 | }; |
| 99 | |
Michal Vasko | 4ef1493 | 2015-12-04 11:09:10 +0100 | [diff] [blame] | 100 | #endif /* ENABLE_TLS */ |
Radek Krejci | 4339024 | 2015-10-08 15:34:04 +0200 | [diff] [blame] | 101 | |
Michal Vasko | 0457bb4 | 2016-01-08 15:49:13 +0100 | [diff] [blame] | 102 | struct nc_server_opts { |
| 103 | struct ly_ctx *ctx; |
| 104 | |
| 105 | NC_WD_MODE wd_basic_mode; |
| 106 | int wd_also_supported; |
| 107 | int interleave_capab; |
| 108 | |
| 109 | uint16_t hello_timeout; |
| 110 | uint16_t idle_timeout; |
Michal Vasko | 0457bb4 | 2016-01-08 15:49:13 +0100 | [diff] [blame] | 111 | |
| 112 | struct nc_bind { |
| 113 | char *address; |
| 114 | uint16_t port; |
| 115 | int sock; |
| 116 | NC_TRANSPORT_IMPL ti; |
| 117 | } *binds; |
| 118 | uint16_t bind_count; |
| 119 | }; |
| 120 | |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 121 | /** |
Michal Vasko | c111ac5 | 2015-12-08 14:36:36 +0100 | [diff] [blame] | 122 | * Sleep time in microseconds to wait between unsuccessful reading due to EAGAIN or EWOULDBLOCK. |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 123 | */ |
| 124 | #define NC_READ_SLEEP 100 |
| 125 | |
| 126 | /** |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 127 | * Number of sockets kept waiting to be accepted. |
| 128 | */ |
| 129 | #define NC_REVERSE_QUEUE 1 |
| 130 | |
| 131 | /** |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 132 | * @brief type of the session |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 133 | */ |
| 134 | typedef enum { |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 135 | NC_CLIENT, /**< client side */ |
| 136 | NC_SERVER /**< server side */ |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 137 | } NC_SIDE; |
| 138 | |
| 139 | /** |
| 140 | * @brief Enumeration of the supported NETCONF protocol versions |
| 141 | */ |
| 142 | typedef enum { |
| 143 | NC_VERSION_10 = 0, /**< NETCONV 1.0 - RFC 4741, 4742 */ |
| 144 | NC_VERSION_11 = 1 /**< NETCONF 1.1 - RFC 6241, 6242 */ |
| 145 | } NC_VERSION; |
| 146 | |
| 147 | #define NC_VERSION_10_ENDTAG "]]>]]>" |
| 148 | #define NC_VERSION_10_ENDTAG_LEN 6 |
| 149 | |
| 150 | /** |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 151 | * @brief Container to serialize PRC messages |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 152 | */ |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 153 | struct nc_msg_cont { |
| 154 | struct lyxml_elem *msg; |
| 155 | struct nc_msg_cont *next; |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 156 | }; |
| 157 | |
| 158 | /** |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 159 | * @brief NETCONF session structure |
| 160 | */ |
| 161 | struct nc_session { |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 162 | NC_STATUS status; /**< status of the session */ |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 163 | NC_SESSION_TERM_REASON term_reason; /**< reason of termination, if status is NC_STATUS_INVALID */ |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 164 | NC_SIDE side; /**< side of the session: client or server */ |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 165 | |
| 166 | /* NETCONF data */ |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 167 | uint32_t id; /**< NETCONF session ID (session-id-type) */ |
| 168 | NC_VERSION version; /**< NETCONF protocol version */ |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 169 | pthread_t *notif; /**< running notifications thread - TODO server-side only? */ |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 170 | |
| 171 | /* Transport implementation */ |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 172 | NC_TRANSPORT_IMPL ti_type; /**< transport implementation type to select items from ti union */ |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 173 | pthread_mutex_t *ti_lock; /**< lock to access ti. Note that in case of libssh TI, it can be shared with other |
Michal Vasko | b7ea243 | 2015-10-26 15:38:40 +0100 | [diff] [blame] | 174 | NETCONF sessions on the same SSH session (but different SSH channel) */ |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 175 | union { |
| 176 | struct { |
| 177 | int in; /**< input file descriptor */ |
| 178 | int out; /**< output file descriptor */ |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 179 | } fd; /**< NC_TI_FD transport implementation structure */ |
Michal Vasko | fb2fb76 | 2015-10-27 11:44:32 +0100 | [diff] [blame] | 180 | #ifdef ENABLE_SSH |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 181 | struct { |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 182 | ssh_channel channel; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 183 | ssh_session session; |
| 184 | struct nc_session *next; /**< pointer to the next NETCONF session on the same |
| 185 | SSH session, but different SSH channel. If no such session exists, it is NULL. |
| 186 | otherwise there is a ring list of the NETCONF sessions */ |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 187 | } libssh; |
| 188 | #endif |
| 189 | #ifdef ENABLE_TLS |
| 190 | SSL *tls; |
| 191 | #endif |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 192 | } ti; /**< transport implementation data */ |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 193 | const char *username; |
| 194 | const char *host; |
Michal Vasko | 38a7c6c | 2015-12-04 12:29:20 +0100 | [diff] [blame] | 195 | uint16_t port; |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 196 | |
| 197 | /* other */ |
| 198 | struct ly_ctx *ctx; /**< libyang context of the session */ |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 199 | uint8_t flags; /**< various flags of the session - TODO combine with status and/or side */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 200 | #define NC_SESSION_SHAREDCTX 0x01 |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 201 | |
| 202 | /* client side only data */ |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 203 | uint64_t msgid; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 204 | const char **cpblts; /**< list of server's capabilities on client side */ |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 205 | struct nc_msg_cont *replies; /**< queue for RPC replies received instead of notifications */ |
| 206 | struct nc_msg_cont *notifs; /**< queue for notifications received instead of RPC reply */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 207 | |
| 208 | /* server side only data */ |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 209 | #ifdef ENABLE_SSH |
Michal Vasko | 06e2243 | 2016-01-15 10:30:06 +0100 | [diff] [blame] | 210 | /* additional flags */ |
| 211 | # define NC_SESSION_SSH_AUTHENTICATED 0x02 |
| 212 | # define NC_SESSION_SSH_SUBSYS_NETCONF 0x04 |
| 213 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 214 | uint16_t ssh_auth_attempts; |
| 215 | #endif |
| 216 | #ifdef ENABLE_TLS |
Michal Vasko | 06e2243 | 2016-01-15 10:30:06 +0100 | [diff] [blame] | 217 | X509 *tls_cert; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 218 | #endif |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 219 | }; |
| 220 | |
Michal Vasko | fb89d77 | 2016-01-08 12:25:35 +0100 | [diff] [blame] | 221 | struct nc_pollsession { |
| 222 | struct { |
| 223 | int fd; |
| 224 | short events; |
| 225 | short revents; |
| 226 | struct nc_session *session; |
| 227 | } *sessions; |
| 228 | uint16_t session_count; |
| 229 | }; |
| 230 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 231 | NC_MSG_TYPE nc_send_msg(struct nc_session *session, struct lyd_node *op); |
| 232 | |
| 233 | int session_ti_lock(struct nc_session *session, int32_t timeout); |
| 234 | |
| 235 | int session_ti_unlock(struct nc_session *session); |
| 236 | |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 237 | /** |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 238 | * @brief Fill libyang context in \p session. Context models are based on the stored session |
| 239 | * capabilities. If the server does not support \<get-schema\>, the models are searched |
| 240 | * for in the directory set using nc_schema_searchpath(). |
| 241 | * |
| 242 | * @param[in] session Session to create the context for. |
| 243 | * @return 0 on success, non-zero on failure. |
| 244 | */ |
Michal Vasko | 57eb940 | 2015-12-08 14:38:12 +0100 | [diff] [blame] | 245 | int nc_ctx_check_and_fill(struct nc_session *session); |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 246 | |
| 247 | /** |
| 248 | * @brief Create and connect a socket. |
| 249 | * |
| 250 | * @param[in] host Hostname to connect to. |
| 251 | * @param[in] port Port to connect on. |
| 252 | * @return Connected socket or -1 on error. |
| 253 | */ |
| 254 | int nc_connect_getsocket(const char *host, unsigned short port); |
| 255 | |
| 256 | /** |
| 257 | * @brief Perform NETCONF handshake on \p session. |
| 258 | * |
| 259 | * @param[in] session NETCONF session to use. |
| 260 | * @return 0 on success, non-zero on failure. |
| 261 | */ |
| 262 | int nc_handshake(struct nc_session *session); |
| 263 | |
| 264 | /** |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 265 | * @brief Accept a new (Call Home) connection. |
| 266 | * |
| 267 | * @param[in] port Port to listen on. |
| 268 | * @param[in] timeout Timeout in msec. |
| 269 | * @param[out] server_port Port the new connection is connected on. Can be NULL. |
| 270 | * @param[out] server_host Host the new connection was initiated from. Can be NULL. |
| 271 | * @return Connected socket with the new connection, -1 on error. |
| 272 | */ |
| 273 | int nc_callhome_accept_connection(uint16_t port, int32_t timeout, uint16_t *server_port, char **server_host); |
| 274 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame^] | 275 | /** |
| 276 | * @brief Create a listening socket. |
| 277 | * |
| 278 | * @param[in] address IP address to listen on. |
| 279 | * @param[in] port Port to listen on. |
| 280 | * @return Listening socket, -1 on error. |
| 281 | */ |
Michal Vasko | fb89d77 | 2016-01-08 12:25:35 +0100 | [diff] [blame] | 282 | int nc_sock_listen(const char *address, uint32_t port); |
| 283 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame^] | 284 | /** |
| 285 | * @brief Accept a new connection on a listening socket. |
| 286 | * |
| 287 | * @param[in] binds Structure with the listening sockets. |
| 288 | * @param[in] bind_count Number of \p binds. |
| 289 | * @param[in] timeout Timeout for accepting. |
| 290 | * @param[out] ti Type of transport of the accepted connection. Can be NULL. |
| 291 | * @param[out] host Host of the remote peer. Can be NULL. |
| 292 | * @param[out] port Port of the new connection. Can be NULL. |
| 293 | * @return Accepted socket of the new connection, -1 on error. |
| 294 | */ |
Michal Vasko | fb89d77 | 2016-01-08 12:25:35 +0100 | [diff] [blame] | 295 | int nc_sock_accept(struct nc_bind *binds, uint16_t bind_count, int timeout, NC_TRANSPORT_IMPL *ti, char **host, uint16_t *port); |
| 296 | |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 297 | #ifdef ENABLE_SSH |
| 298 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame^] | 299 | /** |
| 300 | * @brief Establish SSH transport on a socket. |
| 301 | * |
| 302 | * @param[in] session Session structure of the new connection. |
| 303 | * @param[in] sock Socket of the new connection. |
| 304 | * @param[in] timeout Timeout for all the related tasks. |
| 305 | * @return 1 on success, 0 on timeout, -1 on error. |
| 306 | */ |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 307 | int nc_accept_ssh_session(struct nc_session *session, int sock, int timeout); |
| 308 | |
| 309 | #endif |
| 310 | |
| 311 | #ifdef ENABLE_TLS |
| 312 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame^] | 313 | /** |
| 314 | * @brief Establish TLS transport on a socket. |
| 315 | * |
| 316 | * @param[in] session Session structure of the new connection. |
| 317 | * @param[in] sock Socket of the new connection. |
| 318 | * @param[in] timeout Timeout for all the related tasks. |
| 319 | * @return 1 on success, 0 on timeout, -1 on error. |
| 320 | */ |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 321 | int nc_accept_tls_session(struct nc_session *session, int sock, int timeout); |
| 322 | |
| 323 | #endif |
| 324 | |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 325 | /** |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 326 | * Functions |
| 327 | * - io.c |
| 328 | */ |
| 329 | |
| 330 | /** |
| 331 | * @brief Read message from the wire. |
| 332 | * |
| 333 | * Accepts hello, rpc, rpc-reply and notification. Received string is transformed into |
| 334 | * libyang XML tree and the message type is detected from the top level element. |
| 335 | * |
| 336 | * @param[in] session NETCONF session from which the message is being read. |
| 337 | * @param[in] timeout Timeout in milliseconds. Negative value means infinite timeout, |
| 338 | * zero value causes to return immediately. |
| 339 | * @param[out] data XML tree built from the read data. |
| 340 | * @return Type of the read message. #NC_MSG_WOULDBLOCK is returned if timeout is positive |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 341 | * (or zero) value and it passed out without any data on the wire. #NC_MSG_ERROR is |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 342 | * returned on error and #NC_MSG_NONE is never returned by this function. |
| 343 | */ |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 344 | NC_MSG_TYPE nc_read_msg_poll(struct nc_session* session, int timeout, struct lyxml_elem **data); |
| 345 | |
| 346 | /** |
| 347 | * @brief Read message from the wire. |
| 348 | * |
| 349 | * Accepts hello, rpc, rpc-reply and notification. Received string is transformed into |
| 350 | * libyang XML tree and the message type is detected from the top level element. |
| 351 | * |
| 352 | * @param[in] session NETCONF session from which the message is being read. |
| 353 | * @param[out] data XML tree built from the read data. |
| 354 | * @return Type of the read message. #NC_MSG_WOULDBLOCK is returned if timeout is positive |
| 355 | * (or zero) value and it passed out without any data on the wire. #NC_MSG_ERROR is |
| 356 | * returned on error and #NC_MSG_NONE is never returned by this function. |
| 357 | */ |
| 358 | NC_MSG_TYPE nc_read_msg(struct nc_session* session, struct lyxml_elem **data); |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 359 | |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 360 | /** |
| 361 | * @brief Write message into wire. |
| 362 | * |
| 363 | * @param[in] session NETCONF session to which the message will be written. |
| 364 | * @param[in] type Type of the message to write. According to the type, the |
| 365 | * specific additional parameters are required or accepted: |
| 366 | * - #NC_MSG_RPC |
| 367 | * - `struct lyd_node *op;` - operation (content of the \<rpc/\> to be sent. Required parameter. |
| 368 | * - `const char *attrs;` - additional attributes to be added into the \<rpc/\> element. |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 369 | * Required parameter. |
| 370 | * `message-id` attribute is added automatically and default namespace is set to #NC_NS_BASE. |
| 371 | * Optional parameter. |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 372 | * - #NC_MSG_REPLY |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 373 | * - `struct lyxml_node *rpc_elem;` - root of the RPC object to reply to. Required parameter. |
| 374 | * - `struct nc_server_reply *reply;` - RPC reply. Required parameter. |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 375 | * - #NC_MSG_NOTIF |
| 376 | * - TODO: content |
| 377 | * @return 0 on success |
| 378 | */ |
| 379 | int nc_write_msg(struct nc_session *session, NC_MSG_TYPE type, ...); |
| 380 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame^] | 381 | /** |
| 382 | * @brief Check whether a session is still connected (on transport layer). |
| 383 | * |
| 384 | * @param[in] session Session to check. |
| 385 | * @return 1 if connected, 0 if not. |
| 386 | */ |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 387 | int nc_session_is_connected(struct nc_session *session); |
| 388 | |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 389 | #endif /* NC_SESSION_PRIVATE_H_ */ |