Radek Krejci | d0d1952 | 2015-09-02 13:49:25 +0200 | [diff] [blame] | 1 | /** |
| 2 | * \file netconf.h |
| 3 | * \author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * \brief libnetconf2's general public functions and structures definitions. |
| 5 | * |
| 6 | * Copyright (c) 2015 CESNET, z.s.p.o. |
| 7 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
Michal Vasko | afd416b | 2016-02-25 14:51:46 +0100 | [diff] [blame] | 11 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 12 | * https://opensource.org/licenses/BSD-3-Clause |
Radek Krejci | d0d1952 | 2015-09-02 13:49:25 +0200 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #ifndef NC_NETCONF_H_ |
| 16 | #define NC_NETCONF_H_ |
| 17 | |
| 18 | #include <time.h> |
| 19 | |
| 20 | #ifdef __cplusplus |
| 21 | extern "C" { |
| 22 | #endif |
| 23 | |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 24 | #define NC_NS_BASE "urn:ietf:params:xml:ns:netconf:base:1.0" |
| 25 | #define NC_NS_NOTIF "urn:ietf:params:xml:ns:netconf:notification:1.0" |
| 26 | |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 27 | /** @brief Default NETCONF over SSH port */ |
Michal Vasko | 38a7c6c | 2015-12-04 12:29:20 +0100 | [diff] [blame] | 28 | #define NC_PORT_SSH 830 |
| 29 | /** @brief Default NETCONF over SSH Call Home port */ |
| 30 | #define NC_PORT_CH_SSH 6666 |
| 31 | |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 32 | /** @brief Default NETCONF over TLS port */ |
Michal Vasko | 38a7c6c | 2015-12-04 12:29:20 +0100 | [diff] [blame] | 33 | #define NC_PORT_TLS 6513 |
| 34 | /** @brief Default NETCONF over TLS Call Home port */ |
| 35 | #define NC_PORT_CH_TLS 6667 |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 36 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 37 | /** @brief Microseconds after which tasks are repeated until the full timeout elapses */ |
Michal Vasko | 38fff79 | 2016-02-02 15:50:17 +0100 | [diff] [blame] | 38 | #define NC_TIMEOUT_STEP 100 |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 39 | |
Radek Krejci | d0d1952 | 2015-09-02 13:49:25 +0200 | [diff] [blame] | 40 | /** |
| 41 | * @brief Enumeration of reasons of the NETCONF session termination as defined in RFC 6470. |
| 42 | */ |
| 43 | typedef enum NC_SESSION_TERM_REASON { |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 44 | NC_SESSION_TERM_NONE = 0, /**< session still running */ |
| 45 | NC_SESSION_TERM_CLOSED, /**< closed by client in a normal fashion */ |
| 46 | NC_SESSION_TERM_KILLED, /**< session was terminated by \<kill-session\> operation */ |
| 47 | NC_SESSION_TERM_DROPPED, /**< transport layer connection was unexpectedly closed */ |
| 48 | NC_SESSION_TERM_TIMEOUT, /**< terminated because of inactivity */ |
| 49 | NC_SESSION_TERM_BADHELLO, /**< \<hello\> message was invalid */ |
| 50 | NC_SESSION_TERM_OTHER /**< terminated for some other reason */ |
Radek Krejci | d0d1952 | 2015-09-02 13:49:25 +0200 | [diff] [blame] | 51 | } NC_SESSION_TERM_REASON; |
| 52 | |
| 53 | /** |
Radek Krejci | 4339024 | 2015-10-08 15:34:04 +0200 | [diff] [blame] | 54 | * @brief Enumeration of NETCONF message types. |
| 55 | */ |
| 56 | typedef enum NC_MSG_TYPE { |
| 57 | NC_MSG_ERROR, /**< error return value */ |
| 58 | NC_MSG_WOULDBLOCK, /**< timeout return value */ |
| 59 | NC_MSG_NONE, /**< no message at input or message was processed internally */ |
| 60 | NC_MSG_HELLO, /**< \<hello\> message */ |
| 61 | NC_MSG_RPC, /**< \<rpc\> message */ |
| 62 | NC_MSG_REPLY, /**< \<rpc-reply\> message */ |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 63 | NC_MSG_NOTIF /**< \<notification\> message */ |
Radek Krejci | 4339024 | 2015-10-08 15:34:04 +0200 | [diff] [blame] | 64 | } NC_MSG_TYPE; |
| 65 | |
| 66 | /** |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 67 | * @brief Enumeration of the supported types of datastores defined by NETCONF |
| 68 | */ |
| 69 | typedef enum NC_DATASTORE_TYPE { |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 70 | NC_DATASTORE_ERROR = 0, /**< error state of functions returning the datastore type */ |
| 71 | NC_DATASTORE_CONFIG, /**< value describing that the datastore is set as config */ |
| 72 | NC_DATASTORE_URL, /**< value describing that the datastore data should be given from the URL */ |
| 73 | NC_DATASTORE_RUNNING, /**< base NETCONF's datastore containing the current device configuration */ |
| 74 | NC_DATASTORE_STARTUP, /**< separated startup datastore as defined in Distinct Startup Capability */ |
| 75 | NC_DATASTORE_CANDIDATE /**< separated working datastore as defined in Candidate Configuration Capability */ |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 76 | } NC_DATASTORE; |
| 77 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 78 | /** |
| 79 | * @brief Enumeration of NETCONF with-defaults capability modes. |
| 80 | */ |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 81 | typedef enum NC_WITHDEFAULTS_MODE { |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 82 | NC_WD_UNKNOWN = 0, /**< invalid mode */ |
| 83 | NC_WD_ALL = 0x01, /**< report-all mode */ |
| 84 | NC_WD_ALL_TAG = 0x02, /**< report-all-tagged mode */ |
| 85 | NC_WD_TRIM = 0x04, /**< trim mode */ |
| 86 | NC_WD_EXPLICIT = 0x08 /**< explicit mode */ |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 87 | } NC_WD_MODE; |
| 88 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 89 | /** |
| 90 | * @brief Enumeration of NETCONF (both server and client) rpc-reply types. |
| 91 | */ |
Michal Vasko | 495c946 | 2016-01-15 11:27:43 +0100 | [diff] [blame] | 92 | typedef enum NC_REPLY { |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 93 | NC_RPL_OK, /**< OK rpc-reply */ |
| 94 | NC_RPL_DATA, /**< DATA rpc-reply */ |
| 95 | NC_RPL_ERROR, /**< ERROR rpc-reply */ |
| 96 | NC_RPL_NOTIF /**< notification (client-only) */ |
Michal Vasko | 495c946 | 2016-01-15 11:27:43 +0100 | [diff] [blame] | 97 | } NC_RPL; |
| 98 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 99 | /** |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 100 | * @brief Enumeration of function parameter treatments. |
| 101 | */ |
| 102 | typedef enum NC_PARAMTYPE { |
| 103 | NC_PARAMTYPE_CONST, /**< use the parameter directly, do not free */ |
| 104 | NC_PARAMTYPE_FREE, /**< use the parameter directly, free afterwards */ |
| 105 | NC_PARAMTYPE_DUP_AND_FREE /**< make a copy of the argument, free afterwards */ |
| 106 | } NC_PARAMTYPE; |
| 107 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 108 | #if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS) |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 109 | |
| 110 | /** |
| 111 | * @brief Free all the dynamically allocated thread-specific libssl/libcrypto |
| 112 | * resources. |
| 113 | * |
| 114 | * This function should be called only if any of #nc_ssh_init(), #nc_tls_init(), |
| 115 | * or #nc_ssh_tls_init() was called. Call it in every thread your application |
| 116 | * creates just before the thread exits. In the last thread (usually the main one) |
| 117 | * call only #nc_ssh_destroy(), #nc_ssh_tls_destroy(), or #nc_tls_destroy, |
| 118 | * depending on what you used for initialization. |
| 119 | */ |
| 120 | void nc_thread_destroy(void); |
| 121 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 122 | #endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */ |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 123 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 124 | #if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS) |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 125 | |
| 126 | /** |
| 127 | * @brief Initialize both libssh and libssl/libcrypto libraries for thread-safe usage. |
| 128 | * Call this function even if you, for instance, will use only SSH transport, |
| 129 | * but want to use some libssl/libcrypto functions in your application. |
| 130 | * |
| 131 | * Must be called before calling any libnetconf2 SSH and/or TLS function. |
| 132 | */ |
| 133 | void nc_ssh_tls_init(void); |
| 134 | |
| 135 | /** |
| 136 | * @brief Free all the dynamically allocated libssh and libssl/libcrypto resources. |
| 137 | * |
| 138 | * No libnetconf2 SSH/TLS, libssh, and libcrypto/libssl function should be |
| 139 | * called afterwards. |
| 140 | */ |
| 141 | void nc_ssh_tls_destroy(void); |
| 142 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 143 | #endif /* NC_ENABLED_SSH && NC_ENABLED_TLS */ |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 144 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 145 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 58f3155 | 2016-01-19 12:39:15 +0100 | [diff] [blame] | 146 | |
| 147 | /** |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 148 | * @brief Initialize libssh for thread-safe usage. If you plan to use libcrypto/ |
| 149 | * /libssl in your application as well, please call #nc_ssh_tls_init() |
| 150 | * instead. |
Michal Vasko | 58f3155 | 2016-01-19 12:39:15 +0100 | [diff] [blame] | 151 | * |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 152 | * Must be called before calling any libnetconf2 SSH function. |
Michal Vasko | 58f3155 | 2016-01-19 12:39:15 +0100 | [diff] [blame] | 153 | */ |
| 154 | void nc_ssh_init(void); |
| 155 | |
| 156 | /** |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 157 | * @brief Free all the dynamically allocated libssh resources. |
Michal Vasko | 58f3155 | 2016-01-19 12:39:15 +0100 | [diff] [blame] | 158 | * |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 159 | * No libnetconf2 SSH and libssh function should be called afterwards. |
Michal Vasko | 58f3155 | 2016-01-19 12:39:15 +0100 | [diff] [blame] | 160 | */ |
| 161 | void nc_ssh_destroy(void); |
| 162 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 163 | #endif /* NC_ENABLED_SSH */ |
Michal Vasko | 58f3155 | 2016-01-19 12:39:15 +0100 | [diff] [blame] | 164 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 165 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 58f3155 | 2016-01-19 12:39:15 +0100 | [diff] [blame] | 166 | |
| 167 | /** |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 168 | * @brief Initialize libssl/libcrypto for thread-safe usage. If you plan to use libssh |
| 169 | * in your application as well, please call #nc_ssh_tls_init() instead. |
Michal Vasko | 58f3155 | 2016-01-19 12:39:15 +0100 | [diff] [blame] | 170 | * |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 171 | * Must be called before calling any libnetconf2 TLS function. |
Michal Vasko | 58f3155 | 2016-01-19 12:39:15 +0100 | [diff] [blame] | 172 | */ |
| 173 | void nc_tls_init(void); |
| 174 | |
| 175 | /** |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 176 | * @brief Free all the dynamically allocated libssl/libcrypto resources. |
| 177 | * |
| 178 | * No libnetconf2 TLS and libssl/libcrypto function should be called afterwards. |
Michal Vasko | 58f3155 | 2016-01-19 12:39:15 +0100 | [diff] [blame] | 179 | */ |
| 180 | void nc_tls_destroy(void); |
| 181 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 182 | #endif /* NC_ENABLED_TLS */ |
Michal Vasko | 58f3155 | 2016-01-19 12:39:15 +0100 | [diff] [blame] | 183 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 184 | /** |
Radek Krejci | d0d1952 | 2015-09-02 13:49:25 +0200 | [diff] [blame] | 185 | * @brief Transform given time_t (seconds since the epoch) into the RFC 3339 format |
| 186 | * accepted by NETCONF functions. |
| 187 | * |
| 188 | * This is a reverse function to nc_datetime2time(). |
| 189 | * |
| 190 | * @param[in] time time_t type value returned e.g. by time(). |
| 191 | * @param[in] tz timezone name for the result. See tzselect(1) for list of |
| 192 | * correct values. If not specified (NULL), the result is provided in UTC (Zulu). |
| 193 | * @return Printed string in a format compliant to RFC 3339. It is up to the |
| 194 | * caller to free the returned string. |
| 195 | */ |
| 196 | char* nc_time2datetime(time_t time, const char* tz); |
| 197 | |
| 198 | /** |
| 199 | * @brief Transform given string in RFC 3339 compliant format to the time_t |
| 200 | * (seconds since the epoch) accepted by most Linux functions. |
| 201 | * |
| 202 | * This is a reverse function to nc_time2datetime(). |
| 203 | * |
| 204 | * @param[in] datetime Time structure returned e.g. by localtime(). |
| 205 | * @return time_t value of the given string. |
| 206 | */ |
| 207 | time_t nc_datetime2time(const char* datetime); |
| 208 | |
Radek Krejci | d0d1952 | 2015-09-02 13:49:25 +0200 | [diff] [blame] | 209 | #ifdef __cplusplus |
| 210 | } |
| 211 | #endif |
| 212 | |
| 213 | #endif /* NC_NETCONF_H_ */ |