]>
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 srvx.
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.
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));
42 #define INADDR_NONE 0xffffffffL
44 #ifndef INADDR_LOOPBACK
45 #define INADDR_LOOPBACK 0x7f000001L
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)
52 #define PRINTF_LIKE(M,N) __attribute__((format (printf, M, N)))
54 #define PRINTF_LIKE(M,N)
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@*/
63 #define UNUSED_ARG(ARG) ARG
66 #if defined(WITH_MALLOC_DMALLOC)
67 #define DMALLOC_FUNC_CHECK 1
70 #elif defined(WITH_MALLOC_MPATROL)
73 #elif defined(WITH_MALLOC_BOEHM_GC)
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)
89 extern int quit_services
;
90 extern struct log_type
*MAIN_LOG
;
92 int create_socket_client(struct uplinkNode
*target
);
93 void close_socket(void);
95 typedef void (*exit_func_t
)(void);
96 void reg_exit_func(exit_func_t handler
);
97 void call_exit_funcs(void);
99 char *mysep(char **sepstr
, char *delim
);
100 const char *inttobase64(char *buf
, unsigned int v
, unsigned int count
);
101 unsigned long base64toint(const char *s
, int count
);
102 int split_line(char *line
, int irc_colon
, int argv_size
, char *argv
[]);
104 /* match_ircglobs(oldglob, newglob) returns non-zero if oldglob is a superset of newglob */
105 #define match_ircglobs !mmatch
106 int mmatch(const char *glob
, const char *newglob
);
107 int match_ircglob(const char *text
, const char *glob
);
108 int user_matches_glob(struct userNode
*user
, const char *glob
, int include_nick
);
110 int is_ircmask(const char *text
);
111 int is_gline(const char *text
);
113 char *sanitize_ircmask(char *text
);
115 unsigned long ParseInterval(const char *interval
);
116 unsigned long ParseVolume(const char *volume
);
117 int parse_ipmask(const char *str
, struct in_addr
*addr
, unsigned long *mask
);
118 #define MATCH_IPMASK(test, addr, mask) (((ntohl(test.s_addr) & mask) ^ (ntohl(addr.s_addr) & mask)) == 0)
120 #define MD5_CRYPT_LENGTH 42
121 /* buffer[] must be at least MD5_CRYPT_LENGTH bytes long */
122 const char *cryptpass(const char *pass
, char buffer
[]);
123 int checkpass(const char *pass
, const char *crypt
);
125 int split_ircmask(char *text
, char **nick
, char **ident
, char **host
);
126 char *unsplit_string(char *set
[], unsigned int max
, char *dest
);
128 #define DECLARE_LIST(STRUCTNAME,ITEMTYPE) struct STRUCTNAME {\
129 unsigned int used, size;\
132 void STRUCTNAME##_init(struct STRUCTNAME *list);\
133 void STRUCTNAME##_append(struct STRUCTNAME *list, ITEMTYPE new_item);\
134 int STRUCTNAME##_remove(struct STRUCTNAME *list, ITEMTYPE new_item);\
135 void STRUCTNAME##_clean(struct STRUCTNAME *list)
137 #define DEFINE_LIST(STRUCTNAME,ITEMTYPE) \
138 void STRUCTNAME##_init(struct STRUCTNAME *list) {\
141 list->list = malloc(list->size*sizeof(list->list[0]));\
143 void STRUCTNAME##_append(struct STRUCTNAME *list, ITEMTYPE new_item) {\
144 if (list->used == list->size) {\
145 list->size = list->size ? (list->size << 1) : 4;\
146 list->list = realloc(list->list, list->size*sizeof(list->list[0]));\
148 list->list[list->used++] = new_item;\
150 int STRUCTNAME##_remove(struct STRUCTNAME *list, ITEMTYPE new_item) {\
151 unsigned int n, found;\
152 for (found=n=0; n<list->used; n++) {\
153 if (list->list[n] == new_item) {\
154 memmove(list->list+n, list->list+n+1, (list->used-n-1)*sizeof(list->list[n]));\
161 void STRUCTNAME##_clean(struct STRUCTNAME *list) {\
162 list->used = list->size = 0;\
166 /* The longest string that is likely to be produced in English is "10
167 * minutes, and 10 seconds" (27 characters). Other languages will
168 * vary, so there's plenty of leeway.
170 #define INTERVALLEN 50
173 char *intervalString(char *output
, time_t interval
, struct handle_info
*hi
);
174 int getipbyname(const char *name
, unsigned long *ip
);
175 int set_policer_param(const char *param
, void *data
, void *extra
);
176 const char *strtab(unsigned int ii
);
178 void tools_init(void);
179 void tools_cleanup(void);
181 int irccasecmp(const char *stra
, const char *strb
);
182 int ircncasecmp(const char *stra
, const char *strb
, unsigned int len
);
183 const char *irccasestr(const char *haystack
, const char *needle
);
185 DECLARE_LIST(string_buffer
, char);
186 void string_buffer_append_string(struct string_buffer
*buf
, const char *tail
);
187 void string_buffer_append_substring(struct string_buffer
*buf
, const char *tail
, unsigned int len
);
188 void string_buffer_append_vprintf(struct string_buffer
*buf
, const char *fmt
, va_list args
);
189 void string_buffer_append_printf(struct string_buffer
*buf
, const char *fmt
, ...);
190 void string_buffer_replace(struct string_buffer
*buf
, unsigned int from
, unsigned int len
, const char *repl
);
192 #define enabled_string(string) (!irccasecmp((string), "on") || !strcmp((string), "1") || !irccasecmp((string), "enabled"))
193 #define disabled_string(string) (!irccasecmp((string), "off") || !strcmp((string), "0") || !irccasecmp((string), "disabled"))
194 #define true_string(string) (!irccasecmp((string), "true") || !strcmp((string), "1") || !irccasecmp((string), "yes"))
195 #define false_string(string) (!irccasecmp((string), "false") || !strcmp((string), "0") || !irccasecmp((string), "no"))
197 #endif /* ifdef COMMON_H */