~ubuntu-branches/ubuntu/oneiric/jabberd2/oneiric-security

« back to all changes in this revision

Viewing changes to util/util.h

  • Committer: Bazaar Package Importer
  • Author(s): Nicolai Spohrer
  • Date: 2008-08-12 16:13:43 UTC
  • mfrom: (1.1.3 upstream) (0.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20080812161343-6trz3r97dtevxd17
Tags: 2.2.1-1ubuntu1
* Merge with Debian unstable (LP: #257130), remaining changes:
  - debian/control:
    + Modify Maintainer field as per spec
    + Depend on libdb4.6-dev instead of libdb4.4-dev
    + Added Conflicts and Replaces: ..., jabber for jabberd2
  - debian/rules: Added libtoolize call (jabberd2 ships with
     an older ltmain.sh version that conflicts with the
     current libtool version)
  - debian/init: create /var/run/jabber directory with correct
     permissions
* Dropped changes:
  - Debian already depends on libpq-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
#include <errno.h>
32
32
#include <assert.h>
33
33
 
 
34
#include <expat.h>
 
35
 
34
36
#ifdef HAVE_SYS_TYPES_H
35
37
# include <sys/types.h>
36
38
#endif
62
64
# include <arpa/inet.h>
63
65
#endif
64
66
 
 
67
#ifndef PATH_MAX
 
68
#ifndef MAXPATHLEN
 
69
# define PATH_MAX 512
 
70
#else
 
71
# define PATH_MAX MAXPATHLEN
 
72
#endif
 
73
#endif
 
74
 
 
75
#ifdef USE_LIBSUBST
65
76
#include "subst/subst.h"
 
77
#endif
66
78
 
67
79
#include "util/util_compat.h"
68
80
 
69
81
#ifndef INCL_UTIL_H
70
82
#define INCL_UTIL_H
71
83
 
 
84
/* jabberd2 Windows DLL */
 
85
#ifndef JABBERD2_API
 
86
# ifdef _WIN32
 
87
#  ifdef JABBERD2_EXPORTS
 
88
#   define JABBERD2_API  __declspec(dllexport)
 
89
#  else /* JABBERD2_EXPORTS */
 
90
#   define JABBERD2_API  __declspec(dllimport)
 
91
#  endif /* JABBERD2_EXPORTS */
 
92
# else /* _WIN32 */
 
93
#  define JABBERD2_API extern
 
94
# endif /* _WIN32 */
 
95
#endif /* JABBERD2_API */
 
96
 
72
97
#ifdef __cplusplus
73
98
extern "C" {
74
99
#endif
75
100
 
76
 
/* expat is available */
77
 
#define HAVE_EXPAT 1
78
 
 
79
 
 
80
101
/* crypto hashing utils */
81
102
#include "sha1.h"
82
103
#include "md5.h"
83
104
 
84
 
 
85
 
/* --------------------------------------------------------- */
86
 
/*                                                           */
87
 
/* Pool-based memory management routines                     */
88
 
/*                                                           */
89
 
/* --------------------------------------------------------- */
90
 
 
91
 
#ifdef POOL_DEBUG
92
 
/* prime number for top # of pools debugging */
93
 
#define POOL_NUM 40009
94
 
#endif
95
 
 
96
 
/** pheap - singular allocation of memory */
97
 
struct pheap
98
 
{
99
 
    void *block;
100
 
    int size, used;
101
 
};
102
 
 
103
 
/** pool_cleaner - callback type which is associated
104
 
   with a pool entry; invoked when the pool entry is 
105
 
   free'd */
106
 
typedef void (*pool_cleaner)(void *arg);
107
 
 
108
 
/** pfree - a linked list node which stores an
109
 
   allocation chunk, plus a callback */
110
 
struct pfree
111
 
{
112
 
    pool_cleaner f;
113
 
    void *arg;
114
 
    struct pheap *heap;
115
 
    struct pfree *next;
116
 
};
117
 
 
118
 
/** pool - base node for a pool. Maintains a linked list
119
 
   of pool entries (pfree) */
120
 
typedef struct pool_struct
121
 
{
122
 
    int size;
123
 
    struct pfree *cleanup;
124
 
    struct pfree *cleanup_tail;
125
 
    struct pheap *heap;
126
 
#ifdef POOL_DEBUG
127
 
    char name[8], zone[32];
128
 
    int lsize;
129
 
} _pool, *pool;
130
 
#define pool_new() _pool_new(ZONE) 
131
 
#define pool_heap(i) _pool_new_heap(i,ZONE) 
132
 
#else
133
 
} _pool, *pool;
134
 
#define pool_heap(i) _pool_new_heap(i,NULL,0) 
135
 
#define pool_new() _pool_new(NULL,0)
136
 
#endif
137
 
 
138
 
pool _pool_new(char *zone, int line); /* new pool :) */
139
 
pool _pool_new_heap(int size, char *zone, int line); /* creates a new memory pool with an initial heap size */
140
 
void *pmalloc(pool p, int size); /* wrapper around malloc, takes from the pool, cleaned up automatically */
141
 
void *pmalloc_x(pool p, int size, char c); /* Wrapper around pmalloc which prefils buffer with c */
142
 
void *pmalloco(pool p, int size); /* YAPW for zeroing the block */
143
 
char *pstrdup(pool p, const char *src); /* wrapper around strdup, gains mem from pool */
144
 
void pool_stat(int full); /* print to stderr the changed pools and reset */
145
 
char *pstrdupx(pool p, const char *src, int len); /* use given len */
146
 
void pool_cleanup(pool p, pool_cleaner f, void *arg); /* calls f(arg) before the pool is freed during cleanup */
147
 
void pool_free(pool p); /* calls the cleanup functions, frees all the data on the pool, and deletes the pool itself */
148
 
int pool_size(pool p); /* returns total bytes allocated in this pool */
149
 
 
150
 
 
151
 
 
 
105
#include <util/nad.h>
 
106
#include <util/pool.h>
 
107
#include <util/xhash.h>
152
108
 
153
109
/* --------------------------------------------------------- */
154
110
/*                                                           */
155
111
/* String management routines                                */
156
112
/*                                                           */
157
113
/** --------------------------------------------------------- */
158
 
char *j_strdup(const char *str); /* provides NULL safe strdup wrapper */
159
 
char *j_strcat(char *dest, char *txt); /* strcpy() clone */
160
 
int j_strcmp(const char *a, const char *b); /* provides NULL safe strcmp wrapper */
161
 
int j_strcasecmp(const char *a, const char *b); /* provides NULL safe strcasecmp wrapper */
162
 
int j_strncmp(const char *a, const char *b, int i); /* provides NULL safe strncmp wrapper */
163
 
int j_strncasecmp(const char *a, const char *b, int i); /* provides NULL safe strncasecmp wrapper */
164
 
int j_strlen(const char *a); /* provides NULL safe strlen wrapper */
165
 
int j_atoi(const char *a, int def); /* checks for NULL and uses default instead, convienence */
166
 
char *j_attr(const char** atts, char *attr); /* decode attr's (from expat) */
167
 
char *j_strnchr(const char *s, int c, int n); /* like strchr, but only searches n chars */
 
114
JABBERD2_API char *j_strdup(const char *str); /* provides NULL safe strdup wrapper */
 
115
JABBERD2_API char *j_strcat(char *dest, char *txt); /* strcpy() clone */
 
116
JABBERD2_API int j_strcmp(const char *a, const char *b); /* provides NULL safe strcmp wrapper */
 
117
JABBERD2_API int j_strcasecmp(const char *a, const char *b); /* provides NULL safe strcasecmp wrapper */
 
118
JABBERD2_API int j_strncmp(const char *a, const char *b, int i); /* provides NULL safe strncmp wrapper */
 
119
JABBERD2_API int j_strncasecmp(const char *a, const char *b, int i); /* provides NULL safe strncasecmp wrapper */
 
120
JABBERD2_API int j_strlen(const char *a); /* provides NULL safe strlen wrapper */
 
121
JABBERD2_API int j_atoi(const char *a, int def); /* checks for NULL and uses default instead, convienence */
 
122
JABBERD2_API char *j_attr(const char** atts, const char *attr); /* decode attr's (from expat) */
 
123
JABBERD2_API char *j_strnchr(const char *s, int c, int n); /* like strchr, but only searches n chars */
168
124
 
169
125
/** old convenience function, now in str.c */
170
 
void shahash_r(const char* str, char hashbuf[41]);
171
 
 
172
 
/* --------------------------------------------------------- */
173
 
/*                                                           */
174
 
/* Hashtable functions                                       */
175
 
/*                                                           */
176
 
/* --------------------------------------------------------- */
177
 
typedef struct xhn_struct
178
 
{
179
 
    struct xhn_struct *next;
180
 
    const char *key;
181
 
    void *val;
182
 
} *xhn, _xhn;
183
 
 
184
 
typedef struct xht_struct
185
 
{
186
 
    pool p;
187
 
    int prime;
188
 
    int dirty;
189
 
    int count;
190
 
    struct xhn_struct *zen;
191
 
    int iter_bucket;
192
 
    xhn iter_node;
193
 
} *xht, _xht;
194
 
 
195
 
xht xhash_new(int prime);
196
 
void xhash_put(xht h, const char *key, void *val);
197
 
void xhash_putx(xht h, const char *key, int len, void *val);
198
 
void *xhash_get(xht h, const char *key);
199
 
void *xhash_getx(xht h, const char *key, int len);
200
 
void xhash_zap(xht h, const char *key);
201
 
void xhash_zapx(xht h, const char *key, int len);
202
 
void xhash_free(xht h);
203
 
typedef void (*xhash_walker)(xht h, const char *key, void *val, void *arg);
204
 
void xhash_walk(xht h, xhash_walker w, void *arg);
205
 
int xhash_dirty(xht h);
206
 
int xhash_count(xht h);
207
 
pool xhash_pool(xht h);
208
 
 
209
 
/* iteration functions */
210
 
int xhash_iter_first(xht h);
211
 
int xhash_iter_next(xht h);
212
 
void xhash_iter_zap(xht h);
213
 
int xhash_iter_get(xht h, const char **key, void **val);
 
126
JABBERD2_API void shahash_r(const char* str, char hashbuf[41]);
214
127
 
215
128
/* --------------------------------------------------------- */
216
129
/*                                                           */
217
130
/* XML escaping utils                                        */
218
131
/*                                                           */
219
132
/* --------------------------------------------------------- */
220
 
char *strescape(pool p, char *buf, int len); /* Escape <>&'" chars */
221
 
char *strunescape(pool p, char *buf);
 
133
JABBERD2_API char *strescape(pool_t p, char *buf, int len); /* Escape <>&'" chars */
 
134
JABBERD2_API char *strunescape(pool_t p, char *buf);
222
135
 
223
136
 
224
137
/* --------------------------------------------------------- */
234
147
 
235
148
typedef struct spool_struct
236
149
{
237
 
    pool p;
 
150
    pool_t p;
238
151
    int len;
239
152
    struct spool_node *last;
240
153
    struct spool_node *first;
241
154
} *spool;
242
155
 
243
 
spool spool_new(pool p); /* create a string pool */
244
 
void spooler(spool s, ...); /* append all the char * args to the pool, terminate args with s again */
245
 
char *spool_print(spool s); /* return a big string */
246
 
void spool_add(spool s, char *str); /* add a single string to the pool */
247
 
void spool_escape(spool s, char *raw, int len); /* add and xml escape a single string to the pool */
248
 
char *spools(pool p, ...); /* wrap all the spooler stuff in one function, the happy fun ball! */
 
156
JABBERD2_API spool spool_new(pool_t p); /* create a string pool */
 
157
JABBERD2_API void spooler(spool s, ...); /* append all the char * args to the pool, terminate args with s again */
 
158
JABBERD2_API char *spool_print(spool s); /* return a big string */
 
159
JABBERD2_API void spool_add(spool s, char *str); /* add a single string to the pool */
 
160
JABBERD2_API void spool_escape(spool s, char *raw, int len); /* add and xml escape a single string to the pool */
 
161
JABBERD2_API char *spools(pool_t p, ...); /* wrap all the spooler stuff in one function, the happy fun ball! */
249
162
 
250
163
 
251
164
/* known namespace uri */
252
 
#define uri_STREAMS     "http://etherx.jabber.org/streams"
253
 
#define uri_CLIENT      "jabber:client"
254
 
#define uri_SERVER      "jabber:server"
255
 
#define uri_DIALBACK    "jabber:server:dialback"
256
 
#define uri_TLS         "urn:ietf:params:xml:ns:xmpp-tls"
257
 
#define uri_SASL        "urn:ietf:params:xml:ns:xmpp-sasl"
258
 
#define uri_BIND        "urn:ietf:params:xml:ns:xmpp-bind"
259
 
#define uri_XSESSION    "urn:ietf:params:xml:ns:xmpp-session"
260
 
#define uri_STREAM_ERR  "urn:ietf:params:xml:ns:xmpp-streams"
261
 
#define uri_STANZA_ERR  "urn:ietf:params:xml:ns:xmpp-stanzas"
262
 
#define uri_COMPONENT   "http://jabberd.jabberstudio.org/ns/component/1.0"
263
 
#define uri_SESSION     "http://jabberd.jabberstudio.org/ns/session/1.0"
264
 
#define uri_RESOLVER    "http://jabberd.jabberstudio.org/ns/resolver/1.0"
265
 
#define uri_XDATA       "jabber:x:data"
266
 
#define uri_XML         "http://www.w3.org/XML/1998/namespace"
267
 
 
268
 
 
269
 
/*
270
 
 * JID manipulation. Validity is checked via stringprep, using the "nodeprep",
271
 
 * "nameprep" and "resourceprep" profiles (see xmpp-core section 3).
272
 
 *
273
 
 * The provided functions are mainly for convenience. The application should
274
 
 * fill out node, domain and resource directly. When they modify these, they
275
 
 * should either call jid_expand(), or set the dirty flag.
276
 
 */
277
 
 
278
 
/** preparation cache, for speed */
279
 
typedef struct prep_cache_st {
280
 
    xht             node;
281
 
    xht             domain;
282
 
    xht             resource;
283
 
} *prep_cache_t;
284
 
 
285
 
prep_cache_t    prep_cache_new(void);
286
 
void            prep_cache_free(prep_cache_t pc);
287
 
char            *prep_cache_node_get(prep_cache_t pc, char *from);
288
 
void            prep_cache_node_set(prep_cache_t pc, char *from, char *to);
289
 
char            *prep_cache_domain_get(prep_cache_t pc, char *from);
290
 
void            prep_cache_domain_set(prep_cache_t pc, char *from, char *to);
291
 
char            *prep_cache_resource_get(prep_cache_t pc, char *from);
292
 
void            prep_cache_resource_set(prep_cache_t pc, char *from, char *to);
293
 
 
294
 
/** these sizings come from xmpp-core */
295
 
#define MAXLEN_JID_COMP  1023    /* XMPP (RFC3920) 3.1 */
296
 
#define MAXLEN_JID       3071    /* nodename (1023) + '@' + domain (1023) + '/' + resource (1023) = 3071 */
297
 
 
298
 
typedef struct jid_st {
299
 
    /* cache for prep, if any */
300
 
    prep_cache_t    pc;
301
 
 
302
 
    /* basic components of the jid */
303
 
    unsigned char   *node;
304
 
    unsigned char   *domain;
305
 
    unsigned char   *resource;
306
 
 
307
 
    /* Points to jid broken with \0s into componets. node/domain/resource point
308
 
     * into this string (or to statically allocated empty string, if they are
309
 
     * empty) */
310
 
    unsigned char   *jid_data;
311
 
    /* Valid only when jid_data != NULL. When = 0, jid_data is statically
312
 
     * allocated. Otherwise it tells length of the allocated data. Used to
313
 
     * implement jid_dup() */
314
 
    size_t          jid_data_len;
315
 
 
316
 
    /* the "user" part of the jid (sans resource) */
317
 
    unsigned char   *_user;
318
 
 
319
 
    /* the complete jid */
320
 
    unsigned char   *_full;
321
 
 
322
 
    /* application should set to 1 if user/full need regenerating */
323
 
    int             dirty;
324
 
 
325
 
    /* for lists of jids */
326
 
    struct jid_st    *next;
327
 
} *jid_t;
328
 
 
329
 
typedef enum {
330
 
    jid_NODE    = 1,
331
 
    jid_DOMAIN  = 2,
332
 
    jid_RESOURCE = 3
333
 
} jid_part_t;
334
 
 
335
 
/** JID static buffer **/
336
 
typedef char jid_static_buf[3*1024];
337
 
 
338
 
/** make a new jid, and call jid_reset() to populate it */
339
 
jid_t               jid_new(prep_cache_t pc, const unsigned char *id, int len);
340
 
 
341
 
/** Make jid to use static buffer (jid data won't be allocated dynamically, but
342
 
 * given buffer will be always used. */
343
 
void                jid_static(jid_t jid, jid_static_buf *buf);
344
 
 
345
 
/** clear and populate the jid with the given id. if id == NULL, just clears the jid to 0 */
346
 
jid_t               jid_reset(jid_t jid, const unsigned char *id, int len);
347
 
jid_t               jid_reset_components(jid_t jid, const unsigned char *node, const unsigned char *domain, const unsigned char *resource);
348
 
 
349
 
/** free the jid */
350
 
void                jid_free(jid_t jid);
351
 
 
352
 
/** do string preparation on a jid */
353
 
int                 jid_prep(jid_t jid);
354
 
 
355
 
/** fill jid's resource with a random string **/
356
 
void                jid_random_part(jid_t jid, jid_part_t part);
357
 
 
358
 
/** expands user and full if the dirty flag is set */
359
 
void                jid_expand(jid_t jid);
360
 
 
361
 
/** return the user or full jid. these call jid_expand to make sure the user and
362
 
 * full jid are up to date */
363
 
const unsigned char *jid_user(jid_t jid);
364
 
const unsigned char *jid_full(jid_t jid);
365
 
 
366
 
/** compare two user or full jids. these call jid_expand, then strcmp. returns
367
 
 * 0 if they're the same, < 0 if a < b, > 0 if a > b */
368
 
int                 jid_compare_user(jid_t a, jid_t b);
369
 
int                 jid_compare_full(jid_t a, jid_t b);
370
 
 
371
 
/** duplicate a jid */
372
 
jid_t               jid_dup(jid_t jid);
373
 
 
374
 
/** list helpers */
375
 
 
376
 
/** see if a jid is present in a list */
377
 
int                 jid_search(jid_t list, jid_t jid);
378
 
 
379
 
/** remove a jid from a list, and return the new list */
380
 
jid_t               jid_zap(jid_t list, jid_t jid);
381
 
 
382
 
/** insert of a copy of jid into list, avoiding dups */
383
 
jid_t               jid_append(jid_t list, jid_t jid);
384
 
 
 
165
#include "util/uri.h"
 
166
 
 
167
/* JID manipulation */
 
168
#include "util/jid.h"
385
169
 
386
170
/* logging */
387
171
 
399
183
 
400
184
typedef struct log_facility_st
401
185
{
402
 
    char        *facility;
 
186
    const char  *facility;
403
187
    int         number;
404
188
} log_facility_t;
405
189
 
406
 
extern log_t    log_new(log_type_t type, char *ident, char *facility);
407
 
extern void     log_write(log_t log, int level, const char *msgfmt, ...);
408
 
extern void     log_free(log_t log);
409
 
 
410
 
 
411
 
/* Not A DOM */
412
 
 
413
 
/* using nad:
414
 
 * 
415
 
 * nad is very simplistic, and requires all string handling to use a length.
416
 
 * Apps using this must be aware of the structure and access it directly for
417
 
 * most information. nads can only be built by successively using the _append_
418
 
 * functions correctly. After built, they can be modified using other functions,
419
 
 * or by direct access. To access cdata on an elem or attr, use nad->cdata +
420
 
 * nad->xxx[index].ixxx for the start, and .lxxx for len.
421
 
 *
422
 
 * Namespace support seems to work, but hasn't been thoroughly tested. in
423
 
 * particular, editing the nad after its creation might have quirks. use at
424
 
 * your own risk! Note that nad_add_namespace() brings a namespace into scope
425
 
 * for the next element added with nad_append_elem(), nad_insert_elem() or
426
 
 * nad_wrap_elem() (and by extension, any of its subelements). This is the same
427
 
 * way that Expat does things, so nad_add_namespace() can be driven from the
428
 
 * Expat's StartNamespaceDeclHandler.
429
 
 */
430
 
 
431
 
typedef struct nad_st **nad_cache_t;
432
 
 
433
 
struct nad_elem_st
434
 
{
435
 
    int parent;
436
 
    int iname, lname;
437
 
    int icdata, lcdata; /* cdata within this elem (up to first child) */
438
 
    int itail, ltail; /* cdata after this elem */
439
 
    int attr;
440
 
    int ns;
441
 
    int my_ns;
442
 
    int depth;
443
 
};
444
 
 
445
 
struct nad_attr_st
446
 
{
447
 
    int iname, lname;
448
 
    int ival, lval;
449
 
    int my_ns;
450
 
    int next;
451
 
};
452
 
 
453
 
struct nad_ns_st
454
 
{
455
 
    int iuri, luri;
456
 
    int iprefix, lprefix;
457
 
    int next;
458
 
};
459
 
 
460
 
typedef struct nad_st
461
 
{
462
 
    nad_cache_t cache;   /* he who gave us life */
463
 
    struct nad_elem_st *elems;
464
 
    struct nad_attr_st *attrs;
465
 
    struct nad_ns_st *nss;
466
 
    char *cdata;
467
 
    int *depths; /* for tracking the last elem at a depth */
468
 
    int elen, alen, nlen, clen, dlen;
469
 
    int ecur, acur, ncur, ccur;
470
 
    int scope; /* currently scoped namespaces, get attached to the next element */
471
 
    struct nad_st *next; /* for keeping a list of nads */
472
 
} *nad_t;
473
 
 
474
 
/** create a new cache for nads */
475
 
nad_cache_t nad_cache_new(void);
476
 
 
477
 
/** free the cache */
478
 
void nad_cache_free(nad_cache_t cache);
479
 
 
480
 
/** create a new nad */
481
 
nad_t nad_new(nad_cache_t cache);
482
 
 
483
 
/** copy a nad */
484
 
nad_t nad_copy(nad_t nad);
485
 
 
486
 
/** free that nad */
487
 
void nad_free(nad_t nad);
488
 
 
489
 
/** find the next element with this name/depth */
490
 
/** 0 for siblings, 1 for children and so on */
491
 
int nad_find_elem(nad_t nad, int elem, int ns, const char *name, int depth);
492
 
 
493
 
/** find the first matching attribute (and optionally value) */
494
 
int nad_find_attr(nad_t nad, int elem, int ns, const char *name, const char *val);
495
 
 
496
 
/** find the first matching namespace (and optionally prefix) */
497
 
int nad_find_namespace(nad_t nad, int elem, const char *uri, const char *prefix);
498
 
 
499
 
/** find a namespace in scope (and optionally prefix) */
500
 
int nad_find_scoped_namespace(nad_t nad, const char *uri, const char *prefix);
501
 
 
502
 
/** reset or store the given attribute */
503
 
void nad_set_attr(nad_t nad, int elem, int ns, const char *name, const char *val, int vallen);
504
 
 
505
 
/** insert and return a new element as a child of this one */
506
 
int nad_insert_elem(nad_t nad, int elem, int ns, const char *name, const char *cdata);
507
 
 
508
 
/** wrap an element with another element */
509
 
void nad_wrap_elem(nad_t nad, int elem, int ns, const char *name);
510
 
 
511
 
/** append and return a new element */
512
 
int nad_append_elem(nad_t nad, int ns, const char *name, int depth);
513
 
 
514
 
/** append attribs to the last element */
515
 
int nad_append_attr(nad_t nad, int ns, const char *name, const char *val);
516
 
 
517
 
/** append more cdata to the last element */
518
 
void nad_append_cdata(nad_t nad, const char *cdata, int len, int depth);
519
 
 
520
 
/** add a namespace to the next element (ie, called when the namespace comes into scope) */
521
 
int nad_add_namespace(nad_t nad, const char *uri, const char *prefix);
522
 
 
523
 
/** declare a namespace on an already existing element */
524
 
int nad_append_namespace(nad_t nad, int elem, const char *uri, const char *prefix);
525
 
 
526
 
/** create a string representation of the given element (and children), point references to it */
527
 
void nad_print(nad_t nad, int elem, char **xml, int *len);
528
 
 
529
 
/** serialize and deserialize a nad */
530
 
void nad_serialize(nad_t nad, char **buf, int *len);
531
 
nad_t nad_deserialize(nad_cache_t cache, const char *buf);
532
 
 
533
 
#ifdef HAVE_EXPAT
534
 
/** create a nad from raw xml */
535
 
nad_t nad_parse(nad_cache_t cache, const char *buf, int len);
536
 
#endif
537
 
 
538
 
/* these are some helpful macros */
539
 
#define NAD_ENAME(N,E) (N->cdata + N->elems[E].iname)
540
 
#define NAD_ENAME_L(N,E) (N->elems[E].lname)
541
 
#define NAD_CDATA(N,E) (N->cdata + N->elems[E].icdata)
542
 
#define NAD_CDATA_L(N,E) (N->elems[E].lcdata)
543
 
#define NAD_ANAME(N,A) (N->cdata + N->attrs[A].iname)
544
 
#define NAD_ANAME_L(N,A) (N->attrs[A].lname)
545
 
#define NAD_AVAL(N,A) (N->cdata + N->attrs[A].ival)
546
 
#define NAD_AVAL_L(N,A) (N->attrs[A].lval)
547
 
#define NAD_NURI(N,NS) (N->cdata + N->nss[NS].iuri)
548
 
#define NAD_NURI_L(N,NS) (N->nss[NS].luri)
549
 
#define NAD_NPREFIX(N,NS) (N->cdata + N->nss[NS].iprefix)
550
 
#define NAD_NPREFIX_L(N,NS) (N->nss[NS].lprefix)
551
 
 
552
 
#define NAD_ENS(N,E) (N->elems[E].my_ns)
553
 
#define NAD_ANS(N,A) (N->attrs[A].my_ns)
554
 
 
 
190
JABBERD2_API log_t    log_new(log_type_t type, const char *ident, const char *facility);
 
191
JABBERD2_API void     log_write(log_t log, int level, const char *msgfmt, ...);
 
192
JABBERD2_API void     log_free(log_t log);
555
193
 
556
194
/* config files */
557
195
typedef struct config_elem_st   *config_elem_t;
573
211
    char                ***attrs;
574
212
};
575
213
 
576
 
extern config_t         config_new(void);
577
 
extern int              config_load(config_t c, char *file);
578
 
extern config_elem_t    config_get(config_t c, char *key);
579
 
extern char             *config_get_one(config_t c, char *key, int num);
580
 
extern int              config_count(config_t c, char *key);
581
 
extern char             *config_get_attr(config_t c, char *key, int num, char *attr);
582
 
extern void             config_free(config_t);
 
214
JABBERD2_API config_t         config_new(void);
 
215
JABBERD2_API int              config_load(config_t c, const char *file);
 
216
JABBERD2_API config_elem_t    config_get(config_t c, const char *key);
 
217
JABBERD2_API char             *config_get_one(config_t c, const char *key, int num);
 
218
JABBERD2_API int              config_count(config_t c, const char *key);
 
219
JABBERD2_API char             *config_get_attr(config_t c, const char *key, int num, const char *attr);
 
220
JABBERD2_API void             config_free(config_t);
583
221
 
584
222
 
585
223
/*
603
241
    int             ndeny;
604
242
} *access_t;
605
243
 
606
 
access_t    access_new(int order);
607
 
void        access_free(access_t access);
608
 
int         access_allow(access_t access, char *ip, char *mask);
609
 
int         access_deny(access_t access, char *ip, char *mask);
610
 
int         access_check(access_t access, char *ip);
 
244
JABBERD2_API access_t    access_new(int order);
 
245
JABBERD2_API void        access_free(access_t access);
 
246
JABBERD2_API int         access_allow(access_t access, char *ip, char *mask);
 
247
JABBERD2_API int         access_deny(access_t access, char *ip, char *mask);
 
248
JABBERD2_API int         access_check(access_t access, char *ip);
611
249
 
612
250
 
613
251
/*
626
264
    time_t          bad;        /* time we went bad, or 0 if we're not */
627
265
} *rate_t;
628
266
 
