]>
Commit | Line | Data |
---|---|---|
8a959859 CP |
1 | /* |
2 | * CIDR functionality | |
3 | */ | |
4 | ||
5 | #include "newsearch.h" | |
6 | ||
7 | #include <stdio.h> | |
8 | #include <stdlib.h> | |
b0733e58 | 9 | #include <string.h> |
8a959859 CP |
10 | |
11 | #include "../irc/irc_config.h" | |
12 | #include "../lib/irc_string.h" | |
13 | #include "../lib/irc_ipv6.h" | |
14 | ||
15 | struct cidr_localdata { | |
b0733e58 GB |
16 | struct irc_in_addr ip; |
17 | unsigned char bits; | |
8a959859 CP |
18 | }; |
19 | ||
20 | void *cidr_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput); | |
21 | void cidr_free(searchCtx *ctx, struct searchNode *thenode); | |
22 | ||
23 | struct searchNode *cidr_parse(searchCtx *ctx, int argc, char **argv) { | |
31686847 | 24 | struct searchNode *thenode, *convsn; |
8a959859 | 25 | struct cidr_localdata *c; |
8a959859 | 26 | struct irc_in_addr ip; |
b0733e58 | 27 | unsigned char bits; |
31686847 CP |
28 | char *p; |
29 | int ret; | |
30 | ||
31 | if(argc != 1) { | |
8a959859 CP |
32 | parseError = "usage: cidr ip/mask"; |
33 | return NULL; | |
34 | } | |
35 | ||
31686847 CP |
36 | if (!(convsn=argtoconststr("cidr", ctx, argv[0], &p))) |
37 | return NULL; | |
38 | ||
b0733e58 | 39 | ret = ipmask_parse(p, &ip, &bits); |
31686847 CP |
40 | convsn->free(ctx, convsn); |
41 | ||
42 | if(!ret) { | |
43 | parseError = "usage: cidr ip/mask"; | |
44 | return NULL; | |
45 | } | |
46 | ||
8a959859 CP |
47 | if (!(thenode=(struct searchNode *)malloc(sizeof (struct searchNode)))) { |
48 | parseError = "malloc: could not allocate memory for this search."; | |
49 | return NULL; | |
50 | } | |
51 | ||
52 | thenode->returntype = RETURNTYPE_BOOL; | |
53 | if (!(c = malloc(sizeof(struct cidr_localdata)))) { | |
54 | /* couldn't malloc() memory for thenode->localdata, so free thenode to avoid leakage */ | |
55 | parseError = "malloc: could not allocate memory for this search."; | |
56 | free(thenode); | |
57 | return NULL; | |
58 | } | |
59 | ||
b0733e58 GB |
60 | memcpy(&c->ip, &ip, sizeof(struct irc_in_addr)); |
61 | c->bits = bits; | |
8a959859 CP |
62 | |
63 | thenode->localdata = (void *)c; | |
64 | thenode->free = cidr_free; | |
65 | thenode->exe = cidr_exe; | |
66 | ||
67 | return thenode; | |
68 | } | |
69 | ||
70 | void *cidr_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput) { | |
71 | nick *np = (nick *)theinput; | |
72 | struct cidr_localdata *c = thenode->localdata; | |
8a959859 | 73 | |
b0733e58 | 74 | if(!ipmask_check(&np->p_ipaddr, &c->ip, c->bits)) |
8a959859 CP |
75 | return (void *)0; |
76 | ||
b0733e58 | 77 | return (void *)1; |
8a959859 CP |
78 | } |
79 | ||
80 | void cidr_free(searchCtx *ctx, struct searchNode *thenode) { | |
81 | free(thenode->localdata); | |
82 | free(thenode); | |
83 | } |