added mutex around XMPP calls
[public/netxms.git] / src / libstrophe / common.h
1 /* common.h
2 ** strophe XMPP client library -- internal common structures
3 **
4 ** Copyright (C) 2005-2009 Collecta, Inc.
5 **
6 ** This software is provided AS-IS with no warranty, either express or
7 ** implied.
8 **
9 ** This software is distributed under license and may not be copied,
10 ** modified or distributed except as expressly authorized under the
11 ** terms of the license contained in the file LICENSE.txt in this
12 ** distribution.
13 */
14
15 /** @file
16 * Internally used functions and structures.
17 */
18
19 #ifndef __LIBSTROPHE_COMMON_H__
20 #define __LIBSTROPHE_COMMON_H__
21
22 #include <stdio.h>
23 #include <stdarg.h>
24 #ifndef _WIN32
25 #include <stdint.h>
26 #endif
27
28
29 #include "strophe.h"
30 #include "sock.h"
31 #include "tls.h"
32 #include "hash.h"
33 #include "util.h"
34 #include "parser.h"
35
36 /** run-time context **/
37
38 typedef struct _xmpp_connlist_t {
39 xmpp_conn_t *conn;
40 struct _xmpp_connlist_t *next;
41 } xmpp_connlist_t;
42
43 struct _xmpp_ctx_t {
44 const xmpp_mem_t *mem;
45 const xmpp_log_t *log;
46
47 xmpp_loop_status_t loop_status;
48 xmpp_connlist_t *connlist;
49 };
50
51
52 /* convenience functions for accessing the context */
53 void *xmpp_alloc(const xmpp_ctx_t * const ctx, const size_t size);
54 void *xmpp_realloc(const xmpp_ctx_t * const ctx, void *p,
55 const size_t size);
56 char *xmpp_strdup(const xmpp_ctx_t * const ctx, const char * const s);
57
58 void xmpp_log(const xmpp_ctx_t * const ctx,
59 const xmpp_log_level_t level,
60 const char * const area,
61 const char * const fmt,
62 va_list ap);
63
64 /* wrappers for xmpp_log at specific levels */
65 void xmpp_error(const xmpp_ctx_t * const ctx,
66 const char * const area,
67 const char * const fmt,
68 ...);
69 void xmpp_warn(const xmpp_ctx_t * const ctx,
70 const char * const area,
71 const char * const fmt,
72 ...);
73 void xmpp_info(const xmpp_ctx_t * const ctx,
74 const char * const area,
75 const char * const fmt,
76 ...);
77 void xmpp_debug(const xmpp_ctx_t * const ctx,
78 const char * const area,
79 const char * const fmt,
80 ...);
81
82 /** jid */
83 /* these return new strings that must be xmpp_free()'d */
84 char *xmpp_jid_new(xmpp_ctx_t *ctx, const char *node,
85 const char *domain,
86 const char *resource);
87 char *xmpp_jid_bare(xmpp_ctx_t *ctx, const char *jid);
88 char *xmpp_jid_node(xmpp_ctx_t *ctx, const char *jid);
89 char *xmpp_jid_domain(xmpp_ctx_t *ctx, const char *jid);
90 char *xmpp_jid_resource(xmpp_ctx_t *ctx, const char *jid);
91
92
93 /** connection **/
94
95 /* opaque connection object */
96 typedef enum {
97 XMPP_STATE_DISCONNECTED,
98 XMPP_STATE_CONNECTING,
99 XMPP_STATE_CONNECTED
100 } xmpp_conn_state_t;
101
102 typedef struct _xmpp_send_queue_t xmpp_send_queue_t;
103 struct _xmpp_send_queue_t {
104 char *data;
105 size_t len;
106 size_t written;
107
108 xmpp_send_queue_t *next;
109 };
110
111 typedef struct _xmpp_handlist_t xmpp_handlist_t;
112 struct _xmpp_handlist_t {
113 /* common members */
114 int user_handler;
115 void *handler;
116 void *userdata;
117 int enabled; /* handlers are added disabled and enabled after the
118 * handler chain is processed to prevent stanzas from
119 * getting processed by newly added handlers */
120 xmpp_handlist_t *next;
121
122 union {
123 /* timed handlers */
124 struct {
125 unsigned long period;
126 uint64_t last_stamp;
127 } timed;
128 /* id handlers */
129 struct {
130 char *id;
131 } id;
132 /* normal handlers */
133 struct {
134 char *ns;
135 char *name;
136 char *type;
137 } normal;
138 } data;
139 };
140
141 #define SASL_MASK_PLAIN 0x01
142 #define SASL_MASK_DIGESTMD5 0x02
143 #define SASL_MASK_ANONYMOUS 0x04
144
145 typedef void (*xmpp_open_handler)(xmpp_conn_t * const conn);
146
147 struct _xmpp_conn_t {
148 unsigned int ref;
149 xmpp_ctx_t *ctx;
150 xmpp_conn_type_t type;
151
152 xmpp_conn_state_t state;
153 uint64_t timeout_stamp;
154 int error;
155 xmpp_stream_error_t *stream_error;
156 sock_t sock;
157 tls_t *tls;
158
159 int tls_support;
160 int tls_disabled;
161 int tls_failed; /* set when tls fails, so we don't try again */
162 int sasl_support; /* if true, field is a bitfield of supported
163 mechanisms */
164 int secured; /* set when stream is secured with TLS */
165
166 /* if server returns <bind/> or <session/> we must do them */
167 int bind_required;
168 int session_required;
169
170 char *lang;
171 char *domain;
172 char *connectdomain;
173 char *connectport;
174 char *jid;
175 char *pass;
176 char *bound_jid;
177 char *stream_id;
178
179 /* send queue and parameters */
180 int blocking_send;
181 int send_queue_max;
182 int send_queue_len;
183 xmpp_send_queue_t *send_queue_head;
184 xmpp_send_queue_t *send_queue_tail;
185
186 /* xml parser */
187 int reset_parser;
188 parser_t *parser;
189
190 /* timeouts */
191 unsigned int connect_timeout;
192
193 /* event handlers */
194
195 /* stream open handler */
196 xmpp_open_handler open_handler;
197
198 /* user handlers only get called after authentication */
199 int authenticated;
200
201 /* connection events handler */
202 xmpp_conn_handler conn_handler;
203 void *userdata;
204
205 /* other handlers */
206 xmpp_handlist_t *timed_handlers;
207 hash_t *id_handlers;
208 xmpp_handlist_t *handlers;
209 };
210
211 void conn_disconnect(xmpp_conn_t * const conn);
212 void conn_disconnect_clean(xmpp_conn_t * const conn);
213 void conn_open_stream(xmpp_conn_t * const conn);
214 void conn_prepare_reset(xmpp_conn_t * const conn, xmpp_open_handler handler);
215 void conn_parser_reset(xmpp_conn_t * const conn);
216
217
218 typedef enum {
219 XMPP_STANZA_UNKNOWN,
220 XMPP_STANZA_TEXT,
221 XMPP_STANZA_TAG
222 } xmpp_stanza_type_t;
223
224 struct _xmpp_stanza_t {
225 int ref;
226 xmpp_ctx_t *ctx;
227
228 xmpp_stanza_type_t type;
229
230 xmpp_stanza_t *prev;
231 xmpp_stanza_t *next;
232 xmpp_stanza_t *children;
233 xmpp_stanza_t *parent;
234
235 char *data;
236
237 hash_t *attributes;
238 };
239
240 /* handler management */
241 void handler_fire_stanza(xmpp_conn_t * const conn,
242 xmpp_stanza_t * const stanza);
243 uint64_t handler_fire_timed(xmpp_ctx_t * const ctx);
244 void handler_reset_timed(xmpp_conn_t *conn, int user_only);
245 void handler_add_timed(xmpp_conn_t * const conn,
246 xmpp_timed_handler handler,
247 const unsigned long period,
248 void * const userdata);
249 void handler_add_id(xmpp_conn_t * const conn,
250 xmpp_handler handler,
251 const char * const id,
252 void * const userdata);
253 void handler_add(xmpp_conn_t * const conn,
254 xmpp_handler handler,
255 const char * const ns,
256 const char * const name,
257 const char * const type,
258 void * const userdata);
259
260 /* utility functions */
261 void disconnect_mem_error(xmpp_conn_t * const conn);
262
263 /* auth functions */
264 void auth_handle_open(xmpp_conn_t * const conn);
265
266 /* replacement snprintf and vsnprintf */
267 int xmpp_snprintf (char *str, size_t count, const char *fmt, ...);
268 int xmpp_vsnprintf (char *str, size_t count, const char *fmt, va_list arg);
269
270 #endif /* __LIBSTROPHE_COMMON_H__ */