OSDN Git Service

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