1 #ifndef KAZOO_EI_H
2 #define KAZOO_EI_H
3 
4 #include <switch.h>
5 #include <ei.h>
6 
7 #define MODNAME "mod_kazoo"
8 #define BUNDLE "community"
9 #define RELEASE "v1.5.0-1"
10 #define VERSION "mod_kazoo v1.5.0-1 community"
11 
12 #define KZ_MAX_SEPARATE_STRINGS 10
13 #define HOSTNAME_MAX 1024
14 #define NODENAME_MAX 1024
15 
16 typedef enum {KAZOO_FETCH_PROFILE, KAZOO_EVENT_PROFILE} kazoo_profile_type;
17 
18 typedef enum {ERLANG_TUPLE, ERLANG_MAP} kazoo_json_term;
19 
20 typedef struct ei_xml_agent_s ei_xml_agent_t;
21 typedef ei_xml_agent_t *ei_xml_agent_ptr;
22 
23 typedef struct kazoo_event kazoo_event_t;
24 typedef kazoo_event_t *kazoo_event_ptr;
25 
26 typedef struct kazoo_event_profile kazoo_event_profile_t;
27 typedef kazoo_event_profile_t *kazoo_event_profile_ptr;
28 
29 typedef struct kazoo_fetch_profile kazoo_fetch_profile_t;
30 typedef kazoo_fetch_profile_t *kazoo_fetch_profile_ptr;
31 
32 typedef struct kazoo_config_t kazoo_config;
33 typedef kazoo_config *kazoo_config_ptr;
34 
35 #include "kazoo_fields.h"
36 #include "kazoo_config.h"
37 
38 struct ei_send_msg_s {
39 	ei_x_buff buf;
40 	erlang_pid pid;
41 };
42 typedef struct ei_send_msg_s ei_send_msg_t;
43 
44 struct ei_received_msg_s {
45 	ei_x_buff buf;
46 	erlang_msg msg;
47 };
48 typedef struct ei_received_msg_s ei_received_msg_t;
49 
50 
51 typedef struct ei_event_stream_s ei_event_stream_t;
52 typedef struct ei_node_s ei_node_t;
53 
54 struct ei_event_binding_s {
55 	char id[SWITCH_UUID_FORMATTED_LENGTH + 1];
56 	switch_event_node_t *node;
57 	switch_event_types_t type;
58 	const char *subclass_name;
59 	ei_event_stream_t* stream;
60 	kazoo_event_ptr event;
61 
62 	struct ei_event_binding_s *next;
63 };
64 typedef struct ei_event_binding_s ei_event_binding_t;
65 
66 struct ei_event_stream_s {
67 	switch_memory_pool_t *pool;
68 	ei_event_binding_t *bindings;
69 	switch_queue_t *queue;
70 	switch_socket_t *acceptor;
71 	switch_pollset_t *pollset;
72 	switch_pollfd_t *pollfd;
73 	switch_socket_t *socket;
74 	switch_mutex_t *socket_mutex;
75 	switch_bool_t connected;
76 	switch_time_t connected_time;
77 	char remote_ip[48];
78 	uint16_t remote_port;
79 	char local_ip[48];
80 	uint16_t local_port;
81 	erlang_pid pid;
82 	uint32_t flags;
83 	ei_node_t *node;
84 	short event_stream_framing;
85 	short event_stream_keepalive;
86 	switch_interval_time_t queue_timeout;
87 	struct ei_event_stream_s *next;
88 };
89 
90 struct ei_node_s {
91 	int nodefd;
92 	switch_atomic_t pending_bgapi;
93 	switch_atomic_t receive_handlers;
94 	switch_memory_pool_t *pool;
95 	ei_event_stream_t *event_streams;
96 	switch_mutex_t *event_streams_mutex;
97 	switch_queue_t *send_msgs;
98 	switch_queue_t *received_msgs;
99 	char *peer_nodename;
100 	switch_time_t created_time;
101 	switch_socket_t *socket;
102 	char remote_ip[48];
103 	uint16_t remote_port;
104 	char local_ip[48];
105 	uint16_t local_port;
106 	uint32_t flags;
107 	int legacy;
108 	short event_stream_framing;
109 	short event_stream_keepalive;
110 	switch_interval_time_t event_stream_queue_timeout;
111 	switch_interval_time_t receiver_queue_timeout;
112 	switch_interval_time_t sender_queue_timeout;
113 	struct ei_node_s *next;
114 };
115 
116 
117 struct xml_fetch_reply_s {
118 	char uuid_str[SWITCH_UUID_FORMATTED_LENGTH + 1];
119 	char *xml_str;
120 	struct xml_fetch_reply_s *next;
121 };
122 typedef struct xml_fetch_reply_s xml_fetch_reply_t;
123 
124 struct fetch_handler_s {
125 	erlang_pid pid;
126 	struct fetch_handler_s *next;
127 };
128 typedef struct fetch_handler_s fetch_handler_t;
129 
130 struct ei_xml_client_s {
131 	ei_node_t *ei_node;
132 	fetch_handler_t *fetch_handlers;
133 	struct ei_xml_client_s *next;
134 };
135 typedef struct ei_xml_client_s ei_xml_client_t;
136 
137 struct ei_xml_agent_s {
138 	switch_memory_pool_t *pool;
139 	switch_xml_section_t section;
140 	switch_thread_rwlock_t *lock;
141 	ei_xml_client_t *clients;
142 	switch_mutex_t *current_client_mutex;
143 	ei_xml_client_t *current_client;
144 	switch_mutex_t *replies_mutex;
145 	switch_thread_cond_t *new_reply;
146 	xml_fetch_reply_t *replies;
147 	kazoo_fetch_profile_ptr profile;
148 
149 };
150 
151 struct kz_globals_s {
152 	switch_memory_pool_t *pool;
153 	switch_atomic_t threads;
154 	switch_socket_t *acceptor;
155 	struct ei_cnode_s ei_cnode;
156 	switch_thread_rwlock_t *ei_nodes_lock;
157 	ei_node_t *ei_nodes;
158 
159 	switch_xml_binding_t *config_fetch_binding;
160 	switch_xml_binding_t *directory_fetch_binding;
161 	switch_xml_binding_t *dialplan_fetch_binding;
162 	switch_xml_binding_t *channels_fetch_binding;
163 	switch_xml_binding_t *languages_fetch_binding;
164 	switch_xml_binding_t *chatplan_fetch_binding;
165 
166 	switch_hash_t *event_filter;
167 	int epmdfd;
168 	int node_worker_threads;
169 	switch_bool_t nat_map;
170 	switch_bool_t ei_shortname;
171 	int ei_compat_rel;
172 	char *ip;
173 	char *hostname;
174 	struct hostent* hostname_ent;
175 	char *ei_cookie;
176 	char *ei_nodename;
177 	uint32_t flags;
178 	int send_all_headers;
179 	int send_all_private_headers;
180 	int connection_timeout;
181 	int ei_receive_timeout;
182 	switch_interval_time_t node_sender_queue_timeout;
183 	switch_interval_time_t node_receiver_queue_timeout;
184 	int receive_msg_preallocate;
185 	int event_stream_preallocate;
186 	int send_msg_batch;
187 	short event_stream_framing;
188 	short event_stream_keepalive;
189 	switch_interval_time_t event_stream_queue_timeout;
190 	switch_port_t port;
191 	int config_fetched;
192 	int io_fault_tolerance;
193 	switch_interval_time_t io_fault_tolerance_sleep;
194 	kazoo_event_profile_ptr events;
195 	kazoo_config_ptr definitions;
196 	kazoo_config_ptr event_handlers;
197 	kazoo_config_ptr fetch_handlers;
198 	kazoo_json_term  json_encoding;
199 
200 	char **profile_vars_prefixes;
201 	char **kazoo_var_prefixes;
202 
203 	int legacy_events;
204 	uint8_t tweaks[KZ_TWEAK_MAX];
205 	switch_bool_t expand_headers_on_fetch;
206 
207 	switch_interval_time_t delay_before_initial_fetch;
208 
209 
210 };
211 typedef struct kz_globals_s kz_globals_t;
212 extern kz_globals_t kazoo_globals;
213 
214 /* kazoo_event_stream.c */
215 ei_event_stream_t *find_event_stream(ei_event_stream_t *event_streams, const erlang_pid *from);
216 
217 //ei_event_stream_t *new_event_stream(ei_event_stream_t **event_streams, const erlang_pid *from);
218 ei_event_stream_t *new_event_stream(ei_node_t *ei_node, const erlang_pid *from);
219 
220 
221 switch_status_t remove_event_stream(ei_event_stream_t **event_streams, const erlang_pid *from);
222 switch_status_t remove_event_streams(ei_event_stream_t **event_streams);
223 unsigned long get_stream_port(const ei_event_stream_t *event_stream);
224 switch_status_t add_event_binding(ei_event_stream_t *event_stream, const char *event_name);
225 //switch_status_t add_event_binding(ei_event_stream_t *event_stream, const switch_event_types_t event_type, const char *subclass_name);
226 switch_status_t remove_event_binding(ei_event_stream_t *event_stream, const switch_event_types_t event_type, const char *subclass_name);
227 switch_status_t remove_event_bindings(ei_event_stream_t *event_stream);
228 
229 /* kazoo_node.c */
230 switch_status_t new_kazoo_node(int nodefd, ErlConnect *conn);
231 
232 /* kazoo_ei_utils.c */
233 void close_socket(switch_socket_t **sock);
234 void close_socketfd(int *sockfd);
235 switch_socket_t *create_socket_with_port(switch_memory_pool_t *pool, switch_port_t port);
236 switch_socket_t *create_socket(switch_memory_pool_t *pool);
237 switch_status_t create_ei_cnode(const char *ip_addr, const char *name, struct ei_cnode_s *ei_cnode);
238 switch_status_t ei_compare_pids(const erlang_pid *pid1, const erlang_pid *pid2);
239 void ei_encode_switch_event_headers(ei_x_buff *ebuf, switch_event_t *event);
240 void ei_encode_switch_event_headers_2(ei_x_buff *ebuf, switch_event_t *event, int decode);
241 void ei_encode_json(ei_x_buff *ebuf, cJSON *JObj);
242 void ei_link(ei_node_t *ei_node, erlang_pid * from, erlang_pid * to);
243 void ei_encode_switch_event(ei_x_buff * ebuf, switch_event_t *event);
244 int ei_helper_send(ei_node_t *ei_node, erlang_pid* to, ei_x_buff *buf);
245 int ei_decode_atom_safe(char *buf, int *index, char *dst);
246 int ei_decode_string_or_binary_limited(char *buf, int *index, int maxsize, char *dst);
247 int ei_decode_string_or_binary(char *buf, int *index, char **dst);
248 switch_status_t create_acceptor();
249 switch_hash_t *create_default_filter();
250 void kz_erl_init();
251 void kz_erl_shutdown();
252 SWITCH_DECLARE(switch_status_t) ei_queue_pop(switch_queue_t *queue, void **data, switch_interval_time_t timeout);
253 
254 void fetch_config();
255 
256 switch_status_t kazoo_load_config();
257 void kazoo_destroy_config();
258 void kz_set_hostname();
259 
260 #define _ei_x_encode_string(buf, string) { ei_x_encode_binary(buf, string, strlen(string)); }
261 
262 /* kazoo_fetch_agent.c */
263 switch_status_t bind_fetch_agents();
264 switch_status_t unbind_fetch_agents();
265 switch_status_t remove_xml_clients(ei_node_t *ei_node);
266 switch_status_t add_fetch_handler(ei_node_t *ei_node, erlang_pid *from, switch_xml_binding_t *binding);
267 switch_status_t remove_fetch_handlers(ei_node_t *ei_node, erlang_pid *from);
268 switch_status_t fetch_reply(char *uuid_str, char *xml_str, switch_xml_binding_t *binding);
269 switch_status_t handle_api_command_streams(ei_node_t *ei_node, switch_stream_handle_t *stream);
270 
271 void bind_event_profiles(kazoo_event_ptr event);
272 void rebind_fetch_profiles(kazoo_config_ptr fetch_handlers);
273 switch_status_t kazoo_config_handlers(switch_xml_t cfg);
274 
275 /* runtime */
276 SWITCH_MODULE_RUNTIME_FUNCTION(mod_kazoo_runtime);
277 
278 
279 
280 #define kz_test_tweak(flag) (kazoo_globals.tweaks[flag] ? 1 : 0)
281 #define kz_set_tweak(flag) kazoo_globals.tweaks[flag] = 1
282 #define kz_clear_tweak(flag) kazoo_globals.tweaks[flag] = 0
283 
284 #endif /* KAZOO_EI_H */
285 
286 /* For Emacs:
287  * Local Variables:
288  * mode:c
289  * indent-tabs-mode:t
290  * tab-width:4
291  * c-basic-offset:4
292  * End:
293  * For VIM:
294  * vim:set softtabstop=4 shiftwidth=4 tabstop=4:
295  */
296