]> jfr.im git - irc/quakenet/newserv.git/blame - newsearch/ns-cidr.c
LUA: port luadb to dbapi2 to drop postgres dependency
[irc/quakenet/newserv.git] / newsearch / ns-cidr.c
CommitLineData
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
15struct cidr_localdata {
b0733e58
GB
16 struct irc_in_addr ip;
17 unsigned char bits;
8a959859
CP
18};
19
20void *cidr_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput);
21void cidr_free(searchCtx *ctx, struct searchNode *thenode);
22
23struct 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
70void *cidr_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput) {
71 nick *np = (nick *)theinput;
72 struct cidr_localdata *c = thenode->localdata;
8a959859 73
fdcb5d66 74 if(!ipmask_check(&np->ipaddress, &c->ip, c->bits))
8a959859
CP
75 return (void *)0;
76
b0733e58 77 return (void *)1;
8a959859
CP
78}
79
80void cidr_free(searchCtx *ctx, struct searchNode *thenode) {
81 free(thenode->localdata);
82 free(thenode);
83}