OSDN Git Service

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