00001
00009 #ifndef __INET_H
00010 #define __INET_H
00011
00012 #include "GMem.h"
00013 #include "GContainers.h"
00014 #include "LgiNetInc.h"
00015 #include "GStream.h"
00016 #include "LgiInterfaces.h"
00017
00018 #if defined WIN32
00019
00020 #elif defined BEOS
00021
00022 #include <sys/socket.h>
00023 #include <netdb.h>
00024
00025 #elif defined ATHEOS
00026
00027 #include <atheos/socket.h>
00028
00029 #else
00030
00031 typedef int SOCKET;
00032
00033 #endif
00034
00035 #define DEFAULT_TIMEOUT 30
00036
00037
00038 #define FTP_PORT 21
00039 #define SMTP_PORT 25
00040 #define HTTP_PORT 80
00041 #define SMTP_SSL_PORT 465
00042 #define POP3_PORT 110
00043 #define POP3_SSL_PORT 995
00044 #define IMAP_PORT 143
00045 #define IMAP_SSL_PORT 993
00046
00047
00048
00050 #define GSocket_Log "Log"
00052 #define GSocket_Progress "Progress"
00054 #define GSocket_TransferSize "TransferSize"
00056 #define GSocket_Protocol "Protocol"
00057
00058
00059 LgiNetFunc bool HaveNetConnection();
00060 LgiNetFunc bool WhatsMyIp(char *IpAddr);
00061
00063 LgiNetFunc void MDStringToDigest
00064 (
00066 unsigned char digest[16],
00068 char *Str,
00070 int Len = -1
00071 );
00072
00073
00074
00075 class GNetwork;
00076
00078 class LgiNetClass GNetwork
00079 {
00080 bool SocketsOpen;
00081
00082 public:
00083 GNetwork();
00084 virtual ~GNetwork();
00085
00087 bool IsValid() { return SocketsOpen; }
00088
00090 bool EnumInterfaces(List<char> &Lst);
00091 };
00092
00094 class LgiNetClass GSocket : public GSocketI, public GStream
00095 {
00096 friend class GNetwork;
00097
00098 protected:
00099 class GSocketImplPrivate *d;
00100
00101
00102 void Log(const char *Msg, int Ret, const char *Buf, int Len);
00103
00104 public:
00105 int BytesWritten;
00106 int BytesRead;
00107
00109 GSocket(GStreamI *logger = 0);
00110
00112 ~GSocket();
00113
00115 OsSocket Handle(OsSocket Set = INVALID_SOCKET);
00116
00118 bool IsOK();
00119
00121 bool GetLocalIp(char *IpAddr);
00122
00124 int GetLocalPort();
00125
00127 bool GetRemoteIp(char *IpAddr);
00128
00130 int GetRemotePort();
00131
00133 int GetTimeout();
00134
00136 void SetTimeout(int ms);
00137
00139 bool IsReadable(int TimeoutMs = 0);
00140
00142 bool IsWritable(int TimeoutMs = 0);
00143
00145 bool CanAccept(int TimeoutMs = 0);
00146
00148 bool IsBlocking();
00149
00151 void IsBlocking(bool block);
00152
00154 bool IsDelayed();
00155
00157 void IsDelayed(bool Delay);
00158
00160 int Open
00161 (
00163 const char *HostAddr,
00165 int Port
00166 );
00167
00169 bool IsOpen();
00170
00172 int Close();
00173
00175 bool Listen(int Port = 0);
00176
00178 bool Accept
00179 (
00181 GSocketI *c
00182 );
00183
00186 int Write
00187 (
00189 const void *Data,
00191 int Len,
00193 int Flags = 0
00194 );
00195
00202 int Read
00203 (
00205 void *Data,
00207 int Len,
00209 int Flags = 0
00210 );
00211
00213 int Error(void *Param = 0);
00214
00216 int64 GetSize() { return -1; }
00218 int64 SetSize(int64 Size) { return -1; }
00220 int64 GetPos() { return -1; }
00222 int64 SetPos(int64 Pos) { return -1; }
00223
00225 void OnDisconnect();
00226
00228 void OnRead(char *Data, int Len) {}
00229
00231 void OnWrite(const char *Data, int Len) {}
00232
00234 void OnError(int ErrorCode, const char *ErrorDescription) {}
00235
00237 void OnInformation(const char *Str) {}
00238
00240 int SetParameter
00241 (
00243 int Param,
00244 int Value
00245 ) { return false; }
00246
00248 bool GetUdp();
00250 void SetUdp(bool b);
00252 int ReadUdp(void *Buffer, int Size, int Flags, uint32 *Ip = 0, uint16 *Port = 0);
00254 int WriteUdp(void *Buffer, int Size, int Flags, uint32 Ip, uint16 Port);
00255 };
00256
00257 class LgiNetClass GSocks5Socket : public GSocket
00258 {
00259 GAutoString Proxy;
00260 int Port;
00261 GAutoString UserName;
00262 GAutoString Password;
00263
00264 protected:
00265 bool Socks5Connected;
00266
00267 public:
00268 GSocks5Socket();
00269
00270 GSocks5Socket &operator=(const GSocks5Socket &s)
00271 {
00272 Proxy.Reset(NewStr(s.Proxy));
00273 UserName.Reset(NewStr(s.UserName));
00274 Password.Reset(NewStr(s.Password));
00275 Port = s.Port;
00276 return *this;
00277 }
00278
00279
00280 void SetProxy(char *proxy, int port, char *username, char *password);
00281 void SetProxy(const GSocks5Socket *s);
00282 int Open(char *HostAddr, int port);
00283
00284
00285 bool Listen(int Port) { return false; }
00286 };
00287
00289 class LgiNetClass GUri
00290 {
00291 public:
00292 char *Protocol;
00293 char *User;
00294 char *Pass;
00295 char *Host;
00296 int Port;
00297 char *Path;
00298 char *Anchor;
00299
00301 GUri
00302 (
00304 const char *uri = 0
00305 );
00306 ~GUri();
00307
00309 bool Set(const char *uri);
00310
00312 char *Get();
00313
00315 void Empty();
00316
00318 GAutoString Encode
00319 (
00321 char *s,
00323 char *ExtraCharsToEncode = 0
00324 );
00325
00327 GAutoString Decode(char *s);
00328
00329 GUri &operator =(GUri &u);
00330 GUri &operator =(char *s) { Set(s); return *this; }
00331 };
00332
00334 class LgiNetClass GProxyUri : public GUri
00335 {
00336 public:
00337 GProxyUri();
00338 };
00339
00340 #endif
00341