]>
jfr.im git - irc/quakenet/newserv.git/blob - newsearch/ns-cidr.c
10 #include "../irc/irc_config.h"
11 #include "../lib/irc_string.h"
12 #include "../lib/irc_ipv6.h"
14 struct cidr_localdata
{
19 void *cidr_exe(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
);
20 void cidr_free(searchCtx
*ctx
, struct searchNode
*thenode
);
22 struct searchNode
*cidr_parse(searchCtx
*ctx
, int argc
, char **argv
) {
23 struct searchNode
*thenode
, *convsn
;
24 struct cidr_localdata
*c
;
26 struct irc_in_addr ip
;
31 parseError
= "usage: cidr ip/mask";
35 if (!(convsn
=argtoconststr("cidr", ctx
, argv
[0], &p
)))
38 ret
= ipmask_parse(p
, &ip
, &mask
);
39 convsn
->free(ctx
, convsn
);
42 parseError
= "usage: cidr ip/mask";
46 if(!irc_in_addr_is_ipv4(&ip
)) {
47 parseError
= "cidr: sorry, no IPv6 yet";
54 parseError
= "cidr: bad mask supplied";
58 if (!(thenode
=(struct searchNode
*)malloc(sizeof (struct searchNode
)))) {
59 parseError
= "malloc: could not allocate memory for this search.";
63 thenode
->returntype
= RETURNTYPE_BOOL
;
64 if (!(c
= malloc(sizeof(struct cidr_localdata
)))) {
65 /* couldn't malloc() memory for thenode->localdata, so free thenode to avoid leakage */
66 parseError
= "malloc: could not allocate memory for this search.";
73 } else if(mask
< 32) {
74 c
->mask
= 0xffffffff << (32 - mask
);
77 c
->ip
= irc_in_addr_v4_to_int(&ip
) & c
->mask
;
79 thenode
->localdata
= (void *)c
;
80 thenode
->free
= cidr_free
;
81 thenode
->exe
= cidr_exe
;
86 void *cidr_exe(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
87 nick
*np
= (nick
*)theinput
;
88 struct cidr_localdata
*c
= thenode
->localdata
;
90 struct irc_in_addr
*sin
;
92 sin
= &np
->ipnode
->prefix
->sin
;
93 if(!irc_in_addr_is_ipv4(sin
))
96 ip
= irc_in_addr_v4_to_int(sin
);
97 if((ip
& c
->mask
) == c
->ip
)
103 void cidr_free(searchCtx
*ctx
, struct searchNode
*thenode
) {
104 free(thenode
->localdata
);