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