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
00017 #if defined WIN32
00018
00019 #elif defined BEOS
00020
00021 #include <socket.h>
00022 #include <netdb.h>
00023
00024 #elif defined ATHEOS
00025
00026 #include <atheos/socket.h>
00027
00028 #else
00029
00030 typedef int SOCKET;
00031
00032 #endif
00033
00034 #define DEFAULT_TIMEOUT 30
00035
00036
00037 #define SMTP_PORT 25
00038 #define SMTP_SSL_PORT 465
00039 #define POP3_PORT 110
00040 #define POP3_SSL_PORT 995
00041 #define IMAP_PORT 143
00042 #define IMAP_SSL_PORT 993
00043
00044
00045
00047 #define GSocket_Log 1
00048
00049 #define GSocket_Progress 2
00050
00051 #define GSocket_TransferSize 3
00052
00053 #define GSocket_Protocol 4
00054
00055 #if defined BEOS
00056
00057 #include "NetworkKit.h"
00058 #define ValidSocket(s) ((s)>=0)
00059 #define INVALID_SOCKET -1
00060 typedef int OsSocket;
00061
00062 #elif defined ATHEOS
00063
00064 #define ValidSocket(s) ((s)>=0)
00065 #define INVALID_SOCKET -1
00066 typedef int OsSocket;
00067
00068 #elif defined WIN32
00069
00070 #include <winsock2.h>
00071 #define ValidSocket(s) ((s) != INVALID_SOCKET)
00072 typedef SOCKET OsSocket;
00073
00074 #elif defined LINUX
00075
00076 #define ValidSocket(s) ((s)>=0)
00077 #define INVALID_SOCKET -1
00078 typedef int OsSocket;
00079
00080 #endif
00081
00082
00083 enum
00084 {
00085 NET_LOG_NONE = 0,
00086 NET_LOG_HEX_DUMP = 1,
00087 NET_LOG_ALL_BYTES = 2
00088 };
00089
00090
00091 LgiNetFunc bool HaveNetConnection();
00092 LgiNetFunc bool WhatsMyIp(char *IpAddr);
00093 LgiNetFunc void MDStringToDigest(unsigned char digest[16], char *Str);
00094
00095
00096 class GNetwork;
00097 class GSocket;
00098
00100 class LgiNetClass GNetwork
00101 {
00102 bool SocketsOpen;
00103
00104 public:
00105 GNetwork();
00106 virtual ~GNetwork();
00107
00109 bool IsValid() { return SocketsOpen; }
00110
00112 bool EnumInterfaces(List<char> &Lst);
00113 };
00114
00116 enum GSocketProp
00117 {
00119 GReadable,
00121 GWriteable,
00123 GAcceptable,
00125 GAsync,
00126 GException,
00127 GNoDelay,
00128 };
00129
00132 class LgiNetClass GSocket : public GStream
00133 {
00134 public:
00135 virtual ~GSocket() {}
00136
00138 virtual OsSocket Handle(OsSocket Set = INVALID_SOCKET) = 0;
00140 virtual bool WhatsMyIp
00141 (
00143 char *IpAddr
00144 ) = 0;
00146 virtual int WhatsMyPort() = 0;
00148 virtual bool GetRemoteIp(char *IpAddr) = 0;
00150 virtual bool IsReadable() = 0;
00152 virtual bool Cancel() = 0;
00154 virtual void Cancel(bool i) = 0;
00156 virtual int Is
00157 (
00159 GSocketProp p
00160 ) = 0;
00162 virtual void Is(GSocketProp p, int i) = 0;
00164 virtual void SetLogFile(char *FileName = 0, int Type = NET_LOG_HEX_DUMP) = 0;
00165
00167 virtual bool Listen(int Port = 0) = 0;
00169 virtual bool Accept(GSocket *c) = 0;
00170
00172 virtual int Error() = 0;
00173
00175 virtual void OnDisconnect() = 0;
00177 virtual void OnRead(char *Data, int Len) = 0;
00179 virtual void OnWrite(char *Data, int Len) = 0;
00181 virtual void OnError(int ErrorCode, char *ErrorDescription) = 0;
00183 virtual void OnInformation(char *Str) = 0;
00185 virtual int SetParameter(int Param, int Value) = 0;
00186 };
00187
00189 class LgiNetClass GSocketImpl : public GSocket
00190 {
00191 friend class GNetwork;
00192
00193 protected:
00194 class GSocketImplPrivate *d;
00195
00196
00197 void Log(char *Msg, int Ret, char *Buf, int Len);
00198
00199 public:
00200 int BytesWritten;
00201 int BytesRead;
00202
00204 GSocketImpl();
00205
00207 ~GSocketImpl();
00208
00210 OsSocket Handle(OsSocket Set = INVALID_SOCKET);
00211
00213 bool IsOK();
00214
00216 void SetLogFile
00217 (
00219 char *FileName = 0,
00221 int Type = NET_LOG_HEX_DUMP
00222 );
00223
00225 bool WhatsMyIp(char *IpAddr);
00226
00228 int WhatsMyPort();
00229
00231 bool GetRemoteIp(char *IpAddr);
00232
00234 bool IsReadable();
00235
00237 int Is(GSocketProp p);
00238
00240 void Is(GSocketProp p, int i);
00241
00243 bool Cancel();
00244
00246 void Cancel(bool i);
00247
00249 int Open
00250 (
00252 char *HostAddr,
00254 int Port
00255 );
00256
00258 bool IsOpen();
00259
00261 int Close();
00262
00264 bool Listen(int Port = 0);
00265
00267 bool Accept
00268 (
00270 GSocket *c
00271 );
00272
00275 int Write
00276 (
00278 void *Data,
00280 int Len,
00282 int Flags
00283 );
00284
00291 int Read
00292 (
00294 void *Data,
00296 int Len,
00298 int Flags
00299 );
00300
00302 int Error();
00303
00305 int64 GetSize() { return -1; }
00307 int64 SetSize(int64 Size) { return -1; }
00309 int64 GetPos() { return -1; }
00311 int64 SetPos(int64 Pos) { return -1; }
00312
00314 void OnDisconnect();
00315
00317 void OnRead(char *Data, int Len) {}
00318
00320 void OnWrite(char *Data, int Len) {}
00321
00323 void OnError(int ErrorCode, char *ErrorDescription) {}
00324
00326 void OnInformation(char *Str) {}
00327
00329 int SetParameter
00330 (
00332 int Param,
00333 int Value
00334 ) { return false; }
00335 };
00336
00337 class LgiNetClass GSocks5Socket : public GSocketImpl
00338 {
00339 char *Proxy;
00340 int Port;
00341 char *UserName;
00342 char *Password;
00343
00344 protected:
00345 bool Socks5Connected;
00346
00347 public:
00348 GSocks5Socket(char *proxy, int port, char *username, char *password);
00349 ~GSocks5Socket();
00350
00351
00352 int Open(char *HostAddr, int port);
00353
00354
00355 bool Listen(int Port) { return false; }
00356 };
00357
00359 class LgiNetClass GHost
00360 {
00361 int DefaultPort;
00362
00363 public:
00365 char *Server;
00367 int Port;
00368
00370 GHost
00371 (
00373 int DefaultPort,
00375 char *Str = 0
00376 );
00377
00378 ~GHost();
00379
00380 void Empty();
00381 GHost &operator =(char *s);
00382 operator int()
00383 {
00384 return Server != 0;
00385 }
00386 };
00387
00388 #endif
00389