629
 
rate_t      rate_new(int total, int seconds, int wait);
630
 
void        rate_free(rate_t rt);
631
 
void        rate_reset(rate_t rt);
632
 
void        rate_add(rate_t rt, int count);
633
 
int         rate_left(rate_t rt);
634
 
int         rate_check(rate_t rt);          /* 1 == good, 0 == bad */
 
267
JABBERD2_API rate_t      rate_new(int total, int seconds, int wait);
 
268
JABBERD2_API void        rate_free(rate_t rt);
 
269
JABBERD2_API void        rate_reset(rate_t rt);
 
270
 
 
271
/**
 
272
 * Add a number of events to the counter.  This takes care of moving
 
273
 * the sliding window, if we've moved outside the previous window.
 
274
 */
 
275
JABBERD2_API void        rate_add(rate_t rt, int count);
 
276
 
 
277
/**
 
278
 * @return The amount of events we have left before we hit the rate
 
279
 *         limit.  This could be number of bytes, or number of
 
280
 *         connection attempts, etc.
 
281
 */
 
282
JABBERD2_API int         rate_left(rate_t rt);
 
283
 
 
284
/**
 
285
 * @return 1 if we're under the rate limit and everything is fine or
 
286
 *         0 if the rate limit has been exceeded and we should throttle
 
287
 *         something.
 
288
 */
 
