/
bnbt.cpp
422 lines (330 loc) · 9.33 KB
/
bnbt.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/***
*
* BNBT Beta 8.0 - A C++ BitTorrent Tracker
* Copyright (C) 2003-2004 Trevor Hogan
*
* CBTT variations (C) 2003-2005 Harold Feit
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
***/
#include <signal.h>
#include "bnbt.h"
#include "bnbt_mysql.h"
#include "config.h"
#include "link.h"
#include "server.h"
#ifdef WIN32
#include "util_ntservice.h"
#endif
#include "util.h"
#ifndef WIN32
int GetLastError( ) { return errno; }
#endif
time_t giStartTime;
unsigned long GetTime( )
{
return (unsigned long)( time( NULL ) - giStartTime );
}
unsigned long GetRealTime( )
{
return (unsigned long)( time( NULL ) );
}
CServer *gpServer;
CLink *gpLink;
CLinkServer *gpLinkServer;
CMutex gmtxOutput;
string gstrErrorLogDir;
string gstrErrorLogFilePattern;
string gstrErrorLogFile;
FILE *gpErrorLog;
string gstrAccessLogDir;
string gstrAccessLogFilePattern;
string gstrAccessLogFile;
FILE *gpAccessLog;
unsigned long giErrorLogCount;
unsigned long giAccessLogCount;
int giFlushInterval;
bool gbDebug;
unsigned int giMaxConns;
unsigned int giMaxRecvSize;
string gstrStyle;
string gstrCharSet;
string gstrRealm;
// TCP window size
int giSO_SNDBUF;
int giSO_RECBUF;
void sigCatcher( int sig )
{
signal( SIGABRT, sigCatcher );
signal( SIGINT, sigCatcher );
// 2006/12/04 and SIGTERM and SIGHUP
signal( SIGTERM, sigCatcher );
#ifndef WIN32
signal( SIGHUP, sigCatcher );
#endif
if( gpServer )
{
if( gpServer->isDying( ) )
exit( 1 );
else
gpServer->Kill( );
}
else
exit( 1 );
}
int main( int argc, char *argv[] )
{
#ifdef WIN32
if( argv[0] )
{
char *szEndPos = strrchr( argv[0], '\\' );
if( szEndPos )
{
char *szEXEPath = new char[szEndPos - argv[0] + 1];
memcpy( szEXEPath, argv[0], szEndPos - argv[0] );
szEXEPath[szEndPos - argv[0]] = '\0';
SetCurrentDirectory( szEXEPath );
delete [] szEXEPath;
}
}
if( argc > 1 )
{
CFG_Open( CFG_FILE );
#define BNBT_SERVICE_NAME const_cast<LPSTR> (CFG_GetString( "cbtt_service_name", "CBTT Service" ).c_str())
CFG_Close( CFG_FILE );
printf( "Service name ");
printf( BNBT_SERVICE_NAME );
printf( "\n" );
if( _stricmp( argv[1], "-i" ) == 0 )
{
// install service
if( UTIL_NTServiceTest( ) )
printf( "BNBT Service is already installed!\n" );
else
{
if( UTIL_NTServiceInstall( ) )
printf( "BNBT Service installed.\n" );
else
printf( "BNBT Service failed to install (error %d).\n", GetLastError( ) );
}
return 0;
}
else if( _stricmp( argv[1], "-u" ) == 0 )
{
// uninstall service
if( !UTIL_NTServiceTest( ) )
printf( "BNBT Service is not installed!\n" );
else
{
if( UTIL_NTServiceUninstall( ) )
printf( "BNBT Service uninstalled.\n" );
else
printf( "BNBT Service failed to uninstall (error %d).\n", GetLastError( ) );
}
return 0;
}
else if( _stricmp( argv[1], "-start" ) == 0 )
{
// start
if( !UTIL_NTServiceTest( ) )
printf( "BNBT Service is not installed!\n" );
else
{
printf( "Starting BNBT Service.\n" );
if( !UTIL_NTServiceStart( ) )
printf( "BNBT Service failed to start (error %d).\n", GetLastError( ) );
}
return 0;
}
else if( _stricmp( argv[1], "-stop" ) == 0 )
{
// stop
if( !UTIL_NTServiceTest( ) )
printf( "BNBT Service is not installed!\n" );
else
{
printf( "Stopping BNBT Service.\n" );
if( !UTIL_NTServiceStop( ) )
printf( "BNBT Service failed to stop (error %d).\n", GetLastError( ) );
}
return 0;
}
else if( _stricmp( argv[1], "-s" ) == 0 )
{
// internal start
SERVICE_TABLE_ENTRY st[] = {
{ BNBT_SERVICE_NAME, NTServiceMain },
{ NULL, NULL }
};
StartServiceCtrlDispatcher( st );
return 0;
}
}
#else
// disable SIGPIPE since some systems like OS X don't define MSG_NOSIGNAL
signal( SIGPIPE, SIG_IGN );
#endif
// catch SIGABRT and SIGINT
// 2006/12/04 and SIGTERM and SIGHUP
signal( SIGABRT, sigCatcher );
signal( SIGINT, sigCatcher );
signal( SIGTERM, sigCatcher );
#ifndef WIN32
signal( SIGHUP, sigCatcher );
#endif
return bnbtmain( );
}
int bnbtmain( )
{
gmtxOutput.Initialize( );
srand( time( NULL ) );
giStartTime = time( NULL );
CFG_Open( CFG_FILE );
CFG_SetDefaults( );
CFG_Close( CFG_FILE );
gstrErrorLogFilePattern = CFG_GetString( "bnbt_error_log_file_pattern", "%Y-%m-%de.log" );
gstrErrorLogDir = CFG_GetString( "bnbt_error_log_dir", string( ) );
if( !gstrErrorLogDir.empty( ) && gstrErrorLogDir[gstrErrorLogDir.size( ) - 1] != PATH_SEP )
gstrErrorLogDir += PATH_SEP;
gpErrorLog = NULL;
gstrAccessLogFilePattern = CFG_GetString( "bnbt_access_log_file_pattern", "%Y-%m-%d.log" );
gstrAccessLogDir = CFG_GetString( "bnbt_access_log_dir", string( ) );
if( !gstrAccessLogDir.empty( ) && gstrAccessLogDir[gstrAccessLogDir.size( ) - 1] != PATH_SEP )
gstrAccessLogDir += PATH_SEP;
gpAccessLog = NULL;
giErrorLogCount = 0;
giAccessLogCount = 0;
giFlushInterval = CFG_GetInt( "bnbt_flush_interval", 100 );
gbDebug = CFG_GetInt( "bnbt_debug", 1 ) == 0 ? false : true;
giMaxConns = CFG_GetInt( "bnbt_max_conns", 64 );
giMaxRecvSize = CFG_GetInt( "bnbt_max_recv_size", 524288 );
gstrStyle = CFG_GetString( "bnbt_style_sheet", string( ) );
gstrCharSet = CFG_GetString( "bnbt_charset", "iso-8859-1" );
gstrRealm = CFG_GetString( "bnbt_realm", "BNBT" );
// start winsock
// TCP window size
giSO_RECBUF = CFG_GetInt( "xbnbt_so_recbuf", 128 ) * 1024;
giSO_SNDBUF = CFG_GetInt( "xbnbt_so_sndbuf", 128 ) * 1024;
#ifdef WIN32
WSADATA wsaData;
int iResult = WSAStartup( MAKEWORD(2,2), &wsaData );
if ( iResult != NO_ERROR )
{
UTIL_LogPrint( "error - unable to start winsock (error %d)\n", GetLastError( ) );
if( gpAccessLog )
fclose( gpAccessLog );
if( gpErrorLog )
fclose( gpErrorLog );
gmtxOutput.Destroy( );
return 1;
}
#endif
// start mysql
#ifdef BNBT_MYSQL
if( !( gpMySQL = mysql_init( NULL ) ) )
{
UTIL_LogPrint( "mysql error - %s\n", mysql_error( gpMySQL ) );
if( gpAccessLog )
fclose( gpAccessLog );
if( gpErrorLog )
fclose( gpErrorLog );
gmtxOutput.Destroy( );
return 1;
}
gstrMySQLHost = CFG_GetString( "mysql_host", string( ) );
gstrMySQLDatabase = CFG_GetString( "mysql_database", "bnbt" );
gstrMySQLUser = CFG_GetString( "mysql_user", string( ) );
gstrMySQLPassword = CFG_GetString( "mysql_password", string( ) );
giMySQLPort = CFG_GetInt( "mysql_port", 0 );
if( !( mysql_real_connect( gpMySQL, gstrMySQLHost.c_str( ), gstrMySQLUser.c_str( ), gstrMySQLPassword.c_str( ), gstrMySQLDatabase.c_str( ), giMySQLPort, NULL, 0 ) ) )
{
UTIL_LogPrint( "mysql error - %s\n", mysql_error( gpMySQL ) );
if( gpAccessLog )
fclose( gpAccessLog );
if( gpErrorLog )
fclose( gpErrorLog );
gmtxOutput.Destroy( );
return 1;
}
UTIL_LogPrint( "mysql - connected\n" );
#endif
gpServer = new CServer( );
gpLink = NULL;
gpLinkServer = NULL;
if( CFG_GetInt( "bnbt_tlink_server", 0 ) != 0 )
gpLinkServer = new CLinkServer( );
else
{
if( !CFG_GetString( "bnbt_tlink_connect", string( ) ).empty( ) )
{
#ifdef WIN32
if( _beginthread( ( void (*)(void *) )StartLink, 0, NULL ) == -1 )
UTIL_LogPrint( "error - unable to spawn link thread\n" );
#else
pthread_t thread;
// set detached state since we don't need to join with any threads
pthread_attr_t attr;
pthread_attr_init( &attr );
pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_DETACHED );
int c = pthread_create( &thread, &attr, ( void * (*)(void *) )StartLink, NULL );
if( c != 0 )
UTIL_LogPrint( "error - unable to spawn link thread (error %s)\n", strerror( c ) );
#endif
}
}
while( 1 )
{
if( gpServer->Update( true ) )
{
delete gpServer;
gpServer = NULL;
if( gpLinkServer )
{
delete gpLinkServer;
gpLinkServer = NULL;
}
break;
}
if( gpLinkServer )
gpLinkServer->Update( );
}
// wait for the link or it might make a big mess
if( gpLink )
gpLink->Kill( );
unsigned long iStart = GetTime( );
while( gpLink )
{
UTIL_LogPrint( "server - waiting for link to disconnect\n" );
MILLISLEEP( 1000 );
if( GetTime( ) - iStart > 60 )
{
UTIL_LogPrint( "server - waited 60 seconds, exiting anyway\n" );
break;
}
}
#ifdef BNBT_MYSQL
mysql_close( gpMySQL );
#endif
if( gpAccessLog )
fclose( gpAccessLog );
if( gpErrorLog )
fclose( gpErrorLog );
gmtxOutput.Destroy( );
#ifdef WIN32
WSACleanup( );
#endif
return 0;
}