binrpc_api.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2006 iptelorg GmbH
  5. *
  6. * This file is part of ser, a free SIP server.
  7. *
  8. * ser is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. * For a license to use the ser software under conditions
  14. * other than those described here, or to purchase support for this
  15. * software, please contact iptel.org by e-mail at the following addresses:
  16. * [email protected]
  17. *
  18. * ser is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  26. */
  27. /*
  28. * History:
  29. * --------
  30. * 2006-11-09 created (vlada)
  31. */
  32. #ifndef BINRPC_API_H_
  33. #define BINRPC_API_H_
  34. #include "../../modules/ctl/binrpc.h"
  35. struct binrpc_handle {
  36. int socket;
  37. int proto;
  38. int sock_type;
  39. unsigned char* buf;
  40. int buf_size;
  41. };
  42. struct binrpc_response_handle {
  43. unsigned char *reply_buf;
  44. struct binrpc_parse_ctx in_pkt;
  45. };
  46. /**
  47. * Function: binrpc_open_connection
  48. *
  49. * Description:
  50. * The function open_connection ensures opening of appropriate device for
  51. * future communication. It can create unix socket or TCP/UDP connection
  52. * depending on input parameteres.
  53. *
  54. * @param handle [in]: handler that will be used for saving of obtained socket;
  55. * if this function succeed, this handler must be freed via calling of
  56. * binrpc_close_connection function
  57. * @param name [in]: host IP address or FQDN or unix socket name
  58. * @param port [in]: host port; in case of unix socket the value is omitted
  59. * @param proto [in]: type of communication protocol; allowed values are
  60. * UDP_SOCK, TCP_SOCK, UNIXS_SOCK, UNIXD_SOCK
  61. * @param reply_socket [in]: force reply socket name, for the unix datagram
  62. * socket mode
  63. * @param sock_dir [in]: specify directory where the reply socket will be
  64. * created; if set to NULL, the default value will be used (/tmp)
  65. *
  66. * @return 0 on success, -1 on failure.
  67. *
  68. * */
  69. int binrpc_open_connection(
  70. struct binrpc_handle* handle,
  71. char* name, int port, int proto,
  72. char* reply_socket, char* sock_dir);
  73. /**
  74. * Function: binrpc_open_connection_url
  75. *
  76. * Description:
  77. * The function is similar as open_connection but target is specified using url.
  78. *
  79. * @param handle [in]: handler that will be used for saving of obtained socket;
  80. * if this function succeed, this handle must be freed via calling of
  81. * binrpc_close_connection function
  82. * @param url [in]: [tcp|udp|unix|unixs|unixd] ":" host_socket ":" [port | reply_socket]
  83. * Note: unix = unixs
  84. *
  85. * @return 0 on success, -1 on failure.
  86. *
  87. * */
  88. int binrpc_open_connection_url(struct binrpc_handle* handle, char* url);
  89. /**
  90. * Function: binrpc_close_connection
  91. *
  92. * Description:
  93. * The function close_connection ensures freeing of active socket that is
  94. * represent by handler
  95. *
  96. * @param handle [in]: active connection descriptor
  97. *
  98. * @return 0 on success, -1 on failure.
  99. * none
  100. * */
  101. void binrpc_close_connection(struct binrpc_handle* handle);
  102. /**
  103. * Function: binrpc_send_command
  104. *
  105. * Description:
  106. * The function send_command provides interface for communication with server
  107. * application via binary rpc protocol. It sends request over unix socket or
  108. * TCP/UDPto the host and reads a respons.
  109. *
  110. * @param handle [in]: a descriptor of connection
  111. * @param method [in]: string value of XMLRPC method (e.g. system.listMethods)
  112. * @param args [in]: two dimension array of method's attributes
  113. * @param arg_count [in]: number of method's attributes
  114. * @param resp_handle [out]: structure for holding binary form of response, must be deallocated using binrpc_release_response
  115. *
  116. * @return 0 on success, -1 on failure.
  117. *
  118. * */
  119. int binrpc_send_command(
  120. struct binrpc_handle* handle,
  121. char* method, char** args, int arg_count,
  122. struct binrpc_response_handle* resp_handle);
  123. /**
  124. * Function: binrpc_send_command_ex
  125. *
  126. * Description:
  127. * The function send_command_ex is equivalent of send_command and in addition
  128. * provides possibility to pass already prepared input values.
  129. *
  130. * @param handle [in]: a descriptor of connection
  131. * @param pkt [in]: packet to be sent
  132. * @param resp_handle [out]: structure for holding binary form of response, must be deallocated using binrpc_release_response
  133. *
  134. * @return 0 on success, -1 on failure.
  135. *
  136. * */
  137. int binrpc_send_command_ex(
  138. struct binrpc_handle* handle, struct binrpc_pkt* pkt,
  139. struct binrpc_response_handle *resp_handle);
  140. /**
  141. * Function: binrpc_release_response
  142. *
  143. * Description:
  144. * The function releases response handle created in binrpc_send_command
  145. *
  146. * @param resp_handle [in]: structure for holding binary form of response to be released
  147. *
  148. * @return 0 on success, -1 on failure.
  149. *
  150. * */
  151. void binrpc_release_response(
  152. struct binrpc_response_handle *resp_handle
  153. );
  154. /**
  155. * Function: binrpc_get_response_type
  156. *
  157. * Description:
  158. * The function get_response_type provides information about type of response.
  159. *
  160. * @return 1 on valid failure response, 0 on valid successfull, -1 on failure.
  161. *
  162. * */
  163. int binrpc_get_response_type(struct binrpc_response_handle *resp_handle);
  164. /*
  165. * Function: binrpc_parse_response
  166. *
  167. * Description:
  168. * parse the body into a malloc allocated, binrpc_val array. Ensure that caller and callee are using the same structure alignment!
  169. *
  170. * @param vals [out]: array of values allocated via (binrpc)malloc; it must be freed "manually"
  171. * @param val_count [in/out]: number of records in a list
  172. * @param resp_handle [in]: structure for holding binary form of response
  173. *
  174. * @return -1 failure.
  175. *
  176. * */
  177. int binrpc_parse_response(
  178. struct binrpc_val** vals,
  179. int* val_count,
  180. struct binrpc_response_handle *resp_handle
  181. );
  182. /*
  183. * Function: binrpc_parse_error_response
  184. *
  185. * Description:
  186. * parse the error response
  187. *
  188. * @param resp_handle [in]: structure for holding binary form of response
  189. * @param err_no [out]: error code
  190. * @param err [out]: error stringt
  191. *
  192. * @return -1 failure.
  193. *
  194. * */
  195. int binrpc_parse_error_response(
  196. struct binrpc_response_handle *resp_handle,
  197. int *err_no,
  198. char **err
  199. );
  200. /**
  201. * Function: binrpc_print_response
  202. *
  203. * Description:
  204. * The function print_response prints binrpc response to the standard output in
  205. * readable format.
  206. *
  207. * @param resp_handle [in]: structure for holding binary form of response
  208. * @param fmt [in]: output format that will be used during printing response to
  209. * the standard output
  210. *
  211. * @return 0 on success, -1 on failure
  212. *
  213. * */
  214. int binrpc_print_response(struct binrpc_response_handle *resp_handle, char* fmt);
  215. /**
  216. * Function: binrpc_response_to_text
  217. *
  218. * Description:
  219. * The function binrpc_response_to_text provides functionality to convert result from
  220. * binary form into null terminated text buffer. Records from response are
  221. * separated with character provided in parameter delimiter.
  222. *
  223. * @param resp_handle [in]: structure for holding binary form of response
  224. * @param txt_rsp [out]: buffer that will be used for text form of result; it
  225. * can be passed into function as a NULL and in this case the function will
  226. * alloc some memory that must be freed by user! This function can also
  227. * realloc txt_rsp buffer if there is not enough space for whole response
  228. * @param txt_rsp_len [in/out]: this parameter specify number of allocated but
  229. * empty characters in txt_rsp buffer; value of this parameter can be modify
  230. * in case that the reallocation will be necessary
  231. * @param delimiter [in]: a character that will be used for separation of
  232. * records; if no value is provided, the character for new line is used ('\n')
  233. *
  234. * @return 0 on success, -1 on failure
  235. *
  236. * */
  237. int binrpc_response_to_text(
  238. struct binrpc_response_handle *resp_handle,
  239. unsigned char** txt_rsp, int* txt_rsp_len,
  240. char delimiter);
  241. /**
  242. * Function: binrpc_set_mallocs
  243. *
  244. * Description:
  245. * The function binrpc_set_mallocs allows to programmer use its own function
  246. * for memory handling.
  247. *
  248. * @param _malloc [in]: pointer to function that ensures memory allocation
  249. * @param _realloc [in]: pointer to function that ensures memory reallocation
  250. * @param _free [in]: pointer to function that ensures memory deallocation
  251. *
  252. * */
  253. void binrpc_set_mallocs(void* _malloc, void* _realloc, void* _free);
  254. /**
  255. * Function: binrpc_get_last_errs
  256. *
  257. * Description:
  258. * The function returns last error that occured when function returned FATAL_ERROR
  259. *
  260. * */
  261. char *binrpc_get_last_errs();
  262. /**
  263. * Function: binrpc_clear_last_err
  264. *
  265. * Description:
  266. * The function clears binrpc_last_errs buffer
  267. *
  268. * */
  269. void binrpc_clear_last_err();
  270. /**
  271. * Function: binrpc_free_rpc_array
  272. *
  273. * Description:
  274. * The function frees memory allocated internally to store reply values
  275. * and finally frees the values array
  276. *
  277. * */
  278. void binrpc_free_rpc_array(struct binrpc_val* a, int size);
  279. #endif /*BINRPC_API_H_*/