blob: a2fb96ab5006ea8cc96bd9b8e1acc96b4626d75c [file] [log] [blame]
Radek Krejcid0d19522015-09-02 13:49:25 +02001/**
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 Krejci9b81f5b2016-02-24 13:14:49 +01008 * 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 Vaskoafd416b2016-02-25 14:51:46 +010011 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +010012 * https://opensource.org/licenses/BSD-3-Clause
Radek Krejcid0d19522015-09-02 13:49:25 +020013 */
14
15#ifndef NC_NETCONF_H_
16#define NC_NETCONF_H_
17
18#include <time.h>
19
20#ifdef __cplusplus
21extern "C" {
22#endif
23
Radek Krejci206fcd62015-10-07 15:42:48 +020024#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 Krejciac6d3472015-10-22 15:47:18 +020027/** @brief Default NETCONF over SSH port */
Michal Vasko38a7c6c2015-12-04 12:29:20 +010028#define NC_PORT_SSH 830
29/** @brief Default NETCONF over SSH Call Home port */
30#define NC_PORT_CH_SSH 6666
31
Radek Krejciac6d3472015-10-22 15:47:18 +020032/** @brief Default NETCONF over TLS port */
Michal Vasko38a7c6c2015-12-04 12:29:20 +010033#define NC_PORT_TLS 6513
34/** @brief Default NETCONF over TLS Call Home port */
35#define NC_PORT_CH_TLS 6667
Radek Krejciac6d3472015-10-22 15:47:18 +020036
Michal Vasko0190bc32016-03-02 15:47:49 +010037/** @brief Microseconds after which tasks are repeated until the full timeout elapses.
38 * A second (1000 000) should be divisible by this number without remain.
39 */
40#define NC_TIMEOUT_STEP 20
Michal Vasko086311b2016-01-08 09:53:11 +010041
Radek Krejcid0d19522015-09-02 13:49:25 +020042/**
Michal Vasko3a889fd2016-09-30 12:16:37 +020043 * @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 Krejcid0d19522015-09-02 13:49:25 +020051 * @brief Enumeration of reasons of the NETCONF session termination as defined in RFC 6470.
52 */
53typedef enum NC_SESSION_TERM_REASON {
Michal Vasko428087d2016-01-14 16:04:28 +010054 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 Krejcid0d19522015-09-02 13:49:25 +020061} NC_SESSION_TERM_REASON;
62
63/**
Radek Krejci43390242015-10-08 15:34:04 +020064 * @brief Enumeration of NETCONF message types.
65 */
66typedef enum NC_MSG_TYPE {
Michal Vasko71ba2da2016-05-04 10:53:16 +020067 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 Vasko71090fc2016-05-24 16:37:28 +020071 NC_MSG_BAD_HELLO, /**< \<hello\> message parsing failed */
Michal Vasko71ba2da2016-05-04 10:53:16 +020072 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 Krejci43390242015-10-08 15:34:04 +020076} NC_MSG_TYPE;
77
78/**
Radek Krejci695d4fa2015-10-22 13:23:54 +020079 * @brief Enumeration of the supported types of datastores defined by NETCONF
80 */
81typedef enum NC_DATASTORE_TYPE {
Michal Vasko7f1c78b2016-01-19 09:52:14 +010082 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 Krejci695d4fa2015-10-22 13:23:54 +020088} NC_DATASTORE;
89
Michal Vasko1a38c862016-01-15 15:50:07 +010090/**
91 * @brief Enumeration of NETCONF with-defaults capability modes.
92 */
Michal Vasko7bcb48e2016-01-15 10:28:54 +010093typedef enum NC_WITHDEFAULTS_MODE {
Michal Vasko1a38c862016-01-15 15:50:07 +010094 NC_WD_UNKNOWN = 0, /**< invalid mode */
Radek Krejci36dfdb32016-09-01 16:56:35 +020095 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 Vasko7bcb48e2016-01-15 10:28:54 +010099} NC_WD_MODE;
100
Michal Vasko1a38c862016-01-15 15:50:07 +0100101/**
102 * @brief Enumeration of NETCONF (both server and client) rpc-reply types.
103 */
Michal Vasko495c9462016-01-15 11:27:43 +0100104typedef enum NC_REPLY {
Michal Vasko1a38c862016-01-15 15:50:07 +0100105 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 Vasko495c9462016-01-15 11:27:43 +0100109} NC_RPL;
110
Radek Krejci695d4fa2015-10-22 13:23:54 +0200111/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100112 * @brief Enumeration of function parameter treatments.
113 */
114typedef 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 Krejci53691be2016-02-22 13:58:37 +0100120#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vasko5e228792016-02-03 15:30:24 +0100121
122/**
123 * @brief Free all the dynamically allocated thread-specific libssl/libcrypto
124 * resources.
125 *
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100126 * This function should be called only if init was called. Call it in every
Michal Vasko8f0c0282016-02-29 10:17:14 +0100127 * thread your application creates just before the thread exits. In the last thread
128 * (usually the main one) call only nc_destroy().
Michal Vasko5e228792016-02-03 15:30:24 +0100129 */
130void nc_thread_destroy(void);
131
Michal Vasko8f0c0282016-02-29 10:17:14 +0100132#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vasko58f31552016-01-19 12:39:15 +0100133
Michal Vasko1a38c862016-01-15 15:50:07 +0100134/**
Radek Krejcid0d19522015-09-02 13:49:25 +0200135 * @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 Vasko8c1bfab2016-05-25 11:17:02 +0200140 * @param[in] time Time to convert.
141 * @param[in] tz Timezone name for the result. See tzselect(1) for list of
Radek Krejciebe263f2016-05-31 15:59:07 +0200142 * correct values. If not specified (NULL) or unknown/invalid, the result is provided in UTC (Zulu).
Michal Vasko8c1bfab2016-05-25 11:17:02 +0200143 * @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 Krejciebe263f2016-05-31 15:59:07 +0200145 * otherwise it is up to the caller to free the returned string. NULL on error.
Radek Krejcid0d19522015-09-02 13:49:25 +0200146 */
Michal Vasko8c1bfab2016-05-25 11:17:02 +0200147char* nc_time2datetime(time_t time, const char* tz, char *buf);
Radek Krejcid0d19522015-09-02 13:49:25 +0200148
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 Krejciebe263f2016-05-31 15:59:07 +0200156 * @return time_t value of the given string, -1 on error.
Radek Krejcid0d19522015-09-02 13:49:25 +0200157 */
158time_t nc_datetime2time(const char* datetime);
159
Radek Krejcid0d19522015-09-02 13:49:25 +0200160#ifdef __cplusplus
161}
162#endif
163
164#endif /* NC_NETCONF_H_ */