]> jfr.im git - irc/quakenet/newserv.git/blob - patricia/patricia_commands.c
f423f0a329062e083a6415316037f0bc8fc236eb
[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 #include "../lib/version.h"
17
18 MODULE_VERSION("")
19
20 FILE* dumpip_logfp;
21 int nc_cmd_dumptree(void *source, int cargc, char **cargv);
22 int nc_cmd_nodecount(void *source, int cargc, char **cargv);
23
24 void _init() {
25 if (!(dumpip_logfp = fopen("log/iplist", "w")))
26 Error("dumpip", ERR_ERROR, "Failed to open log file!");
27 registercontrolhelpcmd("dumptree", NO_DEVELOPER, 2, &nc_cmd_dumptree,
28 "Dumps diaganostic information on the patricia trie structure\n"
29 "Usage: dumptree <ipv4|ipv6|cidr4|cidr6> [int]\n"
30 "Nodes with prefixies Only:\n"
31 "No arguments - default prints: ptr, ip\n"
32 "1: ptr, prefixptr, bitlen, refcount, ip\n"
33 "2: ptr, bit, usercount, ip\n"
34 "3: ptr, leftptr, rightptr, parentptr\n"
35 "4: ptr, ext0, ext1, ext2, ext3, ext4\n"
36 "All Notes (inc no prefixies):\n"
37 "10: ptr, prefixptr, ip\n"
38 "11: ptr, prefixbitlen, refcount,ip\n"
39 "12: ptr, bitlen, usercount, ip\n"
40 "13: ptr, leftptr, rightptr, parentptr\n"
41 "14: ptr, ext0, ext1, ext2, ext3, ext4");
42 registercontrolhelpcmd("nodecount", NO_OPER, 1, &nc_cmd_nodecount, "Displays number of users on a given ipv4/6 or cidr4/6");
43 }
44
45 void _fini() {
46 if (dumpip_logfp)
47 fclose(dumpip_logfp);
48 deregistercontrolcmd("dumptree", &nc_cmd_dumptree);
49 deregistercontrolcmd("nodecount", &nc_cmd_nodecount);
50 }
51
52 int nc_cmd_dumptree(void *source, int cargc, char **cargv) {
53 nick *np=(nick *)source;
54 struct irc_in_addr sin;
55 unsigned char bits;
56 patricia_node_t *head, *node;
57 unsigned int level=0;
58 int i = 0;
59
60 if (cargc < 1) {
61 controlreply(np, "Syntax: dumptree <ipv4|ipv6|cidr4|cidr6>");
62 return CMD_OK;
63 }
64
65 if (ipmask_parse(cargv[0], &sin, &bits) == 0) {
66 controlreply(np, "Invalid mask.");
67 return CMD_OK;
68 }
69
70 if (cargc>1) {
71 level=strtoul(cargv[1],NULL,10);
72 }
73
74 head = refnode(iptree, &sin, bits);
75
76 if (level < 10) {
77 PATRICIA_WALK(head, node)
78 {
79 switch (level) {
80 case 0:
81 controlreply(np,"%p: %s", node, IPtostr(node->prefix->sin));
82 break;
83 case 1:
84 controlreply(np,"%p: prefix %p, bit %d, ref_count %d, IP: %s",node, node->prefix,
85 node->prefix->bitlen, node->prefix->ref_count, IPtostr(node->prefix->sin));
86 break;
87 case 2:
88 controlreply(np,"%p: bit: %d, usercount: %d, IP: %s", node, node->bit, node->usercount, IPtostr(node->prefix->sin));
89 break;
90 case 3:
91 controlreply(np,"%p: L: %p, R: %p P: %p", node, node->l, node->r, node->parent);
92 break;
93 case 4:
94 controlreply(np,"%p: 0: %p, 1: %p, 2: %p, 3: %p, 4: %p", node,
95 node->exts[0], node->exts[1], node->exts[2], node->exts[3], node->exts[4]);
96 break;
97 default:
98 if( i == 0 ) controlreply(np,"Invalid Level");
99 }
100 if ( i++ > 500) {
101 controlreply(np,"too many... aborting...");
102 break;
103 }
104 }
105 PATRICIA_WALK_END;
106 } else {
107 PATRICIA_WALK_ALL(head, node)
108 {
109 switch (level) {
110 case 10:
111 controlreply(np,"%p: prefix: %p %s", node, node->prefix, node->prefix?IPtostr(node->prefix->sin):"");
112 break;
113 case 11:
114 if(node->prefix)
115 controlreply(np,"%p: prefix bit: %d, ref_count %d, IP: %s",node,
116 node->prefix->bitlen, node->prefix->ref_count, IPtostr(node->prefix->sin));
117 else
118 controlreply(np,"%p: --", node);
119 break;
120 case 12:
121 controlreply(np,"%p: bit: %d, usercount: %d, IP: %s", node, node->bit, node->usercount, node->prefix?IPtostr(node->prefix->sin):"");
122 break;
123 case 13:
124 controlreply(np,"%p: L: %p, R: %p P: %p", node, node->l, node->r, node->parent);
125 break;
126 case 14:
127 controlreply(np,"%p%s 0: %p, 1: %p, 2: %p, 3: %p, 4: %p", node, node->prefix?"-":":",
128 node->exts[0], node->exts[1], node->exts[2], node->exts[3], node->exts[4]);
129 break;
130 default:
131 if ( i == 0 ) controlreply(np,"Invalid Level");
132 }
133 if ( i++ > 500) {
134 controlreply(np,"too many... aborting...");
135 break;
136 }
137 }
138 PATRICIA_WALK_END;
139 }
140 derefnode(iptree, head);
141 return CMD_OK;
142 }
143
144 int nc_cmd_nodecount(void *source, int cargc, char **cargv) {
145 nick *np = (nick *)source;
146 struct irc_in_addr sin;
147 unsigned char bits;
148 patricia_node_t *head;
149 int count;
150
151 if (cargc < 1) {
152 controlreply(np, "Syntax: nodecount <ipv4|ipv6|cidr4|cidr6>");
153 return CMD_OK;
154 }
155
156 if (ipmask_parse(cargv[0], &sin, &bits) == 0) {
157 controlreply(np, "Invalid mask.");
158
159 return CMD_OK;
160 }
161
162 head = refnode(iptree, &sin, bits);
163
164 count = head->usercount;
165
166 derefnode(iptree, head);
167
168 controlreply(np, "%d user(s) found.", count);
169
170 return CMD_OK;
171 }
172