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 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
99 ALLOW_BROADCAST = 0x02,
100 ALLOW_NETWORK = 0x04,
109 bool initServer(Host &);
110 void initIncoming(ClientSocket *,unsigned int);
111 void initOutgoing(TYPE);
112 void initGIV(Host &, GnuID &);
113 void initPCP(Host &);
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];}
126 void addBytes(unsigned int);
127 bool isOlderThan(Servent *s)
131 unsigned int t = sys->getTime();
132 return ((t-lastConnect) > (t-s->lastConnect));
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 *);
147 static bool pingHost(Host &,GnuID &);
149 bool getLocalURL(char *);
150 bool getLocalTypeURL(char *, ChanInfo::TYPE);
152 // various types of handshaking are needed
153 void handshakePLS(ChanHitList **, int, bool);
154 void handshakePLS(ChanInfo &, bool);
156 void handshakeHTML(char *);
158 void handshakeCMD(char *);
159 bool handshakeAuth(HTTP &,const char *,bool);
164 void processOutPCP();
165 void processOutChannel();
167 bool handshakeStream(ChanInfo &);
168 void handshakeGiv(GnuID &);
170 void handshakeICY(Channel::SRC_TYPE,bool);
171 void handshakeIncoming();
172 void handshakePOST();
173 void handshakeRTSP(RTSP &);
174 void handshakeHTTP(HTTP &,bool);
176 void handshakeRemoteFile(const char *);
177 void handshakeLocalFile(const char *);
179 static void handshakeOutgoingPCP(AtomStream &,Host &,GnuID &,String &,bool);
180 static void handshakeIncomingPCP(AtomStream &,Host &,GnuID &,String &);
182 void processIncomingPCP(bool);
184 bool waitForChannelHeader(ChanInfo &);
185 ChanInfo findChannel(char *str,ChanInfo &);
187 bool writeVariable(Stream &, const String &);
190 // the "mainloop" of servents
191 void processGnutella();
193 void processServent();
194 void processStream(bool,ChanInfo &);
195 void processPCP(bool,bool);
197 bool procAtoms(AtomStream &);
198 void procRootAtoms(AtomStream &,int);
199 void procHeloAtoms(AtomStream &,int,bool);
200 void procGetAtoms(AtomStream &,int);
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 &);
210 static void readICYHeader(HTTP &, ChanInfo &, char *);
211 bool canStream(Channel *);
213 bool isConnected() {return status == S_CONNECTED;}
214 bool isListening() {return status == S_LISTENING;}
217 bool isFiltered(int);
219 // connection handling funcs
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);
238 static char *statusMsgs[],*typeMsgs[];
241 unsigned int lastConnect,lastPing,lastPacket;
257 char loginPassword[64];
260 bool priorityConnect;
266 ClientSocket *sock,*pushSock;
271 unsigned int syncPos,streamPos;
274 ChanInfo::PROTOCOL outputProtocol;
276 GnuPacketBuffer outPacketsNorm,outPacketsPri;
278 unsigned int bytesPerSecond;
283 PCPStream *pcpStream;
287 unsigned int lastSkipTime;
288 unsigned int lastSkipCount;
289 unsigned int waitPort;
294 extern char *nextCGIarg(char *cp, char *cmd, char *arg);