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