/[LeafOK_CVS]/innwebd/base_passive.cpp
ViewVC logotype

Contents of /innwebd/base_passive.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.6 - (show annotations)
Sun Jul 4 22:49:03 2004 UTC (21 years, 8 months ago) by sysadm
Branch: MAIN
Changes since 1.5: +1 -1 lines
Content type: text/x-c++src
CloseHandle after use

1 /*******************************************************/
2 /* */
3 /* LeafOK Innbbsd */
4 /* Copyright (C) LeafOK.com, 2003-2004 */
5 /* */
6 /* Programmed by Leaf */
7 /* E-mail:leaflet@leafok.com QQ:6049044 */
8 /* */
9 /* http://bbs.leafok.com */
10 /* http://bbs.leafok.net */
11 /* http://bbs.fenglin.info */
12 /* */
13 /*******************************************************/
14
15 #include "StdAfx.h"
16 #include ".\base_passive.h"
17 #include ".\base_active.h"
18
19 using namespace std;
20
21 // UINT base_passive::uThreadCount = 0;
22
23 base_passive::base_passive(void)
24 {
25 }
26
27 base_passive::~base_passive(void)
28 {
29 this->s_close();
30 this->db_close();
31 }
32
33 int base_passive::s_connect(const char* hostaddr, unsigned int port)
34 {
35 SOCKADDR_IN sin;
36
37 if (this->isConnected)
38 {
39 this->s_close();
40 }
41
42 //Create a TCP/IP socket
43 if ((this->s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET)
44 {
45 syslog << logfile::log_head << "Cannot create socket" << endl;
46 return -1;
47 }
48
49 //fill in sockaddr_in struct
50
51 sin.sin_family = PF_INET;
52 sin.sin_port = htons(port);
53 sin.sin_addr.s_addr = (strlen(hostaddr) > 0 ? inet_addr(hostaddr) : INADDR_ANY);
54
55 //bind the socket
56 if(bind(this->s, (LPSOCKADDR)&sin, sizeof(sin)) == SOCKET_ERROR)
57 {
58 syslog << logfile::log_head << "Cannot bind" << endl;
59 return -1;
60 }
61
62 if(listen(this->s,5) == SOCKET_ERROR)
63 {
64 syslog << logfile::log_head << "Cannot listen" << endl;
65 return -1;
66 }
67
68 strcpy(this->hostaddr,hostaddr);
69 this->port = port;
70
71 this->isConnected = true;
72
73 return 0;
74 }
75
76 int base_passive::s_close(void)
77 {
78 if (!this->isConnected)
79 return 0;
80
81 if (closesocket(this->s) == SOCKET_ERROR)
82 {
83 syslog << logfile::log_head << "Close socket error" << endl;
84 }
85
86 this->isConnected = false;
87
88 return 0;
89 }
90
91 int base_passive::work()
92 {
93 HANDLE hThreadCurrent;
94 ULONG ulThreadId;
95 base_passive* p;
96 SOCKADDR_IN sin;
97 int namelen;
98
99 if (this->running)
100 return 1;
101
102 if (this->load_priv() != 0)
103 return -1;
104
105 this->SetThreadPool(new thread_pool(MAX_CLIENT));
106 if (this->p_ThreadPool->GetLastError() != 0)
107 {
108 return -1;
109 }
110
111 if (this->s_connect(this->w_address,this->w_port) == 0)
112 {
113 this->Startup();
114
115 syslog << logfile::log_head << "Listen at port [" << this->w_port << "]" << endl;
116
117 while(!this->IsShutdown())
118 {
119 if ((p = this->new_client()) == NULL)
120 {
121 syslog << logfile::log_head << "Assign new client failed" << endl;
122 continue;
123 }
124
125 p->SetParentThread(this);
126 p->configure(this->innd_id,this->innd_name,this->innd_server,this->innd_uid,this->w_address,this->w_port,this->w_conn_str);
127
128 this->running = true;
129
130 this->GetParentThread()->GetThreadPool()->SetThreadStatus(GetCurrentThreadId(),thread_pool::S_WAITING);
131
132 namelen = sizeof(sin);
133 if ((p->s = accept(this->s,(LPSOCKADDR)&sin,&namelen)) == INVALID_SOCKET)
134 {
135 syslog << logfile::log_head << "Socket accept failed (" << GetLastError() <<")" << endl;
136 this->unload_priv();
137 this->Shutdown();
138 this->running = false;
139 return -2;
140 }
141
142 if (this->IsShutdown())
143 {
144 break;
145 }
146
147 this->GetParentThread()->GetThreadPool()->SetThreadStatus(GetCurrentThreadId(),tread_pool::S_WORKING);
148
149
150 strcpy(p->hostaddr,inet_ntoa(sin.sin_addr));
151 p->port = sin.sin_port;
152 p->isConnected = true;
153
154 // strcpy(p->w_conn_str,this->w_conn_str);
155
156 if (this->get_priv(p)!=0)
157 {
158 p->s_send("502 Load access file failed.\r\n");
159 delete(p);
160 continue;
161 }
162
163 if (!p->access.get)
164 {
165 p->s_send("502 You are not in my access file.\r\n");
166 delete(p);
167 continue;
168 }
169
170 // if (base_passive::uThreadCount < MAX_CLIENT)
171 if (this->GetThreadPool()->GetThreadCount() < MAX_CLIENT)
172 {
173 if (hThreadCurrent = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)base_passive::AcceptThread,(LPVOID)p,0,&ulThreadId))
174 {
175 CloseHandle(hThreadCurrent);
176 syslog << logfile::log_head << "Create accept thread ... OK" << endl;
177 }
178 else
179 {
180 delete(p);
181 syslog << logfile::log_head << "Create accept thread ... Failed" << endl;
182 }
183 }
184 else
185 {
186 p->s_send("502 Too many connections\r\n");
187 delete(p);
188 }
189 }
190 delete(p);
191 Sleep(1000*5); // Wait several seconds for all threads to exit
192 }
193 this->s_close();
194
195 this->unload_priv();
196
197 this->running = false;
198
199 return 0;
200 }
201
202 DWORD base_passive::AcceptThread(LPVOID pParam)
203 {
204 base_passive* p;
205
206 try
207 {
208 p = (base_passive*)pParam;
209
210 p->ulMainThreadId = GetCurrentThreadId();
211
212 if (p->GetParentThread()->GetThreadPool()->AddThread(GetCurrentThreadId(), CLOCKS_PER_SEC * 30) != 0)
213 {
214 syslog << logfile::log_head << "Register thread ... Failed" << endl;
215 }
216 p->GetParentThread()->GetThreadPool()->SetThreadStatus(GetCurrentThreadId(),thread_pool::S_WORKING);
217
218 if (p->db_env_init() == 0)
219 {
220 p->Accept();
221 }
222 else
223 {
224 syslog << logfile::log_head << "DB_ENV_INIT failed" << endl;
225 }
226
227 if (p->GetParentThread()->GetThreadPool()->RemoveThread(GetCurrentThreadId()) != 0)
228 {
229 syslog << logfile::log_head << "Unregister thread ... Failed" << endl;
230 }
231 // base_passive::uThreadCount--;
232
233 delete(p);
234 syslog << logfile::log_head << "Delete accept thread ... OK" << endl;
235 }
236 catch(CException* e)
237 {
238 syslog << logfile::log_head << "Error in AcceptThread()" << endl;
239 e->Delete();
240 return -1;
241 }
242
243 return 0;
244 }
245
246 int base_passive::get_priv(base_passive* p)
247 {
248 try
249 {
250 //Default value
251 strcpy(p->access.ip,p->hostaddr);
252 p->access.control = false;
253 p->access.get = true;
254 p->access.post = false;
255 p->access.ihave = false;
256
257 //Check access list
258 for (int i=1; i<this->access_array.GetCount(); i++)
259 {
260 if (strcmp(this->access_array[i]->ip,p->hostaddr) == 0)
261 {
262 p->access = *(this->access_array[i]);
263 break;
264 }
265 }
266 }
267 catch(CException* e)
268 {
269 syslog << logfile::log_head << "Error in get_priv()" << endl;
270 e->Delete();
271 return -1;
272 }
273
274 return 0;
275 }
276
277 int base_passive::load_priv(void)
278 {
279 CRecordset RS;
280 CString sql;
281 CDBVariant dbVar;
282 Access_STRUCT* p_access;
283
284 try
285 {
286 if (this->db_open(this->w_conn_str) == 0)
287 {
288 RS.m_pDatabase = &(this->Db);
289 }
290 else
291 return -2;
292
293 //Check access list
294 sql = "select * from innd_access where enable order by ip";
295 RS.Open(CRecordset::snapshot,sql,CRecordset::readOnly);
296 this->access_array.SetSize(RS.GetRecordCount());
297 while(!RS.IsEOF())
298 {
299 p_access = new Access_STRUCT();
300
301 RS.GetFieldValue("ip",dbVar,SQL_C_CHAR);
302 strcpy(p_access->ip,*(dbVar.m_pstring));
303 RS.GetFieldValue("control",dbVar,SQL_C_SSHORT);
304 p_access->control = (dbVar.m_iVal!=0);
305 RS.GetFieldValue("get",dbVar,SQL_C_SSHORT);
306 p_access->get = (dbVar.m_iVal!=0);
307 RS.GetFieldValue("post",dbVar,SQL_C_SSHORT);
308 p_access->post = (dbVar.m_iVal!=0);
309 RS.GetFieldValue("ihave",dbVar,SQL_C_SSHORT);
310 p_access->ihave = (dbVar.m_iVal!=0);
311
312 this->access_array.Add(p_access);
313 RS.MoveNext();
314 }
315 RS.Close();
316
317 this->db_close();
318 }
319 catch(CException* e)
320 {
321 syslog << logfile::log_head << "Error in load_priv()" << endl;
322 e->Delete();
323 return -1;
324 }
325
326 return 0;
327 }
328
329 int base_passive::unload_priv(void)
330 {
331 Access_STRUCT* p_access;
332
333 try
334 {
335 for (int i=1; i<this->access_array.GetCount();i++)
336 {
337 p_access = this->access_array[i];
338 delete (p_access);
339 }
340 this->access_array.RemoveAll();
341 }
342 catch(CException* e)
343 {
344 syslog << logfile::log_head << "Error in unload_priv()" << endl;
345 e->Delete();
346 return -1;
347 }
348
349 return 0;
350 }
351
352 int base_passive::w_call(void)
353 {
354 this->work();
355 return 0;
356 }

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