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> |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 4 | * \author Michal Vasko <mvasko@cesnet.cz> |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 5 | * \brief libnetconf2 session manipulation |
| 6 | * |
| 7 | * Copyright (c) 2015 CESNET, z.s.p.o. |
| 8 | * |
| 9 | * Redistribution and use in source and binary forms, with or without |
| 10 | * modification, are permitted provided that the following conditions |
| 11 | * are met: |
| 12 | * 1. Redistributions of source code must retain the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer. |
| 14 | * 2. Redistributions in binary form must reproduce the above copyright |
| 15 | * notice, this list of conditions and the following disclaimer in |
| 16 | * the documentation and/or other materials provided with the |
| 17 | * distribution. |
| 18 | * 3. Neither the name of the Company nor the names of its contributors |
| 19 | * may be used to endorse or promote products derived from this |
| 20 | * software without specific prior written permission. |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #ifndef NC_SESSION_PRIVATE_H_ |
| 25 | #define NC_SESSION_PRIVATE_H_ |
| 26 | |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 27 | #include <stdint.h> |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 28 | #include <pthread.h> |
| 29 | |
Michal Vasko | 4ef1493 | 2015-12-04 11:09:10 +0100 | [diff] [blame] | 30 | #include <libyang/libyang.h> |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 31 | |
Michal Vasko | 4ef1493 | 2015-12-04 11:09:10 +0100 | [diff] [blame] | 32 | #include "libnetconf.h" |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 33 | #include "netconf.h" |
Michal Vasko | 4ef1493 | 2015-12-04 11:09:10 +0100 | [diff] [blame] | 34 | #include "session.h" |
| 35 | |
Michal Vasko | fb2fb76 | 2015-10-27 11:44:32 +0100 | [diff] [blame] | 36 | #ifdef ENABLE_SSH |
Michal Vasko | 4ef1493 | 2015-12-04 11:09:10 +0100 | [diff] [blame] | 37 | |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 38 | # include <libssh/libssh.h> |
| 39 | # include <libssh/callbacks.h> |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 40 | # include <libssh/server.h> |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 41 | |
| 42 | /* seconds */ |
Michal Vasko | c111ac5 | 2015-12-08 14:36:36 +0100 | [diff] [blame] | 43 | # define NC_SSH_TIMEOUT 10 |
Michal Vasko | 5c2f795 | 2016-01-22 13:16:31 +0100 | [diff] [blame] | 44 | /* number of all supported authentication methods */ |
Michal Vasko | c111ac5 | 2015-12-08 14:36:36 +0100 | [diff] [blame] | 45 | # define NC_SSH_AUTH_COUNT 3 |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 46 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 47 | struct nc_ssh_client_opts { |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 48 | /* SSH authentication method preferences */ |
| 49 | struct { |
| 50 | NC_SSH_AUTH_TYPE type; |
| 51 | short int value; |
Michal Vasko | c111ac5 | 2015-12-08 14:36:36 +0100 | [diff] [blame] | 52 | } auth_pref[NC_SSH_AUTH_COUNT]; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 53 | |
| 54 | /* SSH key pairs */ |
| 55 | struct { |
| 56 | char *pubkey_path; |
| 57 | char *privkey_path; |
| 58 | int privkey_crypt; |
| 59 | } *keys; |
| 60 | int key_count; |
| 61 | }; |
| 62 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 63 | struct nc_ssh_server_opts { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 64 | ssh_bind sshbind; |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 65 | pthread_mutex_t sshbind_lock; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 66 | |
| 67 | struct { |
Michal Vasko | 76e3a35 | 2016-01-18 09:07:00 +0100 | [diff] [blame] | 68 | const char *path; |
| 69 | const char *username; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 70 | } *authkeys; |
| 71 | uint16_t authkey_count; |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 72 | pthread_mutex_t authkey_lock; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 73 | |
| 74 | int auth_methods; |
| 75 | uint16_t auth_attempts; |
| 76 | uint16_t auth_timeout; |
| 77 | }; |
| 78 | |
Michal Vasko | 4ef1493 | 2015-12-04 11:09:10 +0100 | [diff] [blame] | 79 | #endif /* ENABLE_SSH */ |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 80 | |
| 81 | #ifdef ENABLE_TLS |
Michal Vasko | 4ef1493 | 2015-12-04 11:09:10 +0100 | [diff] [blame] | 82 | |
Michal Vasko | c111ac5 | 2015-12-08 14:36:36 +0100 | [diff] [blame] | 83 | # include <openssl/bio.h> |
| 84 | # include <openssl/ssl.h> |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 85 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 86 | struct nc_tls_client_opts { |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 87 | SSL_CTX *tls_ctx; |
| 88 | X509_STORE *tls_store; |
| 89 | }; |
| 90 | |
Michal Vasko | 0457bb4 | 2016-01-08 15:49:13 +0100 | [diff] [blame] | 91 | struct nc_tls_server_opts { |
| 92 | SSL_CTX *tls_ctx; |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 93 | pthread_mutex_t tls_ctx_lock; |
| 94 | |
Michal Vasko | 0457bb4 | 2016-01-08 15:49:13 +0100 | [diff] [blame] | 95 | X509_STORE *crl_store; |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 96 | pthread_mutex_t crl_lock; |
Michal Vasko | 0457bb4 | 2016-01-08 15:49:13 +0100 | [diff] [blame] | 97 | |
Michal Vasko | 5e3f339 | 2016-01-20 11:13:01 +0100 | [diff] [blame] | 98 | struct nc_ctn { |
Michal Vasko | 0457bb4 | 2016-01-08 15:49:13 +0100 | [diff] [blame] | 99 | uint32_t id; |
| 100 | const char *fingerprint; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 101 | NC_TLS_CTN_MAPTYPE map_type; |
Michal Vasko | 0457bb4 | 2016-01-08 15:49:13 +0100 | [diff] [blame] | 102 | const char *name; |
Michal Vasko | 5e3f339 | 2016-01-20 11:13:01 +0100 | [diff] [blame] | 103 | struct nc_ctn *next; |
Michal Vasko | 0457bb4 | 2016-01-08 15:49:13 +0100 | [diff] [blame] | 104 | } *ctn; |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 105 | pthread_mutex_t ctn_lock; |
Michal Vasko | 0457bb4 | 2016-01-08 15:49:13 +0100 | [diff] [blame] | 106 | }; |
| 107 | |
Michal Vasko | 4ef1493 | 2015-12-04 11:09:10 +0100 | [diff] [blame] | 108 | #endif /* ENABLE_TLS */ |
Radek Krejci | 4339024 | 2015-10-08 15:34:04 +0200 | [diff] [blame] | 109 | |
Michal Vasko | 0457bb4 | 2016-01-08 15:49:13 +0100 | [diff] [blame] | 110 | struct nc_server_opts { |
| 111 | struct ly_ctx *ctx; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 112 | pthread_mutex_t ctx_lock; |
Michal Vasko | 0457bb4 | 2016-01-08 15:49:13 +0100 | [diff] [blame] | 113 | |
| 114 | NC_WD_MODE wd_basic_mode; |
| 115 | int wd_also_supported; |
| 116 | int interleave_capab; |
| 117 | |
| 118 | uint16_t hello_timeout; |
| 119 | uint16_t idle_timeout; |
Michal Vasko | 0457bb4 | 2016-01-08 15:49:13 +0100 | [diff] [blame] | 120 | |
| 121 | struct nc_bind { |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 122 | const char *address; |
Michal Vasko | 0457bb4 | 2016-01-08 15:49:13 +0100 | [diff] [blame] | 123 | uint16_t port; |
| 124 | int sock; |
| 125 | NC_TRANSPORT_IMPL ti; |
| 126 | } *binds; |
| 127 | uint16_t bind_count; |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 128 | pthread_mutex_t bind_lock; |
| 129 | |
| 130 | uint32_t new_session_id; |
| 131 | pthread_spinlock_t sid_lock; |
Michal Vasko | 0457bb4 | 2016-01-08 15:49:13 +0100 | [diff] [blame] | 132 | }; |
| 133 | |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 134 | /** |
Michal Vasko | c111ac5 | 2015-12-08 14:36:36 +0100 | [diff] [blame] | 135 | * 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] | 136 | */ |
| 137 | #define NC_READ_SLEEP 100 |
| 138 | |
| 139 | /** |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 140 | * Number of sockets kept waiting to be accepted. |
| 141 | */ |
| 142 | #define NC_REVERSE_QUEUE 1 |
| 143 | |
| 144 | /** |
Michal Vasko | 5c2f795 | 2016-01-22 13:16:31 +0100 | [diff] [blame] | 145 | * @brief Type of the session |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 146 | */ |
| 147 | typedef enum { |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 148 | NC_CLIENT, /**< client side */ |
| 149 | NC_SERVER /**< server side */ |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 150 | } NC_SIDE; |
| 151 | |
| 152 | /** |
| 153 | * @brief Enumeration of the supported NETCONF protocol versions |
| 154 | */ |
| 155 | typedef enum { |
Michal Vasko | 5c2f795 | 2016-01-22 13:16:31 +0100 | [diff] [blame] | 156 | NC_VERSION_10 = 0, /**< NETCONF 1.0 - RFC 4741, 4742 */ |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 157 | NC_VERSION_11 = 1 /**< NETCONF 1.1 - RFC 6241, 6242 */ |
| 158 | } NC_VERSION; |
| 159 | |
| 160 | #define NC_VERSION_10_ENDTAG "]]>]]>" |
| 161 | #define NC_VERSION_10_ENDTAG_LEN 6 |
| 162 | |
| 163 | /** |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 164 | * @brief Container to serialize PRC messages |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 165 | */ |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 166 | struct nc_msg_cont { |
| 167 | struct lyxml_elem *msg; |
| 168 | struct nc_msg_cont *next; |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 169 | }; |
| 170 | |
| 171 | /** |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 172 | * @brief NETCONF session structure |
| 173 | */ |
| 174 | struct nc_session { |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 175 | NC_STATUS status; /**< status of the session */ |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 176 | 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] | 177 | NC_SIDE side; /**< side of the session: client or server */ |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 178 | |
| 179 | /* NETCONF data */ |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 180 | uint32_t id; /**< NETCONF session ID (session-id-type) */ |
| 181 | NC_VERSION version; /**< NETCONF protocol version */ |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 182 | pthread_t *notif; /**< running notifications thread - TODO server-side only? */ |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 183 | |
| 184 | /* Transport implementation */ |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 185 | 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] | 186 | 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] | 187 | NETCONF sessions on the same SSH session (but different SSH channel) */ |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 188 | union { |
| 189 | struct { |
| 190 | int in; /**< input file descriptor */ |
| 191 | int out; /**< output file descriptor */ |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 192 | } fd; /**< NC_TI_FD transport implementation structure */ |
Michal Vasko | fb2fb76 | 2015-10-27 11:44:32 +0100 | [diff] [blame] | 193 | #ifdef ENABLE_SSH |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 194 | struct { |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 195 | ssh_channel channel; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 196 | ssh_session session; |
| 197 | struct nc_session *next; /**< pointer to the next NETCONF session on the same |
| 198 | SSH session, but different SSH channel. If no such session exists, it is NULL. |
| 199 | otherwise there is a ring list of the NETCONF sessions */ |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 200 | } libssh; |
| 201 | #endif |
| 202 | #ifdef ENABLE_TLS |
| 203 | SSL *tls; |
| 204 | #endif |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 205 | } ti; /**< transport implementation data */ |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 206 | const char *username; |
| 207 | const char *host; |
Michal Vasko | 38a7c6c | 2015-12-04 12:29:20 +0100 | [diff] [blame] | 208 | uint16_t port; |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 209 | |
| 210 | /* other */ |
| 211 | struct ly_ctx *ctx; /**< libyang context of the session */ |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 212 | 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] | 213 | #define NC_SESSION_SHAREDCTX 0x01 |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame^] | 214 | #define NC_SESSION_CALLHOME 0x02 |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 215 | |
| 216 | /* client side only data */ |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 217 | uint64_t msgid; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 218 | const char **cpblts; /**< list of server's capabilities on client side */ |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 219 | struct nc_msg_cont *replies; /**< queue for RPC replies received instead of notifications */ |
| 220 | 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] | 221 | |
| 222 | /* server side only data */ |
Michal Vasko | 5c2f795 | 2016-01-22 13:16:31 +0100 | [diff] [blame] | 223 | time_t last_rpc; /**< time the last RPC was received on this session */ |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 224 | #ifdef ENABLE_SSH |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 225 | /* SSH session authenticated */ |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame^] | 226 | # define NC_SESSION_SSH_AUTHENTICATED 0x04 |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 227 | /* netconf subsystem requested */ |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame^] | 228 | # define NC_SESSION_SSH_SUBSYS_NETCONF 0x08 |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 229 | /* new SSH message arrived */ |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame^] | 230 | # define NC_SESSION_SSH_NEW_MSG 0x10 |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 231 | /* this session is passed to nc_sshcb_msg() */ |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame^] | 232 | # define NC_SESSION_SSH_MSG_CB 0x20 |
Michal Vasko | 06e2243 | 2016-01-15 10:30:06 +0100 | [diff] [blame] | 233 | |
Michal Vasko | 5c2f795 | 2016-01-22 13:16:31 +0100 | [diff] [blame] | 234 | uint16_t ssh_auth_attempts; /**< number of failed SSH authentication attempts */ |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 235 | #endif |
| 236 | #ifdef ENABLE_TLS |
Michal Vasko | 5c2f795 | 2016-01-22 13:16:31 +0100 | [diff] [blame] | 237 | X509 *tls_cert; /**< TLS client certificate it used for authentication */ |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 238 | #endif |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 239 | }; |
| 240 | |
Michal Vasko | fb89d77 | 2016-01-08 12:25:35 +0100 | [diff] [blame] | 241 | struct nc_pollsession { |
Michal Vasko | 3a71513 | 2016-01-21 15:40:31 +0100 | [diff] [blame] | 242 | struct pollfd *pfds; |
| 243 | struct nc_session **sessions; |
Michal Vasko | fb89d77 | 2016-01-08 12:25:35 +0100 | [diff] [blame] | 244 | uint16_t session_count; |
| 245 | }; |
| 246 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 247 | NC_MSG_TYPE nc_send_msg(struct nc_session *session, struct lyd_node *op); |
| 248 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 249 | int nc_timedlock(pthread_mutex_t *lock, int timeout, int *elapsed); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 250 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 251 | void nc_subtract_elapsed(int *timeout, struct timespec *old_ts); |
| 252 | |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 253 | /** |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 254 | * @brief Fill libyang context in \p session. Context models are based on the stored session |
| 255 | * capabilities. If the server does not support \<get-schema\>, the models are searched |
| 256 | * for in the directory set using nc_schema_searchpath(). |
| 257 | * |
| 258 | * @param[in] session Session to create the context for. |
| 259 | * @return 0 on success, non-zero on failure. |
| 260 | */ |
Michal Vasko | 57eb940 | 2015-12-08 14:38:12 +0100 | [diff] [blame] | 261 | int nc_ctx_check_and_fill(struct nc_session *session); |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 262 | |
| 263 | /** |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 264 | * @brief Perform NETCONF handshake on \p session. |
| 265 | * |
| 266 | * @param[in] session NETCONF session to use. |
| 267 | * @return 0 on success, non-zero on failure. |
| 268 | */ |
| 269 | int nc_handshake(struct nc_session *session); |
| 270 | |
| 271 | /** |
Michal Vasko | f05562c | 2016-01-20 12:06:43 +0100 | [diff] [blame] | 272 | * @brief Create a socket connection. |
| 273 | * |
| 274 | * @param[in] host Hostname to connect to. |
| 275 | * @param[in] port Port to connect on. |
| 276 | * @return Connected socket or -1 on error. |
| 277 | */ |
| 278 | int nc_sock_connect(const char *host, uint16_t port); |
| 279 | |
| 280 | /** |
| 281 | * @brief Accept a new socket connection. |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 282 | * |
| 283 | * @param[in] port Port to listen on. |
Michal Vasko | f05562c | 2016-01-20 12:06:43 +0100 | [diff] [blame] | 284 | * @param[in] timeout Timeout in milliseconds. |
| 285 | * @param[out] peer_host Host the new connection was initiated from. Can be NULL. |
| 286 | * @param[out] peer_port Port the new connection is connected on. Can be NULL. |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 287 | * @return Connected socket with the new connection, -1 on error. |
| 288 | */ |
Michal Vasko | f05562c | 2016-01-20 12:06:43 +0100 | [diff] [blame] | 289 | int nc_sock_accept(uint16_t port, int timeout, char **peer_host, uint16_t *peer_port); |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 290 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 291 | /** |
| 292 | * @brief Create a listening socket. |
| 293 | * |
| 294 | * @param[in] address IP address to listen on. |
| 295 | * @param[in] port Port to listen on. |
| 296 | * @return Listening socket, -1 on error. |
| 297 | */ |
Michal Vasko | f05562c | 2016-01-20 12:06:43 +0100 | [diff] [blame] | 298 | int nc_sock_listen(const char *address, uint16_t port); |
Michal Vasko | fb89d77 | 2016-01-08 12:25:35 +0100 | [diff] [blame] | 299 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 300 | /** |
| 301 | * @brief Accept a new connection on a listening socket. |
| 302 | * |
| 303 | * @param[in] binds Structure with the listening sockets. |
| 304 | * @param[in] bind_count Number of \p binds. |
| 305 | * @param[in] timeout Timeout for accepting. |
| 306 | * @param[out] ti Type of transport of the accepted connection. Can be NULL. |
| 307 | * @param[out] host Host of the remote peer. Can be NULL. |
| 308 | * @param[out] port Port of the new connection. Can be NULL. |
| 309 | * @return Accepted socket of the new connection, -1 on error. |
| 310 | */ |
Michal Vasko | f05562c | 2016-01-20 12:06:43 +0100 | [diff] [blame] | 311 | int nc_sock_accept_binds(struct nc_bind *binds, uint16_t bind_count, int timeout, NC_TRANSPORT_IMPL *ti, char **host, uint16_t *port); |
Michal Vasko | fb89d77 | 2016-01-08 12:25:35 +0100 | [diff] [blame] | 312 | |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 313 | #ifdef ENABLE_SSH |
| 314 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 315 | /** |
| 316 | * @brief Establish SSH transport on a socket. |
| 317 | * |
| 318 | * @param[in] session Session structure of the new connection. |
| 319 | * @param[in] sock Socket of the new connection. |
| 320 | * @param[in] timeout Timeout for all the related tasks. |
| 321 | * @return 1 on success, 0 on timeout, -1 on error. |
| 322 | */ |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 323 | int nc_accept_ssh_session(struct nc_session *session, int sock, int timeout); |
| 324 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 325 | /** |
| 326 | * @brief Callback called when a new SSH message is received. |
| 327 | * |
| 328 | * @param[in] sshsession SSH session the message arrived on. |
| 329 | * @param[in] msg SSH message itself. |
| 330 | * @param[in] data NETCONF session running on \p sshsession. |
| 331 | * @return 0 if the message was handled, 1 if it is left up to libssh. |
| 332 | */ |
| 333 | int nc_sshcb_msg(ssh_session sshsession, ssh_message msg, void *data); |
| 334 | |
| 335 | /** |
| 336 | * @brief Inspect what exactly happened if a SSH session socket poll |
| 337 | * returned POLLIN. |
| 338 | * |
| 339 | * @param[in] session NETCONF session communicating on the socket. |
| 340 | * @param[in,out] timeout Timeout for locking ti_lock, gets updated. |
| 341 | * @return 0 - timeout, |
| 342 | * 1 if \p session channel has data, |
| 343 | * 2 if some other channel has data, |
| 344 | * 3 on \p session status change, |
| 345 | * 4 on new SSH message, |
| 346 | * 5 on new NETCONF SSH channel, |
| 347 | * -1 on error. |
| 348 | */ |
| 349 | int nc_ssh_pollin(struct nc_session *session, int *timeout); |
| 350 | |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 351 | #endif |
| 352 | |
| 353 | #ifdef ENABLE_TLS |
| 354 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 355 | /** |
| 356 | * @brief Establish TLS transport on a socket. |
| 357 | * |
| 358 | * @param[in] session Session structure of the new connection. |
| 359 | * @param[in] sock Socket of the new connection. |
| 360 | * @param[in] timeout Timeout for all the related tasks. |
| 361 | * @return 1 on success, 0 on timeout, -1 on error. |
| 362 | */ |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 363 | int nc_accept_tls_session(struct nc_session *session, int sock, int timeout); |
| 364 | |
| 365 | #endif |
| 366 | |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 367 | /** |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 368 | * Functions |
| 369 | * - io.c |
| 370 | */ |
| 371 | |
| 372 | /** |
| 373 | * @brief Read message from the wire. |
| 374 | * |
| 375 | * Accepts hello, rpc, rpc-reply and notification. Received string is transformed into |
| 376 | * libyang XML tree and the message type is detected from the top level element. |
| 377 | * |
| 378 | * @param[in] session NETCONF session from which the message is being read. |
| 379 | * @param[in] timeout Timeout in milliseconds. Negative value means infinite timeout, |
| 380 | * zero value causes to return immediately. |
| 381 | * @param[out] data XML tree built from the read data. |
| 382 | * @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] | 383 | * (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] | 384 | * returned on error and #NC_MSG_NONE is never returned by this function. |
| 385 | */ |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 386 | NC_MSG_TYPE nc_read_msg_poll(struct nc_session* session, int timeout, struct lyxml_elem **data); |
| 387 | |
| 388 | /** |
| 389 | * @brief Read message from the wire. |
| 390 | * |
| 391 | * Accepts hello, rpc, rpc-reply and notification. Received string is transformed into |
| 392 | * libyang XML tree and the message type is detected from the top level element. |
| 393 | * |
| 394 | * @param[in] session NETCONF session from which the message is being read. |
| 395 | * @param[out] data XML tree built from the read data. |
| 396 | * @return Type of the read message. #NC_MSG_WOULDBLOCK is returned if timeout is positive |
| 397 | * (or zero) value and it passed out without any data on the wire. #NC_MSG_ERROR is |
| 398 | * returned on error and #NC_MSG_NONE is never returned by this function. |
| 399 | */ |
| 400 | 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] | 401 | |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 402 | /** |
| 403 | * @brief Write message into wire. |
| 404 | * |
| 405 | * @param[in] session NETCONF session to which the message will be written. |
| 406 | * @param[in] type Type of the message to write. According to the type, the |
| 407 | * specific additional parameters are required or accepted: |
| 408 | * - #NC_MSG_RPC |
| 409 | * - `struct lyd_node *op;` - operation (content of the \<rpc/\> to be sent. Required parameter. |
| 410 | * - `const char *attrs;` - additional attributes to be added into the \<rpc/\> element. |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 411 | * Required parameter. |
| 412 | * `message-id` attribute is added automatically and default namespace is set to #NC_NS_BASE. |
| 413 | * Optional parameter. |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 414 | * - #NC_MSG_REPLY |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 415 | * - `struct lyxml_node *rpc_elem;` - root of the RPC object to reply to. Required parameter. |
| 416 | * - `struct nc_server_reply *reply;` - RPC reply. Required parameter. |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 417 | * - #NC_MSG_NOTIF |
| 418 | * - TODO: content |
| 419 | * @return 0 on success |
| 420 | */ |
| 421 | int nc_write_msg(struct nc_session *session, NC_MSG_TYPE type, ...); |
| 422 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 423 | /** |
| 424 | * @brief Check whether a session is still connected (on transport layer). |
| 425 | * |
| 426 | * @param[in] session Session to check. |
| 427 | * @return 1 if connected, 0 if not. |
| 428 | */ |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 429 | int nc_session_is_connected(struct nc_session *session); |
| 430 | |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 431 | #endif /* NC_SESSION_PRIVATE_H_ */ |