blob: 15d4ce11b1f9d6c89b39ab40d573f39a039ac7f9 [file] [log] [blame]
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001/*!
Michal Vasko30aa3822015-11-25 10:41:54 +01002 * \file netopeerguid.h
3 * \brief NETCONF daemon header for NetopeerGUI
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02004 * \author Tomas Cejka <cejkat@cesnet.cz>
5 * \author Radek Krejci <rkrejci@cesnet.cz>
Michal Vaskoa53ef882015-11-24 11:02:01 +01006 * \author Michal Vasko <mvasko@cesnet.cz>
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02007 * \date 2011
8 * \date 2012
9 * \date 2013
Tomas Cejka09629492014-07-10 15:58:06 +020010 * \date 2014
Michal Vaskoa53ef882015-11-24 11:02:01 +010011 * \date 2015
Tomas Cejkaaf7a1562013-04-13 02:27:43 +020012 */
13/*
Michal Vaskoa53ef882015-11-24 11:02:01 +010014 * Copyright (C) 2011-2015 CESNET
Tomas Cejkaaf7a1562013-04-13 02:27:43 +020015 *
16 * LICENSE TERMS
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in
25 * the documentation and/or other materials provided with the
26 * distribution.
27 * 3. Neither the name of the Company nor the names of its contributors
28 * may be used to endorse or promote products derived from this
29 * software without specific prior written permission.
30 *
31 * ALTERNATIVELY, provided that this notice is retained in full, this
32 * product may be distributed under the terms of the GNU General Public
33 * License (GPL) version 2 or later, in which case the provisions
34 * of the GPL apply INSTEAD OF those given above.
35 *
36 * This software is provided ``as is'', and any express or implied
37 * warranties, including, but not limited to, the implied warranties of
38 * merchantability and fitness for a particular purpose are disclaimed.
39 * In no event shall the company or contributors be liable for any
40 * direct, indirect, incidental, special, exemplary, or consequential
41 * damages (including, but not limited to, procurement of substitute
42 * goods or services; loss of use, data, or profits; or business
43 * interruption) however caused and on any theory of liability, whether
44 * in contract, strict liability, or tort (including negligence or
45 * otherwise) arising in any way out of the use of this software, even
46 * if advised of the possibility of such damage.
47 *
48 */
49#ifndef __MOD_NETCONF_COMMON_H
50#define __MOD_NETCONF_COMMON_H
51
52#include <pthread.h>
Tomas Cejka45ab59f2013-05-15 00:10:49 +020053#include <json/json.h>
Michal Vaskoda0ab5c2015-11-13 13:24:51 +010054#include <libyang/libyang.h>
Tomas Cejkaaf7a1562013-04-13 02:27:43 +020055
Michal Vaskoc3146782015-11-04 14:46:41 +010056#define UNUSED(x) UNUSED_ ## x __attribute__((__unused__))
57
Tomas Cejka09629492014-07-10 15:58:06 +020058/**
59 * \brief Check if pointer is not NULL, free memory and set pointer to NULL
60 */
61#define CHECK_AND_FREE(pointer) if (pointer != NULL) { free(pointer); pointer = NULL; }
62
Tomas Cejkaaf7a1562013-04-13 02:27:43 +020063typedef struct notification {
Michal Vaskoda0ab5c2015-11-13 13:24:51 +010064 time_t eventtime;
65 char* content;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +020066} notification_t;
67
68struct session_with_mutex {
Michal Vaskoda0ab5c2015-11-13 13:24:51 +010069 struct nc_session *session; /**< netconf session */
70 unsigned int session_key; /**< unique session identifier throughout all the sessions */
71 notification_t *notifications;
Michal Vaskoc3146782015-11-04 14:46:41 +010072 int notif_count;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +010073 json_object *hello_message;
74 char ntfc_subscribed; /**< 0 when notifications are not subscribed */
75 char closed; /**< 0 when session is terminated */
76 time_t last_activity;
77 struct ly_ctx *ctx;
78 pthread_mutex_t lock; /**< mutex protecting the session from multiple access */
Michal Vaskoc3146782015-11-04 14:46:41 +010079
Michal Vaskoda0ab5c2015-11-13 13:24:51 +010080 struct session_with_mutex *prev;
Michal Vaskoc3146782015-11-04 14:46:41 +010081 struct session_with_mutex *next;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +020082};
83
Michal Vaskoc3146782015-11-04 14:46:41 +010084struct pass_to_thread {
85 int client; /**< opened socket */
86 struct session_with_mutex *netconf_sessions_list; /**< ?? */
87};
Tomas Cejkaaf7a1562013-04-13 02:27:43 +020088
Tomas Cejkaaf7a1562013-04-13 02:27:43 +020089extern pthread_rwlock_t session_lock; /**< mutex protecting netconf_session_list from multiple access errors */
90
Tomas Cejka442258e2014-04-01 18:17:18 +020091extern pthread_key_t err_reply_key;
92extern pthread_mutex_t json_lock;
93
Michal Vaskoda0ab5c2015-11-13 13:24:51 +010094json_object *create_error_reply(const char *errmess);
Tomas Cejkaef531ee2013-11-12 16:07:00 +010095
Tomas Cejkaef531ee2013-11-12 16:07:00 +010096#define DEBUG(...) do { \
Michal Vaskoc3146782015-11-04 14:46:41 +010097 fprintf(stderr, __VA_ARGS__); \
98 fprintf(stderr, "\n"); \
Tomas Cejkacf44e522015-04-24 17:29:21 +020099} while (0);
100
101#define ERROR(...) do { \
Michal Vaskoc3146782015-11-04 14:46:41 +0100102 fprintf(stderr, __VA_ARGS__); \
103 fprintf(stderr, "\n"); \
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100104} while (0);
Tomas Cejkac7929632013-10-24 19:25:15 +0200105
Tomas Cejka442258e2014-04-01 18:17:18 +0200106#define GETSPEC_ERR_REPLY \
107json_object **err_reply_p = (json_object **) pthread_getspecific(err_reply_key); \
108json_object *err_reply = ((err_reply_p != NULL)?(*err_reply_p):NULL);
109
110#define CHECK_ERR_SET_REPLY \
111if (reply == NULL) { \
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100112 GETSPEC_ERR_REPLY \
113 if (err_reply != NULL) { \
114 /* use filled err_reply from libnetconf's callback */ \
115 reply = err_reply; \
116 } \
Tomas Cejka442258e2014-04-01 18:17:18 +0200117}
118
119#define CHECK_ERR_SET_REPLY_ERR(errmsg) \
120if (reply == NULL) { \
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100121 GETSPEC_ERR_REPLY \
122 if (err_reply == NULL) { \
123 reply = create_error_reply(errmsg); \
124 } else { \
125 /* use filled err_reply from libnetconf's callback */ \
126 reply = err_reply; \
127 } \
Tomas Cejka442258e2014-04-01 18:17:18 +0200128}
129void create_err_reply_p();
130void clean_err_reply();
131void free_err_reply();
132
Tomas Cejka8ce138c2015-04-27 23:25:25 +0200133NC_MSG_TYPE netconf_send_recv_timed(struct nc_session *session, nc_rpc *rpc,
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100134 int timeout, nc_reply **reply);
Tomas Cejka8ce138c2015-04-27 23:25:25 +0200135
Tomas Cejkaaf7a1562013-04-13 02:27:43 +0200136#endif