Radek Krejci | 4339024 | 2015-10-08 15:34:04 +0200 | [diff] [blame] | 1 | /** |
| 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 Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 26 | #include <stdint.h> |
| 27 | |
Michal Vasko | fb2fb76 | 2015-10-27 11:44:32 +0100 | [diff] [blame] | 28 | #ifdef ENABLE_SSH |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 29 | # include <libssh/libssh.h> |
Michal Vasko | fb2fb76 | 2015-10-27 11:44:32 +0100 | [diff] [blame] | 30 | #endif /* ENABLE_SSH */ |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 31 | |
| 32 | #ifdef ENABLE_TLS |
| 33 | # include <openssl/ssl.h> |
| 34 | #endif /* ENABLE_TLS */ |
| 35 | |
Radek Krejci | 4339024 | 2015-10-08 15:34:04 +0200 | [diff] [blame] | 36 | #include "messages.h" |
| 37 | |
| 38 | /** |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 39 | * @brief Enumeration of possible session statuses |
| 40 | */ |
| 41 | typedef 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 Krejci | 4339024 | 2015-10-08 15:34:04 +0200 | [diff] [blame] | 49 | * @brief NETCONF session object |
| 50 | */ |
| 51 | struct nc_session; |
| 52 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 53 | #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 | */ |
| 66 | int 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 Krejci | 1d06db4 | 2015-10-22 13:39:49 +0200 | [diff] [blame] | 86 | struct nc_session *nc_connect_inout(int fdin, int fdout, struct ly_ctx *ctx); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 87 | |
Michal Vasko | fb2fb76 | 2015-10-27 11:44:32 +0100 | [diff] [blame] | 88 | #ifdef ENABLE_SSH |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 89 | |
| 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 Krejci | 1d06db4 | 2015-10-22 13:39:49 +0200 | [diff] [blame] | 94 | * it is supposed to use nc_connect_libssh(). |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 95 | * |
| 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 Krejci | 1d06db4 | 2015-10-22 13:39:49 +0200 | [diff] [blame] | 110 | struct nc_session *nc_connect_ssh(const char *host, unsigned short port, const char* username, struct ly_ctx *ctx); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 111 | |
| 112 | /** |
| 113 | * @brief Connect to the NETCONF server using the provided SSH (libssh) session. |
| 114 | * |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 115 | * 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 Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 120 | * Function is provided only via nc_client.h header file and only when libnetconf2 is compiled with libssh support. |
| 121 | * |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 122 | * @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 Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 124 | * @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 Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 131 | struct nc_session *nc_connect_libssh(ssh_session ssh_session, struct ly_ctx *ctx); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 132 | |
| 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 Krejci | 1d06db4 | 2015-10-22 13:39:49 +0200 | [diff] [blame] | 139 | * it has to be created by nc_connect_ssh(), nc_connect_libssh() or nc_connect_ssh_channel(). |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 140 | * @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 Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 145 | * @return Created NETCONF session object or NULL in case of error. |
| 146 | */ |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 147 | struct nc_session *nc_connect_ssh_channel(struct nc_session *session, struct ly_ctx *ctx); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 148 | |
Michal Vasko | fb2fb76 | 2015-10-27 11:44:32 +0100 | [diff] [blame] | 149 | #endif /* ENABLE_SSH */ |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 150 | |
| 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 Krejci | 1d06db4 | 2015-10-22 13:39:49 +0200 | [diff] [blame] | 157 | * it is supposed to use nc_connect_libssl(). |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 158 | * |
| 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 Krejci | 1d06db4 | 2015-10-22 13:39:49 +0200 | [diff] [blame] | 173 | struct nc_session *nc_connect_tls(const char *host, unsigned short port, const char *username, struct ly_ctx *ctx); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 174 | |
| 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 Krejci | 1d06db4 | 2015-10-22 13:39:49 +0200 | [diff] [blame] | 188 | struct nc_session *nc_connect_libssl(SSL *tls, struct ly_ctx *ctx); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 189 | |
| 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 | */ |
| 199 | void nc_session_free(struct nc_session *session); |
| 200 | |
Radek Krejci | 4339024 | 2015-10-08 15:34:04 +0200 | [diff] [blame] | 201 | /** |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 202 | * @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 Krejci | 4339024 | 2015-10-08 15:34:04 +0200 | [diff] [blame] | 211 | */ |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 212 | NC_MSG_TYPE nc_recv_rpc(struct nc_session* session, int timeout, struct nc_rpc_server **rpc); |
Radek Krejci | 4339024 | 2015-10-08 15:34:04 +0200 | [diff] [blame] | 213 | |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 214 | /** |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 215 | * @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 Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 224 | */ |
| 225 | NC_MSG_TYPE nc_recv_reply(struct nc_session* session, int timeout, struct nc_reply **reply); |
| 226 | |
| 227 | /** |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 228 | * @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 Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 237 | */ |
| 238 | NC_MSG_TYPE nc_recv_notif(struct nc_session* session, int timeout, struct nc_notif **notif); |
| 239 | |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 240 | /** |
| 241 | * @brief Send NETCONF RPC message via the session. |
| 242 | * |
| 243 | * @param[in] session NETCONF session where the RPC will be written. |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 244 | * @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 Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 246 | * @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 Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 249 | NC_MSG_TYPE nc_send_rpc(struct nc_session *session, struct nc_rpc *rpc); |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 250 | |
Radek Krejci | 4339024 | 2015-10-08 15:34:04 +0200 | [diff] [blame] | 251 | #endif /* NC_SESSION_H_ */ |