]> jfr.im git - irc/quakenet/newserv.git/blame - patricia/patricia.c
Merge default.
[irc/quakenet/newserv.git] / patricia / patricia.c
CommitLineData
f9e9119b
P
1#include "../core/nsmalloc.h"
2#include "../lib/irc_string.h"
3#include "../lib/version.h"
4#include "patricia.h"
5#include "../core/error.h"
6#include "../core/hooks.h"
7#include "../control/control.h"
8
9#include <assert.h> /* assert */
10#include <stdio.h>
11#include <string.h>
12
13MODULE_VERSION("");
14
15patricia_tree_t *iptree;
16sstring *nodeextnames[PATRICIA_MAXSLOTS];
17
18void patriciastats(int hooknum, void *arg);
19
20void _init(void) {
21 iptree = patricia_new_tree(PATRICIA_MAXBITS);
22 assert(iptree);
23
24 registerhook(HOOK_CORE_STATSREQUEST,&patriciastats);
25}
26
27void _fini(void) {
28 deregisterhook(HOOK_CORE_STATSREQUEST,&patriciastats);
29 patricia_destroy_tree (iptree, NULL);
30 nsfreeall(POOL_PATRICIA);
31}
32
33void patriciastats(int hooknum, void *arg) {
34 long level=(long)arg;
35 char buf[100];
36 patricia_node_t *head, *node;
80882063 37 int i,j,k,l,refcnt;
f9e9119b 38
08a983e0 39 if (level <= 5)
f9e9119b
P
40 return;
41
42 sprintf(buf, "Patricia: %6d Active Nodes (%d bits)", iptree->num_active_node, iptree->maxbits);
43 triggerhook(HOOK_CORE_STATSREPLY,buf);
44
45 head = iptree->head;
46
80882063 47 i=0;j=0,refcnt=0;
f9e9119b 48 PATRICIA_WALK_ALL(head, node) {
80882063
P
49 if ( node->prefix ) {
50 refcnt+=node->prefix->ref_count;
f9e9119b 51 j++;
80882063 52 } else
f9e9119b
P
53 i++;
54 } PATRICIA_WALK_END;
80882063 55 sprintf(buf, "Patricia: %6d Nodes, %6d Prefix (walk all), references %6d", i,j, refcnt);
f9e9119b
P
56 triggerhook(HOOK_CORE_STATSREPLY,buf);
57
58 head = iptree->head;
80882063 59 i=0;j=0;k=0;l=0,refcnt=0;
f9e9119b
P
60 PATRICIA_WALK(head, node) {
61 if ( node->prefix ) {
62 if (irc_in_addr_is_ipv4(&(node->prefix->sin)))
63 k++;
64 else
65 l++;
80882063 66 refcnt+=node->prefix->ref_count;
f9e9119b
P
67 j++;
68 } else
69 i++;
70 } PATRICIA_WALK_END;
80882063 71 sprintf(buf, "Patricia: %6d Nodes, %6d Prefix (walk prefixes only), references %6d", i,j, refcnt);
f9e9119b
P
72 triggerhook(HOOK_CORE_STATSREPLY,buf);
73 sprintf(buf, "Patricia: %6d IP4Node, %6d IP6Node", k, l);
74 triggerhook(HOOK_CORE_STATSREPLY,buf);
75
76 j=0;
77 for (i=0;i<PATRICIA_MAXSLOTS;i++) {
78 if (nodeextnames[i]!=NULL) {
79 j++;
80 }
81 }
82 sprintf(buf, "Patricia: %6d ExtsUsed, %5d Max", j,PATRICIA_MAXSLOTS);
83 triggerhook(HOOK_CORE_STATSREPLY,buf);
84
85}
86
87int registernodeext(const char *name) {
88 int i;
89
90 if (findnodeext(name)!=-1) {
91 Error("patricia",ERR_WARNING,"Tried to register duplicate node extension %s",name);
92 return -1;
93 }
94
95 for (i=0;i<PATRICIA_MAXSLOTS;i++) {
96 if (nodeextnames[i]==NULL) {
97 nodeextnames[i]=getsstring(name,100);
98 return i;
99 }
100 }
101
102 Error("patricia",ERR_WARNING,"Tried to register too many extensions: %s",name);
103 return -1;
104}
105
106int findnodeext(const char *name) {
107 int i;
108
109 for (i=0;i<PATRICIA_MAXSLOTS;i++) {
110 if (nodeextnames[i]!=NULL && !ircd_strcmp(name,nodeextnames[i]->content)) {
111 return i;
112 }
113 }
114
115 return -1;
116}
117
118void releasenodeext(int index) {
119 patricia_node_t *head, *node;
120
121 freesstring(nodeextnames[index]);
122 nodeextnames[index]=NULL;
123
124 head = iptree->head;
125
801fd068 126 PATRICIA_WALK_CLEAR(head, node)
f9e9119b 127 {
801fd068 128 if ( node->exts[index] ) {
609fa18c
P
129 derefnode(iptree,node);
130 node->exts[index]=NULL;
801fd068 131 }
801fd068 132 } PATRICIA_WALK_CLEAR_END;
f9e9119b
P
133}
134