blob: 2fcea4a8f59cd2f79f3ba183a29bf0610fdb451d [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
9 */
10/*
11 * Copyright (C) 2011-2013 CESNET
12 *
13 * LICENSE TERMS
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in
22 * the documentation and/or other materials provided with the
23 * distribution.
24 * 3. Neither the name of the Company nor the names of its contributors
25 * may be used to endorse or promote products derived from this
26 * software without specific prior written permission.
27 *
28 * ALTERNATIVELY, provided that this notice is retained in full, this
29 * product may be distributed under the terms of the GNU General Public
30 * License (GPL) version 2 or later, in which case the provisions
31 * of the GPL apply INSTEAD OF those given above.
32 *
33 * This software is provided ``as is'', and any express or implied
34 * warranties, including, but not limited to, the implied warranties of
35 * merchantability and fitness for a particular purpose are disclaimed.
36 * In no event shall the company or contributors be liable for any
37 * direct, indirect, incidental, special, exemplary, or consequential
38 * damages (including, but not limited to, procurement of substitute
39 * goods or services; loss of use, data, or profits; or business
40 * interruption) however caused and on any theory of liability, whether
41 * in contract, strict liability, or tort (including negligence or
42 * otherwise) arising in any way out of the use of this software, even
43 * if advised of the possibility of such damage.
44 *
45 */
46#ifndef __MOD_NETCONF_COMMON_H
47#define __MOD_NETCONF_COMMON_H
48
49#include <pthread.h>
50#include <httpd.h>
51#include <http_log.h>
52#include <http_config.h>
53#include <apr_hash.h>
Tomas Cejka45ab59f2013-05-15 00:10:49 +020054#include <json/json.h>
Tomas Cejkaaf7a1562013-04-13 02:27:43 +020055
56struct pass_to_thread {
57 int client; /**< opened socket */
58 apr_pool_t * pool; /**< ?? */
59 server_rec * server; /**< ?? */
60 apr_hash_t * netconf_sessions_list; /**< ?? */
61};
62
63typedef struct notification {
64 time_t eventtime;
65 char* content;
66} notification_t;
67
68struct session_with_mutex {
69 struct nc_session * session; /**< netconf session */
70 apr_array_header_t *notifications;
Tomas Cejka45ab59f2013-05-15 00:10:49 +020071 json_object *hello_message;
Tomas Cejka654f84e2013-04-19 11:55:01 +020072 char ntfc_subscribed; /**< 0 when notifications are not subscribed */
Tomas Cejka6e8f4262013-07-10 09:20:19 +020073 char closed; /**< 0 when session is terminated */
Tomas Cejkaaf7a1562013-04-13 02:27:43 +020074 apr_time_t last_activity;
75 pthread_mutex_t lock; /**< mutex protecting the session from multiple access */
76};
77
78typedef struct {
79 apr_pool_t *pool;
80 apr_proc_t *forkproc;
81 char* sockname;
82} mod_netconf_cfg;
83
84
85extern pthread_rwlock_t session_lock; /**< mutex protecting netconf_session_list from multiple access errors */
86
Tomas Cejka442258e2014-04-01 18:17:18 +020087extern pthread_key_t err_reply_key;
88extern pthread_mutex_t json_lock;
89
Tomas Cejkac7929632013-10-24 19:25:15 +020090json_object *create_error(const char *errmess);
Tomas Cejkaef531ee2013-11-12 16:07:00 +010091json_object *create_ok();
92
93extern server_rec *http_server;
94#ifndef HTTPD_INDEPENDENT
95# define APLOGERROR(...) ap_log_error(APLOG_MARK, APLOG_ERR, 0, http_server, __VA_ARGS__);
96#else
97# define APLOGERROR(...)
98#endif
99#define DEBUG(...) do { \
100 if (http_server != NULL) { \
101 APLOGERROR(__VA_ARGS__); \
102 } else { \
103 fprintf(stderr, __VA_ARGS__); \
104 fprintf(stderr, "\n"); \
105 } \
106} while (0);
Tomas Cejkac7929632013-10-24 19:25:15 +0200107
Tomas Cejka442258e2014-04-01 18:17:18 +0200108#define GETSPEC_ERR_REPLY \
109json_object **err_reply_p = (json_object **) pthread_getspecific(err_reply_key); \
110json_object *err_reply = ((err_reply_p != NULL)?(*err_reply_p):NULL);
111
112#define CHECK_ERR_SET_REPLY \
113if (reply == NULL) { \
114 GETSPEC_ERR_REPLY \
115 if (err_reply != NULL) { \
116 /* use filled err_reply from libnetconf's callback */ \
117 reply = err_reply; \
118 } \
119}
120
121#define CHECK_ERR_SET_REPLY_ERR(errmsg) \
122if (reply == NULL) { \
123 GETSPEC_ERR_REPLY \
124 if (err_reply == NULL) { \
125 reply = create_error(errmsg); \
126 } else { \
127 /* use filled err_reply from libnetconf's callback */ \
128 reply = err_reply; \
129 } \
130}
131void create_err_reply_p();
132void clean_err_reply();
133void free_err_reply();
134
Tomas Cejkaaf7a1562013-04-13 02:27:43 +0200135#endif
136