esl.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. /*
  2. * Copyright (c) 2007-2014, 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. #ifndef _ESL_H_
  34. #define _ESL_H_
  35. #include <stdarg.h>
  36. #ifdef __cplusplus
  37. extern "C" {
  38. #endif /* defined(__cplusplus) */
  39. #define esl_copy_string(_x, _y, _z) strncpy(_x, _y, _z - 1)
  40. #define esl_set_string(_x, _y) esl_copy_string(_x, _y, sizeof(_x))
  41. #define ESL_VA_NONE "%s", ""
  42. typedef struct esl_event_header esl_event_header_t;
  43. typedef struct esl_event esl_event_t;
  44. typedef enum {
  45. ESL_POLL_READ = (1 << 0),
  46. ESL_POLL_WRITE = (1 << 1),
  47. ESL_POLL_ERROR = (1 << 2)
  48. } esl_poll_t;
  49. typedef enum {
  50. ESL_EVENT_TYPE_PLAIN,
  51. ESL_EVENT_TYPE_XML,
  52. ESL_EVENT_TYPE_JSON
  53. } esl_event_type_t;
  54. #ifdef WIN32
  55. #define ESL_SEQ_FWHITE FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY
  56. #define ESL_SEQ_BWHITE FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE
  57. #define ESL_SEQ_FRED FOREGROUND_RED | FOREGROUND_INTENSITY
  58. #define ESL_SEQ_BRED FOREGROUND_RED
  59. #define ESL_SEQ_FMAGEN FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_INTENSITY
  60. #define ESL_SEQ_BMAGEN FOREGROUND_BLUE | FOREGROUND_RED
  61. #define ESL_SEQ_FCYAN FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY
  62. #define ESL_SEQ_BCYAN FOREGROUND_GREEN | FOREGROUND_BLUE
  63. #define ESL_SEQ_FGREEN FOREGROUND_GREEN | FOREGROUND_INTENSITY
  64. #define ESL_SEQ_BGREEN FOREGROUND_GREEN
  65. #define ESL_SEQ_FYELLOW FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY
  66. #define ESL_SEQ_BYELLOW FOREGROUND_RED | FOREGROUND_GREEN
  67. #define ESL_SEQ_DEFAULT_COLOR ESL_SEQ_FWHITE
  68. #define ESL_SEQ_FBLUE FOREGROUND_BLUE | FOREGROUND_INTENSITY
  69. #define ESL_SEQ_BBLUE FOREGROUND_BLUE
  70. #define ESL_SEQ_FBLACK 0 | FOREGROUND_INTENSITY
  71. #define ESL_SEQ_BBLACK 0
  72. #else
  73. #define ESL_SEQ_ESC "\033["
  74. /* Ansi Control character suffixes */
  75. #define ESL_SEQ_HOME_CHAR 'H'
  76. #define ESL_SEQ_HOME_CHAR_STR "H"
  77. #define ESL_SEQ_CLEARLINE_CHAR '1'
  78. #define ESL_SEQ_CLEARLINE_CHAR_STR "1"
  79. #define ESL_SEQ_CLEARLINEEND_CHAR "K"
  80. #define ESL_SEQ_CLEARSCR_CHAR0 '2'
  81. #define ESL_SEQ_CLEARSCR_CHAR1 'J'
  82. #define ESL_SEQ_CLEARSCR_CHAR "2J"
  83. #define ESL_SEQ_DEFAULT_COLOR ESL_SEQ_ESC ESL_SEQ_END_COLOR /* Reset to Default fg/bg color */
  84. #define ESL_SEQ_AND_COLOR ";" /* To add multiple color definitions */
  85. #define ESL_SEQ_END_COLOR "m" /* To end color definitions */
  86. /* Foreground colors values */
  87. #define ESL_SEQ_F_BLACK "30"
  88. #define ESL_SEQ_F_RED "31"
  89. #define ESL_SEQ_F_GREEN "32"
  90. #define ESL_SEQ_F_YELLOW "33"
  91. #define ESL_SEQ_F_BLUE "34"
  92. #define ESL_SEQ_F_MAGEN "35"
  93. #define ESL_SEQ_F_CYAN "36"
  94. #define ESL_SEQ_F_WHITE "37"
  95. /* Background colors values */
  96. #define ESL_SEQ_B_BLACK "40"
  97. #define ESL_SEQ_B_RED "41"
  98. #define ESL_SEQ_B_GREEN "42"
  99. #define ESL_SEQ_B_YELLOW "43"
  100. #define ESL_SEQ_B_BLUE "44"
  101. #define ESL_SEQ_B_MAGEN "45"
  102. #define ESL_SEQ_B_CYAN "46"
  103. #define ESL_SEQ_B_WHITE "47"
  104. /* Preset escape sequences - Change foreground colors only */
  105. #define ESL_SEQ_FBLACK ESL_SEQ_ESC ESL_SEQ_F_BLACK ESL_SEQ_END_COLOR
  106. #define ESL_SEQ_FRED ESL_SEQ_ESC ESL_SEQ_F_RED ESL_SEQ_END_COLOR
  107. #define ESL_SEQ_FGREEN ESL_SEQ_ESC ESL_SEQ_F_GREEN ESL_SEQ_END_COLOR
  108. #define ESL_SEQ_FYELLOW ESL_SEQ_ESC ESL_SEQ_F_YELLOW ESL_SEQ_END_COLOR
  109. #define ESL_SEQ_FBLUE ESL_SEQ_ESC ESL_SEQ_F_BLUE ESL_SEQ_END_COLOR
  110. #define ESL_SEQ_FMAGEN ESL_SEQ_ESC ESL_SEQ_F_MAGEN ESL_SEQ_END_COLOR
  111. #define ESL_SEQ_FCYAN ESL_SEQ_ESC ESL_SEQ_F_CYAN ESL_SEQ_END_COLOR
  112. #define ESL_SEQ_FWHITE ESL_SEQ_ESC ESL_SEQ_F_WHITE ESL_SEQ_END_COLOR
  113. #define ESL_SEQ_BBLACK ESL_SEQ_ESC ESL_SEQ_B_BLACK ESL_SEQ_END_COLOR
  114. #define ESL_SEQ_BRED ESL_SEQ_ESC ESL_SEQ_B_RED ESL_SEQ_END_COLOR
  115. #define ESL_SEQ_BGREEN ESL_SEQ_ESC ESL_SEQ_B_GREEN ESL_SEQ_END_COLOR
  116. #define ESL_SEQ_BYELLOW ESL_SEQ_ESC ESL_SEQ_B_YELLOW ESL_SEQ_END_COLOR
  117. #define ESL_SEQ_BBLUE ESL_SEQ_ESC ESL_SEQ_B_BLUE ESL_SEQ_END_COLOR
  118. #define ESL_SEQ_BMAGEN ESL_SEQ_ESC ESL_SEQ_B_MAGEN ESL_SEQ_END_COLOR
  119. #define ESL_SEQ_BCYAN ESL_SEQ_ESC ESL_SEQ_B_CYAN ESL_SEQ_END_COLOR
  120. #define ESL_SEQ_BWHITE ESL_SEQ_ESC ESL_SEQ_B_WHITE ESL_SEQ_END_COLOR
  121. /* Preset escape sequences */
  122. #define ESL_SEQ_HOME ESL_SEQ_ESC ESL_SEQ_HOME_CHAR_STR
  123. #define ESL_SEQ_CLEARLINE ESL_SEQ_ESC ESL_SEQ_CLEARLINE_CHAR_STR
  124. #define ESL_SEQ_CLEARLINEEND ESL_SEQ_ESC ESL_SEQ_CLEARLINEEND_CHAR
  125. #define ESL_SEQ_CLEARSCR ESL_SEQ_ESC ESL_SEQ_CLEARSCR_CHAR ESL_SEQ_HOME
  126. #endif
  127. #if !defined(_XOPEN_SOURCE) && !defined(__FreeBSD__) && !defined(__NetBSD__) && !defined(__OpenBSD__)
  128. #define _XOPEN_SOURCE 600
  129. #endif
  130. #ifndef HAVE_STRINGS_H
  131. #define HAVE_STRINGS_H 1
  132. #endif
  133. #ifndef HAVE_SYS_SOCKET_H
  134. #define HAVE_SYS_SOCKET_H 1
  135. #endif
  136. #ifndef __WINDOWS__
  137. #if defined(WIN32) || defined(WIN64) || defined(_MSC_VER) || defined(_WIN32)
  138. #define __WINDOWS__
  139. #endif
  140. #endif
  141. #ifdef _MSC_VER
  142. #ifndef __inline__
  143. #define __inline__ __inline
  144. #endif
  145. #if (_MSC_VER >= 1400) /* VC8+ */
  146. #ifndef _CRT_SECURE_NO_DEPRECATE
  147. #define _CRT_SECURE_NO_DEPRECATE
  148. #endif
  149. #ifndef _CRT_NONSTDC_NO_DEPRECATE
  150. #define _CRT_NONSTDC_NO_DEPRECATE
  151. #endif
  152. #endif
  153. #ifndef strcasecmp
  154. #define strcasecmp(s1, s2) _stricmp(s1, s2)
  155. #endif
  156. #ifndef strncasecmp
  157. #define strncasecmp(s1, s2, n) _strnicmp(s1, s2, n)
  158. #endif
  159. #ifndef snprintf
  160. #define snprintf _snprintf
  161. #endif
  162. #ifndef S_IRUSR
  163. #define S_IRUSR _S_IREAD
  164. #endif
  165. #ifndef S_IWUSR
  166. #define S_IWUSR _S_IWRITE
  167. #endif
  168. #undef HAVE_STRINGS_H
  169. #undef HAVE_SYS_SOCKET_H
  170. #endif
  171. #include <time.h>
  172. #ifndef WIN32
  173. #include <sys/time.h>
  174. #endif
  175. #include <stdarg.h>
  176. #include <stdio.h>
  177. #include <stdlib.h>
  178. #include <string.h>
  179. #ifndef WIN32
  180. #include <sys/types.h>
  181. #include <sys/select.h>
  182. #include <netinet/tcp.h>
  183. #include <signal.h>
  184. #include <unistd.h>
  185. #include <ctype.h>
  186. #endif
  187. #ifdef HAVE_STRINGS_H
  188. #include <strings.h>
  189. #endif
  190. #include <assert.h>
  191. #if (_MSC_VER >= 1400) // VC8+
  192. #define esl_assert(expr) assert(expr);__analysis_assume( expr )
  193. #endif
  194. #ifndef esl_assert
  195. #define esl_assert(_x) assert(_x)
  196. #endif
  197. #define esl_safe_free(_x) if (_x) free(_x); _x = NULL
  198. #define esl_strlen_zero(s) (!s || *(s) == '\0')
  199. #define esl_strlen_zero_buf(s) (*(s) == '\0')
  200. #define end_of(_s) *(*_s == '\0' ? _s : _s + strlen(_s) - 1)
  201. #ifdef WIN32
  202. #include <winsock2.h>
  203. #include <windows.h>
  204. typedef SOCKET esl_socket_t;
  205. #if !defined(_STDINT) && !defined(uint32_t)
  206. typedef unsigned __int64 uint64_t;
  207. typedef unsigned __int32 uint32_t;
  208. typedef unsigned __int16 uint16_t;
  209. typedef unsigned __int8 uint8_t;
  210. typedef __int64 int64_t;
  211. typedef __int32 int32_t;
  212. typedef __int16 int16_t;
  213. typedef __int8 int8_t;
  214. #endif
  215. typedef intptr_t esl_ssize_t;
  216. typedef int esl_filehandle_t;
  217. #define ESL_SOCK_INVALID INVALID_SOCKET
  218. #define strerror_r(num, buf, size) strerror_s(buf, size, num)
  219. #if defined(ESL_DECLARE_STATIC)
  220. #define ESL_DECLARE(type) type __stdcall
  221. #define ESL_DECLARE_NONSTD(type) type __cdecl
  222. #define ESL_DECLARE_DATA
  223. #elif defined(ESL_EXPORTS)
  224. #define ESL_DECLARE(type) __declspec(dllexport) type __stdcall
  225. #define ESL_DECLARE_NONSTD(type) __declspec(dllexport) type __cdecl
  226. #define ESL_DECLARE_DATA __declspec(dllexport)
  227. #else
  228. #define ESL_DECLARE(type) __declspec(dllimport) type __stdcall
  229. #define ESL_DECLARE_NONSTD(type) __declspec(dllimport) type __cdecl
  230. #define ESL_DECLARE_DATA __declspec(dllimport)
  231. #endif
  232. #else
  233. #define ESL_DECLARE(type) type
  234. #define ESL_DECLARE_NONSTD(type) type
  235. #define ESL_DECLARE_DATA
  236. #include <stdint.h>
  237. #include <sys/types.h>
  238. #include <sys/ioctl.h>
  239. #include <stdarg.h>
  240. #include <sys/socket.h>
  241. #include <netinet/in.h>
  242. #include <netdb.h>
  243. #define ESL_SOCK_INVALID -1
  244. typedef int esl_socket_t;
  245. typedef ssize_t esl_ssize_t;
  246. typedef int esl_filehandle_t;
  247. #endif
  248. #include "math.h"
  249. #include "esl_json.h"
  250. typedef int16_t esl_port_t;
  251. typedef size_t esl_size_t;
  252. typedef enum {
  253. ESL_SUCCESS,
  254. ESL_FAIL,
  255. ESL_BREAK,
  256. ESL_DISCONNECTED,
  257. ESL_GENERR
  258. } esl_status_t;
  259. #define BUF_CHUNK 65536 * 50
  260. #define BUF_START 65536 * 100
  261. #include <esl_threadmutex.h>
  262. #include <esl_buffer.h>
  263. /*! \brief A handle that will hold the socket information and
  264. different events received. */
  265. typedef struct {
  266. struct sockaddr_storage sockaddr;
  267. struct hostent hostent;
  268. char hostbuf[256];
  269. esl_socket_t sock;
  270. /*! In case of socket error, this will hold the error description as reported by the OS */
  271. char err[256];
  272. /*! The error number reported by the OS */
  273. int errnum;
  274. /*! The inner contents received by the socket. Used only internally. */
  275. esl_buffer_t *packet_buf;
  276. char socket_buf[65536];
  277. /*! Last command reply */
  278. char last_reply[1024];
  279. /*! Last command reply when called with esl_send_recv */
  280. char last_sr_reply[1024];
  281. /*! Last event received. Only populated when **save_event is NULL */
  282. esl_event_t *last_event;
  283. /*! Last event received when called by esl_send_recv */
  284. esl_event_t *last_sr_event;
  285. /*! This will hold already processed events queued by esl_recv_event */
  286. esl_event_t *race_event;
  287. /*! Events that have content-type == text/plain and a body */
  288. esl_event_t *last_ievent;
  289. /*! For outbound socket. Will hold reply information when connect\n\n is sent */
  290. esl_event_t *info_event;
  291. /*! Socket is connected or not */
  292. int connected;
  293. struct sockaddr_in addr;
  294. /*! Internal mutex */
  295. esl_mutex_t *mutex;
  296. int async_execute;
  297. int event_lock;
  298. int destroyed;
  299. } esl_handle_t;
  300. #define esl_test_flag(obj, flag) ((obj)->flags & flag)
  301. #define esl_set_flag(obj, flag) (obj)->flags |= (flag)
  302. #define esl_clear_flag(obj, flag) (obj)->flags &= ~(flag)
  303. /*! \brief Used internally for truth test */
  304. typedef enum {
  305. ESL_TRUE = 1,
  306. ESL_FALSE = 0
  307. } esl_bool_t;
  308. #ifndef __FUNCTION__
  309. #define __FUNCTION__ (const char *)__func__
  310. #endif
  311. #define ESL_PRE __FILE__, __FUNCTION__, __LINE__
  312. #define ESL_LOG_LEVEL_DEBUG 7
  313. #define ESL_LOG_LEVEL_INFO 6
  314. #define ESL_LOG_LEVEL_NOTICE 5
  315. #define ESL_LOG_LEVEL_WARNING 4
  316. #define ESL_LOG_LEVEL_ERROR 3
  317. #define ESL_LOG_LEVEL_CRIT 2
  318. #define ESL_LOG_LEVEL_ALERT 1
  319. #define ESL_LOG_LEVEL_EMERG 0
  320. #define ESL_LOG_DEBUG ESL_PRE, ESL_LOG_LEVEL_DEBUG
  321. #define ESL_LOG_INFO ESL_PRE, ESL_LOG_LEVEL_INFO
  322. #define ESL_LOG_NOTICE ESL_PRE, ESL_LOG_LEVEL_NOTICE
  323. #define ESL_LOG_WARNING ESL_PRE, ESL_LOG_LEVEL_WARNING
  324. #define ESL_LOG_ERROR ESL_PRE, ESL_LOG_LEVEL_ERROR
  325. #define ESL_LOG_CRIT ESL_PRE, ESL_LOG_LEVEL_CRIT
  326. #define ESL_LOG_ALERT ESL_PRE, ESL_LOG_LEVEL_ALERT
  327. #define ESL_LOG_EMERG ESL_PRE, ESL_LOG_LEVEL_EMERG
  328. typedef void (*esl_logger_t)(const char *file, const char *func, int line, int level, const char *fmt, ...);
  329. ESL_DECLARE(int) esl_vasprintf(char **ret, const char *fmt, va_list ap);
  330. ESL_DECLARE_DATA extern esl_logger_t esl_log;
  331. /*! Sets the logger for libesl. Default is the null_logger */
  332. ESL_DECLARE(void) esl_global_set_logger(esl_logger_t logger);
  333. /*! Sets the default log level for libesl */
  334. ESL_DECLARE(void) esl_global_set_default_logger(int level);
  335. #include "esl_event.h"
  336. #include "esl_threadmutex.h"
  337. #include "esl_config.h"
  338. ESL_DECLARE(size_t) esl_url_encode(const char *url, char *buf, size_t len);
  339. ESL_DECLARE(char *)esl_url_decode(char *s);
  340. ESL_DECLARE(const char *)esl_stristr(const char *instr, const char *str);
  341. ESL_DECLARE(int) esl_toupper(int c);
  342. ESL_DECLARE(int) esl_tolower(int c);
  343. ESL_DECLARE(int) esl_snprintf(char *buffer, size_t count, const char *fmt, ...);
  344. typedef void (*esl_listen_callback_t)(esl_socket_t server_sock, esl_socket_t client_sock, struct sockaddr_in *addr, void *user_data);
  345. /*!
  346. \brief Attach a handle to an established socket connection
  347. \param handle Handle to be attached
  348. \param socket Socket to which the handle will be attached
  349. \param addr Structure that will contain the connection descritption (look up your os info)
  350. */
  351. ESL_DECLARE(esl_status_t) esl_attach_handle(esl_handle_t *handle, esl_socket_t socket, struct sockaddr_in *addr);
  352. /*!
  353. \brief Will bind to host and callback when event is received. Used for outbound socket.
  354. \param host Host to bind to
  355. \param port Port to bind to
  356. \param callback Callback that will be called upon data received
  357. */
  358. ESL_DECLARE(esl_status_t) esl_listen(const char *host, esl_port_t port, esl_listen_callback_t callback, void *user_data, esl_socket_t *server_sockP);
  359. ESL_DECLARE(esl_status_t) esl_listen_threaded(const char *host, esl_port_t port, esl_listen_callback_t callback, void *user_data, int max);
  360. /*!
  361. \brief Executes application with sendmsg to a specific UUID. Used for outbound socket.
  362. \param handle Handle that the msg will be sent
  363. \param app Application to execute
  364. \param arg Application arguments
  365. \param uuid Target UUID for the application
  366. */
  367. ESL_DECLARE(esl_status_t) esl_execute(esl_handle_t *handle, const char *app, const char *arg, const char *uuid);
  368. /*!
  369. \brief Send an event
  370. \param handle Handle to which the event should be sent
  371. \param event Event to be sent
  372. */
  373. ESL_DECLARE(esl_status_t) esl_sendevent(esl_handle_t *handle, esl_event_t *event);
  374. /*!
  375. \brief Send an event as a message to be parsed
  376. \param handle Handle to which the event should be sent
  377. \param event Event to be sent
  378. \param uuid a specific uuid if not the default
  379. */
  380. ESL_DECLARE(esl_status_t) esl_sendmsg(esl_handle_t *handle, esl_event_t *event, const char *uuid);
  381. /*!
  382. \brief Connect a handle to a host/port with a specific password. This will also authenticate against the server
  383. \param handle Handle to connect
  384. \param host Host to be connected
  385. \param port Port to be connected
  386. \param password FreeSWITCH server username (optional)
  387. \param password FreeSWITCH server password
  388. \param timeout Connection timeout, in miliseconds
  389. */
  390. ESL_DECLARE(esl_status_t) esl_connect_timeout(esl_handle_t *handle, const char *host, esl_port_t port, const char *user, const char *password, uint32_t timeout);
  391. #define esl_connect(_handle, _host, _port, _user, _password) esl_connect_timeout(_handle, _host, _port, _user, _password, 0)
  392. /*!
  393. \brief Disconnect a handle
  394. \param handle Handle to be disconnected
  395. */
  396. ESL_DECLARE(esl_status_t) esl_disconnect(esl_handle_t *handle);
  397. /*!
  398. \brief Send a raw command using specific handle
  399. \param handle Handle to send the command to
  400. \param cmd Command to send
  401. */
  402. ESL_DECLARE(esl_status_t) esl_send(esl_handle_t *handle, const char *cmd);
  403. /*!
  404. \brief Poll the handle's socket until an event is received or a connection error occurs
  405. \param handle Handle to poll
  406. \param check_q If set to 1, will check the handle queue (handle->race_event) and return the last event from it
  407. \param[out] save_event If this is not NULL, will return the event received
  408. */
  409. ESL_DECLARE(esl_status_t) esl_recv_event(esl_handle_t *handle, int check_q, esl_event_t **save_event);
  410. /*!
  411. \brief Poll the handle's socket until an event is received, a connection error occurs or ms expires
  412. \param handle Handle to poll
  413. \param ms Maximum time to poll
  414. \param check_q If set to 1, will check the handle queue (handle->race_event) and return the last event from it
  415. \param[out] save_event If this is not NULL, will return the event received
  416. */
  417. ESL_DECLARE(esl_status_t) esl_recv_event_timed(esl_handle_t *handle, uint32_t ms, int check_q, esl_event_t **save_event);
  418. /*!
  419. \brief This will send a command and place its response event on handle->last_sr_event and handle->last_sr_reply
  420. \param handle Handle to be used
  421. \param cmd Raw command to send
  422. */
  423. ESL_DECLARE(esl_status_t) esl_send_recv_timed(esl_handle_t *handle, const char *cmd, uint32_t ms);
  424. #define esl_send_recv(_handle, _cmd) esl_send_recv_timed(_handle, _cmd, 0)
  425. /*!
  426. \brief Applies a filter to received events
  427. \param handle Handle to apply the filter to
  428. \param header Header that the filter will be based on
  429. \param value The value of the header to filter
  430. */
  431. ESL_DECLARE(esl_status_t) esl_filter(esl_handle_t *handle, const char *header, const char *value);
  432. /*!
  433. \brief Will subscribe to events on the server
  434. \param handle Handle to which we will subscribe to events
  435. \param etype Event type to subscribe
  436. \param value Which event to subscribe to
  437. */
  438. ESL_DECLARE(esl_status_t) esl_events(esl_handle_t *handle, esl_event_type_t etype, const char *value);
  439. ESL_DECLARE(int) esl_wait_sock(esl_socket_t sock, uint32_t ms, esl_poll_t flags);
  440. ESL_DECLARE(unsigned int) esl_separate_string_string(char *buf, const char *delim, char **array, unsigned int arraylen);
  441. #define esl_recv(_h) esl_recv_event(_h, 0, NULL)
  442. #define esl_recv_timed(_h, _ms) esl_recv_event_timed(_h, _ms, 0, NULL)
  443. static __inline__ int esl_safe_strcasecmp(const char *s1, const char *s2)
  444. {
  445. if (!(s1 && s2)) {
  446. return 1;
  447. }
  448. return strcasecmp(s1, s2);
  449. }
  450. #ifdef __cplusplus
  451. }
  452. #endif /* defined(__cplusplus) */
  453. #endif /* defined(_ESL_H_) */
  454. /* For Emacs:
  455. * Local Variables:
  456. * mode:c
  457. * indent-tabs-mode:t
  458. * tab-width:4
  459. * c-basic-offset:4
  460. * End:
  461. * For VIM:
  462. * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
  463. */