1 // ------------------------------------------------
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.
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 // ------------------------------------------------
22 #pragma warning (disable: 4996)
31 // ----------------------------------
32 class GeneralException
35 GeneralException(const char *m, int e = 0)
44 // -------------------------------------
45 class StreamException : public GeneralException
48 StreamException(const char *m) : GeneralException(m) {}
49 StreamException(const char *m,int e) : GeneralException(m,e) {}
52 // ----------------------------------
53 class SockException : public StreamException
56 SockException(const char *m="Socket") : StreamException(m) {}
57 SockException(const char *m, int e) : StreamException(m,e) {}
59 // ----------------------------------
60 class EOFException : public StreamException
63 EOFException(const char *m="EOF") : StreamException(m) {}
64 EOFException(const char *m, int e) : StreamException(m,e) {}
67 // ----------------------------------
68 class CryptException : public StreamException
71 CryptException(const char *m="Crypt") : StreamException(m) {}
72 CryptException(const char *m, int e) : StreamException(m,e) {}
76 // ----------------------------------
77 class TimeoutException : public StreamException
80 TimeoutException(const char *m="Timeout") : StreamException(m) {}
82 // --------------------------------
86 bool isSame(GnuID &gid)
88 for(int i=0; i<16; i++)
89 if (gid.id[i] != id[i])
97 for(int i=0; i<16; i++)
105 for(int i=0; i<16; i++)
111 void generate(unsigned char = 0);
112 void encode(class Host *, const char *,const char *,unsigned char);
115 void fromStr(const char *);
117 unsigned char getFlags();
119 unsigned char id[16];
120 unsigned int storeTime;
122 // --------------------------------
130 bool contains(GnuID &);
132 unsigned int getOldest();
139 // ----------------------------------
142 inline unsigned int ip3()
146 inline unsigned int ip2()
148 return (ip>>16)&0xff;
150 inline unsigned int ip1()
154 inline unsigned int ip0()
161 Host(unsigned int i, unsigned short p)
176 bool isMemberOf(Host &);
180 return (h.ip == ip) && (h.port == port);
183 bool classType() {return globalIP();}
188 if ((ip3() == 127) && (ip2() == 0) && (ip1() == 0) && (ip0() == 1))
196 if ((ip3() == 172) && (ip2() >= 16) && (ip2() <= 31))
200 if ((ip3() == 192) && (ip2() == 168))
212 // return ((ipByte[3] == 127) && (ipByte[2] == 0) && (ipByte[1] == 0) && (ipByte[0] == 1));
213 return ((ip3() == 127) && (ip2() == 0) && (ip1() == 0) && (ip0() == 1));
222 bool isSameType(Host &h)
224 return ( (globalIP() && h.globalIP()) ||
225 (!globalIP() && !h.globalIP()) );
228 void IPtoStr(char *str)
230 sprintf(str,"%d.%d.%d.%d",(ip>>24)&0xff,(ip>>16)&0xff,(ip>>8)&0xff,(ip)&0xff);
233 void toStr(char *str)
235 sprintf(str,"%d.%d.%d.%d:%d",(ip>>24)&0xff,(ip>>16)&0xff,(ip>>8)&0xff,(ip)&0xff,port);
238 void fromStrIP(const char *,int);
239 void fromStrName(const char *,int);
247 // unsigned char ipByte[4];
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))
261 // ----------------------------------
262 inline bool isWhiteSpace(char c)
264 return (c == ' ') || (c == '\r') || (c == '\n') || (c == '\t');
267 // ----------------------------------
268 inline int strToID(char *str)
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);
283 // ----------------------------------
284 extern void LOG(const char *fmt,...);
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,...);