blob: 338c5d63df641622144beb3f5bfe5628add59cd7 [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 *
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
29extern "C" {
30#endif
31
Radek Krejci206fcd62015-10-07 15:42:48 +020032#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 Krejciac6d3472015-10-22 15:47:18 +020035/** @brief Default NETCONF over SSH port */
Michal Vasko38a7c6c2015-12-04 12:29:20 +010036#define NC_PORT_SSH 830
37/** @brief Default NETCONF over SSH Call Home port */
38#define NC_PORT_CH_SSH 6666
39
Radek Krejciac6d3472015-10-22 15:47:18 +020040/** @brief Default NETCONF over TLS port */
Michal Vasko38a7c6c2015-12-04 12:29:20 +010041#define NC_PORT_TLS 6513
42/** @brief Default NETCONF over TLS Call Home port */
43#define NC_PORT_CH_TLS 6667
Radek Krejciac6d3472015-10-22 15:47:18 +020044
Michal Vasko086311b2016-01-08 09:53:11 +010045/** @brief Microseconds after which tasks are repeated until the full timeout elapses */
Michal Vasko38fff792016-02-02 15:50:17 +010046#define NC_TIMEOUT_STEP 100
Michal Vasko086311b2016-01-08 09:53:11 +010047
Radek Krejcid0d19522015-09-02 13:49:25 +020048/**
49 * @brief Enumeration of reasons of the NETCONF session termination as defined in RFC 6470.
50 */
51typedef enum NC_SESSION_TERM_REASON {
Michal Vasko428087d2016-01-14 16:04:28 +010052 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 Krejcid0d19522015-09-02 13:49:25 +020059} NC_SESSION_TERM_REASON;
60
61/**
Radek Krejci43390242015-10-08 15:34:04 +020062 * @brief Enumeration of NETCONF message types.
63 */
64typedef 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 Krejci5686ff72015-10-09 13:33:56 +020071 NC_MSG_NOTIF /**< \<notification\> message */
Radek Krejci43390242015-10-08 15:34:04 +020072} NC_MSG_TYPE;
73
74/**
Radek Krejci695d4fa2015-10-22 13:23:54 +020075 * @brief Enumeration of the supported types of datastores defined by NETCONF
76 */
77typedef enum NC_DATASTORE_TYPE {
Michal Vasko7f1c78b2016-01-19 09:52:14 +010078 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 Krejci695d4fa2015-10-22 13:23:54 +020084} NC_DATASTORE;
85
Michal Vasko1a38c862016-01-15 15:50:07 +010086/**
87 * @brief Enumeration of NETCONF with-defaults capability modes.
88 */
Michal Vasko7bcb48e2016-01-15 10:28:54 +010089typedef enum NC_WITHDEFAULTS_MODE {
Michal Vasko1a38c862016-01-15 15:50:07 +010090 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 Vasko7bcb48e2016-01-15 10:28:54 +010095} NC_WD_MODE;
96
Michal Vasko1a38c862016-01-15 15:50:07 +010097/**
98 * @brief Enumeration of NETCONF (both server and client) rpc-reply types.
99 */
Michal Vasko495c9462016-01-15 11:27:43 +0100100typedef enum NC_REPLY {
Michal Vasko1a38c862016-01-15 15:50:07 +0100101 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 Vasko495c9462016-01-15 11:27:43 +0100105} NC_RPL;
106
Radek Krejci695d4fa2015-10-22 13:23:54 +0200107/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100108 * @brief Enumeration of function parameter treatments.
109 */
110typedef 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 Vasko5e228792016-02-03 15:30:24 +0100116#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 */
128void 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 */
141void 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 */
149void nc_ssh_tls_destroy(void);
150
151#endif /* ENABLE_SSH && ENABLE_TLS */
152
Michal Vasko58f31552016-01-19 12:39:15 +0100153#ifdef ENABLE_SSH
154
155/**
Michal Vasko5e228792016-02-03 15:30:24 +0100156 * @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 Vasko58f31552016-01-19 12:39:15 +0100159 *
Michal Vasko5e228792016-02-03 15:30:24 +0100160 * Must be called before calling any libnetconf2 SSH function.
Michal Vasko58f31552016-01-19 12:39:15 +0100161 */
162void nc_ssh_init(void);
163
164/**
Michal Vasko5e228792016-02-03 15:30:24 +0100165 * @brief Free all the dynamically allocated libssh resources.
Michal Vasko58f31552016-01-19 12:39:15 +0100166 *
Michal Vasko5e228792016-02-03 15:30:24 +0100167 * No libnetconf2 SSH and libssh function should be called afterwards.
Michal Vasko58f31552016-01-19 12:39:15 +0100168 */
169void nc_ssh_destroy(void);
170
171#endif /* ENABLE_SSH */
172
173#ifdef ENABLE_TLS
174
175/**
Michal Vasko5e228792016-02-03 15:30:24 +0100176 * @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 Vasko58f31552016-01-19 12:39:15 +0100178 *
Michal Vasko5e228792016-02-03 15:30:24 +0100179 * Must be called before calling any libnetconf2 TLS function.
Michal Vasko58f31552016-01-19 12:39:15 +0100180 */
181void nc_tls_init(void);
182
183/**
Michal Vasko5e228792016-02-03 15:30:24 +0100184 * @brief Free all the dynamically allocated libssl/libcrypto resources.
185 *
186 * No libnetconf2 TLS and libssl/libcrypto function should be called afterwards.
Michal Vasko58f31552016-01-19 12:39:15 +0100187 */
188void nc_tls_destroy(void);
189
190#endif /* ENABLE_TLS */
191
Michal Vasko1a38c862016-01-15 15:50:07 +0100192/**
Radek Krejcid0d19522015-09-02 13:49:25 +0200193 * @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 */
204char* 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 */
215time_t nc_datetime2time(const char* datetime);
216
Radek Krejcid0d19522015-09-02 13:49:25 +0200217#ifdef __cplusplus
218}
219#endif
220
221#endif /* NC_NETCONF_H_ */