blob: f8c90ca4e80b8381b4447393c8f2d873ee9d0740 [file] [log] [blame]
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001/*!
2 * \file mod_netconf.c
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003 * \brief NETCONF daemon for Netopeer
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02004 * \author Tomas Cejka <cejkat@cesnet.cz>
5 * \author Radek Krejci <rkrejci@cesnet.cz>
6 * \date 2011
7 * \date 2012
8 * \date 2013
Tomas Cejka09629492014-07-10 15:58:06 +02009 * \date 2014
Tomas Cejkaaf7a1562013-04-13 02:27:43 +020010 */
11/*
Tomas Cejka09629492014-07-10 15:58:06 +020012 * Copyright (C) 2011-2014 CESNET
Tomas Cejkaaf7a1562013-04-13 02:27:43 +020013 *
14 * LICENSE TERMS
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in
23 * the documentation and/or other materials provided with the
24 * distribution.
25 * 3. Neither the name of the Company nor the names of its contributors
26 * may be used to endorse or promote products derived from this
27 * software without specific prior written permission.
28 *
29 * ALTERNATIVELY, provided that this notice is retained in full, this
30 * product may be distributed under the terms of the GNU General Public
31 * License (GPL) version 2 or later, in which case the provisions
32 * of the GPL apply INSTEAD OF those given above.
33 *
34 * This software is provided ``as is'', and any express or implied
35 * warranties, including, but not limited to, the implied warranties of
36 * merchantability and fitness for a particular purpose are disclaimed.
37 * In no event shall the company or contributors be liable for any
38 * direct, indirect, incidental, special, exemplary, or consequential
39 * damages (including, but not limited to, procurement of substitute
40 * goods or services; loss of use, data, or profits; or business
41 * interruption) however caused and on any theory of liability, whether
42 * in contract, strict liability, or tort (including negligence or
43 * otherwise) arising in any way out of the use of this software, even
44 * if advised of the possibility of such damage.
45 *
46 */
47#ifndef __MOD_NETCONF_COMMON_H
48#define __MOD_NETCONF_COMMON_H
49
50#include <pthread.h>
Tomas Cejka45ab59f2013-05-15 00:10:49 +020051#include <json/json.h>
Michal Vaskoda0ab5c2015-11-13 13:24:51 +010052#include <libyang/libyang.h>
Tomas Cejkaaf7a1562013-04-13 02:27:43 +020053
Michal Vaskoc3146782015-11-04 14:46:41 +010054#define UNUSED(x) UNUSED_ ## x __attribute__((__unused__))
55
Tomas Cejka09629492014-07-10 15:58:06 +020056/**
57 * \brief Check if pointer is not NULL, free memory and set pointer to NULL
58 */
59#define CHECK_AND_FREE(pointer) if (pointer != NULL) { free(pointer); pointer = NULL; }
60
Tomas Cejkaaf7a1562013-04-13 02:27:43 +020061typedef struct notification {
Michal Vaskoda0ab5c2015-11-13 13:24:51 +010062 time_t eventtime;
63 char* content;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +020064} notification_t;
65
66struct session_with_mutex {
Michal Vaskoda0ab5c2015-11-13 13:24:51 +010067 struct nc_session *session; /**< netconf session */
68 unsigned int session_key; /**< unique session identifier throughout all the sessions */
69 notification_t *notifications;
Michal Vaskoc3146782015-11-04 14:46:41 +010070 int notif_count;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +010071 json_object *hello_message;
72 char ntfc_subscribed; /**< 0 when notifications are not subscribed */
73 char closed; /**< 0 when session is terminated */
74 time_t last_activity;
75 struct ly_ctx *ctx;
76 pthread_mutex_t lock; /**< mutex protecting the session from multiple access */
Michal Vaskoc3146782015-11-04 14:46:41 +010077
Michal Vaskoda0ab5c2015-11-13 13:24:51 +010078 struct session_with_mutex *prev;
Michal Vaskoc3146782015-11-04 14:46:41 +010079 struct session_with_mutex *next;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +020080};
81
Michal Vaskoc3146782015-11-04 14:46:41 +010082struct pass_to_thread {
83 int client; /**< opened socket */
84 struct session_with_mutex *netconf_sessions_list; /**< ?? */
85};
Tomas Cejkaaf7a1562013-04-13 02:27:43 +020086
Tomas Cejkaaf7a1562013-04-13 02:27:43 +020087extern pthread_rwlock_t session_lock; /**< mutex protecting netconf_session_list from multiple access errors */
88
Tomas Cejka442258e2014-04-01 18:17:18 +020089extern pthread_key_t err_reply_key;
90extern pthread_mutex_t json_lock;
91
Michal Vaskoda0ab5c2015-11-13 13:24:51 +010092json_object *create_error_reply(const char *errmess);
Tomas Cejkaef531ee2013-11-12 16:07:00 +010093
Tomas Cejkaef531ee2013-11-12 16:07:00 +010094#define DEBUG(...) do { \
Michal Vaskoc3146782015-11-04 14:46:41 +010095 fprintf(stderr, __VA_ARGS__); \
96 fprintf(stderr, "\n"); \
Tomas Cejkacf44e522015-04-24 17:29:21 +020097} while (0);
98
99#define ERROR(...) do { \
Michal Vaskoc3146782015-11-04 14:46:41 +0100100 fprintf(stderr, __VA_ARGS__); \
101 fprintf(stderr, "\n"); \
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100102} while (0);
Tomas Cejkac7929632013-10-24 19:25:15 +0200103
Tomas Cejka442258e2014-04-01 18:17:18 +0200104#define GETSPEC_ERR_REPLY \
105json_object **err_reply_p = (json_object **) pthread_getspecific(err_reply_key); \
106json_object *err_reply = ((err_reply_p != NULL)?(*err_reply_p):NULL);
107
108#define CHECK_ERR_SET_REPLY \
109if (reply == NULL) { \
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100110 GETSPEC_ERR_REPLY \
111 if (err_reply != NULL) { \
112 /* use filled err_reply from libnetconf's callback */ \
113 reply = err_reply; \
114 } \
Tomas Cejka442258e2014-04-01 18:17:18 +0200115}
116
117#define CHECK_ERR_SET_REPLY_ERR(errmsg) \
118if (reply == NULL) { \
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100119 GETSPEC_ERR_REPLY \
120 if (err_reply == NULL) { \
121 reply = create_error_reply(errmsg); \
122 } else { \
123 /* use filled err_reply from libnetconf's callback */ \
124 reply = err_reply; \
125 } \
Tomas Cejka442258e2014-04-01 18:17:18 +0200126}
127void create_err_reply_p();
128void clean_err_reply();
129void free_err_reply();
130
Tomas Cejka8ce138c2015-04-27 23:25:25 +0200131NC_MSG_TYPE netconf_send_recv_timed(struct nc_session *session, nc_rpc *rpc,
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100132 int timeout, nc_reply **reply);
Tomas Cejka8ce138c2015-04-27 23:25:25 +0200133
Tomas Cejkaaf7a1562013-04-13 02:27:43 +0200134#endif