freeswitch plugin: Added esl header files and modified freeswitch.c to use it
[collectd.git] / src / esl.h
1 /*
2  * Copyright (c) 2007, Anthony Minessale II
3  * All rights reserved.
4  * 
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  * 
16  * * Neither the name of the original author; nor the names of any contributors
17  * may be used to endorse or promote products derived from this software
18  * without specific prior written permission.
19  * 
20  * 
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER
25  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #ifndef _ESL_H_
35 #define _ESL_H_
36
37 #include <stdarg.h>
38
39 #ifdef __cplusplus
40 extern "C" {
41 #endif /* defined(__cplusplus) */
42
43 #define esl_copy_string(_x, _y, _z) strncpy(_x, _y, _z - 1)
44 #define esl_set_string(_x, _y) esl_copy_string(_x, _y, sizeof(_x))
45
46 typedef struct esl_event_header esl_event_header_t;
47 typedef struct esl_event esl_event_t;
48
49
50 typedef enum {
51         ESL_EVENT_TYPE_PLAIN,
52         ESL_EVENT_TYPE_XML
53 } esl_event_type_t;
54
55 #ifdef WIN32
56 #define ESL_SEQ_FWHITE FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY
57 #define ESL_SEQ_FRED FOREGROUND_RED | FOREGROUND_INTENSITY
58 #define ESL_SEQ_FMAGEN FOREGROUND_BLUE | FOREGROUND_RED
59 #define ESL_SEQ_FCYAN FOREGROUND_GREEN | FOREGROUND_BLUE
60 #define ESL_SEQ_FGREEN FOREGROUND_GREEN
61 #define ESL_SEQ_FYELLOW FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY
62 #define ESL_SEQ_DEFAULT_COLOR ESL_SEQ_FWHITE
63 #else
64 #define ESL_SEQ_ESC "\033["
65 /* Ansi Control character suffixes */
66 #define ESL_SEQ_HOME_CHAR 'H'
67 #define ESL_SEQ_HOME_CHAR_STR "H"
68 #define ESL_SEQ_CLEARLINE_CHAR '1'
69 #define ESL_SEQ_CLEARLINE_CHAR_STR "1"
70 #define ESL_SEQ_CLEARLINEEND_CHAR "K"
71 #define ESL_SEQ_CLEARSCR_CHAR0 '2'
72 #define ESL_SEQ_CLEARSCR_CHAR1 'J'
73 #define ESL_SEQ_CLEARSCR_CHAR "2J"
74 #define ESL_SEQ_DEFAULT_COLOR ESL_SEQ_ESC ESL_SEQ_END_COLOR     /* Reset to Default fg/bg color */
75 #define ESL_SEQ_AND_COLOR ";"   /* To add multiple color definitions */
76 #define ESL_SEQ_END_COLOR "m"   /* To end color definitions */
77 /* Foreground colors values */
78 #define ESL_SEQ_F_BLACK "30"
79 #define ESL_SEQ_F_RED "31"
80 #define ESL_SEQ_F_GREEN "32"
81 #define ESL_SEQ_F_YELLOW "33"
82 #define ESL_SEQ_F_BLUE "34"
83 #define ESL_SEQ_F_MAGEN "35"
84 #define ESL_SEQ_F_CYAN "36"
85 #define ESL_SEQ_F_WHITE "37"
86 /* Background colors values */
87 #define ESL_SEQ_B_BLACK "40"
88 #define ESL_SEQ_B_RED "41"
89 #define ESL_SEQ_B_GREEN "42"
90 #define ESL_SEQ_B_YELLOW "43"
91 #define ESL_SEQ_B_BLUE "44"
92 #define ESL_SEQ_B_MAGEN "45"
93 #define ESL_SEQ_B_CYAN "46"
94 #define ESL_SEQ_B_WHITE "47"
95 /* Preset escape sequences - Change foreground colors only */
96 #define ESL_SEQ_FBLACK ESL_SEQ_ESC ESL_SEQ_F_BLACK ESL_SEQ_END_COLOR
97 #define ESL_SEQ_FRED ESL_SEQ_ESC ESL_SEQ_F_RED ESL_SEQ_END_COLOR
98 #define ESL_SEQ_FGREEN ESL_SEQ_ESC ESL_SEQ_F_GREEN ESL_SEQ_END_COLOR
99 #define ESL_SEQ_FYELLOW ESL_SEQ_ESC ESL_SEQ_F_YELLOW ESL_SEQ_END_COLOR
100 #define ESL_SEQ_FBLUE ESL_SEQ_ESC ESL_SEQ_F_BLUE ESL_SEQ_END_COLOR
101 #define ESL_SEQ_FMAGEN ESL_SEQ_ESC ESL_SEQ_F_MAGEN ESL_SEQ_END_COLOR
102 #define ESL_SEQ_FCYAN ESL_SEQ_ESC ESL_SEQ_F_CYAN ESL_SEQ_END_COLOR
103 #define ESL_SEQ_FWHITE ESL_SEQ_ESC ESL_SEQ_F_WHITE ESL_SEQ_END_COLOR
104 #define ESL_SEQ_BBLACK ESL_SEQ_ESC ESL_SEQ_B_BLACK ESL_SEQ_END_COLOR
105 #define ESL_SEQ_BRED ESL_SEQ_ESC ESL_SEQ_B_RED ESL_SEQ_END_COLOR
106 #define ESL_SEQ_BGREEN ESL_SEQ_ESC ESL_SEQ_B_GREEN ESL_SEQ_END_COLOR
107 #define ESL_SEQ_BYELLOW ESL_SEQ_ESC ESL_SEQ_B_YELLOW ESL_SEQ_END_COLOR
108 #define ESL_SEQ_BBLUE ESL_SEQ_ESC ESL_SEQ_B_BLUE ESL_SEQ_END_COLOR
109 #define ESL_SEQ_BMAGEN ESL_SEQ_ESC ESL_SEQ_B_MAGEN ESL_SEQ_END_COLOR
110 #define ESL_SEQ_BCYAN ESL_SEQ_ESC ESL_SEQ_B_CYAN ESL_SEQ_END_COLOR
111 #define ESL_SEQ_BWHITE ESL_SEQ_ESC ESL_SEQ_B_WHITE ESL_SEQ_END_COLOR
112 /* Preset escape sequences */
113 #define ESL_SEQ_HOME ESL_SEQ_ESC ESL_SEQ_HOME_CHAR_STR
114 #define ESL_SEQ_CLEARLINE ESL_SEQ_ESC ESL_SEQ_CLEARLINE_CHAR_STR
115 #define ESL_SEQ_CLEARLINEEND ESL_SEQ_ESC ESL_SEQ_CLEARLINEEND_CHAR
116 #define ESL_SEQ_CLEARSCR ESL_SEQ_ESC ESL_SEQ_CLEARSCR_CHAR ESL_SEQ_HOME
117 #endif
118
119 #if !defined(_XOPEN_SOURCE) && !defined(__FreeBSD__) && !defined(__NetBSD__)
120 #define _XOPEN_SOURCE 600
121 #endif
122
123 #ifndef HAVE_STRINGS_H
124 #define HAVE_STRINGS_H 1
125 #endif
126 #ifndef HAVE_SYS_SOCKET_H
127 #define HAVE_SYS_SOCKET_H 1
128 #endif
129
130 #ifndef __WINDOWS__
131 #if defined(WIN32) || defined(WIN64) || defined(_MSC_VER) || defined(_WIN32)
132 #define __WINDOWS__
133 #endif
134 #endif
135
136 #ifdef _MSC_VER
137 #ifndef __inline__
138 #define __inline__ __inline
139 #endif
140 #if (_MSC_VER >= 1400)                  /* VC8+ */
141 #ifndef _CRT_SECURE_NO_DEPRECATE
142 #define _CRT_SECURE_NO_DEPRECATE
143 #endif
144 #ifndef _CRT_NONSTDC_NO_DEPRECATE
145 #define _CRT_NONSTDC_NO_DEPRECATE
146 #endif
147 #endif
148 #ifndef strcasecmp
149 #define strcasecmp(s1, s2) _stricmp(s1, s2)
150 #endif
151 #ifndef strncasecmp
152 #define strncasecmp(s1, s2, n) _strnicmp(s1, s2, n)
153 #endif
154 #ifndef snprintf
155 #define snprintf _snprintf
156 #endif
157 #ifndef S_IRUSR
158 #define S_IRUSR _S_IREAD
159 #endif
160 #ifndef S_IWUSR
161 #define S_IWUSR _S_IWRITE
162 #endif
163 #undef HAVE_STRINGS_H
164 #undef HAVE_SYS_SOCKET_H
165 #endif
166
167 #include <time.h>
168 #ifndef WIN32
169 #include <sys/time.h>
170 #endif
171
172 #include <stdarg.h>
173 #include <stdio.h>
174 #include <stdlib.h>
175 #include <string.h>
176 #ifndef WIN32
177 #include <netinet/tcp.h>
178 #include <sys/signal.h>
179 #include <unistd.h>
180 #include <ctype.h>
181 #endif
182
183 #ifdef HAVE_STRINGS_H
184 #include <strings.h>
185 #endif
186 #include <assert.h>
187
188 #if (_MSC_VER >= 1400)                  // VC8+
189 #define esl_assert(expr) assert(expr);__analysis_assume( expr )
190 #endif
191
192 #ifndef esl_assert
193 #define esl_assert(_x) assert(_x)
194 #endif
195
196 #define esl_safe_free(_x) if (_x) free(_x); _x = NULL
197 #define esl_strlen_zero(s) (!s || *(s) == '\0')
198 #define esl_strlen_zero_buf(s) (*(s) == '\0')
199
200 #ifdef WIN32
201 #include <winsock2.h>
202 #include <windows.h>
203 typedef SOCKET esl_socket_t;
204 typedef unsigned __int64 uint64_t;
205 typedef unsigned __int32 uint32_t;
206 typedef unsigned __int16 uint16_t;
207 typedef unsigned __int8 uint8_t;
208 typedef __int64 int64_t;
209 typedef __int32 int32_t;
210 typedef __int16 int16_t;
211 typedef __int8 int8_t;
212 typedef intptr_t esl_ssize_t;
213 typedef int esl_filehandle_t;
214 #define ESL_SOCK_INVALID INVALID_SOCKET
215 #define strerror_r(num, buf, size) strerror_s(buf, size, num)
216 #if defined(ESL_DECLARE_STATIC)
217 #define ESL_DECLARE(type)                       type __stdcall
218 #define ESL_DECLARE_NONSTD(type)                type __cdecl
219 #define ESL_DECLARE_DATA
220 #elif defined(ESL_EXPORTS)
221 #define ESL_DECLARE(type)                       __declspec(dllexport) type __stdcall
222 #define ESL_DECLARE_NONSTD(type)                __declspec(dllexport) type __cdecl
223 #define ESL_DECLARE_DATA                                __declspec(dllexport)
224 #else
225 #define ESL_DECLARE(type)                       __declspec(dllimport) type __stdcall
226 #define ESL_DECLARE_NONSTD(type)                __declspec(dllimport) type __cdecl
227 #define ESL_DECLARE_DATA                                __declspec(dllimport)
228 #endif
229 #else
230 #define ESL_DECLARE(type) type
231 #define ESL_DECLARE_NONSTD(type) type
232 #define ESL_DECLARE_DATA
233 #include <stdint.h>
234 #include <sys/types.h>
235 #include <sys/ioctl.h>
236 #include <stdarg.h>
237 #include <sys/socket.h>
238 #include <netinet/in.h>
239 #include <netdb.h>
240 #define ESL_SOCK_INVALID -1
241 typedef int esl_socket_t;
242 typedef ssize_t esl_ssize_t;
243 typedef int esl_filehandle_t;
244 #endif
245
246 typedef int16_t esl_port_t;
247
248 typedef enum {
249         ESL_SUCCESS,
250         ESL_FAIL,
251         ESL_BREAK,
252         ESL_DISCONNECTED
253 } esl_status_t;
254
255 #include <esl_threadmutex.h>
256
257 typedef struct {
258         struct sockaddr_in sockaddr;
259         struct hostent hostent;
260         char hostbuf[256];
261         esl_socket_t sock;
262         char err[256];
263         int errnum;
264         char header_buf[4196];
265         char last_reply[1024];
266         char last_sr_reply[1024];
267         esl_event_t *last_event;
268         esl_event_t *last_sr_event;
269         esl_event_t *race_event;
270         esl_event_t *last_ievent;
271         esl_event_t *info_event;
272         int connected;
273         struct sockaddr_in addr;
274         esl_mutex_t *mutex;
275         int async_execute;
276         int event_lock;
277 } esl_handle_t;
278
279 typedef enum {
280         ESL_TRUE = 1,
281         ESL_FALSE = 0
282 } esl_bool_t;
283
284 #ifndef __FUNCTION__
285 #define __FUNCTION__ (const char *)__func__
286 #endif
287
288 #define ESL_PRE __FILE__, __FUNCTION__, __LINE__
289 #define ESL_LOG_LEVEL_DEBUG 7
290 #define ESL_LOG_LEVEL_INFO 6
291 #define ESL_LOG_LEVEL_NOTICE 5
292 #define ESL_LOG_LEVEL_WARNING 4
293 #define ESL_LOG_LEVEL_ERROR 3
294 #define ESL_LOG_LEVEL_CRIT 2
295 #define ESL_LOG_LEVEL_ALERT 1
296 #define ESL_LOG_LEVEL_EMERG 0
297
298 #define ESL_LOG_DEBUG ESL_PRE, ESL_LOG_LEVEL_DEBUG
299 #define ESL_LOG_INFO ESL_PRE, ESL_LOG_LEVEL_INFO
300 #define ESL_LOG_NOTICE ESL_PRE, ESL_LOG_LEVEL_NOTICE
301 #define ESL_LOG_WARNING ESL_PRE, ESL_LOG_LEVEL_WARNING
302 #define ESL_LOG_ERROR ESL_PRE, ESL_LOG_LEVEL_ERROR
303 #define ESL_LOG_CRIT ESL_PRE, ESL_LOG_LEVEL_CRIT
304 #define ESL_LOG_ALERT ESL_PRE, ESL_LOG_LEVEL_ALERT
305 #define ESL_LOG_EMERG ESL_PRE, ESL_LOG_LEVEL_EMERG
306 typedef void (*esl_logger_t)(const char *file, const char *func, int line, int level, const char *fmt, ...);
307
308
309 ESL_DECLARE(int) esl_vasprintf(char **ret, const char *fmt, va_list ap);
310
311 ESL_DECLARE_DATA extern esl_logger_t esl_log;
312
313 ESL_DECLARE(void) esl_global_set_logger(esl_logger_t logger);
314 ESL_DECLARE(void) esl_global_set_default_logger(int level);
315
316 #include "esl_event.h"
317 #include "esl_threadmutex.h"
318 #include "esl_config.h"
319
320 ESL_DECLARE(size_t) esl_url_encode(const char *url, char *buf, size_t len);
321 ESL_DECLARE(char *)esl_url_decode(char *s);
322 ESL_DECLARE(const char *)esl_stristr(const char *instr, const char *str);
323 ESL_DECLARE(int) esl_toupper(int c);
324 ESL_DECLARE(int) esl_tolower(int c);
325 ESL_DECLARE(int) esl_snprintf(char *buffer, size_t count, const char *fmt, ...);
326
327
328 typedef void (*esl_listen_callback_t)(esl_socket_t server_sock, esl_socket_t client_sock, struct sockaddr_in *addr);
329
330 ESL_DECLARE(esl_status_t) esl_attach_handle(esl_handle_t *handle, esl_socket_t socket, struct sockaddr_in *addr);
331 ESL_DECLARE(esl_status_t) esl_listen(const char *host, esl_port_t port, esl_listen_callback_t callback);
332 ESL_DECLARE(esl_status_t) esl_execute(esl_handle_t *handle, const char *app, const char *arg, const char *uuid);
333 ESL_DECLARE(esl_status_t) esl_sendevent(esl_handle_t *handle, esl_event_t *event);
334
335 ESL_DECLARE(esl_status_t) esl_connect(esl_handle_t *handle, const char *host, esl_port_t port, const char *password);
336 ESL_DECLARE(esl_status_t) esl_disconnect(esl_handle_t *handle);
337 ESL_DECLARE(esl_status_t) esl_send(esl_handle_t *handle, const char *cmd);
338 ESL_DECLARE(esl_status_t) esl_recv_event(esl_handle_t *handle, int check_q, esl_event_t **save_event);
339 ESL_DECLARE(esl_status_t) esl_recv_event_timed(esl_handle_t *handle, uint32_t ms, int check_q, esl_event_t **save_event);
340 ESL_DECLARE(esl_status_t) esl_send_recv(esl_handle_t *handle, const char *cmd);
341 ESL_DECLARE(esl_status_t) esl_filter(esl_handle_t *handle, const char *header, const char *value);
342 ESL_DECLARE(esl_status_t) esl_events(esl_handle_t *handle, esl_event_type_t etype, const char *value);
343
344 #define esl_recv(_h) esl_recv_event(_h, 0, NULL)
345 #define esl_recv_timed(_h, _ms) esl_recv_event_timed(_h, _ms, 0, NULL)
346
347 static __inline__ int esl_safe_strcasecmp(const char *s1, const char *s2)
348 {
349         if (!(s1 && s2)) {
350                 return 1;
351         }
352
353         return strcasecmp(s1, s2);
354 }
355
356 #ifdef __cplusplus
357 }
358 #endif /* defined(__cplusplus) */
359
360
361 #endif /* defined(_ESL_H_) */
362
363 /* For Emacs:
364  * Local Variables:
365  * mode:c
366  * indent-tabs-mode:t
367  * tab-width:4
368  * c-basic-offset:4
369  * End:
370  * For VIM:
371  * vim:set softtabstop=4 shiftwidth=4 tabstop=4 expandtab:
372  */