From: Martin Mares Date: Wed, 29 Aug 2001 10:40:59 +0000 (+0000) Subject: Added generic functions for IP address access lists. X-Git-Tag: holmes-import~1480 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=04294f573be8073e305f70621e6e74e2440f7f24;p=libucw.git Added generic functions for IP address access lists. --- diff --git a/lib/Makefile b/lib/Makefile index c5430ac0..c353013b 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -7,7 +7,7 @@ SHLIB_OBJS=alloc.o alloc_str.o ctmatch.o db.o fastbuf.o fb-file.o fb-mem.o lists log.o log2.o md5.o md5hex.o mmap.o pagecache.o patimatch.o patmatch.o pool.o \ prime.o random.o realloc.o regex.o timer.o url.o wildmatch.o \ wordsplit.o str_ctype.o str_upper.o bucket.o conf.o object.o sorter.o \ - finger.o proctitle.o + finger.o proctitle.o ipaccess.o obj/lib/libsh.a: $(addprefix obj/lib/,$(SHLIB_OBJS)) diff --git a/lib/ipaccess.c b/lib/ipaccess.c new file mode 100644 index 00000000..8a484169 --- /dev/null +++ b/lib/ipaccess.c @@ -0,0 +1,78 @@ +/* + * Sherlock Library -- IP address access lists + * + * (c) 1997--2001 Martin Mares + */ + +#include "lib/lib.h" +#include "lib/conf.h" +#include "lib/chartype.h" +#include "lib/ipaccess.h" + +#include + +struct ipaccess_entry { + node n; + uns allow; + u32 addr, mask; +}; + +void +ipaccess_init(ipaccess_list *l) +{ + init_list(l); +} + +static byte * +parse_ip(byte *x, u32 *a) +{ + uns i, q; + u32 z = 0; + + for(i=0; i<4; i++) + { + q = 0; + while (Cdigit(*x)) + { + q = q*10 + *x++ - '0'; + if (q > 255) + return "Invalid IP address"; + } + if (*x++ != ((i == 3) ? 0 : '.')) + return "Invalid IP address"; + z = (z << 8) | q; + } + *a = z; + return NULL; +} + +byte * +ipaccess_parse(ipaccess_list *l, byte *c, int is_allow) +{ + byte *p = strchr(c, '/'); + byte *q; + struct ipaccess_entry *a = cfg_malloc(sizeof(struct ipaccess_entry)); + + a->allow = is_allow; + if (p) + { + *p++ = 0; + if (q = parse_ip(p, &a->mask)) + return q; + } + else + a->mask = ~0; + add_tail(l, &a->n); + return parse_ip(c, &a->addr); +} + +int +ipaccess_check(ipaccess_list *l, u32 ip) +{ + struct ipaccess_entry *a; + + DO_FOR_ALL(a, *l) + if (! ((ip ^ a->addr) & a->mask)) + return a->allow; + return 0; +} diff --git a/lib/ipaccess.h b/lib/ipaccess.h new file mode 100644 index 00000000..fefd34a8 --- /dev/null +++ b/lib/ipaccess.h @@ -0,0 +1,13 @@ +/* + * Sherlock Library -- IP address access lists + * + * (c) 1997--2001 Martin Mares + */ + +#include "lib/lists.h" + +typedef list ipaccess_list; + +void ipaccess_init(ipaccess_list *l); +byte *ipaccess_parse(ipaccess_list *l, byte *c, int is_allow); +int ipaccess_check(ipaccess_list *l, u32 ip);