Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 1 | /*! |
Michal Vasko | 30aa382 | 2015-11-25 10:41:54 +0100 | [diff] [blame] | 2 | * \file netopeerguid.h |
| 3 | * \brief NETCONF daemon header for NetopeerGUI |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 4 | * \author Tomas Cejka <cejkat@cesnet.cz> |
| 5 | * \author Radek Krejci <rkrejci@cesnet.cz> |
Michal Vasko | a53ef88 | 2015-11-24 11:02:01 +0100 | [diff] [blame] | 6 | * \author Michal Vasko <mvasko@cesnet.cz> |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 7 | * \date 2011 |
| 8 | * \date 2012 |
| 9 | * \date 2013 |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 10 | * \date 2014 |
Michal Vasko | a53ef88 | 2015-11-24 11:02:01 +0100 | [diff] [blame] | 11 | * \date 2015 |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 12 | */ |
| 13 | /* |
Michal Vasko | a53ef88 | 2015-11-24 11:02:01 +0100 | [diff] [blame] | 14 | * Copyright (C) 2011-2015 CESNET |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 15 | * |
| 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 Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 49 | #ifndef _NETOPEERGUID_H |
| 50 | #define _NETOPEERGUID_H |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 51 | |
| 52 | #include <pthread.h> |
Michal Vasko | 0208a86 | 2016-03-16 09:12:44 +0100 | [diff] [blame] | 53 | #include <json.h> |
Michal Vasko | efdd49f | 2016-04-06 11:13:27 +0200 | [diff] [blame] | 54 | #include <syslog.h> |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 55 | #include <libyang/libyang.h> |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 56 | |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 57 | #define UNUSED(x) UNUSED_ ## x __attribute__((__unused__)) |
| 58 | |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 59 | /** |
| 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 Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 64 | typedef struct notification { |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 65 | time_t eventtime; |
| 66 | char* content; |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 67 | } notification_t; |
| 68 | |
| 69 | struct session_with_mutex { |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 70 | struct nc_session *session; /**< netconf session */ |
| 71 | unsigned int session_key; /**< unique session identifier throughout all the sessions */ |
| 72 | notification_t *notifications; |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 73 | int notif_count; |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 74 | 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 Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 78 | pthread_mutex_t lock; /**< mutex protecting the session from multiple access */ |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 79 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 80 | struct session_with_mutex *prev; |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 81 | struct session_with_mutex *next; |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 82 | }; |
| 83 | |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 84 | struct pass_to_thread { |
| 85 | int client; /**< opened socket */ |
| 86 | struct session_with_mutex *netconf_sessions_list; /**< ?? */ |
| 87 | }; |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 88 | |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 89 | extern pthread_rwlock_t session_lock; /**< mutex protecting netconf_session_list from multiple access errors */ |
| 90 | |
Tomas Cejka | 442258e | 2014-04-01 18:17:18 +0200 | [diff] [blame] | 91 | extern pthread_key_t err_reply_key; |
| 92 | extern pthread_mutex_t json_lock; |
Michal Vasko | efdd49f | 2016-04-06 11:13:27 +0200 | [diff] [blame] | 93 | extern int daemonize; |
Tomas Cejka | 442258e | 2014-04-01 18:17:18 +0200 | [diff] [blame] | 94 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 95 | json_object *create_error_reply(const char *errmess); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 96 | |
Michal Vasko | a719710 | 2016-04-06 11:17:52 +0200 | [diff] [blame] | 97 | #ifdef DBG |
| 98 | |
Michal Vasko | efdd49f | 2016-04-06 11:13:27 +0200 | [diff] [blame] | 99 | #define DEBUG(...) \ |
| 100 | if (daemonize) { \ |
| 101 | syslog(LOG_DEBUG, __VA_ARGS__); \ |
| 102 | } else { \ |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 103 | fprintf(stderr, __VA_ARGS__); \ |
| 104 | fprintf(stderr, "\n"); \ |
Michal Vasko | efdd49f | 2016-04-06 11:13:27 +0200 | [diff] [blame] | 105 | } |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 106 | |
Michal Vasko | a719710 | 2016-04-06 11:17:52 +0200 | [diff] [blame] | 107 | #else |
| 108 | |
| 109 | #define DEBUG(...) |
| 110 | |
| 111 | #endif |
| 112 | |
Michal Vasko | efdd49f | 2016-04-06 11:13:27 +0200 | [diff] [blame] | 113 | #define ERROR(...) \ |
| 114 | if (daemonize) { \ |
| 115 | syslog(LOG_ERR, __VA_ARGS__); \ |
| 116 | } else { \ |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 117 | fprintf(stderr, __VA_ARGS__); \ |
| 118 | fprintf(stderr, "\n"); \ |
Michal Vasko | efdd49f | 2016-04-06 11:13:27 +0200 | [diff] [blame] | 119 | } |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 120 | |
Tomas Cejka | 442258e | 2014-04-01 18:17:18 +0200 | [diff] [blame] | 121 | #define GETSPEC_ERR_REPLY \ |
| 122 | json_object **err_reply_p = (json_object **) pthread_getspecific(err_reply_key); \ |
| 123 | json_object *err_reply = ((err_reply_p != NULL)?(*err_reply_p):NULL); |
| 124 | |
| 125 | #define CHECK_ERR_SET_REPLY \ |
| 126 | if (reply == NULL) { \ |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 127 | GETSPEC_ERR_REPLY \ |
| 128 | if (err_reply != NULL) { \ |
| 129 | /* use filled err_reply from libnetconf's callback */ \ |
| 130 | reply = err_reply; \ |
| 131 | } \ |
Tomas Cejka | 442258e | 2014-04-01 18:17:18 +0200 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | #define CHECK_ERR_SET_REPLY_ERR(errmsg) \ |
| 135 | if (reply == NULL) { \ |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 136 | GETSPEC_ERR_REPLY \ |
| 137 | if (err_reply == NULL) { \ |
| 138 | reply = create_error_reply(errmsg); \ |
| 139 | } else { \ |
| 140 | /* use filled err_reply from libnetconf's callback */ \ |
| 141 | reply = err_reply; \ |
| 142 | } \ |
Tomas Cejka | 442258e | 2014-04-01 18:17:18 +0200 | [diff] [blame] | 143 | } |
| 144 | void create_err_reply_p(); |
| 145 | void clean_err_reply(); |
| 146 | void free_err_reply(); |
| 147 | |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 148 | NC_MSG_TYPE netconf_send_recv_timed(struct nc_session *session, struct nc_rpc *rpc, int timeout, |
| 149 | int strict, struct nc_reply **reply); |
Tomas Cejka | 8ce138c | 2015-04-27 23:25:25 +0200 | [diff] [blame] | 150 | |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 151 | #endif |