289
JABBERD2_API int         rate_check(rate_t rt);
635
290
 
636
291
/*
637
292
 * helpers for ip addresses
643
298
 * serialisation helper functions
644
299
 */
645
300
 
646
 
int         ser_string_get(char **dest, int *source, const char *buf, int len);
647
 
int         ser_int_get(int *dest, int *source, const char *buf, int len);
648
 
void        ser_string_set(char *source, int *dest, char **buf, int *len);
649
 
void        ser_int_set(int source, int *dest, char **buf, int *len);
 
301
JABBERD2_API int         ser_string_get(char **dest, int *source, const char *buf, int len);
 
302
JABBERD2_API int         ser_int_get(int *dest, int *source, const char *buf, int len);
 
303
JABBERD2_API void        ser_string_set(char *source, int *dest, char **buf, int *len);
 
304
JABBERD2_API void        ser_int_set(int source, int *dest, char **buf, int *len);
650
305
 
651
306
/*
652
307
 * priority queues
663
318
};
664
319
 
665
320
typedef struct _jqueue_st {
666
 
    pool            p;
 
321
    pool_t          p;
667
322
    _jqueue_node_t  cache;
668
323
 
669
324
    _jqueue_node_t  front;
670
325
    _jqueue_node_t  back;
671
326
 
672
327
    int             size;
 
328
    char            *key;
 
329
    time_t          init_time;
673
330
} *jqueue_t;
674
331
 
675
 
jqueue_t    jqueue_new(void);
676
 
void        jqueue_free(jqueue_t q);
677
 
void        jqueue_push(jqueue_t q, void *data, int pri);
678
 
void        *jqueue_pull(jqueue_t q);
679
 
int         jqueue_size(jqueue_t q);
 
332
JABBERD2_API jqueue_t    jqueue_new(void);
 
333
JABBERD2_API void        jqueue_free(jqueue_t q);
 
334
JABBERD2_API void        jqueue_push(jqueue_t q, void *data, int pri);
 
335
JABBERD2_API void        *jqueue_pull(jqueue_t q);
 
336
JABBERD2_API int         jqueue_size(jqueue_t q);
 
337
JABBERD2_API time_t      jqueue_age(jqueue_t q);
680
338
 
681
339
 
682
340
/* ISO 8601 / JEP-0082 date/time manipulation */
687
345
    dt_LEGACY   = 4
