Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 1 | /*! |
| 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 Cejka | 45ab59f | 2013-05-15 00:10:49 +0200 | [diff] [blame] | 54 | #include <json/json.h> |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 55 | |
| 56 | struct 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 | |
| 63 | typedef struct notification { |
| 64 | time_t eventtime; |
| 65 | char* content; |
| 66 | } notification_t; |
| 67 | |
| 68 | struct session_with_mutex { |
| 69 | struct nc_session * session; /**< netconf session */ |
| 70 | apr_array_header_t *notifications; |
Tomas Cejka | 45ab59f | 2013-05-15 00:10:49 +0200 | [diff] [blame] | 71 | json_object *hello_message; |
Tomas Cejka | 654f84e | 2013-04-19 11:55:01 +0200 | [diff] [blame] | 72 | char ntfc_subscribed; /**< 0 when notifications are not subscribed */ |
Tomas Cejka | 6e8f426 | 2013-07-10 09:20:19 +0200 | [diff] [blame] | 73 | char closed; /**< 0 when session is terminated */ |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 74 | apr_time_t last_activity; |
| 75 | pthread_mutex_t lock; /**< mutex protecting the session from multiple access */ |
| 76 | }; |
| 77 | |
| 78 | typedef struct { |
| 79 | apr_pool_t *pool; |
| 80 | apr_proc_t *forkproc; |
| 81 | char* sockname; |
| 82 | } mod_netconf_cfg; |
| 83 | |
| 84 | |
| 85 | extern pthread_rwlock_t session_lock; /**< mutex protecting netconf_session_list from multiple access errors */ |
| 86 | |
Tomas Cejka | 442258e | 2014-04-01 18:17:18 +0200 | [diff] [blame] | 87 | extern pthread_key_t err_reply_key; |
| 88 | extern pthread_mutex_t json_lock; |
| 89 | |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 90 | json_object *create_error(const char *errmess); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 91 | json_object *create_ok(); |
| 92 | |
| 93 | extern 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 Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 107 | |
Tomas Cejka | 442258e | 2014-04-01 18:17:18 +0200 | [diff] [blame] | 108 | #define GETSPEC_ERR_REPLY \ |
| 109 | json_object **err_reply_p = (json_object **) pthread_getspecific(err_reply_key); \ |
| 110 | json_object *err_reply = ((err_reply_p != NULL)?(*err_reply_p):NULL); |
| 111 | |
| 112 | #define CHECK_ERR_SET_REPLY \ |
| 113 | if (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) \ |
| 122 | if (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 | } |
| 131 | void create_err_reply_p(); |
| 132 | void clean_err_reply(); |
| 133 | void free_err_reply(); |
| 134 | |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 135 | #endif |
| 136 | |