]>
jfr.im git - irc/evilnet/x3.git/blob - src/common.h
1 /* common.h - Common functions/includes
2 * Copyright 2000-2004 srvx Development Team
4 * This file is part of x3.
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 3 of the License, or
9 * (at your option) any later version.
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.
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.
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));
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)
45 #define PRINTF_LIKE(M,N) __attribute__((format (printf, M, N)))
47 #define PRINTF_LIKE(M,N)
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@*/
56 #define UNUSED_ARG(ARG) ARG
59 #if defined(__GNUC__) && (__GNUC__ < 3)
60 # define GCC_VARMACROS 1
61 #elif !defined(S_SPLINT_S)
62 # define C99_VARMACROS 1
65 #if defined(WITH_MALLOC_DMALLOC)
66 # define DMALLOC_FUNC_CHECK 1
69 #elif defined(WITH_MALLOC_MPATROL)
72 #elif defined(WITH_MALLOC_BOEHM_GC)
79 # define malloc(n) GC_MALLOC(n)
80 # define calloc(m,n) GC_MALLOC((m)*(n))
81 # define realloc(p,n) GC_REALLOC((p),(n))
82 # define free(p) GC_FREE(p)
85 #elif defined(WITH_MALLOC_X3)
87 # define malloc(n) x3_malloc(__FILE__, __LINE__, (n))
89 # define calloc(m,n) x3_malloc(__FILE__, __LINE__, (m)*(n))
91 # define realloc(p,n) x3_realloc(__FILE__, __LINE__, (p), (n))
93 # define free(p) x3_free(__FILE__, __LINE__, (p))
95 # define strdup(s) x3_strdup(__FILE__, __LINE__, (s))
96 extern void *x3_malloc(const char *, unsigned int, size_t);
97 extern void *x3_realloc(const char *, unsigned int, void *, size_t);
98 extern char *x3_strdup(const char *, unsigned int, const char *);
99 extern void x3_free(const char *, unsigned int, void *);
100 # if !defined(NDEBUG)
101 extern void verify(const void *ptr
);
102 # define verify(x) verify(x)
105 #elif defined(WITH_MALLOC_SLAB)
106 # define malloc(n) slab_malloc(__FILE__, __LINE__, (n))
108 # define calloc(m,n) slab_malloc(__FILE__, __LINE__, (m)*(n))
110 # define realloc(p,n) slab_realloc(__FILE__, __LINE__, (p), (n))
112 # define free(p) slab_free(__FILE__, __LINE__, (p))
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)
126 # define verify(ptr) (void)(ptr)
129 extern char *x3_msnprintf(const int size
, const char *format
, ...);
130 #define msnprintf x3_msnprintf
133 extern int quit_services
;
134 extern struct log_type
*MAIN_LOG
;
136 typedef union irc_in_addr
{
142 #define irc_in_addr_is_valid(ADDR) (((ADDR).in6[0] && (ADDR).in6[0] != 65535) \
143 || (ADDR).in6[1] != (ADDR).in6[0] \
144 || (ADDR).in6[2] != (ADDR).in6[0] \
145 || (ADDR).in6[3] != (ADDR).in6[0] \
146 || (ADDR).in6[4] != (ADDR).in6[0] \
147 || (ADDR).in6[5] != (ADDR).in6[0] \
148 || (ADDR).in6[6] != (ADDR).in6[0] \
149 || (ADDR).in6[7] != (ADDR).in6[0])
150 #define irc_in_addr_is_ipv4(ADDR) (!(ADDR).in6[0] && !(ADDR).in6[1] \
151 && !(ADDR).in6[2] && !(ADDR).in6[3] \
152 && !(ADDR).in6[4] && (ADDR).in6[6] \
153 && (!(ADDR).in6[5] || (ADDR).in6[5] == 65535))
154 #define irc_in_addr_is_ipv6(ADDR) !irc_in_addr_is_ipv4(ADDR)
155 #define irc_in_addr_is_loopback(ADDR) (irc_in_addr_is_ipv4(ADDR) ? (ADDR).in6_8[12] == 127 \
156 : (ADDR).in6[0] == 0 && (ADDR).in6[1] == 0 \
157 && (ADDR).in6[2] == 0 && (ADDR).in6[3] == 0 \
158 && (ADDR).in6[4] == 0 && (ADDR).in6[5] == 0 \
159 && (ADDR).in6[6] == 0 && (ADDR).in6[7] == 1)
160 #define IRC_NTOP_MAX_SIZE 40
161 unsigned int irc_ntop(char *output
, unsigned int out_size
, const irc_in_addr_t
*addr
);
162 #define IRC_NTOP_MASK_MAX_SIZE (IRC_NTOP_MAX_SIZE + 4)
163 unsigned int irc_ntop_mask(char *output
, unsigned int out_size
, const irc_in_addr_t
*addr
, unsigned char bits
);
164 unsigned int irc_pton(irc_in_addr_t
*addr
, unsigned char *bits
, const char *input
);
165 unsigned int irc_check_mask(const irc_in_addr_t
*check
, const irc_in_addr_t
*mask
, unsigned char bits
);
166 const char *irc_ntoa(const irc_in_addr_t
*addr
);
168 int create_socket_client(struct uplinkNode
*target
);
169 void close_socket(void);
171 typedef void (*exit_func_t
)(void *extra
);
172 void reg_exit_func(exit_func_t handler
, void *extra
);
173 void call_exit_funcs(void);
175 char *mysep(char **sepstr
, char *delim
);
176 const char *inttobase64(char *buf
, unsigned int v
, unsigned int count
);
177 unsigned long base64toint(const char *s
, int count
);
178 int split_line(char *line
, int irc_colon
, int argv_size
, char *argv
[]);
180 /* match_ircglobs(oldglob, newglob) returns non-zero if oldglob is a superset of newglob */
181 #define match_ircglobs !mmatch
182 int mmatch(const char *glob
, const char *newglob
);
183 int match_ircglob(const char *text
, const char *glob
);
184 #define MATCH_USENICK 1
185 #define MATCH_VISIBLE 2
186 int user_matches_glob(struct userNode
*user
, const char *glob
, int flags
, int shared
);
187 int is_overmask(char *mask
);
190 int is_ircmask(const char *text
);
191 int is_gline(const char *text
);
192 int is_shun(const char *text
);
194 char *sanitize_ircmask(char *text
);
196 unsigned long ParseInterval(const char *interval
);
197 unsigned long ParseVolume(const char *volume
);
199 #define MD5_CRYPT_LENGTH 42
200 /* buffer[] must be at least MD5_CRYPT_LENGTH bytes long */
201 const char *cryptpass(const char *pass
, char buffer
[]);
202 int checkpass(const char *pass
, const char *crypt
);
204 int split_ircmask(char *text
, char **nick
, char **ident
, char **host
);
205 char *unsplit_string(char *set
[], unsigned int max
, char *dest
);
206 extern char* x3_strtok(char** save
, char* str
, char* fs
);
208 int valid_email(const char *email
);
210 #define DECLARE_LIST(STRUCTNAME,ITEMTYPE) struct STRUCTNAME {\
211 unsigned int used, size;\
214 void STRUCTNAME##_init(struct STRUCTNAME *list);\
215 void STRUCTNAME##_append(struct STRUCTNAME *list, ITEMTYPE new_item);\
216 int STRUCTNAME##_remove(struct STRUCTNAME *list, ITEMTYPE new_item);\
217 void STRUCTNAME##_clean(struct STRUCTNAME *list)
219 #define DEFINE_LIST(STRUCTNAME,ITEMTYPE) \
220 void STRUCTNAME##_init(struct STRUCTNAME *list) {\
223 list->list = malloc(list->size*sizeof(list->list[0]));\
225 void STRUCTNAME##_append(struct STRUCTNAME *list, ITEMTYPE new_item) {\
227 if (list->used == list->size) {\
228 list->size = list->size ? (list->size << 1) : 4;\
229 list->list = realloc(list->list, list->size*sizeof(list->list[0]));\
231 list->list[list->used++] = new_item;\
233 int STRUCTNAME##_remove(struct STRUCTNAME *list, ITEMTYPE new_item) {\
234 unsigned int n, found;\
236 for (found=n=0; n<list->used; n++) {\
237 if (list->list[n] == new_item) {\
238 memmove(list->list+n, list->list+n+1, (list->used-n-1)*sizeof(list->list[n]));\
245 void STRUCTNAME##_clean(struct STRUCTNAME *list) {\
246 list->used = list->size = 0;\
251 /* The longest string that is likely to be produced in English is "10
252 * minutes, and 10 seconds" (27 characters). Other languages will
253 * vary, so there's plenty of leeway.
255 #define INTERVALLEN 50
258 char *intervalString(char *output
, time_t interval
, struct handle_info
*hi
);
259 int getipbyname(const char *name
, unsigned long *ip
);
260 int set_policer_param(const char *param
, void *data
, void *extra
);
261 const char *strtab(unsigned int ii
);
262 char *time2str(time_t thetime
);
263 extern char *pretty_mask(char *mask
);
265 void tools_init(void);
266 void tools_cleanup(void);
268 int irccasecmp(const char *stra
, const char *strb
);
269 int ircncasecmp(const char *stra
, const char *strb
, unsigned int len
);
270 const char *irccasestr(const char *haystack
, const char *needle
);
271 char *ircstrlower(char *str
);
273 DECLARE_LIST(string_buffer
, char);
274 void string_buffer_append_string(struct string_buffer
*buf
, const char *tail
);
275 void string_buffer_append_substring(struct string_buffer
*buf
, const char *tail
, unsigned int len
);
276 void string_buffer_append_vprintf(struct string_buffer
*buf
, const char *fmt
, va_list args
);
277 void string_buffer_append_printf(struct string_buffer
*buf
, const char *fmt
, ...);
278 void string_buffer_replace(struct string_buffer
*buf
, unsigned int from
, unsigned int len
, const char *repl
);
280 irc_strtolower(char *str
);
282 int str_is_number(const char *str
);
283 #define enabled_string(string) (!irccasecmp((string), "on") || !strcmp((string), "1") || !irccasecmp((string), "enabled"))
284 #define disabled_string(string) (!irccasecmp((string), "off") || !strcmp((string), "0") || !irccasecmp((string), "disabled"))
285 #define true_string(string) (!irccasecmp((string), "true") || !strcmp((string), "1") || !irccasecmp((string), "yes"))
286 #define false_string(string) (!irccasecmp((string), "false") || !strcmp((string), "0") || !irccasecmp((string), "no"))
288 #endif /* ifdef COMMON_H */