]> jfr.im git - irc/quakenet/newserv.git/blob - patricia/patricia_commands.c
patricia trie changes
[irc/quakenet/newserv.git] / patricia / patricia_commands.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdarg.h>
4 #include <string.h>
5 #include <time.h>
6
7 #include "../nick/nick.h"
8 #include "../localuser/localuserchannel.h"
9 #include "../core/hooks.h"
10 #include "../core/schedule.h"
11 #include "../lib/array.h"
12 #include "../lib/base64.h"
13 #include "../lib/irc_string.h"
14 #include "../lib/splitline.h"
15 #include "../control/control.h"
16
17 FILE* dumpip_logfp;
18 int nc_cmd_dumptree(void *source, int cargc, char **cargv);
19 int nc_cmd_nodecount(void *source, int cargc, char **cargv);
20
21 void _init() {
22 if (!(dumpip_logfp = fopen("log/iplist", "w")))
23 Error("dumpip", ERR_ERROR, "Failed to open log file!");
24 registercontrolcmd("dumptree", 10, 2, &nc_cmd_dumptree);
25 registercontrolcmd("nodecount", 10, 1, &nc_cmd_nodecount);
26 }
27
28 void _fini() {
29 if (dumpip_logfp)
30 fclose(dumpip_logfp);
31 deregistercontrolcmd("dumptree", &nc_cmd_dumptree);
32 deregistercontrolcmd("nodecount", &nc_cmd_nodecount);
33 }
34
35 int nc_cmd_dumptree(void *source, int cargc, char **cargv) {
36 nick *np=(nick *)source;
37 struct irc_in_addr sin;
38 unsigned char bits;
39 patricia_node_t *head, *node;
40 unsigned int level=0;
41 int i = 0;
42
43 if (cargc < 1) {
44 controlreply(np, "Syntax: dumptree <ipv4|ipv6|cidr4|cidr6>");
45 return CMD_OK;
46 }
47
48 if (ipmask_parse(cargv[0], &sin, &bits) == 0) {
49 controlreply(np, "Invalid mask.");
50 return CMD_OK;
51 }
52
53 if (cargc>1) {
54 level=strtoul(cargv[1],NULL,10);
55 }
56
57 head = refnode(iptree, &sin, bits);
58
59 if (level < 10) {
60 PATRICIA_WALK(head, node)
61 {
62 switch (level) {
63 case 0:
64 controlreply(np,"%p: %s", node, IPtostr(node->prefix->sin));
65 break;
66 case 1:
67 controlreply(np,"%p: prefix %p, bit %d, ref_count %d, IP: %s",node, node->prefix,
68 node->prefix->bitlen, node->prefix->ref_count, IPtostr(node->prefix->sin));
69 break;
70 case 2:
71 controlreply(np,"%p: bit: %d, usercount: %d, IP: %s", node, node->bit, node->usercount, IPtostr(node->prefix->sin));
72 break;
73 case 3:
74 controlreply(np,"%p: L: %p, R: %p P: %p", node, node->l, node->r, node->parent);
75 break;
76 case 4:
77 controlreply(np,"%p: 0: %p, 1: %p, 2: %p, 3: %p, 4: %p", node,
78 node->exts[0], node->exts[1], node->exts[2], node->exts[3], node->exts[4]);
79 break;
80 default:
81 if( i == 0 ) controlreply(np,"Invalid Level");
82 }
83 if ( i++ > 500) {
84 controlreply(np,"too many... aborting...");
85 break;
86 }
87 }
88 PATRICIA_WALK_END;
89 } else {
90 PATRICIA_WALK_ALL(head, node)
91 {
92 switch (level) {
93 case 10:
94 controlreply(np,"%p: prefix: %p %s", node, node->prefix, node->prefix?IPtostr(node->prefix->sin):"");
95 break;
96 case 11:
97 if(node->prefix)
98 controlreply(np,"%p: prefix bit: %d, ref_count %d, IP: %s",node,
99 node->prefix->bitlen, node->prefix->ref_count, IPtostr(node->prefix->sin));
100 else
101 controlreply(np,"%p: --", node);
102 break;
103 case 12:
104 controlreply(np,"%p: bit: %d, usercount: %d, IP: %s", node, node->bit, node->usercount, node->prefix?IPtostr(node->prefix->sin):"");
105 break;
106 case 13:
107 controlreply(np,"%p: L: %p, R: %p P: %p", node, node->l, node->r, node->parent);
108 break;
109 case 14:
110 controlreply(np,"%p%s 0: %p, 1: %p, 2: %p, 3: %p, 4: %p", node, node->prefix?"-":":",
111 node->exts[0], node->exts[1], node->exts[2], node->exts[3], node->exts[4]);
112 break;
113 default:
114 if ( i == 0 ) controlreply(np,"Invalid Level");
115 }
116 if ( i++ > 500) {
117 controlreply(np,"too many... aborting...");
118 break;
119 }
120 }
121 PATRICIA_WALK_END;
122 }
123 derefnode(iptree, head);
124 return CMD_OK;
125 }
126
127 int nc_cmd_nodecount(void *source, int cargc, char **cargv) {
128 nick *np = (nick *)source;
129 struct irc_in_addr sin;
130 unsigned char bits;
131 patricia_node_t *head, *node;
132 int count;
133
134 if (cargc < 1) {
135 controlreply(np, "Syntax: nodecount <ipv4|ipv6|cidr4|cidr6>");
136 return CMD_OK;
137 }
138
139 if (ipmask_parse(cargv[0], &sin, &bits) == 0) {
140 controlreply(np, "Invalid mask.");
141
142 return CMD_OK;
143 }
144
145 head = refnode(iptree, &sin, bits);
146
147 count = head->usercount;
148
149 derefnode(iptree, head);
150
151 controlreply(np, "%d user(s) found.", count);
152
153 return CMD_OK;
154 }
155