]>
Commit | Line | Data |
---|---|---|
c86edd1d Q |
1 | /* nickhelpers.c */ |
2 | ||
3 | #include "nick.h" | |
4 | #include "../lib/flags.h" | |
5 | #include "../lib/irc_string.h" | |
6 | #include "../irc/irc_config.h" | |
7 | #include "../core/error.h" | |
8 | #include "../lib/sstring.h" | |
9 | ||
10 | #define hosthash(x) ((crc32i(x))%HOSTHASHSIZE) | |
11 | #define realnamehash(x) ((crc32(x))%REALNAMEHASHSIZE) | |
12 | ||
13 | unsigned int hostmarker; | |
14 | unsigned int realnamemarker; | |
15 | unsigned int nickmarker; | |
16 | ||
17 | host *hosttable[HOSTHASHSIZE]; | |
18 | realname *realnametable[REALNAMEHASHSIZE]; | |
19 | ||
20 | void initnickhelpers() { | |
21 | memset(hosttable,0,sizeof(hosttable)); | |
22 | memset(realnametable,0,sizeof(realnametable)); | |
23 | } | |
24 | ||
25 | host *findhost(const char *hostname) { | |
26 | host *hp; | |
27 | for (hp=hosttable[hosthash(hostname)];hp;hp=(host *)hp->next) { | |
28 | if (!ircd_strcmp(hostname,hp->name->content)) | |
29 | return hp; | |
30 | } | |
31 | return NULL; | |
32 | } | |
33 | ||
34 | host *findorcreatehost(const char *hostname) { | |
35 | host *hp; | |
36 | unsigned long thehash=hosthash(hostname); | |
37 | ||
38 | for (hp=hosttable[thehash];hp;hp=(host *)hp->next) | |
39 | if (!ircd_strcmp(hostname,hp->name->content)) { | |
40 | hp->clonecount++; | |
41 | return hp; | |
42 | } | |
43 | ||
44 | hp=newhost(); | |
45 | hp->name=getsstring(hostname,HOSTLEN); | |
46 | hp->clonecount=1; | |
47 | hp->marker=0; | |
48 | hp->nicks=NULL; | |
49 | hp->next=(struct host *)hosttable[thehash]; | |
50 | hosttable[thehash]=hp; | |
51 | ||
52 | return hp; | |
53 | } | |
54 | ||
55 | void releasehost(host *hp) { | |
56 | host **mhp; | |
57 | if (--(hp->clonecount)==0) { | |
58 | for(mhp=&(hosttable[hosthash(hp->name->content)]);*mhp;mhp=(host **)&((*mhp)->next)) { | |
59 | if ((*mhp)==hp) { | |
60 | (*mhp)=(host *)hp->next; | |
61 | freesstring(hp->name); | |
62 | freehost(hp); | |
63 | return; | |
64 | } | |
65 | } | |
66 | Error("nick",ERR_ERROR,"Unable to remove host %s from hashtable",hp->name->content); | |
67 | } | |
68 | } | |
69 | ||
70 | realname *findrealname(const char *name) { | |
71 | realname *rnp; | |
72 | ||
73 | for (rnp=realnametable[realnamehash(name)];rnp;rnp=(realname *)rnp->next) | |
74 | if (!strcmp(name,rnp->name->content)) | |
75 | return rnp; | |
76 | ||
77 | return NULL; | |
78 | } | |
79 | ||
80 | realname *findorcreaterealname(const char *name) { | |
81 | realname *rnp; | |
82 | unsigned int thehash=realnamehash(name); | |
83 | ||
84 | for (rnp=realnametable[thehash];rnp;rnp=(realname *)rnp->next) | |
85 | if (!strcmp(name,rnp->name->content)) { | |
86 | rnp->usercount++; | |
87 | return rnp; | |
88 | } | |
89 | ||
90 | rnp=newrealname(); | |
91 | rnp->name=getsstring(name,REALLEN); | |
92 | rnp->usercount=1; | |
93 | rnp->marker=0; | |
94 | rnp->nicks=NULL; | |
95 | rnp->next=(struct realname *)realnametable[thehash]; | |
96 | realnametable[thehash]=rnp; | |
97 | ||
98 | return rnp; | |
99 | } | |
100 | ||
101 | void releaserealname(realname *rnp) { | |
102 | realname **mrnp; | |
103 | if (--(rnp->usercount)==0) { | |
104 | for(mrnp=&(realnametable[realnamehash(rnp->name->content)]);*mrnp;mrnp=(realname **)&((*mrnp)->next)) { | |
105 | if ((*mrnp)==rnp) { | |
106 | (*mrnp)=(realname *)rnp->next; | |
107 | freesstring(rnp->name); | |
108 | freerealname(rnp); | |
109 | return; | |
110 | } | |
111 | } | |
112 | Error("nick",ERR_ERROR,"Unable to remove realname %s from hashtable",rnp->name->content); | |
113 | } | |
114 | } | |
115 | ||
116 | unsigned int nexthostmarker() { | |
117 | int i; | |
118 | host *hp; | |
119 | ||
120 | hostmarker++; | |
121 | if (!hostmarker) { | |
122 | /* If we wrapped to zero, zap the marker on all hosts */ | |
123 | for (i=0;i<HOSTHASHSIZE;i++) | |
124 | for (hp=hosttable[i];hp;hp=hp->next) | |
125 | hp->marker=0; | |
126 | hostmarker++; | |
127 | } | |
128 | ||
129 | return hostmarker; | |
130 | } | |
131 | ||
132 | unsigned int nextrealnamemarker() { | |
133 | int i; | |
134 | realname *rnp; | |
135 | ||
136 | realnamemarker++; | |
137 | if (!realnamemarker) { | |
138 | /* If we wrapped to zero, zap the marker on all records */ | |
139 | for (i=0;i<REALNAMEHASHSIZE;i++) | |
140 | for (rnp=realnametable[i];rnp;rnp=rnp->next) | |
141 | rnp->marker=0; | |
142 | realnamemarker++; | |
143 | } | |
144 | ||
145 | return realnamemarker; | |
146 | } | |
147 | ||
148 | unsigned int nextnickmarker() { | |
149 | int i; | |
150 | nick *np; | |
151 | ||
152 | nickmarker++; | |
153 | ||
154 | if (!nickmarker) { | |
155 | /* If we wrapped to zero, zap the marker on all records */ | |
156 | for (i=0;i<NICKHASHSIZE;i++) | |
157 | for (np=nicktable[i];np;np=np->next) | |
158 | np->marker=0; | |
159 | nickmarker++; | |
160 | } | |
161 | ||
162 | return nickmarker; | |
163 | } |