Initial import (again)
[srvx.git] / src / common.h
1 /* common.h - Common functions/includes
2  * Copyright 2000-2004 srvx Development Team
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.  Important limitations are
8  * listed in the COPYING file that accompanies this software.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, email srvx-maintainers@srvx.net.
17  */
18
19 #ifndef COMMON_H
20 #define COMMON_H
21
22 #include "compat.h"
23 #include "proto.h"
24
25 #if !defined(HAVE_LOCALTIME_R) && !defined(__CYGWIN__)
26 extern struct tm *localtime_r(const time_t *clock, struct tm *res);
27 #elif defined(__CYGWIN__)
28 # define localtime_r(clock, res) memcpy(res, localtime(clock), sizeof(struct tm));
29 #endif
30
31 #ifndef true
32 #define true 1
33 #endif
34
35 #ifndef false
36 #define false 0
37 #endif
38
39 #ifndef INADDR_NONE
40 #define INADDR_NONE 0xffffffffL
41 #endif
42 #ifndef INADDR_LOOPBACK
43 #define INADDR_LOOPBACK 0x7f000001L
44 #endif
45
46 #define ArrayLength(x)          (sizeof(x)/sizeof(x[0]))
47 #define safestrncpy(dest, src, len) do { char *d = (dest); const char *s = (src); size_t l = strlen(s)+1;  if ((len) < l) l = (len); memmove(d, s, l); d[l-1] = 0; } while (0)
48
49 #ifdef __GNUC__
50 #define PRINTF_LIKE(M,N) __attribute__((format (printf, M, N)))
51 #else
52 #define PRINTF_LIKE(M,N)
53 #endif
54
55 #if __GNUC__ >= 2
56 #define UNUSED_ARG(ARG) ARG __attribute__((unused))
57 #elif defined(S_SPLINT_S)
58 #define UNUSED_ARG(ARG) /*@unused@*/ ARG
59 #define const /*@observer@*/ /*@temp@*/
60 #else
61 #define UNUSED_ARG(ARG) ARG
62 #endif
63
64 #if defined(WITH_MALLOC_DMALLOC)
65 #define DMALLOC_FUNC_CHECK 1
66 #include <string.h>
67 #include <dmalloc.h>
68 #elif defined(WITH_MALLOC_MPATROL)
69 #include <string.h>
70 #include <mpatrol.h>
71 #elif defined(WITH_MALLOC_BOEHM_GC)
72 #if !defined(NDEBUG)
73 #define GC_DEBUG 1
74 #endif
75 #include <stdlib.h>
76 #include <string.h>
77 #include <gc/gc.h>
78 #define malloc(n) GC_MALLOC(n)
79 #define calloc(m,n) GC_MALLOC((m)*(n))
80 #define realloc(p,n) GC_REALLOC((p),(n))
81 #define free(p) GC_FREE(p)
82 #undef  HAVE_STRDUP
83 #undef strdup
84 #endif
85
86 extern time_t now;
87 extern int quit_services;
88 extern struct log_type *MAIN_LOG;
89
90 int create_socket_client(struct uplinkNode *target);
91 void close_socket(void);
92
93 typedef void (*exit_func_t)(void);
94 void reg_exit_func(exit_func_t handler);
95 void call_exit_funcs(void);
96
97 const char *inttobase64(char *buf, unsigned int v, unsigned int count);
98 unsigned long base64toint(const char *s, int count);
99 int split_line(char *line, int irc_colon, int argv_size, char *argv[]);
100
101 /* match_ircglobs(oldglob, newglob) returns non-zero if oldglob is a superset of newglob */
102 #define match_ircglobs !mmatch
103 int mmatch(const char *glob, const char *newglob);
104 int match_ircglob(const char *text, const char *glob);
105 int user_matches_glob(struct userNode *user, const char *glob, int include_nick);
106
107 int is_ircmask(const char *text);
108 int is_gline(const char *text);
109
110 char *sanitize_ircmask(char *text);
111
112 unsigned long ParseInterval(const char *interval);
113 unsigned long ParseVolume(const char *volume);
114 int parse_ipmask(const char *str, struct in_addr *addr, unsigned long *mask);
115 #define MATCH_IPMASK(test, addr, mask) (((ntohl(test.s_addr) & mask) ^ (ntohl(addr.s_addr) & mask)) == 0)
116
117 #define MD5_CRYPT_LENGTH 42
118 /* buffer[] must be at least MD5_CRYPT_LENGTH bytes long */
119 const char *cryptpass(const char *pass, char buffer[]);
120 int checkpass(const char *pass, const char *crypt);
121
122 int split_ircmask(char *text, char **nick, char **ident, char **host);
123 char *unsplit_string(char *set[], unsigned int max, char *dest);
124
125 #define DECLARE_LIST(STRUCTNAME,ITEMTYPE) struct STRUCTNAME {\
126   unsigned int used, size;\
127   ITEMTYPE *list;\
128 };\
129 void STRUCTNAME##_init(struct STRUCTNAME *list);\
130 void STRUCTNAME##_append(struct STRUCTNAME *list, ITEMTYPE new_item);\
131 int STRUCTNAME##_remove(struct STRUCTNAME *list, ITEMTYPE new_item);\
132 void STRUCTNAME##_clean(struct STRUCTNAME *list)
133
134 #define DEFINE_LIST(STRUCTNAME,ITEMTYPE) \
135 void STRUCTNAME##_init(struct STRUCTNAME *list) {\
136   list->used = 0;\
137   list->size = 8;\
138   list->list = malloc(list->size*sizeof(list->list[0]));\
139 }\
140 void STRUCTNAME##_append(struct STRUCTNAME *list, ITEMTYPE new_item) {\
141   if (list->used == list->size) {\
142     list->size = list->size ? (list->size << 1) : 4;\
143     list->list = realloc(list->list, list->size*sizeof(list->list[0]));\
144   }\
145   list->list[list->used++] = new_item;\
146 }\
147 int STRUCTNAME##_remove(struct STRUCTNAME *list, ITEMTYPE new_item) {\
148     unsigned int n, found;\
149     for (found=n=0; n<list->used; n++) {\
150         if (list->list[n] == new_item) {\
151             memmove(list->list+n, list->list+n+1, (list->used-n-1)*sizeof(list->list[n]));\
152             found = 1;\
153             list->used--;\
154         }\
155     }\
156     return found;\
157 }\
158 void STRUCTNAME##_clean(struct STRUCTNAME *list) {\
159   list->used = list->size = 0;\
160   free(list->list);\
161 }
162
163 /* The longest string that's likely to be produced is "10 minutes, and 10
164    seconds." (27 characters) */
165 #define INTERVALLEN     32
166
167 char *intervalString2(char *output, time_t interval, int brief);
168 #define intervalString(OUTPUT, INTERVAL) intervalString2((OUTPUT), (INTERVAL), 0)
169 int getipbyname(const char *name, unsigned long *ip);
170 int set_policer_param(const char *param, void *data, void *extra);
171 const char *strtab(unsigned int ii);
172
173 void tools_init(void);
174 void tools_cleanup(void);
175
176 int irccasecmp(const char *stra, const char *strb);
177 int ircncasecmp(const char *stra, const char *strb, unsigned int len);
178 const char *irccasestr(const char *haystack, const char *needle);
179
180 DECLARE_LIST(string_buffer, char);
181 void string_buffer_append_string(struct string_buffer *buf, const char *tail);
182 void string_buffer_append_substring(struct string_buffer *buf, const char *tail, unsigned int len);
183 void string_buffer_append_vprintf(struct string_buffer *buf, const char *fmt, va_list args);
184 void string_buffer_append_printf(struct string_buffer *buf, const char *fmt, ...);
185 void string_buffer_replace(struct string_buffer *buf, unsigned int from, unsigned int len, const char *repl);
186
187 #define enabled_string(string)  (!irccasecmp((string), "on") || !strcmp((string), "1") || !irccasecmp((string), "enabled"))
188 #define disabled_string(string) (!irccasecmp((string), "off") || !strcmp((string), "0") || !irccasecmp((string), "disabled"))
189 #define true_string(string)     (!irccasecmp((string), "true") || !strcmp((string), "1") || !irccasecmp((string), "yes"))
190 #define false_string(string)    (!irccasecmp((string), "false") || !strcmp((string), "0") || !irccasecmp((string), "no"))
191
192 #endif /* ifdef COMMON_H */