OSDN Git Service

c4b518b97bf00adc7bc3372ad147b152d9525930
[peercast-im/PeerCastIM.git] / core / common / servent.h
1 // ------------------------------------------------
2 // File : servent.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
21 #ifndef _SERVENT_H
22 #define _SERVENT_H
23
24 // ----------------------------------
25 #include "socket.h"
26 #include "sys.h"
27 #include "gnutella.h"
28 #include "channel.h"
29 #include "http.h"
30 #include "rtsp.h"
31 #include "pcp.h"
32 #include "addrCont.h"
33 #ifdef _WIN32
34 #include "win32/ts_vector.h"
35 #endif
36
37 class HTML;
38
39 class AtomStream;
40
41 // ----------------------------------
42 // Servent handles the actual connection between clients
43 class Servent
44 {
45 public:
46
47         enum 
48         {
49                 MAX_HASH = 500,                 // max. amount of packet hashes Servents can store
50                 MAX_OUTPACKETS = 32             // max. output packets per queue (normal/priority)
51         };
52
53         enum
54         {
55                 MAX_PROC_PACKETS = 300,
56                 MAX_OUTWARD_SIZE = 1024 * 10
57         };
58
59     enum TYPE                                   
60     {
61                 T_NONE,                                 // Not allocated
62         T_INCOMING,                             // Unknown incoming
63         T_SERVER,                               // The main server 
64                 T_RELAY,                                // Outgoing relay
65                 T_DIRECT,                               // Outgoing direct connection
66                 T_COUT,                                 // PCP out connection
67                 T_CIN,                                  // PCP in connection
68                 T_PGNU                                  // old protocol connection              
69     };
70
71     enum STATUS
72     {
73         S_NONE,
74         S_CONNECTING,
75         S_PROTOCOL,
76         S_HANDSHAKE,
77         S_CONNECTED,
78         S_CLOSING,
79                 S_LISTENING,
80                 S_TIMEOUT,
81                 S_REFUSED,
82                 S_VERIFIED,
83                 S_ERROR,
84                 S_WAIT,
85                 S_FREE
86     };
87
88         enum PROTOCOL
89         {
90                 P_UNKNOWN,
91                 P_GNUTELLA06,
92                 P_PCP
93         };
94
95
96         enum SORT
97         {
98                 SORT_NAME = 0,
99                 SORT_BITRATE,
100                 SORT_LISTENERS,
101                 SORT_HOSTS,
102                 SORT_TYPE,
103                 SORT_GENRE
104         };
105
106         enum ALLOW
107         {
108                 ALLOW_HTML              = 0x01,
109                 ALLOW_BROADCAST = 0x02,
110                 ALLOW_NETWORK   = 0x04,
111                 ALLOW_DIRECT    = 0x08,
112                 ALLOW_ALL               = 0xff
113         };
114
115         Servent(int);
116         ~Servent();
117
118         void    reset();
119     bool        initServer(Host &);
120     void        initIncoming(ClientSocket *,unsigned int);
121     void        initOutgoing(TYPE);
122         void    initGIV(Host &, GnuID &);
123         void    initPCP(Host &);
124
125         void    checkFree();
126
127
128
129
130         //      funcs for handling status/type
131         void    setStatus(STATUS);
132         static char *   getTypeStr(Servent::TYPE t) {return typeMsgs[t];}
133         char *  getTypeStr() {return getTypeStr(type);}
134         char *  getStatusStr() {return statusMsgs[status];}
135         int             getOutput();
136         void    addBytes(unsigned int);
137         bool    isOlderThan(Servent *s)
138         {
139                 if (s)
140                 {
141                         unsigned int t = sys->getTime();
142                         return ((t-lastConnect) > (t-s->lastConnect));
143                 }else
144                         return true;
145         }
146
147
148
149         // static funcs that do the actual work in the servent thread
150         static  THREAD_PROC             serverProc(ThreadInfo *);
151         static THREAD_PROC serverProcMain(ThreadInfo *);
152         static  THREAD_PROC             outgoingProc(ThreadInfo *);
153         static THREAD_PROC outgoingProcMain(ThreadInfo *);
154         static  THREAD_PROC             incomingProc(ThreadInfo *);
155         static THREAD_PROC incomingProcMain(ThreadInfo *);
156         static  THREAD_PROC             givProc(ThreadInfo *);
157         static THREAD_PROC givProcMain(ThreadInfo *);
158         static  THREAD_PROC             pcpProc(ThreadInfo *);
159         static  THREAD_PROC             fetchProc(ThreadInfo *);
160
161         static bool     pingHost(Host &,GnuID &);
162
163         bool    getLocalURL(char *);
164         bool    getLocalTypeURL(char *, ChanInfo::TYPE);
165
166         // various types of handshaking are needed
167         void    handshakePLS(ChanHitList **, int, bool);
168         void    handshakePLS(ChanInfo &, bool);
169
170         void    handshakeHTML(char *);
171         void    handshakeXML();
172         void    handshakeCMD(char *);
173         bool    handshakeAuth(HTTP &,const char *,bool);
174         void    handshakeIn();
175         void    handshakeOut();
176
177
178         void    processOutPCP();
179         void    processOutChannel();
180
181         bool    handshakeStream(ChanInfo &);
182         void    handshakeGiv(GnuID &);
183
184         void    handshakeICY(Channel::SRC_TYPE,bool);
185         void    handshakeIncoming();
186         void    handshakePOST();
187         void    handshakeRTSP(RTSP &);
188         void    handshakeHTTP(HTTP &,bool);
189
190         void    handshakeRemoteFile(const char *);
191         void    handshakeLocalFile(const char *);
192
193         static void     handshakeOutgoingPCP(AtomStream &,Host &,GnuID &,String &,bool);
194         static void     handshakeIncomingPCP(AtomStream &,Host &,GnuID &,String &);
195
196         void    processIncomingPCP(bool);
197
198         bool    waitForChannelHeader(ChanInfo &);
199         ChanInfo        findChannel(char *str,ChanInfo &);
200
201         bool    writeVariable(Stream &, const String &);
202
203
204         // the "mainloop" of servents 
205         void    processGnutella();
206         void    processRoot();
207         void    processServent();
208         void    processStream(bool,ChanInfo &);
209         void    processPCP(bool,bool);
210
211         bool    procAtoms(AtomStream &);
212         void    procRootAtoms(AtomStream &,int);
213         void    procHeloAtoms(AtomStream &,int,bool);
214         void    procGetAtoms(AtomStream &,int);
215
216         void    triggerChannel(char *,ChanInfo::PROTOCOL,bool);
217         void    sendPeercastChannel();
218         void    sendRawChannel(bool,bool);
219 //      void    sendRawMultiChannel(bool,bool);
220         void    sendRawMetaChannel(int);
221         void    sendPCPChannel();
222         void    checkPCPComms(Channel *, AtomStream &);
223
224         static void     readICYHeader(HTTP &, ChanInfo &, char *, size_t);
225         bool    canStream(Channel *);
226
227         bool    isConnected() {return status == S_CONNECTED;}
228         bool    isListening() {return status == S_LISTENING;}
229
230         bool    isAllowed(int);
231         bool    isFiltered(int);
232
233         // connection handling funcs
234         void    createSocket();
235         void    kill();
236         void    abort();
237         bool    isPrivate();
238         bool    isLocal();
239
240
241         Host    getHost();
242
243         bool    outputPacket(GnuPacket &,bool);
244         bool    hasSeenPacket(GnuPacket &p) {return seenIDs.contains(p.id);}
245         bool    acceptGIV(ClientSocket *);
246         bool    sendPacket(ChanPacket &,GnuID &,GnuID &,GnuID &,Servent::TYPE);
247
248
249         TYPE type;
250         STATUS status;
251
252         static char *statusMsgs[],*typeMsgs[];
253         GnuStream gnuStream;
254         GnuPacket pack;
255         unsigned int    lastConnect,lastPing,lastPacket;
256         String  agent;
257
258         GnuIDList       seenIDs;
259         GnuID   networkID;
260         int             serventIndex;
261
262         GnuID   remoteID;
263
264         GnuID   chanID;
265
266         GnuID   givID;
267
268         ThreadInfo      thread;
269
270
271         String  loginPassword;
272         String  loginMount;
273
274         bool    priorityConnect;
275         bool    addMetadata;
276         int             nsSwitchNum;
277
278         unsigned        int allow;
279
280     ClientSocket *sock,*pushSock;
281
282         WLock   lock;
283
284         bool    sendHeader;
285         unsigned int syncPos,streamPos;
286         int             servPort;
287
288         ChanInfo::PROTOCOL      outputProtocol;
289
290         GnuPacketBuffer outPacketsNorm,outPacketsPri;
291
292         unsigned int bytesPerSecond;
293         bool    flowControl;
294
295         Servent *next;
296
297         PCPStream *pcpStream;
298         Cookie  cookie;
299
300         int servent_id;
301         unsigned int lastSkipTime;
302         unsigned int lastSkipCount;
303         unsigned int waitPort;
304
305         ChanHit serventHit;
306
307         int channel_id;
308 };
309
310 extern char *nextCGIarg(char *cp, char *cmd, char *arg);
311
312
313 #endif
314