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 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 28 | #ifdef ENABLE_LIBSSH |
| 29 | # include <libssh/libssh.h> |
| 30 | #endif /* ENABLE_LIBSSH */ |
| 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 | |
| 88 | #ifdef ENABLE_LIBSSH |
| 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 | * |
| 115 | * Function is provided only via nc_client.h header file and only when libnetconf2 is compiled with libssh support. |
| 116 | * |
| 117 | * @param[in] ssh_session libssh structure representing SSH session object. |
| 118 | * @param[in] ctx Optional parameter. If set, provides strict YANG context for the session |
| 119 | * (ignoring what is actually supported by the server side). If not set, |
| 120 | * YANG context is created for the session using \<get-schema\> (if supported |
| 121 | * by the server side) or/and by searching for YANG schemas in the searchpath |
| 122 | * (see nc_schema_searchpath()). |
| 123 | * @return Created NETCONF session object or NULL in case of error. |
| 124 | */ |
Radek Krejci | 1d06db4 | 2015-10-22 13:39:49 +0200 | [diff] [blame^] | 125 | 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] | 126 | |
| 127 | /** |
| 128 | * @brief Create another NETCONF session on existing SSH session using separated SSH channel. |
| 129 | * |
| 130 | * Function is provided only via nc_client.h header file and only when libnetconf2 is compiled with libssh support. |
| 131 | * |
| 132 | * @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^] | 133 | * it has to be created by nc_connect_ssh(), nc_connect_libssh() or nc_connect_ssh_channel(). |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 134 | * @return Created NETCONF session object or NULL in case of error. |
| 135 | */ |
Radek Krejci | 1d06db4 | 2015-10-22 13:39:49 +0200 | [diff] [blame^] | 136 | struct nc_session *nc_connect_ssh_channel(struct nc_session *session); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 137 | |
| 138 | #endif /* ENABLE_LIBSSH */ |
| 139 | |
| 140 | #ifdef ENABLE_TLS |
| 141 | |
| 142 | /** |
| 143 | * @brief Connect to the NETCONF server using TLS transport (via libssl) |
| 144 | * |
| 145 | * 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^] | 146 | * it is supposed to use nc_connect_libssl(). |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 147 | * |
| 148 | * Function is provided only via nc_client.h header file and only when libnetconf2 is compiled with TLS support. |
| 149 | * |
| 150 | * @param[in] host Hostname or address (both Ipv4 and IPv6 are accepted) of the target server. |
| 151 | * 'localhost' is used by default if NULL is specified. |
| 152 | * @param[in] port Port number of the target server. Default value 6513 is used if 0 is specified. |
| 153 | * @param[in] username Name of the user to login to the server. The user running the application (detected from the |
| 154 | * effective UID) is used if NULL is specified. |
| 155 | * @param[in] ctx Optional parameter. If set, provides strict YANG context for the session |
| 156 | * (ignoring what is actually supported by the server side). If not set, |
| 157 | * YANG context is created for the session using \<get-schema\> (if supported |
| 158 | * by the server side) or/and by searching for YANG schemas in the searchpath |
| 159 | * (see nc_schema_searchpath()). |
| 160 | * @return Created NETCONF session object or NULL in case of error. |
| 161 | */ |
Radek Krejci | 1d06db4 | 2015-10-22 13:39:49 +0200 | [diff] [blame^] | 162 | 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] | 163 | |
| 164 | /** |
| 165 | * @brief Connect to the NETCONF server using the provided TLS (libssl) session. |
| 166 | * |
| 167 | * Function is provided only via nc_client.h header file and only when libnetconf2 is compiled with TLS support. |
| 168 | * |
| 169 | * @param[in] tls libssl structure representing TLS session object. |
| 170 | * @param[in] ctx Optional parameter. If set, provides strict YANG context for the session |
| 171 | * (ignoring what is actually supported by the server side). If not set, |
| 172 | * YANG context is created for the session using \<get-schema\> (if supported |
| 173 | * by the server side) or/and by searching for YANG schemas in the searchpath |
| 174 | * (see nc_schema_searchpath()). |
| 175 | * @return Created NETCONF session object or NULL in case of error. |
| 176 | */ |
Radek Krejci | 1d06db4 | 2015-10-22 13:39:49 +0200 | [diff] [blame^] | 177 | struct nc_session *nc_connect_libssl(SSL *tls, struct ly_ctx *ctx); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 178 | |
| 179 | #endif /* ENABLE_TLS */ |
| 180 | |
| 181 | #endif /* NC_CLIENT_H_ */ |
| 182 | |
| 183 | /** |
| 184 | * @brief Free the NETCONF session object. |
| 185 | * |
| 186 | * @param[in] session Object to free. |
| 187 | */ |
| 188 | void nc_session_free(struct nc_session *session); |
| 189 | |
Radek Krejci | 4339024 | 2015-10-08 15:34:04 +0200 | [diff] [blame] | 190 | /** |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 191 | * @brief Receive NETCONF RPC. |
| 192 | * |
| 193 | * @param[in] session NETCONF session from which the function gets data. It must be the |
| 194 | * server side session object. |
| 195 | * @param[in] timeout Timeout for reading in milliseconds. Use negative value for infinite |
| 196 | * waiting and 0 for immediate return if data are not available on wire. |
| 197 | * @param[out] notif Resulting object of NETCONF RPC. |
| 198 | * @return NC_MSG_RPC for success, NC_MSG_WOULDBLOCK if timeout reached and NC_MSG_ERROR |
| 199 | * when reading has failed. |
Radek Krejci | 4339024 | 2015-10-08 15:34:04 +0200 | [diff] [blame] | 200 | */ |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 201 | 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] | 202 | |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 203 | /** |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 204 | * @brief Receive NETCONF RPC reply. |
| 205 | * |
| 206 | * @param[in] session NETCONF session from which the function gets data. It must be the |
| 207 | * client side session object. |
| 208 | * @param[in] timeout Timeout for reading in milliseconds. Use negative value for infinite |
| 209 | * waiting and 0 for immediate return if data are not available on wire. |
| 210 | * @param[out] reply Resulting object of NETCONF RPC reply. |
| 211 | * @return NC_MSG_REPLY for success, NC_MSG_WOULDBLOCK if timeout reached and NC_MSG_ERROR |
| 212 | * when reading has failed. |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 213 | */ |
| 214 | NC_MSG_TYPE nc_recv_reply(struct nc_session* session, int timeout, struct nc_reply **reply); |
| 215 | |
| 216 | /** |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 217 | * @brief Receive NETCONF Notification. |
| 218 | * |
| 219 | * @param[in] session NETCONF session from which the function gets data. It must be the |
| 220 | * client side session object. |
| 221 | * @param[in] timeout Timeout for reading in milliseconds. Use negative value for infinite |
| 222 | * waiting and 0 for immediate return if data are not available on wire. |
| 223 | * @param[out] notif Resulting object of NETCONF Notification. |
| 224 | * @return NC_MSG_NOTIF for success, NC_MSG_WOULDBLOCK if timeout reached and NC_MSG_ERROR |
| 225 | * when reading has failed. |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 226 | */ |
| 227 | NC_MSG_TYPE nc_recv_notif(struct nc_session* session, int timeout, struct nc_notif **notif); |
| 228 | |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 229 | /** |
| 230 | * @brief Send NETCONF RPC message via the session. |
| 231 | * |
| 232 | * @param[in] session NETCONF session where the RPC will be written. |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 233 | * @param[in] rpc NETCOFN RPC object to send via specified session. Object can be created by |
| 234 | nc_rpc_lock(), nc_rpc_unlock() and nc_rpc_generic() functions. |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 235 | * @return #NC_MSG_RPC on success, #NC_MSG_WOULDBLOCK in case of busy session |
| 236 | * (try to repeat the function call) and #NC_MSG_ERROR in case of error. |
| 237 | */ |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 238 | 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] | 239 | |
Radek Krejci | 4339024 | 2015-10-08 15:34:04 +0200 | [diff] [blame] | 240 | #endif /* NC_SESSION_H_ */ |