688
346
} datetime_t;
689
347
 
690
 
time_t  datetime_in(char *date);
691
 
void    datetime_out(time_t t, datetime_t type, char *date, int datelen);
 
348
JABBERD2_API time_t  datetime_in(char *date);
 
349
JABBERD2_API void    datetime_out(time_t t, datetime_t type, char *date, int datelen);
692
350
 
693
351
 
694
352
/* base64 functions */
695
 
extern int ap_base64decode_len(const char *bufcoded, int buflen);
696
 
extern int ap_base64decode(char *bufplain, const char *bufcoded, int buflen);
697
 
extern int ap_base64decode_binary(unsigned char *bufplain, const char *bufcoded, int buflen);
698
 
extern int ap_base64encode_len(int len);
699
 
extern int ap_base64encode(char *encoded, const char *string, int len);
700
 
extern int ap_base64encode_binary(char *encoded, const unsigned char *string, int len);
 
353
JABBERD2_API int apr_base64_decode_len(const char *bufcoded, int buflen);
 
354
JABBERD2_API int apr_base64_decode(char *bufplain, const char *bufcoded, int buflen);
 
355
JABBERD2_API int apr_base64_encode_len(int len);
 
356
JABBERD2_API int apr_base64_encode(char *encoded, const char *string, int len);
701
357
 
