source: protocols/jabber/lib.h @ 0a3c243

Last change on this file since 0a3c243 was b7d3cc34, checked in by Wilmer van der Gaast <wilmer@…>, at 2005-11-06T18:23:18Z

Initial repository (0.99 release tree)

  • Property mode set to 100644
File size: 11.1 KB
Line 
1
2#include <string.h>
3#include <stdlib.h>
4#include <sys/types.h>
5#include <stdio.h>
6#include <setjmp.h>
7#include <sys/stat.h>
8#include <fcntl.h>
9#include <errno.h>
10#include <signal.h>
11#include <stdarg.h>
12#include <ctype.h>
13#include <time.h>
14
15#include "xmlparse.h"
16
17int j_strcmp(const char *a, const char *b);
18
19/*
20**  Arrange to use either varargs or stdargs
21*/
22
23#define MAXSHORTSTR     203             /* max short string length */
24#define QUAD_T  unsigned long long
25
26#if defined(__STDC__) || defined(_WIN32)
27
28#include <stdarg.h>
29
30# define VA_LOCAL_DECL  va_list ap;
31# define VA_START(f)    va_start(ap, f)
32# define VA_END         va_end(ap)
33
34#else /* __STDC__ */
35
36# include <varargs.h>
37
38# define VA_LOCAL_DECL  va_list ap;
39# define VA_START(f)    va_start(ap)
40# define VA_END         va_end(ap)
41
42#endif /* __STDC__ */
43
44
45#ifndef INCL_LIB_H
46#define INCL_LIB_H
47
48#ifdef __cplusplus
49extern "C" {
50#endif
51
52/* --------------------------------------------------------- */
53/*                                                           */
54/* Pool-based memory management routines                     */
55/*                                                           */
56/* --------------------------------------------------------- */
57
58#undef POOL_DEBUG
59/*
60 flip these, this should be a prime number for top # of pools debugging
61#define POOL_DEBUG 40009
62*/
63
64/* pheap - singular allocation of memory */
65struct pheap
66{
67    void *block;
68    int size, used;
69};
70
71/* pool_cleaner - callback type which is associated
72   with a pool entry; invoked when the pool entry is
73   free'd */
74typedef void (*pool_cleaner)(void *arg);
75
76/* pfree - a linked list node which stores an
77   allocation chunk, plus a callback */
78struct pfree
79{
80    pool_cleaner f;
81    void *arg;
82    struct pheap *heap;
83    struct pfree *next;
84};
85
86/* pool - base node for a pool. Maintains a linked list
87   of pool entries (pfree) */
88typedef struct pool_struct
89{
90    int size;
91    struct pfree *cleanup;
92    struct pheap *heap;
93#ifdef POOL_DEBUG
94    char name[8], zone[32];
95    int lsize;
96} _pool, *pool;
97#define pool_new() _pool_new(ZONE)
98#define pool_heap(i) _pool_new_heap(i,ZONE)
99#else
100} _pool, *pool;
101#define pool_heap(i) _pool_new_heap(i,NULL)
102#define pool_new() _pool_new(NULL)
103#endif
104
105pool _pool_new(char *zone); /* new pool :) */
106pool _pool_new_heap(int size, char *zone); /* creates a new memory pool with an initial heap size */
107void *pmalloc(pool p, int size); /* wrapper around malloc, takes from the pool, cleaned up automatically */
108void *pmalloc_x(pool p, int size, char c); /* Wrapper around pmalloc which prefils buffer with c */
109void *pmalloco(pool p, int size); /* YAPW for zeroing the block */
110char *pstrdup(pool p, const char *src); /* wrapper around strdup, gains mem from pool */
111void pool_stat(int full); /* print to stderr the changed pools and reset */
112void pool_cleanup(pool p, pool_cleaner f, void *arg); /* calls f(arg) before the pool is freed during cleanup */
113void pool_free(pool p); /* calls the cleanup functions, frees all the data on the pool, and deletes the pool itself */
114
115
116
117
118/* --------------------------------------------------------- */
119/*                                                           */
120/* Socket helper stuff                                       */
121/*                                                           */
122/* --------------------------------------------------------- */
123#ifndef MAXHOSTNAMELEN
124#define MAXHOSTNAMELEN 64
125#endif
126
127#define NETSOCKET_SERVER 0
128#define NETSOCKET_CLIENT 1
129#define NETSOCKET_UDP 2
130
131#ifndef WIN32
132int make_netsocket(u_short port, char *host, int type);
133struct in_addr *make_addr(char *host);
134int set_fd_close_on_exec(int fd, int flag);
135#endif
136
137
138/* --------------------------------------------------------- */
139/*                                                           */
140/* Hashtable functions                                       */
141/*                                                           */
142/* --------------------------------------------------------- */
143typedef struct xhn_struct
144{
145    struct xhn_struct *next;
146    const char *key;
147    void *val;
148} *xhn, _xhn;
149
150char *strescape(pool p, char *);
151
152
153/* --------------------------------------------------------- */
154/*                                                           */
155/* String pools (spool) functions                            */
156/*                                                           */
157/* --------------------------------------------------------- */
158struct spool_node
159{
160    char *c;
161    struct spool_node *next;
162};
163
164typedef struct spool_struct
165{
166    pool p;
167    int len;
168    struct spool_node *last;
169    struct spool_node *first;
170} *spool;
171
172spool spool_new(pool p); /* create a string pool */
173void spooler(spool s, ...); /* append all the char * args to the pool, terminate args with s again */
174char *spool_print(spool s); /* return a big string */
175void spool_add(spool s, char *str); /* add a single char to the pool */
176
177
178/* --------------------------------------------------------- */
179/*                                                           */
180/* xmlnodes - Document Object Model                          */
181/*                                                           */
182/* --------------------------------------------------------- */
183#define NTYPE_TAG    0
184#define NTYPE_ATTRIB 1
185#define NTYPE_CDATA  2
186
187#define NTYPE_LAST   2
188#define NTYPE_UNDEF  -1
189
190/* --------------------------------------------------------------------------
191   Node structure. Do not use directly! Always use accessor macros
192   and methods!
193   -------------------------------------------------------------------------- */
194typedef struct xmlnode_t
195{
196     char*               name;
197     unsigned short      type;
198     char*               data;
199     int                 data_sz;
200     int                 complete;
201     pool               p;
202     struct xmlnode_t*  parent;
203     struct xmlnode_t*  firstchild; 
204     struct xmlnode_t*  lastchild;
205     struct xmlnode_t*  prev; 
206     struct xmlnode_t*  next;
207     struct xmlnode_t*  firstattrib;
208     struct xmlnode_t*  lastattrib;
209} _xmlnode, *xmlnode;
210
211/* Node creation routines */
212xmlnode  xmlnode_wrap(xmlnode x,const char* wrapper);
213xmlnode  xmlnode_new_tag(const char* name);
214xmlnode  xmlnode_new_tag_pool(pool p, const char* name);
215xmlnode  xmlnode_insert_tag(xmlnode parent, const char* name); 
216xmlnode  xmlnode_insert_cdata(xmlnode parent, const char* CDATA, unsigned int size);
217xmlnode  xmlnode_insert_tag_node(xmlnode parent, xmlnode node);
218xmlnode  xmlnode_str(char *str, int len);
219xmlnode  xmlnode_dup(xmlnode x); /* duplicate x */
220xmlnode  xmlnode_dup_pool(pool p, xmlnode x);
221
222/* Node Memory Pool */
223pool xmlnode_pool(xmlnode node);
224
225/* Node editing */
226void xmlnode_hide(xmlnode child);
227void xmlnode_hide_attrib(xmlnode parent, const char *name);
228
229/* Node deletion routine, also frees the node pool! */
230void xmlnode_free(xmlnode node);
231
232/* Locates a child tag by name and returns it */
233xmlnode  xmlnode_get_tag(xmlnode parent, const char* name);
234char* xmlnode_get_tag_data(xmlnode parent, const char* name);
235
236/* Attribute accessors */
237void     xmlnode_put_attrib(xmlnode owner, const char* name, const char* value);
238char*    xmlnode_get_attrib(xmlnode owner, const char* name);
239void     xmlnode_put_expat_attribs(xmlnode owner, const char** atts);
240
241/* Bastard am I, but these are fun for internal use ;-) */
242void     xmlnode_put_vattrib(xmlnode owner, const char* name, void *value);
243void*    xmlnode_get_vattrib(xmlnode owner, const char* name);
244
245/* Node traversal routines */
246xmlnode  xmlnode_get_firstchild(xmlnode parent);
247xmlnode  xmlnode_get_lastchild(xmlnode parent);
248xmlnode  xmlnode_get_nextsibling(xmlnode sibling);
249xmlnode  xmlnode_get_prevsibling(xmlnode sibling);
250xmlnode  xmlnode_get_parent(xmlnode node);
251
252/* Node information routines */
253char*    xmlnode_get_name(xmlnode node);
254char*    xmlnode_get_data(xmlnode node);
255
256int      xmlnode_has_children(xmlnode node);
257
258/* Node-to-string translation */
259char*    xmlnode2str(xmlnode node);
260
261/********** END OLD libxode.h BEGIN OLD jabber.h *************/
262
263
264// #define KARMA_DEBUG
265// default to disable karma
266#define KARMA_READ_MAX(k) (abs(k)*100) /* how much you are allowed to read off the sock */
267#define KARMA_INIT 5   /* internal "init" value */
268#define KARMA_HEARTBEAT 2 /* seconds to register for heartbeat */
269#define KARMA_MAX 10     /* total max karma you can have */
270#define KARMA_INC 1      /* how much to increment every KARMA_HEARTBEAT seconds */
271#define KARMA_DEC 0      /* how much to penalize for reading KARMA_READ_MAX in
272                            KARMA_HEARTBEAT seconds */
273#define KARMA_PENALTY -5 /* where you go when you hit 0 karma */
274#define KARMA_RESTORE 5  /* where you go when you payed your penelty or INIT */
275#define KARMA_RESETMETER 0 /* Reset byte meter on restore default is falst */
276
277struct karma
278{
279    int init; /* struct initialized */
280    int reset_meter; /* reset the byte meter on restore */
281    int val; /* current karma value */
282    long bytes; /* total bytes read (in that time period) */
283    int max;  /* max karma you can have */
284    int inc,dec; /* how much to increment/decrement */
285    int penalty,restore; /* what penalty (<0) or restore (>0) */
286    time_t last_update; /* time this was last incremented */
287};
288
289struct karma *karma_new(pool p); /* creates a new karma object, with default values */
290void karma_copy(struct karma *new, struct karma *old); /* makes a copy of old in new */
291void karma_increment(struct karma *k);          /* inteligently increments karma */
292void karma_decrement(struct karma *k, long bytes_read); /* inteligently decrements karma */
293int karma_check(struct karma *k,long bytes_read); /* checks to see if we have good karma */
294
295
296
297/* --------------------------------------------------------- */
298/*                                                           */
299/* Namespace constants                                       */
300/*                                                           */
301/* --------------------------------------------------------- */
302#define NSCHECK(x,n) (j_strcmp(xmlnode_get_attrib(x,"xmlns"),n) == 0)
303
304#define NS_CLIENT    "jabber:client"
305#define NS_SERVER    "jabber:server"
306#define NS_AUTH      "jabber:iq:auth"
307#define NS_REGISTER  "jabber:iq:register"
308#define NS_ROSTER    "jabber:iq:roster"
309#define NS_OFFLINE   "jabber:x:offline"
310#define NS_AGENT     "jabber:iq:agent"
311#define NS_AGENTS    "jabber:iq:agents"
312#define NS_DELAY     "jabber:x:delay"
313#define NS_VERSION   "jabber:iq:version"
314#define NS_TIME      "jabber:iq:time"
315#define NS_VCARD     "vcard-temp"
316#define NS_PRIVATE   "jabber:iq:private"
317#define NS_SEARCH    "jabber:iq:search"
318#define NS_OOB       "jabber:iq:oob"
319#define NS_XOOB      "jabber:x:oob"
320#define NS_ADMIN     "jabber:iq:admin"
321#define NS_FILTER    "jabber:iq:filter"
322#define NS_AUTH_0K   "jabber:iq:auth:0k"
323#define NS_BROWSE    "jabber:iq:browse"
324#define NS_EVENT     "jabber:x:event"
325#define NS_CONFERENCE "jabber:iq:conference"
326#define NS_SIGNED    "jabber:x:signed"
327#define NS_ENCRYPTED "jabber:x:encrypted"
328#define NS_GATEWAY   "jabber:iq:gateway"
329#define NS_LAST      "jabber:iq:last"
330#define NS_ENVELOPE  "jabber:x:envelope"
331#define NS_EXPIRE    "jabber:x:expire"
332#define NS_XHTML     "http://www.w3.org/1999/xhtml"
333
334#define NS_XDBGINSERT "jabber:xdb:ginsert"
335#define NS_XDBNSLIST  "jabber:xdb:nslist"
336
337
338
339#ifdef __cplusplus
340}
341#endif
342
343#endif  /* INCL_LIB_H */
Note: See TracBrowser for help on using the repository browser.