OSDN Git Service

e424a9dd6d57520bfc305525ee3066c46ff82c5f
[peercast-im/PeerCastIM.git] / c: / Git / PeerCast.root / PeerCast / core / common / servmgr.h
1 // ------------------------------------------------
2 // File : servmgr.h
3 // Date: 4-apr-2002
4 // Author: giles
5 // Desc: 
6 //
7 // (c) 2002 peercast.org
8 // ------------------------------------------------
9 // This program is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation; either version 2 of the License, or
12 // (at your option) any later version.
13
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 // GNU General Public License for more details.
18 // ------------------------------------------------
19
20 #ifndef _SERVMGR_H
21 #define _SERVMGR_H
22
23 #include "servent.h"
24
25 // ----------------------------------
26
27 const int MIN_YP_RETRY = 20;
28 const int MIN_TRACKER_RETRY = 10;
29 const int MIN_RELAY_RETRY = 5;
30
31 // ----------------------------------
32 class BCID
33 {
34 public:
35         BCID()
36         :next(0),valid(true)
37         {}
38
39         bool    writeVariable(Stream &, const String &);
40
41         GnuID   id;
42         String  name,email,url;
43         bool    valid;
44         BCID    *next;
45 };
46
47
48 // ----------------------------------
49 class ServHost
50 {
51 public:
52         enum TYPE
53         {
54                 T_NONE,
55                 T_STREAM,
56                 T_CHANNEL,
57                 T_SERVENT,
58                 T_TRACKER
59         };
60
61         ServHost() {init();}
62         void init()
63         {
64                 host.init();
65                 time = 0;
66                 type = T_NONE;
67         }
68         void init(Host &h, TYPE tp, unsigned int tim)
69         {
70                 init();
71                 host = h;
72                 type = tp;
73                 if (tim)
74                         time = tim;
75                 else
76                         time = sys->getTime();
77         }
78
79         static const char *getTypeStr(TYPE);
80         static TYPE getTypeFromStr(const char *);
81
82         TYPE type;
83         Host host;
84         unsigned int time;
85 };
86 // ----------------------------------
87 class ServFilter 
88 {
89 public:
90         enum 
91         {
92                 F_PRIVATE  = 0x01,
93                 F_BAN      = 0x02,
94                 F_NETWORK  = 0x04,
95                 F_DIRECT   = 0x08
96         };
97
98         ServFilter() {init();}
99         void    init()
100         {
101                 flags = 0;
102                 host.init();
103         }
104
105         bool    writeVariable(Stream &, const String &);
106
107         Host host;
108         unsigned int flags;
109 };
110
111 // ----------------------------------
112 // ServMgr keeps track of Servents
113 class ServMgr
114 {
115
116
117
118 public:
119
120         enum NOTIFY_TYPE
121         {
122                 NT_UPGRADE                      = 0x0001,
123                 NT_PEERCAST                     = 0x0002,
124                 NT_BROADCASTERS         = 0x0004,
125                 NT_TRACKINFO            = 0x0008
126         };
127
128         enum FW_STATE
129         {
130                 FW_OFF,
131                 FW_ON,
132                 FW_UNKNOWN
133         };
134         enum {
135
136                 MAX_HOSTCACHE = 100,            // max. amount of hosts in cache
137                 MIN_HOSTS       = 3,                    // min. amount of hosts that should be kept in cache
138
139                 MAX_OUTGOING = 3,                       // max. number of outgoing servents to use
140                 MAX_INCOMING = 6,                   // max. number of public incoming servents to use
141                 MAX_TRYOUT   = 10,                      // max. number of outgoing servents to try connect
142                 MIN_CONNECTED = 3,                      // min. amount of connected hosts that should be kept
143
144                 MIN_RELAYS = 1,
145
146                 MAX_FILTERS = 50,
147
148                 MAX_VERSIONS = 16,
149
150                 MAX_PREVIEWTIME = 300,          // max. seconds preview per channel available (direct connections)
151                 MAX_PREVIEWWAIT = 300,          // max. seconds wait between previews 
152
153         };
154
155         enum AUTH_TYPE
156         {
157                 AUTH_COOKIE,
158                 AUTH_HTTPBASIC
159         };
160
161         
162
163         ServMgr();
164
165         bool    start();
166
167         Servent *findServent(unsigned int,unsigned short,GnuID &);
168         Servent *findServent(Servent::TYPE);
169         Servent *findServent(Servent::TYPE,Host &,GnuID &);
170         Servent *findOldestServent(Servent::TYPE,bool);
171         Servent *findServentByIndex(int);
172         Servent *findServentByServentID(int);
173
174         bool    writeVariable(Stream &, const String &);
175         Servent *allocServent();
176         unsigned int            numUsed(int);
177         unsigned int            numStreams(GnuID &, Servent::TYPE,bool);
178         unsigned int            numStreams(Servent::TYPE,bool);
179         unsigned int            numConnected(int,bool,unsigned int);
180         unsigned int            numConnected(int t,int tim = 0)
181         {
182                 return numConnected(t,false,tim)+numConnected(t,true,tim);
183         }
184         unsigned int            numConnected();
185         unsigned int            numServents();
186
187         unsigned int            totalConnected()
188         {
189                 //return numConnected(Servent::T_OUTGOING) + numConnected(Servent::T_INCOMING);
190                 return numConnected();
191         }
192         unsigned int            numOutgoing();
193         bool    isFiltered(int,Host &h);
194         bool    addOutgoing(Host,GnuID &,bool);
195         Servent *findConnection(Servent::TYPE,GnuID &);
196
197
198         static  THREAD_PROC             serverProc(ThreadInfo *);
199         static  THREAD_PROC             clientProc(ThreadInfo *);
200         static  THREAD_PROC             trackerProc(ThreadInfo *);
201         static  THREAD_PROC             idleProc(ThreadInfo *);
202
203         int             broadcast(GnuPacket &,Servent * = NULL);
204         int             route(GnuPacket &, GnuID &, Servent * = NULL);
205
206         XML::Node *createServentXML();
207
208         void            connectBroadcaster();
209         void            procConnectArgs(char *,ChanInfo &);
210
211         void            quit();
212         void            closeConnections(Servent::TYPE);
213
214         void            checkFirewall();
215
216         // host cache
217         void                    addHost(Host &,ServHost::TYPE,unsigned int);
218         int                             getNewestServents(Host *,int, Host &);
219         ServHost                getOutgoingServent(GnuID &);
220         void                    deadHost(Host &,ServHost::TYPE);
221         unsigned int    numHosts(ServHost::TYPE);
222         void                    clearHostCache(ServHost::TYPE);
223         bool                    seenHost(Host &,ServHost::TYPE,unsigned int);
224
225         void    setMaxRelays(int);
226         void    setFirewall(FW_STATE);
227         bool    checkForceIP();
228         FW_STATE        getFirewall() {return firewalled;}
229         void    saveSettings(const char *);
230         void    loadSettings(const char *);
231         void    setPassiveSearch(unsigned int);
232         int             findChannel(ChanInfo &);
233         bool    getChannel(char *,ChanInfo &,bool);
234         void    setFilterDefaults();
235
236         bool    acceptGIV(ClientSocket *);
237         void    addVersion(unsigned int);
238
239         void broadcastRootSettings(bool);
240         int broadcastPushRequest(ChanHit &, Host &, GnuID &, Servent::TYPE);
241         void writeRootAtoms(AtomStream &,bool);
242
243         int     broadcastPacket(ChanPacket &,GnuID &,GnuID &,GnuID &,Servent::TYPE type);
244
245         void    addValidBCID(BCID *);
246         void    removeValidBCID(GnuID &);
247         BCID    *findValidBCID(GnuID &);
248         BCID    *findValidBCID(int);
249
250         unsigned int getUptime()
251         {
252                 return sys->getTime()-startTime;
253         }
254
255         bool    seenPacket(GnuPacket &);
256
257
258         bool    needHosts()
259         {
260                 return false;
261                 //return numHosts(ServHost::T_SERVENT) < maxTryout;
262         }
263
264         unsigned int            numActiveOnPort(int);
265         unsigned int            numActive(Servent::TYPE);
266
267         bool    needConnections() 
268         {
269                 return numConnected(Servent::T_PGNU,60) < minGnuIncoming;
270         }
271         bool    tryFull() 
272         {
273                 return false;
274                 //return maxTryout ? numUsed(Servent::T_OUTGOING) > maxTryout: false;
275         }
276
277         bool    pubInOver() 
278         {
279                 return numConnected(Servent::T_PGNU) > maxGnuIncoming;
280 //              return maxIncoming ? numConnected(Servent::T_INCOMING,false) > maxIncoming : false;
281         }
282         bool    pubInFull() 
283         {
284                 return numConnected(Servent::T_PGNU) >= maxGnuIncoming;
285 //              return maxIncoming ? numConnected(Servent::T_INCOMING,false) >= maxIncoming : false;
286         }
287
288         bool    outUsedFull() 
289         {
290                 return false;
291 //              return maxOutgoing ? numUsed(Servent::T_OUTGOING) >= maxOutgoing: false;
292         }
293         bool    outOver() 
294         {
295                 return false;
296 //              return maxOutgoing ? numConnected(Servent::T_OUTGOING) > maxOutgoing : false;
297         }
298
299         bool    controlInFull()
300         {
301                 return numConnected(Servent::T_CIN)>=maxControl;
302         }
303
304         bool    outFull() 
305         {
306                 return false;
307 //              return maxOutgoing ? numConnected(Servent::T_OUTGOING) >= maxOutgoing : false;
308         }
309
310         bool    relaysFull() 
311         {
312                 return numStreams(Servent::T_RELAY,false) >= maxRelays;
313         }
314         bool    directFull() 
315         {
316                 return numStreams(Servent::T_DIRECT,false) >= maxDirect;
317         }
318
319         bool    bitrateFull(unsigned int br) 
320         {
321                 return maxBitrateOut ? (BYTES_TO_KBPS(totalOutput(false))+br) > maxBitrateOut  : false;
322         }
323
324         unsigned int            totalOutput(bool);
325         unsigned int            totalInput(bool);
326
327         static ThreadInfo serverThread,idleThread;
328
329
330         Servent *servents;
331         WLock   lock;
332
333         ServHost        hostCache[MAX_HOSTCACHE];
334
335         char    password[64];
336
337         bool    allowGnutella;
338
339         unsigned int            maxBitrateOut,maxControl,maxRelays,maxDirect;
340         unsigned int            minGnuIncoming,maxGnuIncoming;
341         unsigned int            maxServIn;
342
343         bool    isDisabled;
344         bool    isRoot;
345         int             totalStreams;
346
347         Host    serverHost;
348         String  rootHost;
349         String  rootHost2;
350
351         char    downloadURL[128];
352         String  rootMsg;
353         String  forceIP;
354         char    connectHost[128];
355         GnuID   networkID;
356         unsigned int            firewallTimeout;
357         int             showLog;
358         int             shutdownTimer;
359         bool    pauseLog;
360         bool    forceNormal;
361         bool    useFlowControl;
362         unsigned int lastIncoming;
363
364         bool    restartServer;
365         bool    allowDirect;
366         bool    autoConnect,autoServe,forceLookup;
367         int             queryTTL;
368
369         unsigned int            allowServer1,allowServer2;
370         unsigned int            startTime;
371         unsigned int            tryoutDelay;
372         unsigned int            refreshHTML;
373         unsigned int            relayBroadcast;
374
375         unsigned int notifyMask;
376
377         BCID            *validBCID;
378         GnuID           sessionID;
379
380         ServFilter      filters[MAX_FILTERS];
381         int     numFilters;
382
383
384         CookieList      cookieList;
385         AUTH_TYPE       authType;
386
387         char    htmlPath[128];
388         unsigned int    clientVersions[MAX_VERSIONS],clientCounts[MAX_VERSIONS];
389         int     numVersions;
390
391         int serventNum;
392         String chanLog;
393
394         char modulePath[256]; //JP-EX
395         int enableGetName; //JP-EX
396         int allowConnectPCST; //JP-EX
397         int autoRelayKeep; //JP-EX
398         unsigned int autoMaxRelaySetting; //JP-EX
399         unsigned int autoBumpSkipCount;//JP-EX
400         unsigned int kickPushStartRelays; //JP-EX
401         unsigned int kickPushInterval; //JP-EX
402         unsigned int kickPushTime;
403         bool    isCheckPushStream(); //JP-EX
404         void    banFirewalledHost(); //JP-EX
405         int kickUnrelayableHost(GnuID &, ChanHit &);
406
407         bool    getModulePath; //JP-EX
408         bool    clearPLS; //JP-EX
409         bool    writeLogFile; //JP-EX
410
411         bool    autoPort0Kick;
412         bool    allowOnlyVP;
413         unsigned int kickKeepTime;
414         bool    vpDebug;
415         bool    saveIniChannel;
416         bool    saveGuiPos;
417         bool    keepDownstreams;
418
419         bool topmostGui; // \8dÅ\91O\96Ê
420         bool startWithGui; // \8bN\93®\8e\9e\82ÉGUI
421
422         int maxRelaysIndexTxt;  // for PCRaw (relay)
423
424 private:
425         FW_STATE        firewalled;
426 };
427
428 // ----------------------------------
429 extern ServMgr *servMgr;
430
431
432 #endif