702
358
/* convenience, result string must be free()'d by caller */
703
 
extern char *b64_encode(char *buf, int len);
704
 
extern char *b64_decode(char *buf);
 
359
JABBERD2_API char *b64_encode(char *buf, int len);
 
360
JABBERD2_API char *b64_decode(char *buf);
705
361
 
706
362
 
707
363
/* stanza manipulation */
727
383
#define stanza_err_UNDEFINED_CONDITION      (119)
728
384
#define stanza_err_UNEXPECTED_REQUEST       (120)
729
385
#define stanza_err_OLD_UNAUTH               (121)
730
 
#define stanza_err_LAST                     (122)
731
 
 
732
 
extern nad_t stanza_error(nad_t nad, int elem, int err);
733
 
extern nad_t stanza_tofrom(nad_t nad, int elem);
 
386
#define stanza_err_UNKNOWN_SENDER           (122)
 
387
#define stanza_err_LAST                     (123)
 
388
 
 
389
JABBERD2_API nad_t stanza_error(nad_t nad, int elem, int err);
 
390
JABBERD2_API nad_t stanza_tofrom(nad_t nad, int elem);
 
391
 
 
392
typedef struct _stanza_error_st {
 
393
    const char  *name;
 
394
    const char  *type;
 
395
    const char  *code;
 
396
} *stanza_error_t;
 
