OSDN Git Service

VP27マージ
[peercast-im/PeerCastIM.git] / PeerCast.root / PeerCast / core / common / pcp.h
1 // ------------------------------------------------
2 // File : pcp.h
3 // Date: 1-mar-2004
4 // Author: giles
5 //
6 // (c) 2002-4 peercast.org
7 // ------------------------------------------------
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 // ------------------------------------------------
18
19 #ifndef _PCP_H
20 #define _PCP_H
21
22 // ------------------------------------------------
23
24
25 #include "id.h"
26 #include "cstream.h"
27 #include "channel.h"
28
29 // ------------------------------------------------
30
31 class Servent;
32
33
34 // ------------------------------------------------
35
36 static const ID4 PCP_CONNECT            = "pcp\n";
37
38 static const ID4 PCP_OK                         = "ok";
39
40 static const ID4 PCP_HELO                       = "helo";
41 static const ID4 PCP_HELO_AGENT         = "agnt";
42 static const ID4 PCP_HELO_OSTYPE        = "ostp";
43 static const ID4 PCP_HELO_SESSIONID     = "sid";
44 static const ID4 PCP_HELO_PORT          = "port";
45 static const ID4 PCP_HELO_PING          = "ping";
46 static const ID4 PCP_HELO_PONG          = "pong";
47 static const ID4 PCP_HELO_REMOTEIP      = "rip";
48 static const ID4 PCP_HELO_VERSION       = "ver";
49 static const ID4 PCP_HELO_BCID          = "bcid";
50 static const ID4 PCP_HELO_DISABLE       = "dis";
51
52 static const ID4 PCP_OLEH                       = "oleh";
53
54 static const ID4 PCP_MODE                       = "mode";
55 static const ID4 PCP_MODE_GNUT06        = "gn06";
56
57 static const ID4 PCP_ROOT                       = "root";
58 static const ID4 PCP_ROOT_UPDINT        = "uint";
59 static const ID4 PCP_ROOT_CHECKVER      = "chkv";
60 static const ID4 PCP_ROOT_URL           = "url";
61 static const ID4 PCP_ROOT_UPDATE        = "upd";
62 static const ID4 PCP_ROOT_NEXT          = "next";
63
64
65 static const ID4 PCP_OS_LINUX           = "lnux";
66 static const ID4 PCP_OS_WINDOWS         = "w32";
67 static const ID4 PCP_OS_OSX                     = "osx";
68 static const ID4 PCP_OS_WINAMP          = "wamp";
69 static const ID4 PCP_OS_ZAURUS          = "zaur";
70
71 static const ID4 PCP_GET                        = "get";
72 static const ID4 PCP_GET_ID                     = "id";
73 static const ID4 PCP_GET_NAME           = "name";
74
75 static const ID4 PCP_HOST                       = "host";
76 static const ID4 PCP_HOST_ID            = "id";
77 static const ID4 PCP_HOST_IP            = "ip";
78 static const ID4 PCP_HOST_PORT          = "port";
79 static const ID4 PCP_HOST_NUML          = "numl";
80 static const ID4 PCP_HOST_NUMR          = "numr";
81 static const ID4 PCP_HOST_UPTIME        = "uptm";
82 static const ID4 PCP_HOST_TRACKER       = "trkr";
83 static const ID4 PCP_HOST_CHANID        = "cid";
84 static const ID4 PCP_HOST_VERSION       = "ver";
85 static const ID4 PCP_HOST_VERSION_VP    = "vevp";
86 static const ID4 PCP_HOST_VERSION_EX_PREFIX = "vexp";
87 static const ID4 PCP_HOST_VERSION_EX_NUMBER = "vexn";
88 static const ID4 PCP_HOST_CLAP_PP               = "clap"; //JP-MOD
89 static const ID4 PCP_HOST_FLAGS1        = "flg1";
90 static const ID4 PCP_HOST_OLDPOS        = "oldp";
91 static const ID4 PCP_HOST_NEWPOS        = "newp";
92 static const ID4 PCP_HOST_UPHOST_IP     = "upip";
93 static const ID4 PCP_HOST_UPHOST_PORT   = "uppt";
94 static const ID4 PCP_HOST_UPHOST_HOPS   = "uphp";
95
96 static const ID4 PCP_QUIT                       = "quit";
97
98 static const ID4 PCP_CHAN                       = "chan";
99 static const ID4 PCP_CHAN_ID            = "id";
100 static const ID4 PCP_CHAN_BCID          = "bcid";
101 static const ID4 PCP_CHAN_KEY           = "key";
102
103 static const ID4 PCP_CHAN_PKT           = "pkt";
104 static const ID4 PCP_CHAN_PKT_TYPE      = "type";
105 static const ID4 PCP_CHAN_PKT_POS       = "pos";
106 static const ID4 PCP_CHAN_PKT_HEAD      = "head";
107 static const ID4 PCP_CHAN_PKT_DATA      = "data";
108 static const ID4 PCP_CHAN_PKT_META      = "meta";
109
110 static const ID4 PCP_CHAN_INFO                  = "info";
111 static const ID4 PCP_CHAN_INFO_TYPE             = "type";
112 static const ID4 PCP_CHAN_INFO_BITRATE  = "bitr";
113 static const ID4 PCP_CHAN_INFO_GENRE    = "gnre";
114 static const ID4 PCP_CHAN_INFO_NAME             = "name";
115 static const ID4 PCP_CHAN_INFO_URL              = "url";
116 static const ID4 PCP_CHAN_INFO_DESC             = "desc";
117 static const ID4 PCP_CHAN_INFO_COMMENT  = "cmnt";
118 static const ID4 PCP_CHAN_INFO_PPFLAGS  = "pflg"; //JP-MOD
119
120 static const ID4 PCP_CHAN_TRACK                 = "trck";
121 static const ID4 PCP_CHAN_TRACK_TITLE   = "titl";
122 static const ID4 PCP_CHAN_TRACK_CREATOR = "crea";
123 static const ID4 PCP_CHAN_TRACK_URL             = "url";
124 static const ID4 PCP_CHAN_TRACK_ALBUM   = "albm";
125
126 static const ID4 PCP_MESG                               = "mesg";
127 static const ID4 PCP_MESG_ASCII                 = "asci";               // ascii/sjis to be depreciated.. utf8/unicode is the only supported format from now.
128 static const ID4 PCP_MESG_SJIS                  = "sjis";
129
130 static const ID4 PCP_BCST                               = "bcst";       
131 static const ID4 PCP_BCST_TTL                   = "ttl";        
132 static const ID4 PCP_BCST_HOPS                  = "hops";       
133 static const ID4 PCP_BCST_FROM                  = "from";       
134 static const ID4 PCP_BCST_DEST                  = "dest";       
135 static const ID4 PCP_BCST_GROUP                 = "grp";        
136 static const ID4 PCP_BCST_CHANID                = "cid";        
137 static const ID4 PCP_BCST_VERSION               = "vers";
138 static const ID4 PCP_BCST_VERSION_VP    = "vrvp";
139 static const ID4 PCP_BCST_VERSION_EX_PREFIX = "vexp";
140 static const ID4 PCP_BCST_VERSION_EX_NUMBER = "vexn";
141
142 static const ID4 PCP_PUSH                               = "push";       
143 static const ID4 PCP_PUSH_IP                    = "ip"; 
144 static const ID4 PCP_PUSH_PORT                  = "port";       
145 static const ID4 PCP_PUSH_CHANID                = "cid";        
146
147 static const ID4 PCP_SPKT                               = "spkt";
148
149 static const ID4 PCP_ATOM                               = "atom";       
150
151 static const ID4 PCP_SESSIONID                  = "sid";
152
153 static const int PCP_BCST_GROUP_ALL                     = (char)0xff;   
154 static const int PCP_BCST_GROUP_ROOT            = 1;    
155 static const int PCP_BCST_GROUP_TRACKERS        = 2;    
156 static const int PCP_BCST_GROUP_RELAYS          = 4;    
157
158
159 static const int PCP_ERROR_QUIT                 = 1000;
160 static const int PCP_ERROR_BCST                 = 2000;
161 static const int PCP_ERROR_READ                 = 3000;
162 static const int PCP_ERROR_WRITE                = 4000;
163 static const int PCP_ERROR_GENERAL              = 5000;
164
165 static const int PCP_ERROR_SKIP                         = 1;
166 static const int PCP_ERROR_ALREADYCONNECTED     = 2;
167 static const int PCP_ERROR_UNAVAILABLE          = 3;
168 static const int PCP_ERROR_LOOPBACK                     = 4;
169 static const int PCP_ERROR_NOTIDENTIFIED        = 5;
170 static const int PCP_ERROR_BADRESPONSE          = 6;
171 static const int PCP_ERROR_BADAGENT                     = 7;
172 static const int PCP_ERROR_OFFAIR                       = 8;
173 static const int PCP_ERROR_SHUTDOWN                     = 9;
174 static const int PCP_ERROR_NOROOT                       = 10;
175 static const int PCP_ERROR_BANNED                       = 11;
176
177 static const int PCP_HOST_FLAGS1_TRACKER        = 0x01;
178 static const int PCP_HOST_FLAGS1_RELAY          = 0x02;
179 static const int PCP_HOST_FLAGS1_DIRECT         = 0x04;
180 static const int PCP_HOST_FLAGS1_PUSH           = 0x08;
181 static const int PCP_HOST_FLAGS1_RECV           = 0x10;
182 static const int PCP_HOST_FLAGS1_CIN            = 0x20;
183 static const int PCP_HOST_FLAGS1_PRIVATE        = 0x40;
184
185
186 // ----------------------------------------------
187 class BroadcastState 
188 {
189 public:
190         BroadcastState()
191         :numHops(0)
192         ,forMe(false) 
193         ,streamPos(0)
194         ,group(0)
195         ,servent_id(0)
196         ,ttl(0)
197         {
198                 chanID.clear();
199                 bcID.clear();
200         }
201
202
203         void initPacketSettings()
204         {
205                 forMe = false;
206                 group = 0;
207                 numHops = 0;
208                 bcID.clear();
209                 chanID.clear();
210         }
211
212
213         GnuID chanID,bcID;
214         int numHops;
215         bool forMe;
216         unsigned int streamPos;
217         int group;
218         int servent_id;
219         int ttl;
220 };
221
222 // ----------------------------------------------
223 class PCPStream : public ChannelStream
224 {
225 public:
226         PCPStream(GnuID &rid) 
227         :routeList(1000)
228         {
229                 init(rid);
230         }
231
232         void    init(GnuID &);
233
234         virtual void kill()
235         {
236                 inData.lock.on();
237                 outData.lock.on();
238         }
239
240         virtual bool sendPacket(ChanPacket &,GnuID &);
241         virtual void flush(Stream &);
242         virtual unsigned int flushUb(Stream &, unsigned int);
243         virtual void readHeader(Stream &,Channel *);
244         virtual int readPacket(Stream &,Channel *);
245         virtual void readEnd(Stream &,Channel *);
246
247         int             readPacket(Stream &,BroadcastState &);
248         void    flushOutput(Stream &in,BroadcastState &);
249         static void     readVersion(Stream &);
250
251         int             procAtom(AtomStream &,ID4,int,int,BroadcastState &);
252         int             readAtom(AtomStream &,BroadcastState &);
253         void    readChanAtoms(AtomStream &,int,BroadcastState &);
254 //      void    readHostAtoms(AtomStream &, int, BroadcastState &);
255         void    readHostAtoms(AtomStream &, int, BroadcastState &, ChanHit &, bool flg=true);
256         void    readPushAtoms(AtomStream &, int,BroadcastState &);
257
258         void    readPktAtoms(Channel *,AtomStream &,int,BroadcastState &);
259         void    readRootAtoms(AtomStream &, int,BroadcastState &);
260
261         int             readBroadcastAtoms(AtomStream &,int,BroadcastState &);
262
263         ChanPacketBuffer inData,outData;
264         unsigned int lastPacketTime;
265         unsigned int nextRootPacket;    
266
267         //int   error;
268         GnuIDList       routeList;
269         GnuID   remoteID;
270
271 };
272
273 #endif