]>
Commit | Line | Data |
---|---|---|
d76ed9a9 AS |
1 | /* common.h - Common functions/includes |
2 | * Copyright 2000-2004 srvx Development Team | |
3 | * | |
83ff05c3 | 4 | * This file is part of x3. |
d76ed9a9 AS |
5 | * |
6 | * srvx 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 | #ifndef INADDR_NONE | |
42 | #define INADDR_NONE 0xffffffffL | |
43 | #endif | |
44 | #ifndef INADDR_LOOPBACK | |
45 | #define INADDR_LOOPBACK 0x7f000001L | |
46 | #endif | |
47 | ||
48 | #define ArrayLength(x) (sizeof(x)/sizeof(x[0])) | |
49 | #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) | |
50 | ||
51 | #ifdef __GNUC__ | |
52 | #define PRINTF_LIKE(M,N) __attribute__((format (printf, M, N))) | |
53 | #else | |
54 | #define PRINTF_LIKE(M,N) | |
55 | #endif | |
56 | ||
57 | #if __GNUC__ >= 2 | |
58 | #define UNUSED_ARG(ARG) ARG __attribute__((unused)) | |
59 | #elif defined(S_SPLINT_S) | |
60 | #define UNUSED_ARG(ARG) /*@unused@*/ ARG | |
61 | #define const /*@observer@*/ /*@temp@*/ | |
62 | #else | |
63 | #define UNUSED_ARG(ARG) ARG | |
64 | #endif | |
65 | ||
66 | #if defined(WITH_MALLOC_DMALLOC) | |
ec1a68c8 | 67 | # define DMALLOC_FUNC_CHECK 1 |
68 | # include <string.h> | |
69 | # include <dmalloc.h> | |
d76ed9a9 | 70 | #elif defined(WITH_MALLOC_MPATROL) |
ec1a68c8 | 71 | # include <string.h> |
72 | # include <mpatrol.h> | |
d76ed9a9 | 73 | #elif defined(WITH_MALLOC_BOEHM_GC) |
ec1a68c8 | 74 | # if !defined(NDEBUG) |
75 | # define GC_DEBUG 1 | |
76 | # endif | |
77 | # include <stdlib.h> | |
78 | # include <string.h> | |
79 | # include <gc/gc.h> | |
80 | # define malloc(n) GC_MALLOC(n) | |
81 | # define calloc(m,n) GC_MALLOC((m)*(n)) | |
82 | # define realloc(p,n) GC_REALLOC((p),(n)) | |
83 | # define free(p) GC_FREE(p) | |
84 | # undef HAVE_STRDUP | |
85 | # undef strdup | |
86 | #elif defined(WITH_MALLOC_SRVX) | |
87 | # undef malloc | |
88 | # define malloc(n) srvx_malloc(__FILE__, __LINE__, (n)) | |
89 | # undef calloc | |
90 | # define calloc(m,n) srvx_malloc(__FILE__, __LINE__, (m)*(n)) | |
91 | # undef realloc | |
92 | # define realloc(p,n) srvx_realloc(__FILE__, __LINE__, (p), (n)) | |
93 | # undef free | |
94 | # define free(p) srvx_free(__FILE__, __LINE__, (p)) | |
95 | # undef strdup | |
96 | # define strdup(s) srvx_strdup(__FILE__, __LINE__, (s)) | |
97 | extern void *srvx_malloc(const char *, unsigned int, size_t); | |
98 | extern void *srvx_realloc(const char *, unsigned int, void *, size_t); | |
99 | extern char *srvx_strdup(const char *, unsigned int, const char *); | |
100 | extern void srvx_free(const char *, unsigned int, void *); | |
101 | # if !defined(NDEBUG) | |
102 | extern void verify(const void *ptr); | |
103 | # define verify(x) verify(x) | |
104 | # endif | |
0d16e639 | 105 | #elif defined(WITH_MALLOC_SLAB) |
106 | # define malloc(n) slab_malloc(__FILE__, __LINE__, (n)) | |
107 | # undef calloc | |
108 | # define calloc(m,n) slab_malloc(__FILE__, __LINE__, (m)*(n)) | |
109 | # undef realloc | |
110 | # define realloc(p,n) slab_realloc(__FILE__, __LINE__, (p), (n)) | |
111 | # undef free | |
112 | # define free(p) slab_free(__FILE__, __LINE__, (p)) | |
113 | # undef strdup | |
114 | # define strdup(s) slab_strdup(__FILE__, __LINE__, (s)) | |
115 | extern void *slab_malloc(const char *, unsigned int, size_t); | |
116 | extern void *slab_realloc(const char *, unsigned int, void *, size_t); | |
117 | extern char *slab_strdup(const char *, unsigned int, const char *); | |
118 | extern void slab_free(const char *, unsigned int, void *); | |
119 | # if !defined(NDEBUG) | |
120 | extern void verify(const void *ptr); | |
121 | # define verify(x) verify(x) | |
122 | # endif | |
d76ed9a9 | 123 | #endif |
ec1a68c8 | 124 | |
125 | #ifndef verify | |
126 | # define verify(ptr) (void)(ptr) | |
d76ed9a9 AS |
127 | #endif |
128 | ||
129 | extern time_t now; | |
130 | extern int quit_services; | |
131 | extern struct log_type *MAIN_LOG; | |
132 | ||
133 | int create_socket_client(struct uplinkNode *target); | |
134 | void close_socket(void); | |
135 | ||
136 | typedef void (*exit_func_t)(void); | |
137 | void reg_exit_func(exit_func_t handler); | |
138 | void call_exit_funcs(void); | |
139 | ||
2aef5f4b | 140 | char *mysep(char **sepstr, char *delim); |
d76ed9a9 AS |
141 | const char *inttobase64(char *buf, unsigned int v, unsigned int count); |
142 | unsigned long base64toint(const char *s, int count); | |
143 | int split_line(char *line, int irc_colon, int argv_size, char *argv[]); | |
144 | ||
145 | /* match_ircglobs(oldglob, newglob) returns non-zero if oldglob is a superset of newglob */ | |
146 | #define match_ircglobs !mmatch | |
147 | int mmatch(const char *glob, const char *newglob); | |
148 | int match_ircglob(const char *text, const char *glob); | |
149 | int user_matches_glob(struct userNode *user, const char *glob, int include_nick); | |
150 | ||
151 | int is_ircmask(const char *text); | |
152 | int is_gline(const char *text); | |
153 | ||
154 | char *sanitize_ircmask(char *text); | |
155 | ||
156 | unsigned long ParseInterval(const char *interval); | |
157 | unsigned long ParseVolume(const char *volume); | |
158 | int parse_ipmask(const char *str, struct in_addr *addr, unsigned long *mask); | |
159 | #define MATCH_IPMASK(test, addr, mask) (((ntohl(test.s_addr) & mask) ^ (ntohl(addr.s_addr) & mask)) == 0) | |
160 | ||
161 | #define MD5_CRYPT_LENGTH 42 | |
162 | /* buffer[] must be at least MD5_CRYPT_LENGTH bytes long */ | |
163 | const char *cryptpass(const char *pass, char buffer[]); | |
164 | int checkpass(const char *pass, const char *crypt); | |
165 | ||
166 | int split_ircmask(char *text, char **nick, char **ident, char **host); | |
167 | char *unsplit_string(char *set[], unsigned int max, char *dest); | |
168 | ||
169 | #define DECLARE_LIST(STRUCTNAME,ITEMTYPE) struct STRUCTNAME {\ | |
170 | unsigned int used, size;\ | |
171 | ITEMTYPE *list;\ | |
172 | };\ | |
173 | void STRUCTNAME##_init(struct STRUCTNAME *list);\ | |
174 | void STRUCTNAME##_append(struct STRUCTNAME *list, ITEMTYPE new_item);\ | |
175 | int STRUCTNAME##_remove(struct STRUCTNAME *list, ITEMTYPE new_item);\ | |
176 | void STRUCTNAME##_clean(struct STRUCTNAME *list) | |
177 | ||
178 | #define DEFINE_LIST(STRUCTNAME,ITEMTYPE) \ | |
179 | void STRUCTNAME##_init(struct STRUCTNAME *list) {\ | |
180 | list->used = 0;\ | |
181 | list->size = 8;\ | |
182 | list->list = malloc(list->size*sizeof(list->list[0]));\ | |
183 | }\ | |
184 | void STRUCTNAME##_append(struct STRUCTNAME *list, ITEMTYPE new_item) {\ | |
ec1a68c8 | 185 | verify(list->list);\ |
d76ed9a9 AS |
186 | if (list->used == list->size) {\ |
187 | list->size = list->size ? (list->size << 1) : 4;\ | |
188 | list->list = realloc(list->list, list->size*sizeof(list->list[0]));\ | |
189 | }\ | |
190 | list->list[list->used++] = new_item;\ | |
191 | }\ | |
192 | int STRUCTNAME##_remove(struct STRUCTNAME *list, ITEMTYPE new_item) {\ | |
193 | unsigned int n, found;\ | |
ec1a68c8 | 194 | verify(list->list);\ |
d76ed9a9 AS |
195 | for (found=n=0; n<list->used; n++) {\ |
196 | if (list->list[n] == new_item) {\ | |
197 | memmove(list->list+n, list->list+n+1, (list->used-n-1)*sizeof(list->list[n]));\ | |
198 | found = 1;\ | |
199 | list->used--;\ | |
200 | }\ | |
201 | }\ | |
202 | return found;\ | |
203 | }\ | |
204 | void STRUCTNAME##_clean(struct STRUCTNAME *list) {\ | |
205 | list->used = list->size = 0;\ | |
206 | free(list->list);\ | |
ec1a68c8 | 207 | list->list = NULL;\ |
d76ed9a9 AS |
208 | } |
209 | ||
210 | /* The longest string that is likely to be produced in English is "10 | |
211 | * minutes, and 10 seconds" (27 characters). Other languages will | |
212 | * vary, so there's plenty of leeway. | |
213 | */ | |
214 | #define INTERVALLEN 50 | |
215 | ||
216 | struct handle_info; | |
217 | char *intervalString(char *output, time_t interval, struct handle_info *hi); | |
218 | int getipbyname(const char *name, unsigned long *ip); | |
219 | int set_policer_param(const char *param, void *data, void *extra); | |
220 | const char *strtab(unsigned int ii); | |
eb5d6b73 | 221 | char *time2str(time_t thetime); |
d76ed9a9 AS |
222 | |
223 | void tools_init(void); | |
224 | void tools_cleanup(void); | |
225 | ||
226 | int irccasecmp(const char *stra, const char *strb); | |
227 | int ircncasecmp(const char *stra, const char *strb, unsigned int len); | |
228 | const char *irccasestr(const char *haystack, const char *needle); | |
229 | ||
230 | DECLARE_LIST(string_buffer, char); | |
231 | void string_buffer_append_string(struct string_buffer *buf, const char *tail); | |
232 | void string_buffer_append_substring(struct string_buffer *buf, const char *tail, unsigned int len); | |
233 | void string_buffer_append_vprintf(struct string_buffer *buf, const char *fmt, va_list args); | |
234 | void string_buffer_append_printf(struct string_buffer *buf, const char *fmt, ...); | |
235 | void string_buffer_replace(struct string_buffer *buf, unsigned int from, unsigned int len, const char *repl); | |
236 | ||
237 | #define enabled_string(string) (!irccasecmp((string), "on") || !strcmp((string), "1") || !irccasecmp((string), "enabled")) | |
238 | #define disabled_string(string) (!irccasecmp((string), "off") || !strcmp((string), "0") || !irccasecmp((string), "disabled")) | |
239 | #define true_string(string) (!irccasecmp((string), "true") || !strcmp((string), "1") || !irccasecmp((string), "yes")) | |
240 | #define false_string(string) (!irccasecmp((string), "false") || !strcmp((string), "0") || !irccasecmp((string), "no")) | |
241 | ||
242 | #endif /* ifdef COMMON_H */ |