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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (hide annotations)
Tue Jun 13 16:07:52 2006 UTC (19 years, 9 months ago) by sysadm
Branch: MAIN
Changes since 1.1: +43 -6 lines
Content type: text/x-csrc
Antibot (for non-password protected game)

1 sysadm 1.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 sysadm 1.2 if (!(charstatus_list_head=hashtable_create(prefs_charstatus_max_chars()))) return -1;
113 sysadm 1.1 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 sysadm 1.2 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 sysadm 1.1 }
152     END_HASHTABLE_TRAVERSE_DATA()
153    
154     eventlog(eventlog_level_info,__FUNCTION__,"cleanup charstatus list");
155    
156     return 0;
157     }
158    
159     extern t_charstatus * charstatus_create(const char * charname)
160     {
161     t_charstatus * c;
162    
163 sysadm 1.2 if (total_charstatus >= prefs_charstatus_max_chars())
164 sysadm 1.1 {
165     charstatus_list_cleanup();
166     }
167    
168     c=xmalloc(sizeof(t_charstatus));
169     c->charname=xstrdup(charname);
170     c->charname_hash=charname_hash(charname);
171     c->checknum_error_flag=0;
172     c->frequently_create_count=0;
173     c->last_create_time=time(NULL);
174     c->ban_begin_time=0;
175     if (hashtable_insert_data(charstatus_list_head, c, c->charname_hash)<0) {
176     xfree(c);
177     eventlog(eventlog_level_error,__FUNCTION__,"error add charstatus to list");
178     return NULL;
179     }
180     total_charstatus++;
181    
182     eventlog(eventlog_level_debug,__FUNCTION__,"there are %d charstatus in list", total_charstatus);
183    
184     return c;
185     }
186    
187     extern int charstatus_destroy(t_charstatus * c, t_elem ** curr)
188     {
189     t_elem * elem;
190    
191     ASSERT(c,-1);
192    
193     if (hashtable_remove_data(charstatus_list_head,c,c->charname_hash)<0) {
194     eventlog(eventlog_level_error,__FUNCTION__,"error remove charstatus from list");
195     return -1;
196     }
197     if (c->charname) xfree((void *)c->charname);
198     total_charstatus--;
199     xfree(c);
200    
201     eventlog(eventlog_level_debug,__FUNCTION__,"there are %d charstatus in list", total_charstatus);
202    
203     return 0;
204     }
205    
206     extern t_charstatus * charstatus_list_find_charstatus_by_charname(char const * charname)
207     {
208     t_entry * curr;
209     t_charstatus * c;
210     unsigned int hash;
211    
212     hash=charname_hash(charname);
213     HASHTABLE_TRAVERSE_MATCHING(charstatus_list_head,curr,hash)
214     {
215     if (!(c=entry_get_data(curr))) {
216     eventlog(eventlog_level_error,__FUNCTION__,"got NULL charstatus in list");
217     } else {
218     if (!c->charname) continue;
219     if (!strcmp_charname(c->charname,charname)) {
220     hashtable_entry_release(curr);
221     return c;
222     }
223     }
224     }
225     return NULL;
226     }
227 sysadm 1.2
228     /* For debug purpose */
229     extern int charstatus_list_dump(const char * dumpfile)
230     {
231     t_charstatus * c;
232     t_elem * curr;
233     FILE * fp;
234     char last_create_time[256], ban_begin_time[256];
235    
236     if ((fp=fopen(dumpfile, "w")) == NULL)
237     {
238     eventlog(eventlog_level_error,__FUNCTION__,"cannot open dump file %s", dumpfile);
239     return -1;
240     }
241    
242     BEGIN_HASHTABLE_TRAVERSE_DATA(charstatus_list_head, c)
243     {
244     if (!c) {
245     eventlog(eventlog_level_error,__FUNCTION__,"got NULL charstatus in list");
246     } else {
247     strftime (last_create_time, 256, "%Y-%m-%d %H:%M:%S", localtime (&(c->last_create_time)));
248     strftime (ban_begin_time, 256, "%Y-%m-%d %H:%M:%S", localtime (&(c->ban_begin_time)));
249    
250     fprintf(fp, "%s: checknum_error_flag=%d, frequently_create_count=%d, last_create_time=%s, ban_begin_time=%s\n",
251     c->charname, c->checknum_error_flag, c->frequently_create_count, last_create_time, ban_begin_time);
252     }
253     }
254     END_HASHTABLE_TRAVERSE_DATA()
255    
256     fclose(fp);
257    
258     eventlog(eventlog_level_info,__FUNCTION__,"dump charstatus list");
259    
260     return 0;
261     }

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