PageRenderTime 61ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/jabberd-2.2.16/util/util.h

#
C Header | 466 lines | 291 code | 91 blank | 84 comment | 4 complexity | bf2372916a4aa08a797934a70303d9c1 MD5 | raw file
Possible License(s): GPL-2.0
  1. /*
  2. * jabberd - Jabber Open Source Server
  3. * Copyright (c) 2002 Jeremie Miller, Thomas Muldowney,
  4. * Ryan Eatmon, Robert Norris
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307USA
  19. */
  20. #ifdef HAVE_CONFIG_H
  21. # include <config.h>
  22. #endif
  23. #include "ac-stdint.h"
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <stdarg.h>
  27. #include <time.h>
  28. #include <errno.h>
  29. #include <assert.h>
  30. #include <expat.h>
  31. #ifdef HAVE_SYS_TYPES_H
  32. # include <sys/types.h>
  33. #endif
  34. #ifdef HAVE_NETINET_IN_H
  35. # include <netinet/in.h>
  36. #endif
  37. #if defined(HAVE_SYS_TIME_H)
  38. # include <sys/time.h>
  39. #elif defined(HAVE_SYS_TIMEB_H)
  40. # include <sys/timeb.h>
  41. #endif
  42. #ifdef HAVE_SYSLOG_H
  43. # include <syslog.h>
  44. #endif
  45. #ifdef HAVE_UNISTD_H
  46. # include <unistd.h>
  47. #endif
  48. #include <ctype.h>
  49. #ifdef HAVE_SYS_SOCKET_H
  50. # include <sys/socket.h>
  51. #endif
  52. #ifdef HAVE_NETINET_IN_H
  53. # include <netinet/in.h>
  54. #endif
  55. #ifdef HAVE_ARPA_INET_H
  56. # include <arpa/inet.h>
  57. #endif
  58. #ifndef PATH_MAX
  59. #ifndef MAXPATHLEN
  60. # define PATH_MAX 512
  61. #else
  62. # define PATH_MAX MAXPATHLEN
  63. #endif
  64. #endif
  65. #ifdef USE_LIBSUBST
  66. #include "subst/subst.h"
  67. #endif
  68. #include "util/util_compat.h"
  69. #ifndef INCL_UTIL_H
  70. #define INCL_UTIL_H
  71. /* jabberd2 Windows DLL */
  72. #ifndef JABBERD2_API
  73. # ifdef _WIN32
  74. # ifdef JABBERD2_EXPORTS
  75. # define JABBERD2_API __declspec(dllexport)
  76. # else /* JABBERD2_EXPORTS */
  77. # define JABBERD2_API __declspec(dllimport)
  78. # endif /* JABBERD2_EXPORTS */
  79. # else /* _WIN32 */
  80. # define JABBERD2_API extern
  81. # endif /* _WIN32 */
  82. #endif /* JABBERD2_API */
  83. #ifdef __cplusplus
  84. extern "C" {
  85. #endif
  86. /* crypto hashing utils */
  87. #include "sha1.h"
  88. #include "md5.h"
  89. #include <util/nad.h>
  90. #include <util/pool.h>
  91. #include <util/xhash.h>
  92. /* --------------------------------------------------------- */
  93. /* */
  94. /* String management routines */
  95. /* */
  96. /** --------------------------------------------------------- */
  97. JABBERD2_API char *j_strdup(const char *str); /* provides NULL safe strdup wrapper */
  98. JABBERD2_API char *j_strcat(char *dest, char *txt); /* strcpy() clone */
  99. JABBERD2_API int j_strcmp(const char *a, const char *b); /* provides NULL safe strcmp wrapper */
  100. JABBERD2_API int j_strcasecmp(const char *a, const char *b); /* provides NULL safe strcasecmp wrapper */
  101. JABBERD2_API int j_strncmp(const char *a, const char *b, int i); /* provides NULL safe strncmp wrapper */
  102. JABBERD2_API int j_strncasecmp(const char *a, const char *b, int i); /* provides NULL safe strncasecmp wrapper */
  103. JABBERD2_API int j_strlen(const char *a); /* provides NULL safe strlen wrapper */
  104. JABBERD2_API int j_atoi(const char *a, int def); /* checks for NULL and uses default instead, convienence */
  105. JABBERD2_API char *j_attr(const char** atts, const char *attr); /* decode attr's (from expat) */
  106. JABBERD2_API char *j_strnchr(const char *s, int c, int n); /* like strchr, but only searches n chars */
  107. /** old convenience function, now in str.c */
  108. JABBERD2_API void shahash_r(const char* str, char hashbuf[41]);
  109. JABBERD2_API void shahash_raw(const char* str, unsigned char hashval[20]);
  110. /* --------------------------------------------------------- */
  111. /* */
  112. /* XML escaping utils */
  113. /* */
  114. /* --------------------------------------------------------- */
  115. JABBERD2_API char *strescape(pool_t p, char *buf, int len); /* Escape <>&'" chars */
  116. JABBERD2_API char *strunescape(pool_t p, char *buf);
  117. /* --------------------------------------------------------- */
  118. /* */
  119. /* String pools (spool) functions */
  120. /* */
  121. /* --------------------------------------------------------- */
  122. struct spool_node
  123. {
  124. char *c;
  125. struct spool_node *next;
  126. };
  127. typedef struct spool_struct
  128. {
  129. pool_t p;
  130. int len;
  131. struct spool_node *last;
  132. struct spool_node *first;
  133. } *spool;
  134. JABBERD2_API spool spool_new(pool_t p); /* create a string pool */
  135. JABBERD2_API void spooler(spool s, ...); /* append all the char * args to the pool, terminate args with s again */
  136. JABBERD2_API char *spool_print(spool s); /* return a big string */
  137. JABBERD2_API void spool_add(spool s, char *str); /* add a single string to the pool */
  138. JABBERD2_API void spool_escape(spool s, char *raw, int len); /* add and xml escape a single string to the pool */
  139. JABBERD2_API char *spools(pool_t p, ...); /* wrap all the spooler stuff in one function, the happy fun ball! */
  140. /* known namespace uri */
  141. #include "util/uri.h"
  142. /* JID manipulation */
  143. #include "util/jid.h"
  144. /* logging */
  145. typedef enum {
  146. log_STDOUT,
  147. log_SYSLOG,
  148. log_FILE
  149. } log_type_t;
  150. typedef struct log_st
  151. {
  152. log_type_t type;
  153. FILE *file;
  154. } *log_t;
  155. typedef struct log_facility_st
  156. {
  157. const char *facility;
  158. int number;
  159. } log_facility_t;
  160. JABBERD2_API log_t log_new(log_type_t type, const char *ident, const char *facility);
  161. JABBERD2_API void log_write(log_t log, int level, const char *msgfmt, ...);
  162. JABBERD2_API void log_free(log_t log);
  163. /* config files */
  164. typedef struct config_elem_st *config_elem_t;
  165. typedef struct config_st *config_t;
  166. /** holder for the config hash and nad */
  167. struct config_st
  168. {
  169. xht hash;
  170. nad_t nad;
  171. };
  172. /** a single element */
  173. struct config_elem_st
  174. {
  175. char **values;
  176. int nvalues;
  177. char ***attrs;
  178. };
  179. JABBERD2_API config_t config_new(void);
  180. JABBERD2_API int config_load(config_t c, const char *file);
  181. JABBERD2_API int config_load_with_id(config_t c, const char *file, const char *id);
  182. JABBERD2_API config_elem_t config_get(config_t c, const char *key);
  183. JABBERD2_API const char *config_get_one(config_t c, const char *key, int num);
  184. JABBERD2_API const char *config_get_one_default(config_t c, const char *key, int num, const char *default_value);
  185. JABBERD2_API int config_count(config_t c, const char *key);
  186. JABBERD2_API char *config_get_attr(config_t c, const char *key, int num, const char *attr);
  187. JABBERD2_API char *config_expand(config_t c, const char *value); //! Replaces $(some.value) with config_get_one(c, "some.value", 0)
  188. JABBERD2_API void config_free(config_t);
  189. /*
  190. * IP-based access controls
  191. */
  192. typedef struct access_rule_st
  193. {
  194. struct sockaddr_storage ip;
  195. int mask;
  196. } *access_rule_t;
  197. typedef struct access_st
  198. {
  199. int order; /* 0 = allow,deny 1 = deny,allow */
  200. access_rule_t allow;
  201. int nallow;
  202. access_rule_t deny;
  203. int ndeny;
  204. } *access_t;
  205. JABBERD2_API access_t access_new(int order);
  206. JABBERD2_API void access_free(access_t access);
  207. JABBERD2_API int access_allow(access_t access, char *ip, char *mask);
  208. JABBERD2_API int access_deny(access_t access, char *ip, char *mask);
  209. JABBERD2_API int access_check(access_t access, char *ip);
  210. /*
  211. * rate limiting
  212. */
  213. typedef struct rate_st
  214. {
  215. int total; /* if we exceed this many events */
  216. int seconds; /* in this many seconds */
  217. int wait; /* then go bad for this many seconds */
  218. time_t time; /* time we started counting events */
  219. int count; /* event count */
  220. time_t bad; /* time we went bad, or 0 if we're not */
  221. } *rate_t;
  222. JABBERD2_API rate_t rate_new(int total, int seconds, int wait);
  223. JABBERD2_API void rate_free(rate_t rt);
  224. JABBERD2_API void rate_reset(rate_t rt);
  225. /**
  226. * Add a number of events to the counter. This takes care of moving
  227. * the sliding window, if we've moved outside the previous window.
  228. */
  229. JABBERD2_API void rate_add(rate_t rt, int count);
  230. /**
  231. * @return The amount of events we have left before we hit the rate
  232. * limit. This could be number of bytes, or number of
  233. * connection attempts, etc.
  234. */
  235. JABBERD2_API int rate_left(rate_t rt);
  236. /**
  237. * @return 1 if we're under the rate limit and everything is fine or
  238. * 0 if the rate limit has been exceeded and we should throttle
  239. * something.
  240. */
  241. JABBERD2_API int rate_check(rate_t rt);
  242. /*
  243. * helpers for ip addresses
  244. */
  245. #include "inaddr.h" /* used in mio as well */
  246. /*
  247. * serialisation helper functions
  248. */
  249. JABBERD2_API int ser_string_get(char **dest, int *source, const char *buf, int len);
  250. JABBERD2_API int ser_int_get(int *dest, int *source, const char *buf, int len);
  251. JABBERD2_API void ser_string_set(char *source, int *dest, char **buf, int *len);
  252. JABBERD2_API void ser_int_set(int source, int *dest, char **buf, int *len);
  253. /*
  254. * priority queues
  255. */
  256. typedef struct _jqueue_node_st *_jqueue_node_t;
  257. struct _jqueue_node_st {
  258. void *data;
  259. int priority;
  260. _jqueue_node_t next;
  261. _jqueue_node_t prev;
  262. };
  263. typedef struct _jqueue_st {
  264. pool_t p;
  265. _jqueue_node_t cache;
  266. _jqueue_node_t front;
  267. _jqueue_node_t back;
  268. int size;
  269. char *key;
  270. time_t init_time;
  271. } *jqueue_t;
  272. JABBERD2_API jqueue_t jqueue_new(void);
  273. JABBERD2_API void jqueue_free(jqueue_t q);
  274. JABBERD2_API void jqueue_push(jqueue_t q, void *data, int pri);
  275. JABBERD2_API void *jqueue_pull(jqueue_t q);
  276. JABBERD2_API int jqueue_size(jqueue_t q);
  277. JABBERD2_API time_t jqueue_age(jqueue_t q);
  278. /* ISO 8601 / JEP-0082 date/time manipulation */
  279. typedef enum {
  280. dt_DATE = 1,
  281. dt_TIME = 2,
  282. dt_DATETIME = 3,
  283. dt_LEGACY = 4
  284. } datetime_t;
  285. JABBERD2_API time_t datetime_in(char *date);
  286. JABBERD2_API void datetime_out(time_t t, datetime_t type, char *date, int datelen);
  287. /* base64 functions */
  288. JABBERD2_API int apr_base64_decode_len(const char *bufcoded, int buflen);
  289. JABBERD2_API int apr_base64_decode(char *bufplain, const char *bufcoded, int buflen);
  290. JABBERD2_API int apr_base64_encode_len(int len);
  291. JABBERD2_API int apr_base64_encode(char *encoded, const char *string, int len);
  292. /* convenience, result string must be free()'d by caller */
  293. JABBERD2_API char *b64_encode(char *buf, int len);
  294. JABBERD2_API char *b64_decode(char *buf);
  295. /* stanza manipulation */
  296. #define stanza_err_BAD_REQUEST (100)
  297. #define stanza_err_CONFLICT (101)
  298. #define stanza_err_FEATURE_NOT_IMPLEMENTED (102)
  299. #define stanza_err_FORBIDDEN (103)
  300. #define stanza_err_GONE (104)
  301. #define stanza_err_INTERNAL_SERVER_ERROR (105)
  302. #define stanza_err_ITEM_NOT_FOUND (106)
  303. #define stanza_err_JID_MALFORMED (107)
  304. #define stanza_err_NOT_ACCEPTABLE (108)
  305. #define stanza_err_NOT_ALLOWED (109)
  306. #define stanza_err_PAYMENT_REQUIRED (110)
  307. #define stanza_err_RECIPIENT_UNAVAILABLE (111)
  308. #define stanza_err_REDIRECT (112)
  309. #define stanza_err_REGISTRATION_REQUIRED (113)
  310. #define stanza_err_REMOTE_SERVER_NOT_FOUND (114)
  311. #define stanza_err_REMOTE_SERVER_TIMEOUT (115)
  312. #define stanza_err_RESOURCE_CONSTRAINT (116)
  313. #define stanza_err_SERVICE_UNAVAILABLE (117)
  314. #define stanza_err_SUBSCRIPTION_REQUIRED (118)
  315. #define stanza_err_UNDEFINED_CONDITION (119)
  316. #define stanza_err_UNEXPECTED_REQUEST (120)
  317. #define stanza_err_OLD_UNAUTH (121)
  318. #define stanza_err_UNKNOWN_SENDER (122)
  319. #define stanza_err_LAST (123)
  320. JABBERD2_API nad_t stanza_error(nad_t nad, int elem, int err);
  321. JABBERD2_API nad_t stanza_tofrom(nad_t nad, int elem);
  322. typedef struct _stanza_error_st {
  323. const char *name;
  324. const char *type;
  325. const char *code;
  326. } *stanza_error_t;
  327. JABBERD2_API struct _stanza_error_st _stanza_errors[];
  328. /* hex conversion utils */
  329. JABBERD2_API void hex_from_raw(char *in, int inlen, char *out);
  330. JABBERD2_API int hex_to_raw(char *in, int inlen, char *out);
  331. /* xdata in a seperate file */
  332. #include "xdata.h"
  333. /* debug logging */
  334. JABBERD2_API int get_debug_flag(void);
  335. JABBERD2_API void set_debug_flag(int v);
  336. JABBERD2_API void debug_log(const char *file, int line, const char *msgfmt, ...);
  337. JABBERD2_API void set_debug_file(const char *filename);
  338. JABBERD2_API void set_debug_log_from_config(config_t c);
  339. #define ZONE __FILE__,__LINE__
  340. #define MAX_DEBUG 8192
  341. /* if no debug, basically compile it out */
  342. #ifdef DEBUG
  343. #define log_debug if(get_debug_flag()) debug_log
  344. #else
  345. #define log_debug if(0) debug_log
  346. #endif
  347. /* Portable signal function */
  348. typedef void jsighandler_t(int);
  349. JABBERD2_API jsighandler_t* jabber_signal(int signo, jsighandler_t *func);
  350. #ifdef _WIN32
  351. /* Windows service wrapper function */
  352. typedef int (jmainhandler_t)(int argc, char** argv);
  353. JABBERD2_API int jabber_wrap_service(int argc, char** argv, jmainhandler_t *wrapper, LPCTSTR name, LPCTSTR display, LPCTSTR description, LPCTSTR depends);
  354. #define JABBER_MAIN(name, display, description, depends) jabber_main(int argc, char** argv); \
  355. main(int argc, char** argv) { return jabber_wrap_service(argc, argv, jabber_main, name, display, description, depends); } \
  356. jabber_main(int argc, char** argv)
  357. #else /* _WIN32 */
  358. #define JABBER_MAIN(name, display, description, depends) int main(int argc, char** argv)
  359. #endif /* _WIN32 */
  360. #ifdef __cplusplus
  361. }
  362. #endif
  363. #if XML_MAJOR_VERSION > 1
  364. /* XML_StopParser is present in expat 2.x */
  365. #define HAVE_XML_STOPPARSER
  366. #define HAVE_XML_SETHASHSALT
  367. #endif
  368. /* define TRUE and FALSE if not yet defined */
  369. #ifndef TRUE
  370. #define TRUE 1
  371. #endif
  372. #ifndef FALSE
  373. #define FALSE 0
  374. #endif
  375. #endif /* INCL_UTIL_H */