2
0

iksemel.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /* iksemel (XML parser for Jabber)
  2. ** Copyright (C) 2000-2007 Gurer Ozen <madcat@e-kolay.net>
  3. ** This code is free software; you can redistribute it and/or
  4. ** modify it under the terms of GNU Lesser General Public License.
  5. */
  6. #ifndef IKSEMEL_H
  7. #define IKSEMEL_H 1
  8. #ifdef __cplusplus
  9. #include <cstddef> /* size_t for C++ */
  10. extern "C" {
  11. #else
  12. #include <stddef.h> /* size_t for C */
  13. #endif
  14. /***** object stack *****/
  15. struct ikstack_struct;
  16. typedef struct ikstack_struct ikstack;
  17. ikstack *iks_stack_new (size_t meta_chunk, size_t data_chunk);
  18. void *iks_stack_alloc (ikstack *s, size_t size);
  19. char *iks_stack_strdup (ikstack *s, const char *src, size_t len);
  20. char *iks_stack_strcat (ikstack *s, char *old, size_t old_len, const char *src, size_t src_len);
  21. void iks_stack_stat (ikstack *s, size_t *allocated, size_t *used);
  22. void iks_stack_delete (ikstack **sp);
  23. /***** utilities *****/
  24. void *iks_malloc (size_t size);
  25. void iks_real_free (void *ptr);
  26. void iks_set_mem_funcs (void *(*malloc_func)(size_t size), void (*free_func)(void *ptr));
  27. char *iks_strdup (const char *src);
  28. char *iks_strcat (char *dest, const char *src);
  29. int iks_strcmp (const char *a, const char *b);
  30. int iks_strcasecmp (const char *a, const char *b);
  31. int iks_strncmp (const char *a, const char *b, size_t n);
  32. int iks_strncasecmp (const char *a, const char *b, size_t n);
  33. size_t iks_strlen (const char *src);
  34. char *iks_escape (ikstack *s, char *src, size_t len);
  35. char *iks_unescape (ikstack *s, char *src, size_t len);
  36. #define iks_free(p) if (p) {iks_real_free(p) ; p = NULL;}
  37. /***** dom tree *****/
  38. enum ikstype {
  39. IKS_NONE = 0,
  40. IKS_TAG,
  41. IKS_ATTRIBUTE,
  42. IKS_CDATA
  43. };
  44. struct iks_struct;
  45. typedef struct iks_struct iks;
  46. iks *iks_new (const char *name);
  47. iks *iks_new_within (const char *name, ikstack *s);
  48. iks *iks_insert (iks *x, const char *name);
  49. iks *iks_insert_cdata (iks *x, const char *data, size_t len);
  50. iks *iks_insert_attrib (iks *x, const char *name, const char *value);
  51. iks *iks_insert_node (iks *x, iks *y);
  52. iks *iks_append (iks *x, const char *name);
  53. iks *iks_prepend (iks *x, const char *name);
  54. iks *iks_append_cdata (iks *x, const char *data, size_t len);
  55. iks *iks_prepend_cdata (iks *x, const char *data, size_t len);
  56. void iks_hide (iks *x);
  57. void iks_delete (iks *x);
  58. iks *iks_next (iks *x);
  59. iks *iks_next_tag (iks *x);
  60. iks *iks_prev (iks *x);
  61. iks *iks_prev_tag (iks *x);
  62. iks *iks_parent (iks *x);
  63. iks *iks_root (iks *x);
  64. iks *iks_child (iks *x);
  65. iks *iks_first_tag (iks *x);
  66. iks *iks_attrib (iks *x);
  67. iks *iks_find (iks *x, const char *name);
  68. char *iks_find_cdata (iks *x, const char *name);
  69. char *iks_find_attrib (iks *x, const char *name);
  70. iks *iks_find_with_attrib (iks *x, const char *tagname, const char *attrname, const char *value);
  71. ikstack *iks_stack (iks *x);
  72. enum ikstype iks_type (iks *x);
  73. char *iks_name (iks *x);
  74. char *iks_cdata (iks *x);
  75. size_t iks_cdata_size (iks *x);
  76. int iks_has_children (iks *x);
  77. int iks_has_attribs (iks *x);
  78. char *iks_string (ikstack *s, iks *x);
  79. iks *iks_copy (iks *x);
  80. iks *iks_copy_within (iks *x, ikstack *s);
  81. /***** sax parser *****/
  82. enum ikserror {
  83. IKS_OK = 0,
  84. IKS_NOMEM,
  85. IKS_BADXML,
  86. IKS_HOOK
  87. };
  88. enum ikstagtype {
  89. IKS_OPEN,
  90. IKS_CLOSE,
  91. IKS_SINGLE
  92. };
  93. typedef int (iksTagHook)(void *user_data, char *name, char **atts, int type);
  94. typedef int (iksCDataHook)(void *user_data, char *data, size_t len);
  95. typedef void (iksDeleteHook)(void *user_data);
  96. struct iksparser_struct;
  97. typedef struct iksparser_struct iksparser;
  98. iksparser *iks_sax_new (void *user_data, iksTagHook *tagHook, iksCDataHook *cdataHook);
  99. iksparser *iks_sax_extend (ikstack *s, void *user_data, iksTagHook *tagHook, iksCDataHook *cdataHook, iksDeleteHook *deleteHook);
  100. ikstack *iks_parser_stack (iksparser *prs);
  101. void *iks_user_data (iksparser *prs);
  102. unsigned long iks_nr_bytes (iksparser *prs);
  103. unsigned long iks_nr_lines (iksparser *prs);
  104. int iks_parse (iksparser *prs, const char *data, size_t len, int finish);
  105. void iks_parser_reset (iksparser *prs);
  106. void iks_parser_delete (iksparser *prs);
  107. /***** dom parser *****/
  108. enum iksfileerror {
  109. IKS_FILE_NOFILE = 4,
  110. IKS_FILE_NOACCESS,
  111. IKS_FILE_RWERR
  112. };
  113. iksparser *iks_dom_new (iks **iksptr);
  114. void iks_set_size_hint (iksparser *prs, size_t approx_size);
  115. iks *iks_tree (const char *xml_str, size_t len, int *err);
  116. int iks_load (const char *fname, iks **xptr);
  117. int iks_save (const char *fname, iks *x);
  118. /***** transport layer *****/
  119. enum iksasyncevents {
  120. IKS_ASYNC_RESOLVED,
  121. IKS_ASYNC_CONNECTED,
  122. IKS_ASYNC_WRITE,
  123. IKS_ASYNC_WRITTEN,
  124. IKS_ASYNC_READ,
  125. IKS_ASYNC_CLOSED,
  126. IKS_ASYNC_ERROR
  127. };
  128. typedef struct iksasyncevent_struct {
  129. int event;
  130. int data0;
  131. int data1;
  132. } iksasyncevent;
  133. typedef void (iksTClose)(void *socket);
  134. typedef int (iksTConnect)(iksparser *prs, void **socketptr, const char *server, int port);
  135. typedef int (iksTSend)(void *socket, const char *data, size_t len);
  136. typedef int (iksTRecv)(void *socket, char *buffer, size_t buf_len, int timeout);
  137. typedef int (iksAsyncNotify)(void *user_data, iksasyncevent *event);
  138. typedef int (iksTConnectAsync)(iksparser *prs, void **socketptr, const char *server, const char *server_name, int port, void *notify_data, iksAsyncNotify *notify_func);
  139. #define IKS_TRANSPORT_V1 0
  140. typedef const struct ikstransport_struct {
  141. int abi_version;
  142. /* basic api, connect can be NULL if only async api is provided */
  143. iksTConnect *connect;
  144. iksTSend *send;
  145. iksTRecv *recv;
  146. iksTClose *close;
  147. /* optional async api */
  148. iksTConnectAsync *connect_async;
  149. } ikstransport;
  150. extern ikstransport iks_default_transport;
  151. /***** stream parser *****/
  152. enum iksneterror {
  153. IKS_NET_NODNS = 4,
  154. IKS_NET_NOSOCK,
  155. IKS_NET_NOCONN,
  156. IKS_NET_RWERR,
  157. IKS_NET_NOTSUPP,
  158. IKS_NET_TLSFAIL,
  159. IKS_NET_DROPPED,
  160. IKS_NET_UNKNOWN
  161. };
  162. enum iksnodetype {
  163. IKS_NODE_START,
  164. IKS_NODE_NORMAL,
  165. IKS_NODE_ERROR,
  166. IKS_NODE_STOP
  167. };
  168. enum ikssasltype {
  169. IKS_SASL_PLAIN,
  170. IKS_SASL_DIGEST_MD5
  171. };
  172. #define IKS_JABBER_PORT 5222
  173. typedef int (iksStreamHook)(void *user_data, int type, iks *node);
  174. typedef void (iksLogHook)(void *user_data, const char *data, size_t size, int is_incoming);
  175. iksparser *iks_stream_new (char *name_space, void *user_data, iksStreamHook *streamHook);
  176. void *iks_stream_user_data (iksparser *prs);
  177. void iks_set_log_hook (iksparser *prs, iksLogHook *logHook);
  178. int iks_connect_tcp (iksparser *prs, const char *server, int port);
  179. int iks_connect_fd (iksparser *prs, int fd);
  180. int iks_connect_via (iksparser *prs, const char *server, int port, const char *server_name);
  181. int iks_connect_with (iksparser *prs, const char *server, int port, const char *server_name, ikstransport *trans);
  182. int iks_connect_async (iksparser *prs, const char *server, int port, void *notify_data, iksAsyncNotify *notify_func);
  183. int iks_connect_async_with (iksparser *prs, const char *server, int port, const char *server_name, ikstransport *trans, void *notify_data, iksAsyncNotify *notify_func);
  184. int iks_fd (iksparser *prs);
  185. int iks_recv (iksparser *prs, int timeout);
  186. int iks_send_header (iksparser *prs, const char *to);
  187. int iks_send (iksparser *prs, iks *x);
  188. int iks_send_raw (iksparser *prs, const char *xmlstr);
  189. void iks_disconnect (iksparser *prs);
  190. int iks_has_tls (void);
  191. int iks_is_secure (iksparser *prs);
  192. int iks_start_tls (iksparser *prs);
  193. int iks_proceed_tls (iksparser *prs, const char *cert_file, const char *key_file);
  194. int iks_start_sasl (iksparser *prs, enum ikssasltype type, char *username, char *pass);
  195. /***** jabber *****/
  196. #define IKS_NS_CLIENT "jabber:client"
  197. #define IKS_NS_SERVER "jabber:server"
  198. #define IKS_NS_AUTH "jabber:iq:auth"
  199. #define IKS_NS_AUTH_0K "jabber:iq:auth:0k"
  200. #define IKS_NS_REGISTER "jabber:iq:register"
  201. #define IKS_NS_ROSTER "jabber:iq:roster"
  202. #define IKS_NS_XROSTER "jabber:x:roster"
  203. #define IKS_NS_OFFLINE "jabber:x:offline"
  204. #define IKS_NS_AGENT "jabber:iq:agent"
  205. #define IKS_NS_AGENTS "jabber:iq:agents"
  206. #define IKS_NS_BROWSE "jabber:iq:browse"
  207. #define IKS_NS_CONFERENCE "jabber:iq:conference"
  208. #define IKS_NS_DELAY "jabber:x:delay"
  209. #define IKS_NS_VERSION "jabber:iq:version"
  210. #define IKS_NS_TIME "jabber:iq:time"
  211. #define IKS_NS_VCARD "vcard-temp"
  212. #define IKS_NS_PRIVATE "jabber:iq:private"
  213. #define IKS_NS_SEARCH "jabber:iq:search"
  214. #define IKS_NS_OOB "jabber:iq:oob"
  215. #define IKS_NS_XOOB "jabber:x:oob"
  216. #define IKS_NS_ADMIN "jabber:iq:admin"
  217. #define IKS_NS_FILTER "jabber:iq:filter"
  218. #define IKS_NS_GATEWAY "jabber:iq:gateway"
  219. #define IKS_NS_LAST "jabber:iq:last"
  220. #define IKS_NS_SIGNED "jabber:x:signed"
  221. #define IKS_NS_ENCRYPTED "jabber:x:encrypted"
  222. #define IKS_NS_ENVELOPE "jabber:x:envelope"
  223. #define IKS_NS_EVENT "jabber:x:event"
  224. #define IKS_NS_EXPIRE "jabber:x:expire"
  225. #define IKS_NS_XHTML "http://www.w3.org/1999/xhtml"
  226. #define IKS_NS_XMPP_SASL "urn:ietf:params:xml:ns:xmpp-sasl"
  227. #define IKS_NS_XMPP_BIND "urn:ietf:params:xml:ns:xmpp-bind"
  228. #define IKS_NS_XMPP_SESSION "urn:ietf:params:xml:ns:xmpp-session"
  229. #define IKS_ID_USER 1
  230. #define IKS_ID_SERVER 2
  231. #define IKS_ID_RESOURCE 4
  232. #define IKS_ID_PARTIAL IKS_ID_USER | IKS_ID_SERVER
  233. #define IKS_ID_FULL IKS_ID_USER | IKS_ID_SERVER | IKS_ID_RESOURCE
  234. #define IKS_STREAM_STARTTLS 1
  235. #define IKS_STREAM_SESSION 2
  236. #define IKS_STREAM_BIND 4
  237. #define IKS_STREAM_SASL_PLAIN 8
  238. #define IKS_STREAM_SASL_MD5 16
  239. typedef struct iksid_struct {
  240. char *user;
  241. char *server;
  242. char *resource;
  243. char *partial;
  244. char *full;
  245. } iksid;
  246. iksid *iks_id_new (ikstack *s, const char *jid);
  247. int iks_id_cmp (iksid *a, iksid *b, int parts);
  248. enum ikspaktype {
  249. IKS_PAK_NONE = 0,
  250. IKS_PAK_MESSAGE,
  251. IKS_PAK_PRESENCE,
  252. IKS_PAK_IQ,
  253. IKS_PAK_S10N
  254. };
  255. enum iksubtype {
  256. IKS_TYPE_NONE = 0,
  257. IKS_TYPE_ERROR,
  258. IKS_TYPE_CHAT,
  259. IKS_TYPE_GROUPCHAT,
  260. IKS_TYPE_HEADLINE,
  261. IKS_TYPE_GET,
  262. IKS_TYPE_SET,
  263. IKS_TYPE_RESULT,
  264. IKS_TYPE_SUBSCRIBE,
  265. IKS_TYPE_SUBSCRIBED,
  266. IKS_TYPE_UNSUBSCRIBE,
  267. IKS_TYPE_UNSUBSCRIBED,
  268. IKS_TYPE_PROBE,
  269. IKS_TYPE_AVAILABLE,
  270. IKS_TYPE_UNAVAILABLE
  271. };
  272. enum ikshowtype {
  273. IKS_SHOW_UNAVAILABLE = 0,
  274. IKS_SHOW_AVAILABLE,
  275. IKS_SHOW_CHAT,
  276. IKS_SHOW_AWAY,
  277. IKS_SHOW_XA,
  278. IKS_SHOW_DND
  279. };
  280. typedef struct ikspak_struct {
  281. iks *x;
  282. iksid *from;
  283. iks *query;
  284. char *ns;
  285. char *id;
  286. enum ikspaktype type;
  287. enum iksubtype subtype;
  288. enum ikshowtype show;
  289. } ikspak;
  290. ikspak *iks_packet (iks *x);
  291. iks *iks_make_auth (iksid *id, const char *pass, const char *sid);
  292. iks *iks_make_msg (enum iksubtype type, const char *to, const char *body);
  293. iks *iks_make_s10n (enum iksubtype type, const char *to, const char *msg);
  294. iks *iks_make_pres (enum ikshowtype show, const char *status);
  295. iks *iks_make_iq (enum iksubtype type, const char *xmlns);
  296. iks *iks_make_resource_bind(iksid *id);
  297. iks *iks_make_session(void);
  298. int iks_stream_features(iks *x);
  299. /***** jabber packet filter *****/
  300. #define IKS_RULE_DONE 0
  301. #define IKS_RULE_ID 1
  302. #define IKS_RULE_TYPE 2
  303. #define IKS_RULE_SUBTYPE 4
  304. #define IKS_RULE_FROM 8
  305. #define IKS_RULE_FROM_PARTIAL 16
  306. #define IKS_RULE_NS 32
  307. enum iksfilterret {
  308. IKS_FILTER_PASS,
  309. IKS_FILTER_EAT
  310. };
  311. typedef int (iksFilterHook)(void *user_data, ikspak *pak);
  312. struct iksfilter_struct;
  313. typedef struct iksfilter_struct iksfilter;
  314. struct iksrule_struct;
  315. typedef struct iksrule_struct iksrule;
  316. iksfilter *iks_filter_new (void);
  317. iksrule *iks_filter_add_rule (iksfilter *f, iksFilterHook *filterHook, void *user_data, ...);
  318. void iks_filter_remove_rule (iksfilter *f, iksrule *rule);
  319. void iks_filter_remove_hook (iksfilter *f, iksFilterHook *filterHook);
  320. void iks_filter_packet (iksfilter *f, ikspak *pak);
  321. void iks_filter_delete (iksfilter *f);
  322. /***** sha1 *****/
  323. struct iksha_struct;
  324. typedef struct iksha_struct iksha;
  325. iksha *iks_sha_new (void);
  326. void iks_sha_reset (iksha *sha);
  327. void iks_sha_hash (iksha *sha, const unsigned char *data, size_t len, int finish);
  328. void iks_sha_print (iksha *sha, char *hash);
  329. void iks_sha_delete (iksha *sha);
  330. void iks_sha (const char *data, char *hash);
  331. /***** md5 *****/
  332. struct ikmd5_struct;
  333. typedef struct iksmd5_struct iksmd5;
  334. iksmd5 *iks_md5_new(void);
  335. void iks_md5_reset(iksmd5 *md5);
  336. void iks_md5_hash(iksmd5 *md5, const unsigned char *data, size_t slen, int finish);
  337. void iks_md5_delete(iksmd5 *md5);
  338. void iks_md5_print(iksmd5 *md5, char *buf);
  339. void iks_md5_digest(iksmd5 *md5, unsigned char *digest);
  340. void iks_md5(const char *data, char *buf);
  341. /***** base64 *****/
  342. char *iks_base64_decode(const char *buf);
  343. char *iks_base64_encode(const char *buf, int len);
  344. #ifdef __cplusplus
  345. }
  346. #endif
  347. #endif /* IKSEMEL_H */