1 // ------------------------------------------------
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.
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 // ------------------------------------------------
24 // ----------------------------------
34 #include "win32/ts_vector.h"
41 // ----------------------------------
42 // Servent handles the actual connection between clients
49 MAX_HASH = 500, // max. amount of packet hashes Servents can store
50 MAX_OUTPACKETS = 32 // max. output packets per queue (normal/priority)
55 MAX_PROC_PACKETS = 300,
56 MAX_OUTWARD_SIZE = 1024 * 10
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
109 ALLOW_BROADCAST = 0x02,
110 ALLOW_NETWORK = 0x04,
119 bool initServer(Host &);
120 void initIncoming(ClientSocket *,unsigned int);
121 void initOutgoing(TYPE);
122 void initGIV(Host &, GnuID &);
123 void initPCP(Host &);
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];}
136 void addBytes(unsigned int);
137 bool isOlderThan(Servent *s)
141 unsigned int t = sys->getTime();
142 return ((t-lastConnect) > (t-s->lastConnect));
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 *);
161 static bool pingHost(Host &,GnuID &);
163 bool getLocalURL(char *);
164 bool getLocalTypeURL(char *, ChanInfo::TYPE);
166 // various types of handshaking are needed
167 void handshakePLS(ChanHitList **, int, bool);
168 void handshakePLS(ChanInfo &, bool);
170 void handshakeHTML(char *);
172 void handshakeCMD(char *);
173 bool handshakeAuth(HTTP &,const char *,bool);
178 void processOutPCP();
179 void processOutChannel();
181 bool handshakeStream(ChanInfo &);
182 void handshakeGiv(GnuID &);
184 void handshakeICY(Channel::SRC_TYPE,bool);
185 void handshakeIncoming();
186 void handshakePOST();
187 void handshakeRTSP(RTSP &);
188 void handshakeHTTP(HTTP &,bool);
190 void handshakeRemoteFile(const char *);
191 void handshakeLocalFile(const char *);
193 static void handshakeOutgoingPCP(AtomStream &,Host &,GnuID &,String &,bool);
194 static void handshakeIncomingPCP(AtomStream &,Host &,GnuID &,String &);
196 void processIncomingPCP(bool);
198 bool waitForChannelHeader(ChanInfo &);
199 ChanInfo findChannel(char *str,ChanInfo &);
201 bool writeVariable(Stream &, const String &);
204 // the "mainloop" of servents
205 void processGnutella();
207 void processServent();
208 void processStream(bool,ChanInfo &);
209 void processPCP(bool,bool);
211 bool procAtoms(AtomStream &);
212 void procRootAtoms(AtomStream &,int);
213 void procHeloAtoms(AtomStream &,int,bool);
214 void procGetAtoms(AtomStream &,int);
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 &);
224 static void readICYHeader(HTTP &, ChanInfo &, char *, size_t);
225 bool canStream(Channel *);
227 bool isConnected() {return status == S_CONNECTED;}
228 bool isListening() {return status == S_LISTENING;}
231 bool isFiltered(int);
233 // connection handling funcs
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);
252 static char *statusMsgs[],*typeMsgs[];
255 unsigned int lastConnect,lastPing,lastPacket;
271 String loginPassword;
274 bool priorityConnect;
280 ClientSocket *sock,*pushSock;
285 unsigned int syncPos,streamPos;
288 ChanInfo::PROTOCOL outputProtocol;
290 GnuPacketBuffer outPacketsNorm,outPacketsPri;
292 unsigned int bytesPerSecond;
297 PCPStream *pcpStream;
301 unsigned int lastSkipTime;
302 unsigned int lastSkipCount;
303 unsigned int waitPort;
310 extern char *nextCGIarg(char *cp, char *cmd, char *arg);