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 | |
| 29 | #ifdef ENABLE_LIBSSH |
| 30 | # include <libssh/libssh.h> |
| 31 | # include <libssh/callbacks.h> |
| 32 | #endif |
| 33 | |
| 34 | #ifdef ENABLE_TLS |
| 35 | # include <openssl/bio.h> |
| 36 | # include <openssl/ssl.h> |
| 37 | #endif |
| 38 | |
| 39 | #include <libyang/libyang.h> |
| 40 | |
Radek Krejci | 4339024 | 2015-10-08 15:34:04 +0200 | [diff] [blame] | 41 | #include "libnetconf.h" |
| 42 | #include "session.h" |
| 43 | |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 44 | /** |
| 45 | * Sleep time in microseconds to wait between unsuccessful reading due to EAGAIN or EWOULDBLOCK |
| 46 | */ |
| 47 | #define NC_READ_SLEEP 100 |
| 48 | |
| 49 | /** |
| 50 | * @brief Enumeration of transport implementations (ways how libnetconf implements NETCONF transport protocol) |
| 51 | */ |
| 52 | typedef enum { |
| 53 | NC_TI_FD, /**< file descriptors - use standard input/output, transport protocol is implemented |
| 54 | outside the current application (only for NETCONF over SSH transport) */ |
| 55 | #ifdef ENABLE_LIBSSH |
| 56 | NC_TI_LIBSSH, /**< libssh - use libssh library, only for NETCONF over SSH transport */ |
| 57 | #endif |
| 58 | #ifdef ENABLE_TLS |
Michal Vasko | b7ea243 | 2015-10-26 15:38:40 +0100 | [diff] [blame^] | 59 | NC_TI_OPENSSL /**< OpenSSL - use OpenSSL library, only for NETCONF over TLS transport */ |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 60 | #endif |
| 61 | } NC_TRANSPORT_IMPL; |
| 62 | |
| 63 | /** |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 64 | * @brief type of the session |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 65 | */ |
| 66 | typedef enum { |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 67 | NC_CLIENT, /**< client side */ |
| 68 | NC_SERVER /**< server side */ |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 69 | } NC_SIDE; |
| 70 | |
| 71 | /** |
| 72 | * @brief Enumeration of the supported NETCONF protocol versions |
| 73 | */ |
| 74 | typedef enum { |
| 75 | NC_VERSION_10 = 0, /**< NETCONV 1.0 - RFC 4741, 4742 */ |
| 76 | NC_VERSION_11 = 1 /**< NETCONF 1.1 - RFC 6241, 6242 */ |
| 77 | } NC_VERSION; |
| 78 | |
| 79 | #define NC_VERSION_10_ENDTAG "]]>]]>" |
| 80 | #define NC_VERSION_10_ENDTAG_LEN 6 |
| 81 | |
| 82 | /** |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 83 | * @brief Container to serialize RPC reply objects |
| 84 | */ |
| 85 | struct nc_reply_cont { |
| 86 | struct nc_reply *msg; |
| 87 | struct nc_reply_cont *next; |
| 88 | }; |
| 89 | |
| 90 | /** |
| 91 | * @brief Container to serialize Notification objects |
| 92 | */ |
| 93 | struct nc_notif_cont { |
| 94 | struct nc_notif *msg; |
| 95 | struct nc_notif_cont *next; |
| 96 | }; |
| 97 | |
| 98 | /** |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 99 | * @brief NETCONF session structure |
| 100 | */ |
| 101 | struct nc_session { |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 102 | NC_STATUS status; /**< status of the session */ |
| 103 | NC_SIDE side; /**< side of the session: client or server */ |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 104 | |
| 105 | /* NETCONF data */ |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 106 | uint32_t id; /**< NETCONF session ID (session-id-type) */ |
| 107 | NC_VERSION version; /**< NETCONF protocol version */ |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 108 | pthread_t *notif; /**< running notifications thread - TODO server-side only? */ |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 109 | |
| 110 | /* Transport implementation */ |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 111 | 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] | 112 | 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^] | 113 | NETCONF sessions on the same SSH session (but different SSH channel) */ |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 114 | union { |
| 115 | struct { |
| 116 | int in; /**< input file descriptor */ |
| 117 | int out; /**< output file descriptor */ |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 118 | } fd; /**< NC_TI_FD transport implementation structure */ |
| 119 | #ifdef ENABLE_LIBSSH |
| 120 | struct { |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 121 | ssh_channel channel; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 122 | ssh_session session; |
| 123 | struct nc_session *next; /**< pointer to the next NETCONF session on the same |
| 124 | SSH session, but different SSH channel. If no such session exists, it is NULL. |
| 125 | otherwise there is a ring list of the NETCONF sessions */ |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 126 | } libssh; |
| 127 | #endif |
| 128 | #ifdef ENABLE_TLS |
| 129 | SSL *tls; |
| 130 | #endif |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 131 | } ti; /**< transport implementation data */ |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 132 | const char *username; |
| 133 | const char *host; |
| 134 | unsigned short port; |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 135 | |
| 136 | /* other */ |
| 137 | struct ly_ctx *ctx; /**< libyang context of the session */ |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 138 | uint8_t flags; /**< various flags of the session - TODO combine with status and/or side */ |
| 139 | #define NC_SESSION_SHAREDCTX 0x1 |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 140 | |
| 141 | /* client side only data */ |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 142 | uint64_t msgid; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 143 | const char **cpblts; /**< list of server's capabilities on client side */ |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 144 | struct nc_reply_cont *replies; /**< queue for RPC replies received instead of notifications */ |
| 145 | struct nc_notif_cont *notifs; /**< queue for notifications received instead of RPC reply */ |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 146 | }; |
| 147 | |
| 148 | /** |
| 149 | * Functions |
| 150 | * - io.c |
| 151 | */ |
| 152 | |
| 153 | /** |
| 154 | * @brief Read message from the wire. |
| 155 | * |
| 156 | * Accepts hello, rpc, rpc-reply and notification. Received string is transformed into |
| 157 | * libyang XML tree and the message type is detected from the top level element. |
| 158 | * |
| 159 | * @param[in] session NETCONF session from which the message is being read. |
| 160 | * @param[in] timeout Timeout in milliseconds. Negative value means infinite timeout, |
| 161 | * zero value causes to return immediately. |
| 162 | * @param[out] data XML tree built from the read data. |
| 163 | * @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] | 164 | * (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] | 165 | * returned on error and #NC_MSG_NONE is never returned by this function. |
| 166 | */ |
| 167 | NC_MSG_TYPE nc_read_msg(struct nc_session* session, int timeout, struct lyxml_elem **data); |
| 168 | |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 169 | /** |
| 170 | * @brief Write message into wire. |
| 171 | * |
| 172 | * @param[in] session NETCONF session to which the message will be written. |
| 173 | * @param[in] type Type of the message to write. According to the type, the |
| 174 | * specific additional parameters are required or accepted: |
| 175 | * - #NC_MSG_RPC |
| 176 | * - `struct lyd_node *op;` - operation (content of the \<rpc/\> to be sent. Required parameter. |
| 177 | * - `const char *attrs;` - additional attributes to be added into the \<rpc/\> element. |
| 178 | * `message-id` attribute is added automatically and default namespace is set to |
| 179 | * #NC_NS_BASE. Optional parameter. |
| 180 | * - #NC_MSG_REPLY |
| 181 | * - `struct nc_rpc *rpc;` - RPC object to reply. Required parameter. |
| 182 | * - TODO: content |
| 183 | * - #NC_MSG_NOTIF |
| 184 | * - TODO: content |
| 185 | * @return 0 on success |
| 186 | */ |
| 187 | int nc_write_msg(struct nc_session *session, NC_MSG_TYPE type, ...); |
| 188 | |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 189 | #endif /* NC_SESSION_PRIVATE_H_ */ |