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 |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 9 | * \date 2014 |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 10 | */ |
| 11 | /* |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 12 | * Copyright (C) 2011-2014 CESNET |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 13 | * |
| 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 Cejka | 45ab59f | 2013-05-15 00:10:49 +0200 | [diff] [blame] | 55 | #include <json/json.h> |
Tomas Cejka | b34b7b1 | 2015-06-21 22:54:11 +0200 | [diff] [blame^] | 56 | #include <libssh2.h> |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 57 | |
Tomas Cejka | 0962949 | 2014-07-10 15:58:06 +0200 | [diff] [blame] | 58 | /** |
| 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 Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 63 | struct pass_to_thread { |
| 64 | int client; /**< opened socket */ |
| 65 | apr_pool_t * pool; /**< ?? */ |
| 66 | server_rec * server; /**< ?? */ |
| 67 | apr_hash_t * netconf_sessions_list; /**< ?? */ |
| 68 | }; |
| 69 | |
| 70 | typedef struct notification { |
| 71 | time_t eventtime; |
| 72 | char* content; |
| 73 | } notification_t; |
| 74 | |
| 75 | struct session_with_mutex { |
| 76 | struct nc_session * session; /**< netconf session */ |
| 77 | apr_array_header_t *notifications; |
Tomas Cejka | 45ab59f | 2013-05-15 00:10:49 +0200 | [diff] [blame] | 78 | json_object *hello_message; |
Tomas Cejka | 654f84e | 2013-04-19 11:55:01 +0200 | [diff] [blame] | 79 | char ntfc_subscribed; /**< 0 when notifications are not subscribed */ |
Tomas Cejka | 6e8f426 | 2013-07-10 09:20:19 +0200 | [diff] [blame] | 80 | char closed; /**< 0 when session is terminated */ |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 81 | apr_time_t last_activity; |
| 82 | pthread_mutex_t lock; /**< mutex protecting the session from multiple access */ |
| 83 | }; |
| 84 | |
| 85 | typedef struct { |
| 86 | apr_pool_t *pool; |
| 87 | apr_proc_t *forkproc; |
| 88 | char* sockname; |
| 89 | } mod_netconf_cfg; |
| 90 | |
| 91 | |
| 92 | extern pthread_rwlock_t session_lock; /**< mutex protecting netconf_session_list from multiple access errors */ |
| 93 | |
Tomas Cejka | 442258e | 2014-04-01 18:17:18 +0200 | [diff] [blame] | 94 | extern pthread_key_t err_reply_key; |
| 95 | extern pthread_mutex_t json_lock; |
| 96 | |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 97 | json_object *create_error(const char *errmess); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 98 | json_object *create_ok(); |
| 99 | |
| 100 | extern server_rec *http_server; |
| 101 | #ifndef HTTPD_INDEPENDENT |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 102 | # define APLOGDEBUG(...) ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, __VA_ARGS__); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 103 | # define APLOGERROR(...) ap_log_error(APLOG_MARK, APLOG_ERR, 0, http_server, __VA_ARGS__); |
| 104 | #else |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 105 | # define APLOGDEBUG(...) |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 106 | # define APLOGERROR(...) |
| 107 | #endif |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 108 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 109 | #define DEBUG(...) do { \ |
| 110 | if (http_server != NULL) { \ |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 111 | APLOGDEBUG(__VA_ARGS__); \ |
| 112 | } else { \ |
| 113 | fprintf(stderr, __VA_ARGS__); \ |
| 114 | fprintf(stderr, "\n"); \ |
| 115 | } \ |
| 116 | } while (0); |
| 117 | |
| 118 | #define ERROR(...) do { \ |
| 119 | if (http_server != NULL) { \ |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 120 | APLOGERROR(__VA_ARGS__); \ |
| 121 | } else { \ |
| 122 | fprintf(stderr, __VA_ARGS__); \ |
| 123 | fprintf(stderr, "\n"); \ |
| 124 | } \ |
| 125 | } while (0); |
Tomas Cejka | c792963 | 2013-10-24 19:25:15 +0200 | [diff] [blame] | 126 | |
Tomas Cejka | 442258e | 2014-04-01 18:17:18 +0200 | [diff] [blame] | 127 | #define GETSPEC_ERR_REPLY \ |
| 128 | json_object **err_reply_p = (json_object **) pthread_getspecific(err_reply_key); \ |
| 129 | json_object *err_reply = ((err_reply_p != NULL)?(*err_reply_p):NULL); |
| 130 | |
| 131 | #define CHECK_ERR_SET_REPLY \ |
| 132 | if (reply == NULL) { \ |
| 133 | GETSPEC_ERR_REPLY \ |
| 134 | if (err_reply != NULL) { \ |
| 135 | /* use filled err_reply from libnetconf's callback */ \ |
| 136 | reply = err_reply; \ |
| 137 | } \ |
| 138 | } |
| 139 | |
| 140 | #define CHECK_ERR_SET_REPLY_ERR(errmsg) \ |
| 141 | if (reply == NULL) { \ |
| 142 | GETSPEC_ERR_REPLY \ |
| 143 | if (err_reply == NULL) { \ |
| 144 | reply = create_error(errmsg); \ |
| 145 | } else { \ |
| 146 | /* use filled err_reply from libnetconf's callback */ \ |
| 147 | reply = err_reply; \ |
| 148 | } \ |
| 149 | } |
| 150 | void create_err_reply_p(); |
| 151 | void clean_err_reply(); |
| 152 | void free_err_reply(); |
| 153 | |
Tomas Cejka | 8ce138c | 2015-04-27 23:25:25 +0200 | [diff] [blame] | 154 | NC_MSG_TYPE netconf_send_recv_timed(struct nc_session *session, nc_rpc *rpc, |
| 155 | int timeout, nc_reply **reply); |
| 156 | |
Tomas Cejka | af7a156 | 2013-04-13 02:27:43 +0200 | [diff] [blame] | 157 | #endif |
| 158 | |