/[LeafOK_CVS]/pvpgn-1.7.4/src/d2cs/d2charstatus.c
ViewVC logotype

Contents of /pvpgn-1.7.4/src/d2cs/d2charstatus.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.4 - (show annotations)
Sat Jun 24 09:19:39 2006 UTC (19 years, 8 months ago) by sysadm
Branch: MAIN
CVS Tags: pvpgn_1-7-4-0_MIL, HEAD
Changes since 1.3: +2 -2 lines
Content type: text/x-csrc
no message

1 #include "common/setup_before.h"
2 #include "setup.h"
3
4 #include <ctype.h>
5 #ifdef HAVE_STRING_H
6 # include <string.h>
7 #else
8 # ifdef HAVE_STRINGS_H
9 # include <strings.h>
10 # endif
11 # ifdef HAVE_MEMORY_H
12 # include <memory.h>
13 # endif
14 #endif
15 #ifdef STDC_HEADERS
16 # include <stdlib.h>
17 #else
18 # ifdef HAVE_MALLOC_H
19 # include <malloc.h>
20 # endif
21 #endif
22 #include "compat/memcpy.h"
23 #include "compat/strdup.h"
24 #ifdef HAVE_UNISTD_H
25 # include <unistd.h>
26 #endif
27 #ifdef HAVE_SYS_TYPES_H
28 # include <sys/types.h>
29 #endif
30 #ifdef HAVE_SYS_SOCKET_H
31 # include <sys/socket.h>
32 #endif
33 #include "compat/psock.h"
34 #ifdef HAVE_NETINET_IN_H
35 # include <netinet/in.h>
36 #endif
37 #include "compat/netinet_in.h"
38 #ifdef HAVE_LIMITS_H
39 # include <limits.h>
40 #endif
41 #include "compat/char_bit.h"
42 #ifdef TIME_WITH_SYS_TIME
43 # include <time.h>
44 # include <sys/time.h>
45 #else
46 # ifdef HAVE_SYS_TIME_H
47 # include <sys/time.h>
48 # else
49 # include <time.h>
50 # endif
51 #endif
52 #ifdef HAVE_ASSERT_H
53 # include <assert.h>
54 #endif
55
56 #include "compat/psock.h"
57 #include "compat/strcasecmp.h"
58 #include "connection.h"
59 #include "d2charstatus.h"
60 #include "game.h"
61 #include "gamequeue.h"
62 #include "prefs.h"
63 #include "d2gs.h"
64 #include "net.h"
65 #include "s2s.h"
66 #include "handle_d2gs.h"
67 #include "handle_d2cs.h"
68 #include "handle_init.h"
69 #include "handle_bnetd.h"
70 #include "d2charfile.h"
71 #include "common/fdwatch.h"
72 #include "common/addr.h"
73 #include "common/introtate.h"
74 #include "common/network.h"
75 #include "common/packet.h"
76 #include "common/hashtable.h"
77 #include "common/queue.h"
78 #include "common/eventlog.h"
79 #include "common/xalloc.h"
80 #include "common/setup_after.h"
81
82 static t_hashtable * charstatus_list_head=NULL;
83 static unsigned int total_charstatus=0;
84
85 static unsigned int charname_hash(char const * charname)
86 {
87 unsigned int hash;
88 unsigned int i, len, pos;
89 unsigned int ch;
90
91 ASSERT(charname,0);
92 len=strlen(charname);
93 for (hash=0, i=0, pos=0; i<len; i++) {
94 if (isascii((int)charname[i])) {
95 ch=(unsigned int)(unsigned char)tolower((int)charname[i]);
96 } else {
97 ch=(unsigned int)(unsigned char)charname[i];
98 }
99 hash ^= ROTL(ch,pos,sizeof(unsigned int) * CHAR_BIT);
100 pos += CHAR_BIT-1;
101 }
102 return hash;
103 }
104
105 extern t_hashtable * charstatus_list(void)
106 {
107 return charstatus_list_head;
108 }
109
110 extern int charstatus_list_create(void)
111 {
112 if (!(charstatus_list_head=hashtable_create(prefs_charstatus_max_chars()))) return -1;
113 return 0;
114 }
115
116 extern int charstatus_list_destroy(void)
117 {
118 t_charstatus * c;
119 t_elem * curr;
120
121
122 BEGIN_HASHTABLE_TRAVERSE_DATA(charstatus_list_head, c)
123 {
124 charstatus_destroy(c,&curr);
125 }
126 END_HASHTABLE_TRAVERSE_DATA()
127
128 if (hashtable_destroy(charstatus_list_head)<0) {
129 eventlog(eventlog_level_error,__FUNCTION__,"error destroy charstatus list");
130 return -1;
131 }
132 charstatus_list_head=NULL;
133
134 return 0;
135 }
136
137 extern int charstatus_list_cleanup(void)
138 {
139 t_charstatus * c;
140 t_elem * curr;
141
142
143 BEGIN_HASHTABLE_TRAVERSE_DATA(charstatus_list_head, c)
144 {
145 if (!c) {
146 eventlog(eventlog_level_error,__FUNCTION__,"got NULL charstatus in list");
147 } else {
148 if (time(NULL) - c->last_create_time > prefs_charstatus_max_life())
149 charstatus_destroy(c,&curr);
150 }
151 }
152 END_HASHTABLE_TRAVERSE_DATA()
153
154 return 0;
155 }
156
157 extern t_charstatus * charstatus_create(const char * charname)
158 {
159 t_charstatus * c;
160
161 if (total_charstatus >= prefs_charstatus_max_chars())
162 {
163 charstatus_list_cleanup();
164 }
165
166 c=xmalloc(sizeof(t_charstatus));
167 c->charname=xstrdup(charname);
168 c->charname_hash=charname_hash(charname);
169 c->checknum_error_flag=0;
170 c->frequently_create_count=0;
171 c->last_create_time=time(NULL);
172 c->ban_begin_time=0;
173 if (hashtable_insert_data(charstatus_list_head, c, c->charname_hash)<0) {
174 xfree(c);
175 eventlog(eventlog_level_error,__FUNCTION__,"error add charstatus to list");
176 return NULL;
177 }
178 total_charstatus++;
179
180 eventlog(eventlog_level_debug,__FUNCTION__,"there are %d charstatus in list", total_charstatus);
181
182 return c;
183 }
184
185 extern int charstatus_destroy(t_charstatus * c, t_elem ** curr)
186 {
187 t_elem * elem;
188
189 ASSERT(c,-1);
190
191 if (hashtable_remove_data(charstatus_list_head,c,c->charname_hash)<0) {
192 eventlog(eventlog_level_error,__FUNCTION__,"error remove charstatus from list");
193 return -1;
194 }
195 if (c->charname) xfree((void *)c->charname);
196 total_charstatus--;
197 xfree(c);
198
199 eventlog(eventlog_level_debug,__FUNCTION__,"there are %d charstatus in list", total_charstatus);
200
201 return 0;
202 }
203
204 extern t_charstatus * charstatus_list_find_charstatus_by_charname(char const * charname)
205 {
206 t_entry * curr;
207 t_charstatus * c;
208 unsigned int hash;
209
210 hash=charname_hash(charname);
211 HASHTABLE_TRAVERSE_MATCHING(charstatus_list_head,curr,hash)
212 {
213 if (!(c=entry_get_data(curr))) {
214 eventlog(eventlog_level_error,__FUNCTION__,"got NULL charstatus in list");
215 } else {
216 if (!c->charname) continue;
217 if (!strcmp_charname(c->charname,charname)) {
218 hashtable_entry_release(curr);
219 return c;
220 }
221 }
222 }
223 return NULL;
224 }
225
226 /* For debug purpose */
227 extern int charstatus_list_dump(const char * dumpfile)
228 {
229 t_charstatus * c;
230 t_elem * curr;
231 FILE * fp;
232 char last_create_time[256], ban_begin_time[256];
233
234 /* Cleanup before dump */
235 charstatus_list_cleanup();
236
237 if ((fp=fopen(dumpfile, "w")) == NULL)
238 {
239 eventlog(eventlog_level_error,__FUNCTION__,"cannot open dump file %s", dumpfile);
240 return -1;
241 }
242
243 fprintf(fp, "charname error_flag frequent_count last_create ban_begin\n");
244
245 BEGIN_HASHTABLE_TRAVERSE_DATA(charstatus_list_head, c)
246 {
247 if (!c) {
248 eventlog(eventlog_level_error,__FUNCTION__,"got NULL charstatus in list");
249 } else {
250 strftime (last_create_time, 256, "%Y-%m-%d %H:%M:%S", localtime (&(c->last_create_time)));
251 strftime (ban_begin_time, 256, "%Y-%m-%d %H:%M:%S", localtime (&(c->ban_begin_time)));
252
253 fprintf(fp, "%-16.16s %5u %5u %-20.20s %-20.20s\n",
254 c->charname, c->checknum_error_flag, c->frequently_create_count, last_create_time, ban_begin_time);
255 }
256 }
257 END_HASHTABLE_TRAVERSE_DATA()
258
259 fclose(fp);
260
261 return 0;
262 }

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1