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