]>
Commit | Line | Data |
---|---|---|
1 | /* common.h - Common functions/includes | |
2 | * Copyright 2000-2004 srvx Development Team | |
3 | * | |
4 | * This file is part of x3. | |
5 | * | |
6 | * x3 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 srvx; if not, write to the Free Software Foundation, | |
18 | * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. | |
19 | */ | |
20 | ||
21 | #ifndef COMMON_H | |
22 | #define COMMON_H | |
23 | ||
24 | #include "compat.h" | |
25 | #include "proto.h" | |
26 | ||
27 | #if !defined(HAVE_LOCALTIME_R) && !defined(__CYGWIN__) | |
28 | extern struct tm *localtime_r(const time_t *clock, struct tm *res); | |
29 | #elif defined(__CYGWIN__) | |
30 | # define localtime_r(clock, res) memcpy(res, localtime(clock), sizeof(struct tm)); | |
31 | #endif | |
32 | ||
33 | #ifndef true | |
34 | #define true 1 | |
35 | #endif | |
36 | ||
37 | #ifndef false | |
38 | #define false 0 | |
39 | #endif | |
40 | ||
41 | #define ArrayLength(x) (sizeof(x)/sizeof(x[0])) | |
42 | #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) | |
43 | ||
44 | #ifdef __GNUC__ | |
45 | #define PRINTF_LIKE(M,N) __attribute__((format (printf, M, N))) | |
46 | #else | |
47 | #define PRINTF_LIKE(M,N) | |
48 | #endif | |
49 | ||
50 | #if __GNUC__ >= 2 | |
51 | #define UNUSED_ARG(ARG) ARG __attribute__((unused)) | |
52 | #elif defined(S_SPLINT_S) | |
53 | #define UNUSED_ARG(ARG) /*@unused@*/ ARG | |
54 | #define const /*@observer@*/ /*@temp@*/ | |
55 | #else | |
56 | #define UNUSED_ARG(ARG) ARG | |
57 | #endif | |
58 | ||
59 | #if defined(WITH_MALLOC_DMALLOC) | |
60 | # define DMALLOC_FUNC_CHECK 1 | |
61 | # include <string.h> | |
62 | # include <dmalloc.h> | |
63 | #elif defined(WITH_MALLOC_MPATROL) | |
64 | # include <string.h> | |
65 | # include <mpatrol.h> | |
66 | #elif defined(WITH_MALLOC_BOEHM_GC) | |
67 | # if !defined(NDEBUG) | |
68 | # define GC_DEBUG 1 | |
69 | # endif | |
70 | # include <stdlib.h> | |
71 | # include <string.h> | |
72 | # include <gc/gc.h> | |
73 | # define malloc(n) GC_MALLOC(n) | |
74 | # define calloc(m,n) GC_MALLOC((m)*(n)) | |
75 | # define realloc(p,n) GC_REALLOC((p),(n)) | |
76 | # define free(p) GC_FREE(p) | |
77 | # undef HAVE_STRDUP | |
78 | # undef strdup | |
79 | #elif defined(WITH_MALLOC_X3) | |
80 | # undef malloc | |
81 | # define malloc(n) x3_malloc(__FILE__, __LINE__, (n)) | |
82 | # undef calloc | |
83 | # define calloc(m,n) x3_malloc(__FILE__, __LINE__, (m)*(n)) | |
84 | # undef realloc | |
85 | # define realloc(p,n) x3_realloc(__FILE__, __LINE__, (p), (n)) | |
86 | # undef free | |
87 | # define free(p) x3_free(__FILE__, __LINE__, (p)) | |
88 | # undef strdup | |
89 | # define strdup(s) x3_strdup(__FILE__, __LINE__, (s)) | |
90 | extern void *x3_malloc(const char *, unsigned int, size_t); | |
91 | extern void *x3_realloc(const char *, unsigned int, void *, size_t); | |
92 | extern char *x3_strdup(const char *, unsigned int, const char *); | |
93 | extern void x3_free(const char *, unsigned int, void *); | |
94 | # if !defined(NDEBUG) | |
95 | extern void verify(const void *ptr); | |
96 | # define verify(x) verify(x) | |
97 | # endif | |
98 | ||
99 | #elif defined(WITH_MALLOC_SLAB) | |
100 | # define malloc(n) slab_malloc(__FILE__, __LINE__, (n)) | |
101 | # undef calloc | |
102 | # define calloc(m,n) slab_malloc(__FILE__, __LINE__, (m)*(n)) | |
103 | # undef realloc | |
104 | # define realloc(p,n) slab_realloc(__FILE__, __LINE__, (p), (n)) | |
105 | # undef free | |
106 | # define free(p) slab_free(__FILE__, __LINE__, (p)) | |
107 | # undef strdup | |
108 | # define strdup(s) slab_strdup(__FILE__, __LINE__, (s)) | |
109 | extern void *slab_malloc(const char *, unsigned int, size_t); | |
110 | extern void *slab_realloc(const char *, unsigned int, void *, size_t); | |
111 | extern char *slab_strdup(const char *, unsigned int, const char *); | |
112 | extern void slab_free(const char *, unsigned int, void *); | |
113 | # if !defined(NDEBUG) | |
114 | extern void verify(const void *ptr); | |
115 | # define verify(x) verify(x) | |
116 | # endif | |
117 | #endif | |
118 | ||
119 | #ifndef verify | |
120 | # define verify(ptr) (void)(ptr) | |
121 | #endif | |
122 | ||
123 | extern char *x3_msnprintf(const int size, const char *format, ...); | |
124 | #define msnprintf x3_msnprintf | |
125 | ||
126 | extern time_t now; | |
127 | extern int quit_services; | |
128 | extern struct log_type *MAIN_LOG; | |
129 | ||
130 | typedef union irc_in_addr { | |
131 | uint32_t in6_32[4]; | |
132 | uint16_t in6[8]; | |
133 | uint8_t in6_8[16]; | |
134 | } irc_in_addr_t; | |
135 | ||
136 | #define irc_in_addr_is_valid(ADDR) (((ADDR).in6[0] && (ADDR).in6[0] != 65535) \ | |
137 | || (ADDR).in6[1] != (ADDR).in6[0] \ | |
138 | || (ADDR).in6[2] != (ADDR).in6[0] \ | |
139 | || (ADDR).in6[3] != (ADDR).in6[0] \ | |
140 | || (ADDR).in6[4] != (ADDR).in6[0] \ | |
141 | || (ADDR).in6[5] != (ADDR).in6[0] \ | |
142 | || (ADDR).in6[6] != (ADDR).in6[0] \ | |
143 | || (ADDR).in6[7] != (ADDR).in6[0]) | |
144 | #define irc_in_addr_is_ipv4(ADDR) (!(ADDR).in6[0] && !(ADDR).in6[1] \ | |
145 | && !(ADDR).in6[2] && !(ADDR).in6[3] \ | |
146 | && !(ADDR).in6[4] && (ADDR).in6[6] \ | |
147 | && (!(ADDR).in6[5] || (ADDR).in6[5] == 65535)) | |
148 | #define irc_in_addr_is_ipv6(ADDR) !irc_in_addr_is_ipv4(ADDR) | |
149 | #define irc_in_addr_is_loopback(ADDR) (irc_in_addr_is_ipv4(ADDR) ? (ADDR).in6_8[12] == 127 \ | |
150 | : (ADDR).in6[0] == 0 && (ADDR).in6[1] == 0 \ | |
151 | && (ADDR).in6[2] == 0 && (ADDR).in6[3] == 0 \ | |
152 | && (ADDR).in6[4] == 0 && (ADDR).in6[5] == 0 \ | |
153 | && (ADDR).in6[6] == 0 && (ADDR).in6[7] == 1) | |
154 | #define IRC_NTOP_MAX_SIZE 40 | |
155 | unsigned int irc_ntop(char *output, unsigned int out_size, const irc_in_addr_t *addr); | |
156 | #define IRC_NTOP_MASK_MAX_SIZE (IRC_NTOP_MAX_SIZE + 4) | |
157 | unsigned int irc_ntop_mask(char *output, unsigned int out_size, const irc_in_addr_t *addr, unsigned char bits); | |
158 | unsigned int irc_pton(irc_in_addr_t *addr, unsigned char *bits, const char *input); | |
159 | unsigned int irc_check_mask(const irc_in_addr_t *check, const irc_in_addr_t *mask, unsigned char bits); | |
160 | const char *irc_ntoa(const irc_in_addr_t *addr); | |
161 | ||
162 | int create_socket_client(struct uplinkNode *target); | |
163 | void close_socket(void); | |
164 | ||
165 | typedef void (*exit_func_t)(void); | |
166 | void reg_exit_func(exit_func_t handler); | |
167 | void call_exit_funcs(void); | |
168 | ||
169 | char *mysep(char **sepstr, char *delim); | |
170 | const char *inttobase64(char *buf, unsigned int v, unsigned int count); | |
171 | unsigned long base64toint(const char *s, int count); | |
172 | int split_line(char *line, int irc_colon, int argv_size, char *argv[]); | |
173 | ||
174 | /* match_ircglobs(oldglob, newglob) returns non-zero if oldglob is a superset of newglob */ | |
175 | #define match_ircglobs !mmatch | |
176 | int mmatch(const char *glob, const char *newglob); | |
177 | int match_ircglob(const char *text, const char *glob); | |
178 | #define MATCH_USENICK 1 | |
179 | #define MATCH_VISIBLE 2 | |
180 | int user_matches_glob(struct userNode *user, const char *glob, int flags); | |
181 | int is_overmask(char *mask); | |
182 | ||
183 | ||
184 | int is_ircmask(const char *text); | |
185 | int is_gline(const char *text); | |
186 | int is_shun(const char *text); | |
187 | ||
188 | char *sanitize_ircmask(char *text); | |
189 | ||
190 | unsigned long ParseInterval(const char *interval); | |
191 | unsigned long ParseVolume(const char *volume); | |
192 | ||
193 | #define MD5_CRYPT_LENGTH 42 | |
194 | /* buffer[] must be at least MD5_CRYPT_LENGTH bytes long */ | |
195 | const char *cryptpass(const char *pass, char buffer[]); | |
196 | int checkpass(const char *pass, const char *crypt); | |
197 | ||
198 | int split_ircmask(char *text, char **nick, char **ident, char **host); | |
199 | char *unsplit_string(char *set[], unsigned int max, char *dest); | |
200 | extern char* x3_strtok(char** save, char* str, char* fs); | |
201 | ||
202 | int valid_email(const char *email); | |
203 | ||
204 | #define DECLARE_LIST(STRUCTNAME,ITEMTYPE) struct STRUCTNAME {\ | |
205 | unsigned int used, size;\ | |
206 | ITEMTYPE *list;\ | |
207 | };\ | |
208 | void STRUCTNAME##_init(struct STRUCTNAME *list);\ | |
209 | void STRUCTNAME##_append(struct STRUCTNAME *list, ITEMTYPE new_item);\ | |
210 | int STRUCTNAME##_remove(struct STRUCTNAME *list, ITEMTYPE new_item);\ | |
211 | void STRUCTNAME##_clean(struct STRUCTNAME *list) | |
212 | ||
213 | #define DEFINE_LIST(STRUCTNAME,ITEMTYPE) \ | |
214 | void STRUCTNAME##_init(struct STRUCTNAME *list) {\ | |
215 | list->used = 0;\ | |
216 | list->size = 8;\ | |
217 | list->list = malloc(list->size*sizeof(list->list[0]));\ | |
218 | }\ | |
219 | void STRUCTNAME##_append(struct STRUCTNAME *list, ITEMTYPE new_item) {\ | |
220 | verify(list->list);\ | |
221 | if (list->used == list->size) {\ | |
222 | list->size = list->size ? (list->size << 1) : 4;\ | |
223 | list->list = realloc(list->list, list->size*sizeof(list->list[0]));\ | |
224 | }\ | |
225 | list->list[list->used++] = new_item;\ | |
226 | }\ | |
227 | int STRUCTNAME##_remove(struct STRUCTNAME *list, ITEMTYPE new_item) {\ | |
228 | unsigned int n, found;\ | |
229 | verify(list->list);\ | |
230 | for (found=n=0; n<list->used; n++) {\ | |
231 | if (list->list[n] == new_item) {\ | |
232 | memmove(list->list+n, list->list+n+1, (list->used-n-1)*sizeof(list->list[n]));\ | |
233 | found = 1;\ | |
234 | list->used--;\ | |
235 | }\ | |
236 | }\ | |
237 | return found;\ | |
238 | }\ | |
239 | void STRUCTNAME##_clean(struct STRUCTNAME *list) {\ | |
240 | list->used = list->size = 0;\ | |
241 | free(list->list);\ | |
242 | list->list = NULL;\ | |
243 | } | |
244 | ||
245 | /* The longest string that is likely to be produced in English is "10 | |
246 | * minutes, and 10 seconds" (27 characters). Other languages will | |
247 | * vary, so there's plenty of leeway. | |
248 | */ | |
249 | #define INTERVALLEN 50 | |
250 | ||
251 | struct handle_info; | |
252 | char *intervalString(char *output, time_t interval, struct handle_info *hi); | |
253 | int getipbyname(const char *name, unsigned long *ip); | |
254 | int set_policer_param(const char *param, void *data, void *extra); | |
255 | const char *strtab(unsigned int ii); | |
256 | char *time2str(time_t thetime); | |
257 | extern char *pretty_mask(char *mask); | |
258 | ||
259 | void tools_init(void); | |
260 | void tools_cleanup(void); | |
261 | ||
262 | int irccasecmp(const char *stra, const char *strb); | |
263 | int ircncasecmp(const char *stra, const char *strb, unsigned int len); | |
264 | const char *irccasestr(const char *haystack, const char *needle); | |
265 | ||
266 | DECLARE_LIST(string_buffer, char); | |
267 | void string_buffer_append_string(struct string_buffer *buf, const char *tail); | |
268 | void string_buffer_append_substring(struct string_buffer *buf, const char *tail, unsigned int len); | |
269 | void string_buffer_append_vprintf(struct string_buffer *buf, const char *fmt, va_list args); | |
270 | void string_buffer_append_printf(struct string_buffer *buf, const char *fmt, ...); | |
271 | void string_buffer_replace(struct string_buffer *buf, unsigned int from, unsigned int len, const char *repl); | |
272 | void | |
273 | irc_strtolower(char *str); | |
274 | ||
275 | int str_is_number(const char *str); | |
276 | #define enabled_string(string) (!irccasecmp((string), "on") || !strcmp((string), "1") || !irccasecmp((string), "enabled")) | |
277 | #define disabled_string(string) (!irccasecmp((string), "off") || !strcmp((string), "0") || !irccasecmp((string), "disabled")) | |
278 | #define true_string(string) (!irccasecmp((string), "true") || !strcmp((string), "1") || !irccasecmp((string), "yes")) | |
279 | #define false_string(string) (!irccasecmp((string), "false") || !strcmp((string), "0") || !irccasecmp((string), "no")) | |
280 | ||
281 | #endif /* ifdef COMMON_H */ |