00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087 #ifndef _PETHSOCKET
00088 #define _PETHSOCKET
00089
00090 #ifdef P_USE_PRAGMA
00091 #pragma interface
00092 #endif
00093
00094 #include <ptlib/socket.h>
00095
00096 #ifdef _WIN32
00097 class PWin32PacketDriver;
00098 class PWin32SnmpLibrary;
00099 class PWin32PacketBuffer;
00100
00101 PARRAY(PWin32PackBufArray, PWin32PacketBuffer);
00102 #endif
00103
00107 class PEthSocket : public PSocket
00108 {
00109 PCLASSINFO(PEthSocket, PSocket);
00110
00111 public:
00117 PEthSocket(
00118 PINDEX nReadBuffers = 8,
00119 PINDEX nWriteBuffers = 1,
00120 PINDEX size = 1514
00121 );
00122
00124 ~PEthSocket();
00126
00127
00128 public:
00129 #pragma pack(1)
00130
00132 union Address {
00133 BYTE b[6];
00134 WORD w[3];
00135 struct {
00136 DWORD l;
00137 WORD s;
00138 } ls;
00139
00140 Address();
00141 Address(const BYTE * addr);
00142 Address(const Address & addr);
00143 Address(const PString & str);
00144 Address & operator=(const Address & addr);
00145 Address & operator=(const PString & str);
00146
00147 bool operator==(const BYTE * eth) const;
00148 bool operator!=(const BYTE * eth) const;
00149 bool operator==(const Address & eth) const { return ls.l == eth.ls.l && ls.s == eth.ls.s; }
00150 bool operator!=(const Address & eth) const { return ls.l != eth.ls.l || ls.s != eth.ls.s; }
00151
00152 operator PString() const;
00153
00154 friend ostream & operator<<(ostream & s, const Address & a)
00155 { return s << (PString)a; }
00156 };
00157
00160 struct Frame {
00161 Address dst_addr;
00162 Address src_addr;
00163 union {
00164 struct {
00165 WORD type;
00166 BYTE payload[1500];
00167 } ether;
00168 struct {
00169 WORD length;
00170 BYTE dsap;
00171 BYTE ssap;
00172 BYTE ctrl;
00173 BYTE oui[3];
00174 WORD type;
00175 BYTE payload[1492];
00176 } snap;
00177 };
00178
00183 void Parse(
00184 WORD & type,
00185 BYTE * & payload,
00186 PINDEX & length
00187 );
00188 };
00189 #pragma pack()
00190
00198 virtual BOOL Close();
00199
00212 virtual BOOL Read(
00213 void * buf,
00214 PINDEX len
00215 );
00216
00228 virtual BOOL Write(
00229 const void * buf,
00230 PINDEX len
00231 );
00233
00243 virtual BOOL Connect(
00244 const PString & address
00245 );
00246
00253 virtual BOOL Listen(
00254 unsigned queueSize = 5,
00255 WORD port = 0,
00256 Reusability reuse = AddressIsExclusive
00257 );
00259
00260
00273 BOOL EnumInterfaces(
00274 PINDEX idx,
00275 PString & name
00276 );
00277
00278
00284 BOOL GetAddress(
00285 Address & addr
00286 );
00287
00293 BOOL GetIpAddress(
00294 PIPSocket::Address & addr
00295 );
00296
00303 BOOL GetIpAddress(
00304 PIPSocket::Address & addr,
00305 PIPSocket::Address & netMask
00306 );
00307
00317 BOOL EnumIpAddress(
00318 PINDEX idx,
00319 PIPSocket::Address & addr,
00320 PIPSocket::Address & netMask
00321 );
00322
00323
00325 enum MediumTypes {
00327 MediumLoop,
00329 Medium802_3,
00331 MediumWan,
00333 MediumUnknown,
00334 NumMediumTypes
00335 };
00341 MediumTypes GetMedium();
00343
00344
00347
00348 enum EthTypes {
00350 TypeAll = 3,
00352 TypeIP = 0x800,
00354 TypeX25 = 0x805,
00356 TypeARP = 0x806,
00358 TypeAtalk = 0x809B,
00360 TypeAARP = 0x80F3,
00362 TypeIPX = 0x8137,
00364 TypeIPv6 = 0x86DD
00365 };
00366
00368 enum FilterMask {
00370 FilterDirected = 0x01,
00372 FilterMulticast = 0x02,
00374 FilterAllMulticast = 0x04,
00376 FilterBroadcast = 0x08,
00378 FilterPromiscuous = 0x10
00379 };
00380
00392 BOOL GetFilter(
00393 unsigned & mask,
00394 WORD & type
00395 );
00396
00409 BOOL SetFilter(
00410 unsigned mask,
00411 WORD type = TypeAll
00412 );
00414
00415
00420 BOOL ResetAdaptor();
00421
00429 BOOL ReadPacket(
00430 PBYTEArray & buffer,
00431 Address & dest,
00432 Address & src,
00433 WORD & type,
00434 PINDEX & len,
00435 BYTE * & payload
00436 );
00438
00439 protected:
00440 virtual BOOL OpenSocket();
00441 virtual const char * GetProtocolName() const;
00442
00443
00444 WORD filterType;
00445
00446
00447
00448 #ifdef _WIN32
00449 #include "msos/ptlib/ethsock.h"
00450 #else
00451 #include "unix/ptlib/ethsock.h"
00452 #endif
00453 };
00454
00455 #endif
00456
00457