397
 
 
398
JABBERD2_API struct _stanza_error_st _stanza_errors[];
734
399
 
735
400
 
736
401
/* hex conversion utils */
737
 
void hex_from_raw(char *in, int inlen, char *out);
738
 
int hex_to_raw(char *in, int inlen, char *out);
 
402
JABBERD2_API void hex_from_raw(char *in, int inlen, char *out);
 
403
JABBERD2_API int hex_to_raw(char *in, int inlen, char *out);
739
404
 
740
405
 
741
406
/* xdata in a seperate file */
743
408
 
744
409
 
745
410
/* debug logging */
746
 
int get_debug_flag(void);
747
 
void set_debug_flag(int v);
748
 
void debug_log(char *file, int line, const char *msgfmt, ...);
 
411
JABBERD2_API int get_debug_flag(void);
 
412
JABBERD2_API void set_debug_flag(int v);
 
413
JABBERD2_API void debug_log(const char *file, int line, const char *msgfmt, ...);
749
414
#define ZONE __FILE__,__LINE__
750
415
#define MAX_DEBUG 8192
751
416
 
758
423
 
759
424
/* Portable signal function */
760
425
typedef void jsighandler_t(int);
761
 
jsighandler_t* jabber_signal(int signo,  jsighandler_t *func);
 
426
JABBERD2_API jsighandler_t* jabber_signal(int signo,  jsighandler_t *func);
 
427
 
 
428
#ifdef _WIN32
 
429
/* Windows service wrapper function */
 
430
typedef int (jmainhandler_t)(int argc, char** argv);
 
431
JABBERD2_API int jabber_wrap_service(int argc, char** argv, jmainhandler_t *wrapper, LPCTSTR name, LPCTSTR display, LPCTSTR description, LPCTSTR depends);
 
432
#define JABBER_MAIN(name, display, description, depends) jabber_main(int argc, char** argv); \
 
433
                    main(int argc, char** argv) { return jabber_wrap_service(argc, argv, jabber_main, name, display, description, depends); } \
 
434
                    jabber_main(int argc, char** argv)
 
435
#else /* _WIN32 */
 
436
#define JABBER_MAIN(name, display, description, depends) int main(int argc, char** argv)
 
437
#endif /* _WIN32 */
762
438
 
763
439
#ifdef __cplusplus
764
440
}