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 */ |
Michal Vasko | 3b09029 | 2017-02-03 11:43:47 +0100 | [diff] [blame] | 30 | #define NC_PORT_CH_SSH 4334 |
Michal Vasko | 38a7c6c | 2015-12-04 12:29:20 +0100 | [diff] [blame] | 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 */ |
Michal Vasko | 3b09029 | 2017-02-03 11:43:47 +0100 | [diff] [blame] | 35 | #define NC_PORT_CH_TLS 4335 |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 36 | |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 37 | /** @brief Microseconds after which tasks are repeated until the full timeout elapses. |
Michal Vasko | f471fa0 | 2017-02-15 10:48:12 +0100 | [diff] [blame] | 38 | * A millisecond (1000) should be divisible by this number without remain. |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 39 | */ |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 40 | #define NC_TIMEOUT_STEP 50 |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 41 | |
Radek Krejci | d0d1952 | 2015-09-02 13:49:25 +0200 | [diff] [blame] | 42 | /** |
Michal Vasko | 3a889fd | 2016-09-30 12:16:37 +0200 | [diff] [blame] | 43 | * @brief Set RPC callback to a schema node. |
| 44 | * |
| 45 | * @param[in] node const struct lys_node *node |
| 46 | * @param[in] cb nc_rpc_clb cb |
| 47 | */ |
| 48 | #define nc_set_rpc_callback(node, cb) lys_set_private(node, cb) |
| 49 | |
| 50 | /** |
Radek Krejci | d0d1952 | 2015-09-02 13:49:25 +0200 | [diff] [blame] | 51 | * @brief Enumeration of reasons of the NETCONF session termination as defined in RFC 6470. |
| 52 | */ |
| 53 | typedef enum NC_SESSION_TERM_REASON { |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 54 | NC_SESSION_TERM_NONE = 0, /**< session still running */ |
| 55 | NC_SESSION_TERM_CLOSED, /**< closed by client in a normal fashion */ |
| 56 | NC_SESSION_TERM_KILLED, /**< session was terminated by \<kill-session\> operation */ |
| 57 | NC_SESSION_TERM_DROPPED, /**< transport layer connection was unexpectedly closed */ |
| 58 | NC_SESSION_TERM_TIMEOUT, /**< terminated because of inactivity */ |
| 59 | NC_SESSION_TERM_BADHELLO, /**< \<hello\> message was invalid */ |
| 60 | NC_SESSION_TERM_OTHER /**< terminated for some other reason */ |
Radek Krejci | d0d1952 | 2015-09-02 13:49:25 +0200 | [diff] [blame] | 61 | } NC_SESSION_TERM_REASON; |
| 62 | |
| 63 | /** |
Radek Krejci | 4339024 | 2015-10-08 15:34:04 +0200 | [diff] [blame] | 64 | * @brief Enumeration of NETCONF message types. |
| 65 | */ |
| 66 | typedef enum NC_MSG_TYPE { |
Michal Vasko | 71ba2da | 2016-05-04 10:53:16 +0200 | [diff] [blame] | 67 | NC_MSG_ERROR, /**< error return value */ |
| 68 | NC_MSG_WOULDBLOCK, /**< timeout return value */ |
| 69 | NC_MSG_NONE, /**< no message at input or message was processed internally */ |
| 70 | NC_MSG_HELLO, /**< \<hello\> message */ |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 71 | NC_MSG_BAD_HELLO, /**< \<hello\> message parsing failed */ |
Michal Vasko | 71ba2da | 2016-05-04 10:53:16 +0200 | [diff] [blame] | 72 | NC_MSG_RPC, /**< \<rpc\> message */ |
| 73 | NC_MSG_REPLY, /**< \<rpc-reply\> message */ |
| 74 | NC_MSG_REPLY_ERR_MSGID, /**< \<rpc-reply\> message with missing or wrong message-id attribute value */ |
| 75 | NC_MSG_NOTIF /**< \<notification\> message */ |
Radek Krejci | 4339024 | 2015-10-08 15:34:04 +0200 | [diff] [blame] | 76 | } NC_MSG_TYPE; |
| 77 | |
| 78 | /** |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 79 | * @brief Enumeration of the supported types of datastores defined by NETCONF |
| 80 | */ |
| 81 | typedef enum NC_DATASTORE_TYPE { |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 82 | NC_DATASTORE_ERROR = 0, /**< error state of functions returning the datastore type */ |
| 83 | NC_DATASTORE_CONFIG, /**< value describing that the datastore is set as config */ |
| 84 | NC_DATASTORE_URL, /**< value describing that the datastore data should be given from the URL */ |
| 85 | NC_DATASTORE_RUNNING, /**< base NETCONF's datastore containing the current device configuration */ |
| 86 | NC_DATASTORE_STARTUP, /**< separated startup datastore as defined in Distinct Startup Capability */ |
| 87 | NC_DATASTORE_CANDIDATE /**< separated working datastore as defined in Candidate Configuration Capability */ |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 88 | } NC_DATASTORE; |
| 89 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 90 | /** |
| 91 | * @brief Enumeration of NETCONF with-defaults capability modes. |
| 92 | */ |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 93 | typedef enum NC_WITHDEFAULTS_MODE { |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 94 | NC_WD_UNKNOWN = 0, /**< invalid mode */ |
Radek Krejci | 36dfdb3 | 2016-09-01 16:56:35 +0200 | [diff] [blame] | 95 | NC_WD_ALL, /**< report-all mode */ |
| 96 | NC_WD_ALL_TAG, /**< report-all-tagged mode */ |
| 97 | NC_WD_TRIM, /**< trim mode */ |
| 98 | NC_WD_EXPLICIT /**< explicit mode */ |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 99 | } NC_WD_MODE; |
| 100 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 101 | /** |
| 102 | * @brief Enumeration of NETCONF (both server and client) rpc-reply types. |
| 103 | */ |
Michal Vasko | 495c946 | 2016-01-15 11:27:43 +0100 | [diff] [blame] | 104 | typedef enum NC_REPLY { |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 105 | NC_RPL_OK, /**< OK rpc-reply */ |
| 106 | NC_RPL_DATA, /**< DATA rpc-reply */ |
| 107 | NC_RPL_ERROR, /**< ERROR rpc-reply */ |
| 108 | NC_RPL_NOTIF /**< notification (client-only) */ |
Michal Vasko | 495c946 | 2016-01-15 11:27:43 +0100 | [diff] [blame] | 109 | } NC_RPL; |
| 110 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 111 | /** |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 112 | * @brief Enumeration of function parameter treatments. |
| 113 | */ |
| 114 | typedef enum NC_PARAMTYPE { |
| 115 | NC_PARAMTYPE_CONST, /**< use the parameter directly, do not free */ |
| 116 | NC_PARAMTYPE_FREE, /**< use the parameter directly, free afterwards */ |
| 117 | NC_PARAMTYPE_DUP_AND_FREE /**< make a copy of the argument, free afterwards */ |
| 118 | } NC_PARAMTYPE; |
| 119 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 120 | #if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS) |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 121 | |
| 122 | /** |
| 123 | * @brief Free all the dynamically allocated thread-specific libssl/libcrypto |
| 124 | * resources. |
| 125 | * |
Michal Vasko | a7b8ca5 | 2016-03-01 12:09:29 +0100 | [diff] [blame] | 126 | * This function should be called only if init was called. Call it in every |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 127 | * thread your application creates just before the thread exits. In the last thread |
| 128 | * (usually the main one) call only nc_destroy(). |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 129 | */ |
| 130 | void nc_thread_destroy(void); |
| 131 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 132 | #endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */ |
Michal Vasko | 58f3155 | 2016-01-19 12:39:15 +0100 | [diff] [blame] | 133 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 134 | /** |
Radek Krejci | d0d1952 | 2015-09-02 13:49:25 +0200 | [diff] [blame] | 135 | * @brief Transform given time_t (seconds since the epoch) into the RFC 3339 format |
| 136 | * accepted by NETCONF functions. |
| 137 | * |
| 138 | * This is a reverse function to nc_datetime2time(). |
| 139 | * |
Michal Vasko | 8c1bfab | 2016-05-25 11:17:02 +0200 | [diff] [blame] | 140 | * @param[in] time Time to convert. |
| 141 | * @param[in] tz Timezone name for the result. See tzselect(1) for list of |
Radek Krejci | ebe263f | 2016-05-31 15:59:07 +0200 | [diff] [blame] | 142 | * correct values. If not specified (NULL) or unknown/invalid, the result is provided in UTC (Zulu). |
Michal Vasko | 8c1bfab | 2016-05-25 11:17:02 +0200 | [diff] [blame] | 143 | * @param[in] buf Optional buffer to print the datetime into, should be at least 26 characters long! |
| 144 | * @return Printed string in a format compliant to RFC 3339 stored in \p buf if provided, |
Radek Krejci | ebe263f | 2016-05-31 15:59:07 +0200 | [diff] [blame] | 145 | * otherwise it is up to the caller to free the returned string. NULL on error. |
Radek Krejci | d0d1952 | 2015-09-02 13:49:25 +0200 | [diff] [blame] | 146 | */ |
Michal Vasko | 8c1bfab | 2016-05-25 11:17:02 +0200 | [diff] [blame] | 147 | char* nc_time2datetime(time_t time, const char* tz, char *buf); |
Radek Krejci | d0d1952 | 2015-09-02 13:49:25 +0200 | [diff] [blame] | 148 | |
| 149 | /** |
| 150 | * @brief Transform given string in RFC 3339 compliant format to the time_t |
| 151 | * (seconds since the epoch) accepted by most Linux functions. |
| 152 | * |
| 153 | * This is a reverse function to nc_time2datetime(). |
| 154 | * |
| 155 | * @param[in] datetime Time structure returned e.g. by localtime(). |
Radek Krejci | ebe263f | 2016-05-31 15:59:07 +0200 | [diff] [blame] | 156 | * @return time_t value of the given string, -1 on error. |
Radek Krejci | d0d1952 | 2015-09-02 13:49:25 +0200 | [diff] [blame] | 157 | */ |
| 158 | time_t nc_datetime2time(const char* datetime); |
| 159 | |
Radek Krejci | d0d1952 | 2015-09-02 13:49:25 +0200 | [diff] [blame] | 160 | #ifdef __cplusplus |
| 161 | } |
| 162 | #endif |
| 163 | |
| 164 | #endif /* NC_NETCONF_H_ */ |