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 // ----------------------------------
37 // ----------------------------------
38 // Servent handles the actual connection between clients
45 MAX_HASH = 500, // max. amount of packet hashes Servents can store
46 MAX_OUTPACKETS = 32 // max. output packets per queue (normal/priority)
51 MAX_PROC_PACKETS = 300,
52 MAX_OUTWARD_SIZE = 1024 * 10
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
105 ALLOW_BROADCAST = 0x02,
106 ALLOW_NETWORK = 0x04,
115 bool initServer(Host &);
116 void initIncoming(ClientSocket *,unsigned int);
117 void initOutgoing(TYPE);
118 void initGIV(Host &, GnuID &);
119 void initPCP(Host &);
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];}
132 void addBytes(unsigned int);
133 bool isOlderThan(Servent *s)
137 unsigned int t = sys->getTime();
138 return ((t-lastConnect) > (t-s->lastConnect));
145 // static funcs that do the actual work in the servent thread
146 static THREAD_PROC serverProc(ThreadInfo *);
147 static THREAD_PROC outgoingProc(ThreadInfo *);
148 static THREAD_PROC incomingProc(ThreadInfo *);
149 static THREAD_PROC givProc(ThreadInfo *);
150 static THREAD_PROC pcpProc(ThreadInfo *);
151 static THREAD_PROC fetchProc(ThreadInfo *);
153 static bool pingHost(Host &,GnuID &);
155 bool getLocalURL(char *);
156 bool getLocalTypeURL(char *, ChanInfo::TYPE);
158 // various types of handshaking are needed
159 void handshakePLS(ChanHitList **, int, bool);
160 void handshakePLS(ChanInfo &, bool);
162 void handshakeHTML(char *);
164 void handshakeCMD(char *);
165 bool handshakeAuth(HTTP &,const char *,bool);
170 void processOutPCP();
171 void processOutChannel();
173 bool handshakeStream(ChanInfo &);
174 void handshakeGiv(GnuID &);
176 void handshakeICY(Channel::SRC_TYPE,bool);
177 void handshakeIncoming();
178 void handshakePOST();
179 void handshakeRTSP(RTSP &);
180 void handshakeHTTP(HTTP &,bool);
182 void handshakeRemoteFile(const char *);
183 void handshakeLocalFile(const char *);
185 static void handshakeOutgoingPCP(AtomStream &,Host &,GnuID &,String &,bool);
186 static void handshakeIncomingPCP(AtomStream &,Host &,GnuID &,String &);
188 void processIncomingPCP(bool);
190 bool waitForChannelHeader(ChanInfo &);
191 ChanInfo findChannel(char *str,ChanInfo &);
193 bool writeVariable(Stream &, const String &);
196 // the "mainloop" of servents
197 void processGnutella();
199 void processServent();
200 void processStream(bool,ChanInfo &);
201 void processPCP(bool,bool);
203 bool procAtoms(AtomStream &);
204 void procRootAtoms(AtomStream &,int);
205 void procHeloAtoms(AtomStream &,int,bool);
206 void procGetAtoms(AtomStream &,int);
208 void triggerChannel(char *,ChanInfo::PROTOCOL,bool);
209 void sendPeercastChannel();
210 void sendRawChannel(bool,bool);
211 // void sendRawMultiChannel(bool,bool);
212 void sendRawMetaChannel(int);
213 void sendPCPChannel();
214 void checkPCPComms(Channel *, AtomStream &);
216 static void readICYHeader(HTTP &, ChanInfo &, char *, size_t);
217 bool canStream(Channel *);
219 bool isConnected() {return status == S_CONNECTED;}
220 bool isListening() {return status == S_LISTENING;}
223 bool isFiltered(int);
225 // connection handling funcs
235 bool outputPacket(GnuPacket &,bool);
236 bool hasSeenPacket(GnuPacket &p) {return seenIDs.contains(p.id);}
237 bool acceptGIV(ClientSocket *);
238 bool sendPacket(ChanPacket &,GnuID &,GnuID &,GnuID &,Servent::TYPE);
244 static char *statusMsgs[],*typeMsgs[];
247 unsigned int lastConnect,lastPing,lastPacket;
263 String loginPassword;
266 bool priorityConnect;
272 ClientSocket *sock,*pushSock;
277 unsigned int syncPos,streamPos;
280 ChanInfo::PROTOCOL outputProtocol;
282 GnuPacketBuffer outPacketsNorm,outPacketsPri;
284 unsigned int bytesPerSecond;
289 PCPStream *pcpStream;
293 unsigned int lastSkipTime;
294 unsigned int lastSkipCount;
295 unsigned int waitPort;
302 extern char *nextCGIarg(char *cp, char *cmd, char *arg);