| 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 ".\controld.h"
|
| 17 |
#include ".\base_active.h"
|
| 18 |
#include ".\App_common.h"
|
| 19 |
|
| 20 |
using namespace std;
|
| 21 |
|
| 22 |
bool controld::thread_terminate = true;
|
| 23 |
|
| 24 |
controld::controld(void)
|
| 25 |
:bbsd_startup(false),innd_startup(false),outd_startup(false),startup_delay(0)
|
| 26 |
{
|
| 27 |
}
|
| 28 |
|
| 29 |
controld::~controld(void)
|
| 30 |
{
|
| 31 |
}
|
| 32 |
|
| 33 |
int controld::Accept(void)
|
| 34 |
{
|
| 35 |
CString out,cmd;
|
| 36 |
char temp[256];
|
| 37 |
int result;
|
| 38 |
|
| 39 |
try
|
| 40 |
{
|
| 41 |
out.Format("200 %s ready.",App_common::GetVersion());
|
| 42 |
this->s_send(out);
|
| 43 |
while(!this->thread_terminate)
|
| 44 |
{
|
| 45 |
this->GetParentThread()->GetThreadPool()->SetThreadStatus(GetCurrentThreadId(),thread_pool::S_WAITING);
|
| 46 |
|
| 47 |
if (this->s_receive(out) == 0) //Connection closed
|
| 48 |
break;
|
| 49 |
out.Trim();
|
| 50 |
|
| 51 |
this->GetParentThread()->GetThreadPool()->SetThreadStatus(GetCurrentThreadId(),thread_pool::S_WORKING);
|
| 52 |
|
| 53 |
syslog << logfile::log_head << "Cmd: " << out << endl;
|
| 54 |
|
| 55 |
if (sscanf(out,"%20s",temp) ==1)
|
| 56 |
cmd = strupr(temp);
|
| 57 |
else
|
| 58 |
cmd = "";
|
| 59 |
|
| 60 |
if (cmd == "") //Blank command
|
| 61 |
continue;
|
| 62 |
|
| 63 |
if (cmd == "START")
|
| 64 |
{
|
| 65 |
if (sscanf(out,"%*s %20s",temp) ==1)
|
| 66 |
cmd = strupr(temp);
|
| 67 |
else
|
| 68 |
cmd = "";
|
| 69 |
cmd.Trim();
|
| 70 |
|
| 71 |
if (cmd == "BBSD" || cmd == "INND" || cmd == "OUTD")
|
| 72 |
{
|
| 73 |
if (this->access.control)
|
| 74 |
{
|
| 75 |
out.Format("Starting %s ... ",cmd);
|
| 76 |
this->s_send(out,false);
|
| 77 |
|
| 78 |
if (cmd == "BBSD")
|
| 79 |
{
|
| 80 |
result = ((controld*)(this->GetParentThread()))->bbsd.begin();
|
| 81 |
}
|
| 82 |
if (cmd == "INND")
|
| 83 |
{
|
| 84 |
result = ((controld*)(this->GetParentThread()))->innd.begin();
|
| 85 |
}
|
| 86 |
if (cmd == "OUTD")
|
| 87 |
{
|
| 88 |
result = ((controld*)(this->GetParentThread()))->outd.begin();
|
| 89 |
}
|
| 90 |
|
| 91 |
this->s_send(result == 0 ? "OK" : "Failed");
|
| 92 |
}
|
| 93 |
else
|
| 94 |
{
|
| 95 |
this->s_send("400 Forbidden");
|
| 96 |
}
|
| 97 |
}
|
| 98 |
else
|
| 99 |
{
|
| 100 |
this->s_send("501 service");
|
| 101 |
}
|
| 102 |
continue;
|
| 103 |
}
|
| 104 |
|
| 105 |
if (cmd == "STOP")
|
| 106 |
{
|
| 107 |
if (sscanf(out,"%*s %20s",temp) ==1)
|
| 108 |
cmd = strupr(temp);
|
| 109 |
else
|
| 110 |
cmd = "";
|
| 111 |
cmd.Trim();
|
| 112 |
|
| 113 |
if (cmd == "BBSD" || cmd == "INND" || cmd == "OUTD")
|
| 114 |
{
|
| 115 |
if (this->access.control)
|
| 116 |
{
|
| 117 |
out.Format("Stopping %s ... ",cmd);
|
| 118 |
this->s_send(out,false);
|
| 119 |
|
| 120 |
if (cmd == "BBSD")
|
| 121 |
{
|
| 122 |
result = ((controld*)(this->GetParentThread()))->bbsd.end();
|
| 123 |
}
|
| 124 |
if (cmd == "INND")
|
| 125 |
{
|
| 126 |
result = ((controld*)(this->GetParentThread()))->innd.end();
|
| 127 |
}
|
| 128 |
if (cmd == "OUTD")
|
| 129 |
{
|
| 130 |
result = ((controld*)(this->GetParentThread()))->outd.end();
|
| 131 |
}
|
| 132 |
|
| 133 |
this->s_send(result == 0 ? "OK" : "Failed");
|
| 134 |
}
|
| 135 |
else
|
| 136 |
{
|
| 137 |
this->s_send("400 Forbidden");
|
| 138 |
}
|
| 139 |
}
|
| 140 |
else
|
| 141 |
{
|
| 142 |
this->s_send("501 service");
|
| 143 |
}
|
| 144 |
continue;
|
| 145 |
}
|
| 146 |
|
| 147 |
if (cmd == "SHUTDOWN")
|
| 148 |
{
|
| 149 |
if (this->access.control)
|
| 150 |
{
|
| 151 |
this->s_send("Shutting down ... ",false);
|
| 152 |
|
| 153 |
((controld*)(this->GetParentThread()))->ShutdownChild();
|
| 154 |
|
| 155 |
this->s_send("OK");
|
| 156 |
|
| 157 |
((controld*)(this->GetParentThread()))->Shutdown();
|
| 158 |
break;
|
| 159 |
}
|
| 160 |
else
|
| 161 |
{
|
| 162 |
this->s_send("400 Forbidden");
|
| 163 |
}
|
| 164 |
continue;
|
| 165 |
}
|
| 166 |
|
| 167 |
if (cmd == "RELOAD_PRIV")
|
| 168 |
{
|
| 169 |
this->s_send("Reloading prev_list ... ",false);
|
| 170 |
|
| 171 |
((controld*)(this->GetParentThread()))->unload_priv();
|
| 172 |
if (((controld*)(this->GetParentThread()))->load_priv() == 0)
|
| 173 |
this->s_send("OK");
|
| 174 |
else
|
| 175 |
this->s_send("Failed");
|
| 176 |
|
| 177 |
continue;
|
| 178 |
}
|
| 179 |
|
| 180 |
if (cmd == "ACTIVE")
|
| 181 |
{
|
| 182 |
if (sscanf(out,"%*s %20s",temp) == 1)
|
| 183 |
cmd = strupr(temp);
|
| 184 |
else
|
| 185 |
cmd = "";
|
| 186 |
cmd.Trim();
|
| 187 |
|
| 188 |
if (cmd == "POST" || cmd == "IHAVE")
|
| 189 |
{
|
| 190 |
if (this->access.control)
|
| 191 |
{
|
| 192 |
this->s_send("201 Entering native mode");
|
| 193 |
base_active nntpClient;
|
| 194 |
nntpClient.configure(this->innd_id,this->innd_name,this->innd_server,this->innd_uid,"",0,this->w_conn_str);
|
| 195 |
|
| 196 |
this->GetParentThread()->GetThreadPool()->SetThreadStatus(GetCurrentThreadId(),thread_pool::S_WAITING);
|
| 197 |
|
| 198 |
if (cmd == "POST") // Get & Post
|
| 199 |
{
|
| 200 |
nntpClient.work(true,false);
|
| 201 |
}
|
| 202 |
if (cmd == "IHAVE") // IHAVE only
|
| 203 |
{
|
| 204 |
nntpClient.work(false,true);
|
| 205 |
}
|
| 206 |
|
| 207 |
this->GetParentThread()->GetThreadPool()->SetThreadStatus(GetCurrentThreadId(),thread_pool::S_WORKING);
|
| 208 |
|
| 209 |
this->s_send("Entering passive mode\r\n.");
|
| 210 |
}
|
| 211 |
else
|
| 212 |
{
|
| 213 |
this->s_send("400 Forbidden");
|
| 214 |
}
|
| 215 |
}
|
| 216 |
else
|
| 217 |
{
|
| 218 |
this->s_send("501 mode server port");
|
| 219 |
}
|
| 220 |
continue;
|
| 221 |
}
|
| 222 |
|
| 223 |
if (cmd == "QUIT")
|
| 224 |
{
|
| 225 |
this->s_send("205 .\r\n");
|
| 226 |
break;
|
| 227 |
}
|
| 228 |
|
| 229 |
this->s_send("500 What?");
|
| 230 |
}
|
| 231 |
}
|
| 232 |
catch(CException* e)
|
| 233 |
{
|
| 234 |
syslog << logfile::log_head << "Error in accept()" << endl;
|
| 235 |
e->Delete();
|
| 236 |
return -1;
|
| 237 |
}
|
| 238 |
|
| 239 |
return 0;
|
| 240 |
}
|
| 241 |
|
| 242 |
controld* controld::new_client(void)
|
| 243 |
{
|
| 244 |
return (new controld());
|
| 245 |
}
|
| 246 |
|
| 247 |
bool controld::IsShutdown(void)
|
| 248 |
{
|
| 249 |
return this->thread_terminate;
|
| 250 |
}
|
| 251 |
|
| 252 |
int controld::Shutdown(void)
|
| 253 |
{
|
| 254 |
base_active nntpClient;
|
| 255 |
|
| 256 |
if (this->thread_terminate)
|
| 257 |
return 1;
|
| 258 |
|
| 259 |
this->thread_terminate = true;
|
| 260 |
nntpClient.configure(this->innd_id,this->innd_name,this->innd_server,this->innd_uid,this->w_address,0,this->w_conn_str);
|
| 261 |
nntpClient.s_connect((strlen(this->hostaddr)>0 ? this->hostaddr : "127.0.0.1"),this->port);
|
| 262 |
nntpClient.s_close();
|
| 263 |
|
| 264 |
return 0;
|
| 265 |
}
|
| 266 |
|
| 267 |
int controld::Startup(void)
|
| 268 |
{
|
| 269 |
if (!this->thread_terminate)
|
| 270 |
return 1;
|
| 271 |
|
| 272 |
this->thread_terminate = false;
|
| 273 |
|
| 274 |
return 0;
|
| 275 |
}
|
| 276 |
|
| 277 |
int controld::db_env_init(void)
|
| 278 |
{
|
| 279 |
return 0;
|
| 280 |
}
|
| 281 |
|
| 282 |
int controld::load_conf(const char* conf_file)
|
| 283 |
{
|
| 284 |
// Innd settings
|
| 285 |
char innd_id[50] = "";
|
| 286 |
char innd_name[50] = "";
|
| 287 |
char innd_server[256] = "";
|
| 288 |
unsigned long innd_uid = 0;
|
| 289 |
char innd_address[50] = "";
|
| 290 |
unsigned int bbsd_port = 0;
|
| 291 |
unsigned int innd_port = 0;
|
| 292 |
unsigned int ctrl_port = 0;
|
| 293 |
char conn_str[256] = "";
|
| 294 |
unsigned int if_startup = 0;
|
| 295 |
|
| 296 |
// Load configuration
|
| 297 |
try
|
| 298 |
{
|
| 299 |
char c_name[256];
|
| 300 |
FILE * fin;
|
| 301 |
|
| 302 |
if ((fin = fopen(conf_file,"r")) == NULL)
|
| 303 |
{
|
| 304 |
syslog << logfile::log_head << "Open innd.conf failed" << endl;
|
| 305 |
return -1;
|
| 306 |
}
|
| 307 |
|
| 308 |
while(fscanf(fin,"%s",c_name) != EOF)
|
| 309 |
{
|
| 310 |
if (c_name[0] == '#')
|
| 311 |
continue;
|
| 312 |
fscanf(fin,"%*c");
|
| 313 |
strlwr(c_name);
|
| 314 |
if (strcmp(c_name,"innd_id")==0)
|
| 315 |
{
|
| 316 |
fscanf(fin,"%s",innd_id);
|
| 317 |
}
|
| 318 |
if (strcmp(c_name,"innd_name")==0)
|
| 319 |
{
|
| 320 |
fscanf(fin,"%s",innd_name);
|
| 321 |
}
|
| 322 |
if (strcmp(c_name,"innd_server")==0)
|
| 323 |
{
|
| 324 |
fscanf(fin,"%s",innd_server);
|
| 325 |
}
|
| 326 |
if (strcmp(c_name,"innd_uid")==0)
|
| 327 |
{
|
| 328 |
fscanf(fin,"%ul",&innd_uid);
|
| 329 |
}
|
| 330 |
if (strcmp(c_name,"innd_address")==0)
|
| 331 |
{
|
| 332 |
fscanf(fin,"%s",innd_address);
|
| 333 |
}
|
| 334 |
if (strcmp(c_name,"bbsd_port")==0)
|
| 335 |
{
|
| 336 |
fscanf(fin,"%ud",&bbsd_port);
|
| 337 |
}
|
| 338 |
if (strcmp(c_name,"innd_port")==0)
|
| 339 |
{
|
| 340 |
fscanf(fin,"%ud",&innd_port);
|
| 341 |
}
|
| 342 |
if (strcmp(c_name,"ctrl_port")==0)
|
| 343 |
{
|
| 344 |
fscanf(fin,"%ud",&ctrl_port);
|
| 345 |
}
|
| 346 |
if (strcmp(c_name,"bbsd_startup")==0)
|
| 347 |
{
|
| 348 |
fscanf(fin,"%ud",&if_startup);
|
| 349 |
this->bbsd_startup = (if_startup > 0);
|
| 350 |
}
|
| 351 |
if (strcmp(c_name,"innd_startup")==0)
|
| 352 |
{
|
| 353 |
fscanf(fin,"%ud",&if_startup);
|
| 354 |
this->innd_startup = (if_startup > 0);
|
| 355 |
}
|
| 356 |
if (strcmp(c_name,"outd_startup")==0)
|
| 357 |
{
|
| 358 |
fscanf(fin,"%ud",&if_startup);
|
| 359 |
this->outd_startup = (if_startup > 0);
|
| 360 |
}
|
| 361 |
if (strcmp(c_name,"startup_delay")==0)
|
| 362 |
{
|
| 363 |
fscanf(fin,"%ud",&(this->startup_delay));
|
| 364 |
}
|
| 365 |
if (strcmp(c_name,"conn_str")==0)
|
| 366 |
{
|
| 367 |
fgets(conn_str,255,fin);
|
| 368 |
}
|
| 369 |
}
|
| 370 |
|
| 371 |
fclose(fin);
|
| 372 |
}
|
| 373 |
catch(CException* e)
|
| 374 |
{
|
| 375 |
syslog << logfile::log_head << "Load configuration failed" << endl;
|
| 376 |
e->Delete();
|
| 377 |
return -1;
|
| 378 |
}
|
| 379 |
|
| 380 |
this->configure(innd_id,innd_name,innd_server,innd_uid,innd_address,ctrl_port,conn_str);
|
| 381 |
this->bbsd.configure(innd_id,innd_name,innd_server,innd_uid,innd_address,bbsd_port,conn_str);
|
| 382 |
this->innd.configure(innd_id,innd_name,innd_server,innd_uid,innd_address,innd_port,conn_str);
|
| 383 |
this->outd.configure(innd_id,innd_name,innd_server,innd_uid,innd_address,0,conn_str);
|
| 384 |
|
| 385 |
return 0;
|
| 386 |
}
|
| 387 |
|
| 388 |
void controld::StartupChild(void)
|
| 389 |
{
|
| 390 |
if (this->bbsd_startup)
|
| 391 |
{
|
| 392 |
if (this->bbsd.begin() == 0)
|
| 393 |
{
|
| 394 |
syslog << logfile::log_head << "Begin BBSD ... OK" << endl;
|
| 395 |
}
|
| 396 |
else
|
| 397 |
{
|
| 398 |
syslog << logfile::log_head << "Begin BBSD ... Failed" << endl;
|
| 399 |
}
|
| 400 |
}
|
| 401 |
|
| 402 |
if (this->innd_startup)
|
| 403 |
{
|
| 404 |
if (this->innd.begin() == 0)
|
| 405 |
{
|
| 406 |
syslog << logfile::log_head << "Begin INND ... OK" << endl;
|
| 407 |
}
|
| 408 |
else
|
| 409 |
{
|
| 410 |
syslog << logfile::log_head << "Begin INND ... Failed" << endl;
|
| 411 |
}
|
| 412 |
}
|
| 413 |
|
| 414 |
if (this->outd_startup)
|
| 415 |
{
|
| 416 |
if (this->outd.begin() == 0)
|
| 417 |
{
|
| 418 |
syslog << logfile::log_head << "Begin OUTD ... OK" << endl;
|
| 419 |
}
|
| 420 |
else
|
| 421 |
{
|
| 422 |
syslog << logfile::log_head << "Begin OUTD ... Failed" << endl;
|
| 423 |
}
|
| 424 |
}
|
| 425 |
}
|
| 426 |
|
| 427 |
void controld::StartupDelay(void)
|
| 428 |
{
|
| 429 |
Sleep(this->startup_delay * 1000);
|
| 430 |
}
|
| 431 |
|
| 432 |
void controld::ShutdownChild(void)
|
| 433 |
{
|
| 434 |
if (this->bbsd.IsRunning())
|
| 435 |
{
|
| 436 |
if (this->bbsd.end() == 0)
|
| 437 |
{
|
| 438 |
syslog << logfile::log_head << "End BBSD ... OK" << endl;
|
| 439 |
}
|
| 440 |
else
|
| 441 |
{
|
| 442 |
syslog << logfile::log_head << "End BBSD ... Failed" << endl;
|
| 443 |
}
|
| 444 |
}
|
| 445 |
|
| 446 |
if (this->innd.IsRunning())
|
| 447 |
{
|
| 448 |
if (this->innd.end() == 0)
|
| 449 |
{
|
| 450 |
syslog << logfile::log_head << "End INND ... OK" << endl;
|
| 451 |
}
|
| 452 |
else
|
| 453 |
{
|
| 454 |
syslog << logfile::log_head << "End INND ... Failed" << endl;
|
| 455 |
}
|
| 456 |
}
|
| 457 |
|
| 458 |
if (this->outd.IsRunning())
|
| 459 |
{
|
| 460 |
if (this->outd.end() == 0)
|
| 461 |
{
|
| 462 |
syslog << logfile::log_head << "End OUTD ... OK" << endl;
|
| 463 |
}
|
| 464 |
else
|
| 465 |
{
|
| 466 |
syslog << logfile::log_head << "End OUTD ... Failed" << endl;
|
| 467 |
}
|
| 468 |
}
|
| 469 |
}
|
| 470 |
|
| 471 |
int controld::InitThreadPool(void)
|
| 472 |
{
|
| 473 |
base* p_ParentThread;
|
| 474 |
|
| 475 |
p_ParentThread = new controld();
|
| 476 |
|
| 477 |
this->SetParentThread(p_ParentThread);
|
| 478 |
this->bbsd.SetParentThread(p_ParentThread);
|
| 479 |
this->innd.SetParentThread(p_ParentThread);
|
| 480 |
this->outd.SetParentThread(p_ParentThread);
|
| 481 |
|
| 482 |
p_ParentThread->SetThreadPool(new thread_pool(TS_MAX_THREAD));
|
| 483 |
|
| 484 |
if (p_ParentThread->GetThreadPool()->GetLastError() != 0)
|
| 485 |
{
|
| 486 |
syslog << logfile::log_head << "Create Thread Pool ... failed" << endl;
|
| 487 |
}
|
| 488 |
|
| 489 |
return 0;
|
| 490 |
}
|
| 491 |
|
| 492 |
int controld::CleanupThreadPool(void)
|
| 493 |
{
|
| 494 |
delete(this->GetParentThread());
|
| 495 |
|
| 496 |
return 0;
|
| 497 |
}
|