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 // ------------------------------------------------
23 // --------------------------------
29 #define GNUTELLA_SETUP 0
32 // --------------------------------
33 static const int GNU_FUNC_PING = 0;
34 static const int GNU_FUNC_PONG = 1;
35 static const int GNU_FUNC_QUERY = 128;
36 static const int GNU_FUNC_HIT = 129;
37 static const int GNU_FUNC_PUSH = 64;
39 extern const char *GNU_FUNC_STR(int);
41 // --------------------------------
42 static const char *GNU_PEERCONN = "PEERCAST CONNECT/0.1";
43 static const char *GNU_CONNECT = "GNUTELLA CONNECT/0.6";
44 static const char *GNU_OK = "GNUTELLA/0.6 200 OK";
45 static const char *PCX_PCP_CONNECT = "pcp";
47 static const char *PCX_HS_OS = "x-peercast-os:";
48 static const char *PCX_HS_DL = "x-peercast-download:";
49 static const char *PCX_HS_ID = "x-peercast-id:";
50 static const char *PCX_HS_CHANNELID = "x-peercast-channelid:";
51 static const char *PCX_HS_NETWORKID = "x-peercast-networkid:";
52 static const char *PCX_HS_MSG = "x-peercast-msg:";
53 static const char *PCX_HS_SUBNET = "x-peercast-subnet:";
54 static const char *PCX_HS_FULLHIT = "x-peercast-fullhit:";
55 static const char *PCX_HS_MINBCTTL = "x-peercast-minbcttl:";
56 static const char *PCX_HS_MAXBCTTL = "x-peercast-maxbcttl:";
57 static const char *PCX_HS_RELAYBC = "x-peercast-relaybc:";
58 static const char *PCX_HS_PRIORITY = "x-peercast-priority:";
59 static const char *PCX_HS_FLOWCTL = "x-peercast-flowctl:";
60 static const char *PCX_HS_PCP = "x-peercast-pcp:";
61 static const char *PCX_HS_PINGME = "x-peercast-pingme:";
62 static const char *PCX_HS_PORT = "x-peercast-port:";
63 static const char *PCX_HS_REMOTEIP = "x-peercast-remoteip:";
64 static const char *PCX_HS_POS = "x-peercast-pos:";
65 static const char *PCX_HS_SESSIONID = "x-peercast-sessionid:";
67 // official version number sent to relay to check for updates
68 static const char *PCX_OS_WIN32 = "Win32";
69 static const char *PCX_OS_LINUX = "Linux";
70 static const char *PCX_OS_MACOSX = "Apple-OSX";
71 static const char *PCX_OS_WINAMP2 = "Win32-WinAmp2";
72 static const char *PCX_OS_ACTIVEX = "Win32-ActiveX";
74 static const char *PCX_DL_URL = "http://www.peercast.org/download.php";
76 // version number sent to other clients
77 static const char *PCX_OLDAGENT = "PeerCast/0.119E";
83 // version number used inside packets GUIDs
84 static const int PEERCAST_PACKETID = 0x0000119E;
86 static const char *MIN_ROOTVER = "0.119E";
88 static const char *MIN_CONNECTVER = "0.119D";
89 static const int MIN_PACKETVER = 0x0000119D;
91 static const char *ICY_OK = "ICY 200 OK";
93 // --------------------------------
95 static const int DEFAULT_PORT = 7144;
97 // --------------------------------
104 // --------------------------------
110 // --------------------------------
117 return (idChecksum == h.idChecksum) && (dataChecksum == h.dataChecksum);
120 bool isSameID(Hash &h)
122 return (idChecksum == h.idChecksum);
125 unsigned int idChecksum;
126 unsigned int dataChecksum;
129 // --------------------------------
136 void initPong(Host &, bool, GnuPacket &);
137 void initFind(const char *, class XML *,int);
138 bool initHit(Host &, Channel *, GnuPacket *,bool,bool,bool,bool,int);
139 void initPush(ChanHit &, Host &);
142 void makeChecksumID();
153 // --------------------------------
154 class GnuPacketBuffer
157 GnuPacketBuffer(int s)
159 ,packets(new GnuPacket[size])
170 readPtr = writePtr = 0;
176 return &packets[readPtr%size];
189 int n = numPending();
190 for(int i=0; i<n; i++)
192 int idx = (readPtr+i)%size;
193 if (packets[idx].hops < min)
194 min = packets[idx].hops;
201 int n = numPending();
202 for(int i=0; i<n; i++)
204 int idx = (readPtr+i)%size;
205 if (packets[idx].hops > max)
206 max = packets[idx].hops;
213 return (numPending()*100) / size;
220 int n = numPending();
221 for(int i=0; i<n; i++)
222 tot+=packets[(readPtr+i)%size].len;
228 return writePtr-readPtr;
231 bool write(GnuPacket &p)
233 if ((writePtr-readPtr) >= size)
237 packets[writePtr%size] = p;
245 int readPtr,writePtr;
250 // --------------------------------
251 class GnuStream : public IndirectStream
275 IndirectStream::init(s);
276 packetsIn = packetsOut = 0;
279 bool readPacket(GnuPacket &);
280 void sendPacket(GnuPacket &);
281 R_TYPE processPacket(GnuPacket &, Servent *, GnuID &);
283 static const char *getRouteStr(R_TYPE);
284 bool readHit(Stream &data, ChanHit &ch,int,GnuID &);
290 int packetsIn,packetsOut;