OSDN Git Service

WebGUIのレスポンスが悪かったのを修正
[peercast-im/PeerCastIM.git] / PeerCast.root / PeerCast / core / common / gnutella.h
1 // ------------------------------------------------
2 // File : gnutella.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 #ifndef _GNUTELLA_H
21 #define _GNUTELLA_H
22
23 // --------------------------------
24 #include "stream.h"
25 #include "sys.h"
26
27
28
29 #define GNUTELLA_SETUP 0
30
31
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;
38
39 extern const char *GNU_FUNC_STR(int);
40
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";
46
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:"; 
66
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";
73
74 static const char *PCX_DL_URL           = "http://www.peercast.org/download.php"; 
75
76 // version number sent to other clients
77 static const char *PCX_OLDAGENT         = "PeerCast/0.119E";
78
79
80
81
82
83 // version number used inside packets GUIDs
84 static const int PEERCAST_PACKETID      = 0x0000119E;
85
86 static const char *MIN_ROOTVER          = "0.119E";
87
88 static const char *MIN_CONNECTVER       = "0.119D";
89 static const int MIN_PACKETVER      = 0x0000119D;
90
91 static const char *ICY_OK       = "ICY 200 OK";
92
93 // --------------------------------
94
95 static const int DEFAULT_PORT   = 7144;
96
97 // --------------------------------
98
99 class Servent;
100 class Channel;
101 class ChanHit;
102
103
104 // --------------------------------
105 class GnuPacket
106 {
107 public:
108
109
110         // --------------------------------
111         class Hash
112         {
113         public:
114
115                 bool    isSame(Hash &h)
116                 {
117                         return (idChecksum == h.idChecksum) && (dataChecksum == h.dataChecksum);
118                 }
119
120                 bool    isSameID(Hash &h)
121                 {
122                         return (idChecksum == h.idChecksum);
123                 }
124
125                 unsigned int idChecksum;
126                 unsigned int dataChecksum;
127
128         };
129         // --------------------------------
130
131         enum {
132                 MAX_DATA = 2000
133         };
134
135         void    initPing(int);
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 &);
140
141
142         void makeChecksumID();
143
144         unsigned char func;
145         unsigned char ttl;
146         unsigned char hops;
147         unsigned int    len;
148         Hash    hash;
149         GnuID   id;
150
151         char data[MAX_DATA];
152 };
153 // --------------------------------
154 class GnuPacketBuffer
155 {
156 public:
157         GnuPacketBuffer(int s) 
158         :size(s)
159         ,packets(new GnuPacket[size])
160         {
161                 reset();
162         }
163         ~GnuPacketBuffer()
164         {
165                 delete [] packets;
166         }
167
168         void    reset()
169         {
170                 readPtr = writePtr = 0;
171         }
172
173         GnuPacket *curr()
174         {
175                 if (numPending())
176                         return &packets[readPtr%size];
177                 else
178                         return NULL;
179
180         }
181         void    next()
182         {
183                 readPtr++;
184         }
185
186         int findMinHop()
187         {
188                 int min=100;
189                 int n = numPending();
190                 for(int i=0; i<n; i++)
191                 {
192                         int idx = (readPtr+i)%size;
193                         if (packets[idx].hops < min)
194                                 min = packets[idx].hops;
195                 }
196                 return min;
197         }
198         int findMaxHop()
199         {
200                 int max=0;
201                 int n = numPending();
202                 for(int i=0; i<n; i++)
203                 {
204                         int idx = (readPtr+i)%size;
205                         if (packets[idx].hops > max)
206                                 max = packets[idx].hops;
207                 }
208                 return max;
209         }
210
211         int percentFull()
212         {
213                 return (numPending()*100) / size;
214         }
215
216         
217         int sizeOfPending()
218         {
219                 int tot=0;
220                 int n = numPending();
221                 for(int i=0; i<n; i++)
222                         tot+=packets[(readPtr+i)%size].len;
223                 return tot;
224         }
225
226         int     numPending()
227         {
228                 return writePtr-readPtr;
229         }
230
231         bool    write(GnuPacket &p) 
232         {
233                 if ((writePtr-readPtr) >= size)
234                         return false;
235                 else
236                 {
237                         packets[writePtr%size] = p;
238                         writePtr++;
239                         return true;
240                 }
241         }
242
243         int     size;
244         GnuPacket *packets;
245         int     readPtr,writePtr;
246 };
247
248
249
250 // --------------------------------
251 class GnuStream : public IndirectStream
252 {
253 public:
254
255         enum R_TYPE
256         {
257                 R_PROCESS,
258                 R_DEAD,
259                 R_DISCARD,
260                 R_ACCEPTED,
261                 R_BROADCAST,
262                 R_ROUTE,
263                 R_DUPLICATE,
264                 R_BADVERSION,
265                 R_DROP
266         };
267
268         GnuStream()
269         {
270                 init(NULL);
271         }
272
273         void    init(Stream *s)  
274         {
275                 IndirectStream::init(s);
276                 packetsIn = packetsOut = 0;
277         }
278
279         bool    readPacket(GnuPacket &);
280         void    sendPacket(GnuPacket &);
281         R_TYPE  processPacket(GnuPacket &, Servent *, GnuID &);
282
283         static const char *getRouteStr(R_TYPE);
284         bool    readHit(Stream &data, ChanHit &ch,int,GnuID &);
285
286
287         
288         void    ping(int);
289
290         int             packetsIn,packetsOut;
291         WLock   lock;
292 };
293
294
295 #endif