OSDN Git Service

Vss2Git
[peercast-im/PeerCastIM.git] / PeerCast.root / PeerCast_1 / core / common / common.h
1 // ------------------------------------------------
2 // File : common.h
3 // Date: 4-apr-2002
4 // Author: giles
5 //
6 // (c) 2002 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 _COMMON_H
20 #define _COMMON_H
21
22 #pragma warning (disable: 4996)
23
24 #include <stdio.h>
25 #include <string.h>
26
27 #ifndef NULL
28 #define NULL 0
29 #endif
30
31 // ----------------------------------
32 class GeneralException
33 {
34 public:
35     GeneralException(const char *m, int e = 0) 
36         {
37                 strcpy(msg,m);
38                 err=e;
39         }
40     char msg[128];
41         int err;
42 };
43
44 // -------------------------------------
45 class StreamException : public GeneralException
46 {
47 public:
48         StreamException(const char *m) : GeneralException(m) {}
49         StreamException(const char *m,int e) : GeneralException(m,e) {}
50 };
51
52 // ----------------------------------
53 class SockException : public StreamException
54 {
55 public:
56     SockException(const char *m="Socket") : StreamException(m) {}
57     SockException(const char *m, int e) : StreamException(m,e) {}
58 };
59 // ----------------------------------
60 class EOFException : public StreamException
61 {
62 public:
63     EOFException(const char *m="EOF") : StreamException(m) {}
64     EOFException(const char *m, int e) : StreamException(m,e) {}
65 };
66
67 // ----------------------------------
68 class CryptException : public StreamException
69 {
70 public:
71     CryptException(const char *m="Crypt") : StreamException(m) {}
72     CryptException(const char *m, int e) : StreamException(m,e) {}
73 };
74
75
76 // ----------------------------------
77 class TimeoutException : public StreamException
78 {
79 public:
80     TimeoutException(const char *m="Timeout") : StreamException(m) {}
81 };
82 // --------------------------------
83 class GnuID
84 {
85 public:
86         bool    isSame(GnuID &gid)
87         {
88                 for(int i=0; i<16; i++)
89                         if (gid.id[i] != id[i])
90                                 return false;
91                 return true;
92         }
93
94
95         bool    isSet()
96         {
97                 for(int i=0; i<16; i++)
98                         if (id[i] != 0)
99                                 return true;
100                 return false;
101         }
102
103         void    clear()
104         {
105                 for(int i=0; i<16; i++)
106                         id[i] = 0;
107                 storeTime = 0;
108         }
109
110
111         void    generate(unsigned char = 0);
112         void    encode(class Host *, const char *,const char *,unsigned char);
113
114         void    toStr(char *);
115         void    fromStr(const char *);
116
117         unsigned char   getFlags();
118
119         unsigned char id[16];
120         unsigned int storeTime;
121 };
122 // --------------------------------
123 class GnuIDList 
124 {
125 public:
126         GnuIDList(int);
127         ~GnuIDList();
128         void    clear();
129         void    add(GnuID &);
130         bool    contains(GnuID &);
131         int             numUsed();
132         unsigned int getOldest();
133
134         GnuID   *ids;
135         int             maxID;
136 };
137
138
139 // ----------------------------------
140 class Host
141 {
142     inline unsigned int ip3()
143     {
144         return (ip>>24);
145     }
146     inline unsigned int ip2()
147     {
148         return (ip>>16)&0xff;
149     }
150     inline unsigned int ip1()
151     {
152         return (ip>>8)&0xff;
153     }
154     inline unsigned int ip0()
155     {
156         return ip&0xff;
157     }
158
159 public:
160         Host(){init();}
161         Host(unsigned int i, unsigned short p)
162         {
163                 ip = i;
164                 port = p;
165                 value = 0;
166         }
167
168         void    init()
169         {
170                 ip = 0;
171                 port = 0;
172                 value = 0;
173         }
174
175
176         bool    isMemberOf(Host &);
177
178         bool    isSame(Host &h)
179         {
180                 return (h.ip == ip) && (h.port == port);
181         }
182
183         bool classType() {return globalIP();}
184
185         bool    globalIP()
186         {
187                 // local host
188                 if ((ip3() == 127) && (ip2() == 0) && (ip1() == 0) && (ip0() == 1))
189                         return false;
190
191                 // class A
192                 if (ip3() == 10)
193                         return false;
194
195                 // class B
196                 if ((ip3() == 172) && (ip2() >= 16) && (ip2() <= 31))
197                         return false;
198
199                 // class C
200                 if ((ip3() == 192) && (ip2() == 168))
201                         return false;
202
203                 return true;
204         }
205         bool    localIP()
206         {
207                 return !globalIP();
208         }
209
210         bool    loopbackIP()
211         {
212 //              return ((ipByte[3] == 127) && (ipByte[2] == 0) && (ipByte[1] == 0) && (ipByte[0] == 1));
213                 return ((ip3() == 127) && (ip2() == 0) && (ip1() == 0) && (ip0() == 1));
214         }
215
216         bool    isValid()
217         {
218                 return (ip != 0);
219         }
220
221
222         bool    isSameType(Host &h)
223         {
224                         return ( (globalIP() && h.globalIP()) ||
225                                  (!globalIP() && !h.globalIP()) ); 
226         }
227
228         void    IPtoStr(char *str)
229         {
230                 sprintf(str,"%d.%d.%d.%d",(ip>>24)&0xff,(ip>>16)&0xff,(ip>>8)&0xff,(ip)&0xff);
231         }
232
233         void    toStr(char *str)
234         {
235                 sprintf(str,"%d.%d.%d.%d:%d",(ip>>24)&0xff,(ip>>16)&0xff,(ip>>8)&0xff,(ip)&0xff,port);
236         }
237
238         void    fromStrIP(const char *,int);
239         void    fromStrName(const char *,int);
240
241         bool    isLocalhost();
242
243
244         union
245         {
246                 unsigned int ip;
247 //              unsigned char ipByte[4];
248         };
249
250     unsigned short port;
251         unsigned int value;
252 };
253 // ----------------------------------
254 #define SWAP2(v) ( ((v&0xff)<<8) | ((v&0xff00)>>8) )
255 #define SWAP3(v) (((v&0xff)<<16) | ((v&0xff00)) | ((v&0xff0000)>>16) )
256 #define SWAP4(v) (((v&0xff)<<24) | ((v&0xff00)<<8) | ((v&0xff0000)>>8) | ((v&0xff000000)>>24))
257 #define TOUPPER(c) ((((c) >= 'a') && ((c) <= 'z')) ? (c)+'A'-'a' : (c))
258 #define TONIBBLE(c) ((((c) >= 'A')&&((c) <= 'F')) ? (((c)-'A')+10) : ((c)-'0'))
259 #define BYTES_TO_KBPS(n) (float)(((((float)n)*8.0f)/1024.0f))
260
261 // ----------------------------------
262 inline bool isWhiteSpace(char c)
263 {
264         return (c == ' ') || (c == '\r') || (c == '\n') || (c == '\t');
265 }
266
267 // ----------------------------------
268 inline int strToID(char *str)
269 {
270         union {
271         int i;
272         char s[8];
273     };
274     strncpy(s,str,4);
275     return i;
276 }
277
278 // -----------------------------------
279 char *getCGIarg(const char *str, const char *arg);
280 bool cmpCGIarg(char *str, char *arg, char *value);
281 bool hasCGIarg(char *str, char *arg);
282
283 // ----------------------------------
284 extern void LOG(const char *fmt,...);
285
286 extern void LOG_ERROR(const char *fmt,...);
287 extern void LOG_DEBUG(const char *fmt,...);
288 extern void LOG_NETWORK(const char *fmt,...);
289 extern void LOG_CHANNEL(const char *fmt,...);
290
291
292 #endif
293