/[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.1 - (hide annotations)
Tue Jun 13 12:19:11 2006 UTC (19 years, 9 months ago) by sysadm
Branch: MAIN
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     #define MAX_CHAR_STATUS 500
57    
58     #include "compat/psock.h"
59     #include "compat/strcasecmp.h"
60     #include "connection.h"
61     #include "d2charstatus.h"
62     #include "game.h"
63     #include "gamequeue.h"
64     #include "prefs.h"
65     #include "d2gs.h"
66     #include "net.h"
67     #include "s2s.h"
68     #include "handle_d2gs.h"
69     #include "handle_d2cs.h"
70     #include "handle_init.h"
71     #include "handle_bnetd.h"
72     #include "d2charfile.h"
73     #include "common/fdwatch.h"
74     #include "common/addr.h"
75     #include "common/introtate.h"
76     #include "common/network.h"
77     #include "common/packet.h"
78     #include "common/hashtable.h"
79     #include "common/queue.h"
80     #include "common/eventlog.h"
81     #include "common/xalloc.h"
82     #include "common/setup_after.h"
83    
84     static t_hashtable * charstatus_list_head=NULL;
85     static unsigned int total_charstatus=0;
86    
87     static unsigned int charname_hash(char const * charname)
88     {
89     unsigned int hash;
90     unsigned int i, len, pos;
91     unsigned int ch;
92    
93     ASSERT(charname,0);
94     len=strlen(charname);
95     for (hash=0, i=0, pos=0; i<len; i++) {
96     if (isascii((int)charname[i])) {
97     ch=(unsigned int)(unsigned char)tolower((int)charname[i]);
98     } else {
99     ch=(unsigned int)(unsigned char)charname[i];
100     }
101     hash ^= ROTL(ch,pos,sizeof(unsigned int) * CHAR_BIT);
102     pos += CHAR_BIT-1;
103     }
104     return hash;
105     }
106    
107     extern t_hashtable * charstatus_list(void)
108     {
109     return charstatus_list_head;
110     }
111    
112     extern int charstatus_list_create(void)
113     {
114     if (!(charstatus_list_head=hashtable_create(MAX_CHAR_STATUS))) return -1;
115     return 0;
116     }
117    
118     extern int charstatus_list_destroy(void)
119     {
120     t_charstatus * c;
121     t_elem * curr;
122    
123    
124     BEGIN_HASHTABLE_TRAVERSE_DATA(charstatus_list_head, c)
125     {
126     charstatus_destroy(c,&curr);
127     }
128     END_HASHTABLE_TRAVERSE_DATA()
129    
130     if (hashtable_destroy(charstatus_list_head)<0) {
131     eventlog(eventlog_level_error,__FUNCTION__,"error destroy charstatus list");
132     return -1;
133     }
134     charstatus_list_head=NULL;
135    
136     return 0;
137     }
138    
139     extern int charstatus_list_cleanup(void)
140     {
141     t_charstatus * c;
142     t_elem * curr;
143    
144    
145     BEGIN_HASHTABLE_TRAVERSE_DATA(charstatus_list_head, c)
146     {
147     if (time(NULL) - c->last_create_time > 3600)
148     charstatus_destroy(c,&curr);
149     }
150     END_HASHTABLE_TRAVERSE_DATA()
151    
152     eventlog(eventlog_level_info,__FUNCTION__,"cleanup charstatus list");
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 >= MAX_CHAR_STATUS)
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     }

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