blob: a087efb2f4fbf091bed30e76aa0863c89b950af4 [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 */
Michal Vaskof35ea502016-02-24 10:44:54 +010049#ifndef _NETOPEERGUID_H
50#define _NETOPEERGUID_H
Tomas Cejkaaf7a1562013-04-13 02:27:43 +020051
52#include <pthread.h>
Michal Vasko0208a862016-03-16 09:12:44 +010053#include <json.h>
Michal Vaskoefdd49f2016-04-06 11:13:27 +020054#include <syslog.h>
Michal Vaskoda0ab5c2015-11-13 13:24:51 +010055#include <libyang/libyang.h>
Tomas Cejkaaf7a1562013-04-13 02:27:43 +020056
Michal Vaskoc3146782015-11-04 14:46:41 +010057#define UNUSED(x) UNUSED_ ## x __attribute__((__unused__))
58
Tomas Cejka09629492014-07-10 15:58:06 +020059/**
60 * \brief Check if pointer is not NULL, free memory and set pointer to NULL
61 */
62#define CHECK_AND_FREE(pointer) if (pointer != NULL) { free(pointer); pointer = NULL; }
63
Tomas Cejkaaf7a1562013-04-13 02:27:43 +020064typedef struct notification {
Michal Vaskoda0ab5c2015-11-13 13:24:51 +010065 time_t eventtime;
66 char* content;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +020067} notification_t;
68
69struct session_with_mutex {
Michal Vaskoda0ab5c2015-11-13 13:24:51 +010070 struct nc_session *session; /**< netconf session */
71 unsigned int session_key; /**< unique session identifier throughout all the sessions */
72 notification_t *notifications;
Michal Vaskoc3146782015-11-04 14:46:41 +010073 int notif_count;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +010074 json_object *hello_message;
75 char ntfc_subscribed; /**< 0 when notifications are not subscribed */
76 char closed; /**< 0 when session is terminated */
77 time_t last_activity;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +010078 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;
Michal Vaskoefdd49f2016-04-06 11:13:27 +020093extern int daemonize;
Tomas Cejka442258e2014-04-01 18:17:18 +020094
Michal Vaskoda0ab5c2015-11-13 13:24:51 +010095json_object *create_error_reply(const char *errmess);
Tomas Cejkaef531ee2013-11-12 16:07:00 +010096
Michal Vaskoefdd49f2016-04-06 11:13:27 +020097#define DEBUG(...) \
98if (daemonize) { \
99 syslog(LOG_DEBUG, __VA_ARGS__); \
100} else { \
Michal Vaskoc3146782015-11-04 14:46:41 +0100101 fprintf(stderr, __VA_ARGS__); \
102 fprintf(stderr, "\n"); \
Michal Vaskoefdd49f2016-04-06 11:13:27 +0200103}
Tomas Cejkacf44e522015-04-24 17:29:21 +0200104
Michal Vaskoefdd49f2016-04-06 11:13:27 +0200105#define ERROR(...) \
106if (daemonize) { \
107 syslog(LOG_ERR, __VA_ARGS__); \
108} else { \
Michal Vaskoc3146782015-11-04 14:46:41 +0100109 fprintf(stderr, __VA_ARGS__); \
110 fprintf(stderr, "\n"); \
Michal Vaskoefdd49f2016-04-06 11:13:27 +0200111}
Tomas Cejkac7929632013-10-24 19:25:15 +0200112
Tomas Cejka442258e2014-04-01 18:17:18 +0200113#define GETSPEC_ERR_REPLY \
114json_object **err_reply_p = (json_object **) pthread_getspecific(err_reply_key); \
115json_object *err_reply = ((err_reply_p != NULL)?(*err_reply_p):NULL);
116
117#define CHECK_ERR_SET_REPLY \
118if (reply == NULL) { \
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100119 GETSPEC_ERR_REPLY \
120 if (err_reply != NULL) { \
121 /* use filled err_reply from libnetconf's callback */ \
122 reply = err_reply; \
123 } \
Tomas Cejka442258e2014-04-01 18:17:18 +0200124}
125
126#define CHECK_ERR_SET_REPLY_ERR(errmsg) \
127if (reply == NULL) { \
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100128 GETSPEC_ERR_REPLY \
129 if (err_reply == NULL) { \
130 reply = create_error_reply(errmsg); \
131 } else { \
132 /* use filled err_reply from libnetconf's callback */ \
133 reply = err_reply; \
134 } \
Tomas Cejka442258e2014-04-01 18:17:18 +0200135}
136void create_err_reply_p();
137void clean_err_reply();
138void free_err_reply();
139
Michal Vaskof35ea502016-02-24 10:44:54 +0100140NC_MSG_TYPE netconf_send_recv_timed(struct nc_session *session, struct nc_rpc *rpc, int timeout,
141 int strict, struct nc_reply **reply);
Tomas Cejka8ce138c2015-04-27 23:25:25 +0200142
Tomas Cejkaaf7a1562013-04-13 02:27:43 +0200143#endif