blob: ebb8d5faf668943a4abdbc6faa124d83023eb05d [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 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;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +010077 pthread_mutex_t lock; /**< mutex protecting the session from multiple access */
Michal Vaskoc3146782015-11-04 14:46:41 +010078
Michal Vaskoda0ab5c2015-11-13 13:24:51 +010079 struct session_with_mutex *prev;
Michal Vaskoc3146782015-11-04 14:46:41 +010080 struct session_with_mutex *next;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +020081};
82
Michal Vaskoc3146782015-11-04 14:46:41 +010083struct pass_to_thread {
84 int client; /**< opened socket */
85 struct session_with_mutex *netconf_sessions_list; /**< ?? */
86};
Tomas Cejkaaf7a1562013-04-13 02:27:43 +020087
Tomas Cejkaaf7a1562013-04-13 02:27:43 +020088extern pthread_rwlock_t session_lock; /**< mutex protecting netconf_session_list from multiple access errors */
89
Tomas Cejka442258e2014-04-01 18:17:18 +020090extern pthread_key_t err_reply_key;
91extern pthread_mutex_t json_lock;
92
Michal Vaskoda0ab5c2015-11-13 13:24:51 +010093json_object *create_error_reply(const char *errmess);
Tomas Cejkaef531ee2013-11-12 16:07:00 +010094
Tomas Cejkaef531ee2013-11-12 16:07:00 +010095#define DEBUG(...) do { \
Michal Vaskoc3146782015-11-04 14:46:41 +010096 fprintf(stderr, __VA_ARGS__); \
97 fprintf(stderr, "\n"); \
Tomas Cejkacf44e522015-04-24 17:29:21 +020098} while (0);
99
100#define ERROR(...) do { \
Michal Vaskoc3146782015-11-04 14:46:41 +0100101 fprintf(stderr, __VA_ARGS__); \
102 fprintf(stderr, "\n"); \
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100103} while (0);
Tomas Cejkac7929632013-10-24 19:25:15 +0200104
Tomas Cejka442258e2014-04-01 18:17:18 +0200105#define GETSPEC_ERR_REPLY \
106json_object **err_reply_p = (json_object **) pthread_getspecific(err_reply_key); \
107json_object *err_reply = ((err_reply_p != NULL)?(*err_reply_p):NULL);
108
109#define CHECK_ERR_SET_REPLY \
110if (reply == NULL) { \
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100111 GETSPEC_ERR_REPLY \
112 if (err_reply != NULL) { \
113 /* use filled err_reply from libnetconf's callback */ \
114 reply = err_reply; \
115 } \
Tomas Cejka442258e2014-04-01 18:17:18 +0200116}
117
118#define CHECK_ERR_SET_REPLY_ERR(errmsg) \
119if (reply == NULL) { \
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100120 GETSPEC_ERR_REPLY \
121 if (err_reply == NULL) { \
122 reply = create_error_reply(errmsg); \
123 } else { \
124 /* use filled err_reply from libnetconf's callback */ \
125 reply = err_reply; \
126 } \
Tomas Cejka442258e2014-04-01 18:17:18 +0200127}
128void create_err_reply_p();
129void clean_err_reply();
130void free_err_reply();
131
Michal Vaskof35ea502016-02-24 10:44:54 +0100132NC_MSG_TYPE netconf_send_recv_timed(struct nc_session *session, struct nc_rpc *rpc, int timeout,
133 int strict, struct nc_reply **reply);
Tomas Cejka8ce138c2015-04-27 23:25:25 +0200134
Tomas Cejkaaf7a1562013-04-13 02:27:43 +0200135#endif