blob: 0f9bf7937632aeff85cf0ccee012fec0f828529a [file] [log] [blame]
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001/*!
2 * \file mod_netconf.c
3 * \brief NETCONF Apache modul for Netopeer
4 * \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>
51#include <httpd.h>
52#include <http_log.h>
53#include <http_config.h>
54#include <apr_hash.h>
Tomas Cejka45ab59f2013-05-15 00:10:49 +020055#include <json/json.h>
Tomas Cejkaaf7a1562013-04-13 02:27:43 +020056
Tomas Cejka09629492014-07-10 15:58:06 +020057/**
58 * \brief Check if pointer is not NULL, free memory and set pointer to NULL
59 */
60#define CHECK_AND_FREE(pointer) if (pointer != NULL) { free(pointer); pointer = NULL; }
61
Tomas Cejkaaf7a1562013-04-13 02:27:43 +020062struct pass_to_thread {
63 int client; /**< opened socket */
64 apr_pool_t * pool; /**< ?? */
65 server_rec * server; /**< ?? */
66 apr_hash_t * netconf_sessions_list; /**< ?? */
67};
68
69typedef struct notification {
70 time_t eventtime;
71 char* content;
72} notification_t;
73
74struct session_with_mutex {
75 struct nc_session * session; /**< netconf session */
76 apr_array_header_t *notifications;
Tomas Cejka45ab59f2013-05-15 00:10:49 +020077 json_object *hello_message;
Tomas Cejka654f84e2013-04-19 11:55:01 +020078 char ntfc_subscribed; /**< 0 when notifications are not subscribed */
Tomas Cejka6e8f4262013-07-10 09:20:19 +020079 char closed; /**< 0 when session is terminated */
Tomas Cejkaaf7a1562013-04-13 02:27:43 +020080 apr_time_t last_activity;
81 pthread_mutex_t lock; /**< mutex protecting the session from multiple access */
82};
83
84typedef struct {
85 apr_pool_t *pool;
86 apr_proc_t *forkproc;
87 char* sockname;
88} mod_netconf_cfg;
89
90
91extern pthread_rwlock_t session_lock; /**< mutex protecting netconf_session_list from multiple access errors */
92
Tomas Cejka442258e2014-04-01 18:17:18 +020093extern pthread_key_t err_reply_key;
94extern pthread_mutex_t json_lock;
95
Tomas Cejkac7929632013-10-24 19:25:15 +020096json_object *create_error(const char *errmess);
Tomas Cejkaef531ee2013-11-12 16:07:00 +010097json_object *create_ok();
98
99extern server_rec *http_server;
100#ifndef HTTPD_INDEPENDENT
Tomas Cejkacf44e522015-04-24 17:29:21 +0200101# define APLOGDEBUG(...) ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, __VA_ARGS__);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100102# define APLOGERROR(...) ap_log_error(APLOG_MARK, APLOG_ERR, 0, http_server, __VA_ARGS__);
103#else
Tomas Cejkacf44e522015-04-24 17:29:21 +0200104# define APLOGDEBUG(...)
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100105# define APLOGERROR(...)
106#endif
Tomas Cejkacf44e522015-04-24 17:29:21 +0200107
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100108#define DEBUG(...) do { \
109 if (http_server != NULL) { \
Tomas Cejkacf44e522015-04-24 17:29:21 +0200110 APLOGDEBUG(__VA_ARGS__); \
111 } else { \
112 fprintf(stderr, __VA_ARGS__); \
113 fprintf(stderr, "\n"); \
114 } \
115} while (0);
116
117#define ERROR(...) do { \
118 if (http_server != NULL) { \
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100119 APLOGERROR(__VA_ARGS__); \
120 } else { \
121 fprintf(stderr, __VA_ARGS__); \
122 fprintf(stderr, "\n"); \
123 } \
124} while (0);
Tomas Cejkac7929632013-10-24 19:25:15 +0200125
Tomas Cejka442258e2014-04-01 18:17:18 +0200126#define GETSPEC_ERR_REPLY \
127json_object **err_reply_p = (json_object **) pthread_getspecific(err_reply_key); \
128json_object *err_reply = ((err_reply_p != NULL)?(*err_reply_p):NULL);
129
130#define CHECK_ERR_SET_REPLY \
131if (reply == NULL) { \
132 GETSPEC_ERR_REPLY \
133 if (err_reply != NULL) { \
134 /* use filled err_reply from libnetconf's callback */ \
135 reply = err_reply; \
136 } \
137}
138
139#define CHECK_ERR_SET_REPLY_ERR(errmsg) \
140if (reply == NULL) { \
141 GETSPEC_ERR_REPLY \
142 if (err_reply == NULL) { \
143 reply = create_error(errmsg); \
144 } else { \
145 /* use filled err_reply from libnetconf's callback */ \
146 reply = err_reply; \
147 } \
148}
149void create_err_reply_p();
150void clean_err_reply();
151void free_err_reply();
152
Tomas Cejka8ce138c2015-04-27 23:25:25 +0200153NC_MSG_TYPE netconf_send_recv_timed(struct nc_session *session, nc_rpc *rpc,
154 int timeout, nc_reply **reply);
155
Tomas Cejkaaf7a1562013-04-13 02:27:43 +0200156#endif
157