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
00088
00089
00090 #ifndef _PETHSOCKET
00091 #define _PETHSOCKET
00092
00093 #ifdef P_USE_PRAGMA
00094 #pragma interface
00095 #endif
00096
00097 #include <ptlib/socket.h>
00098
00099 #ifdef _WIN32
00100 class PWin32PacketDriver;
00101 class PWin32SnmpLibrary;
00102 class PWin32PacketBuffer;
00103
00104 PARRAY(PWin32PackBufArray, PWin32PacketBuffer);
00105 #endif
00106
00110 class PEthSocket : public PSocket
00111 {
00112 PCLASSINFO(PEthSocket, PSocket);
00113
00114 public:
00120 PEthSocket(
00121 PINDEX nReadBuffers = 8,
00122 PINDEX nWriteBuffers = 1,
00123 PINDEX size = 1514
00124 );
00125
00127 ~PEthSocket();
00129
00130
00131 public:
00132 #pragma pack(1)
00133
00135 union Address {
00136 BYTE b[6];
00137 WORD w[3];
00138 struct {
00139 DWORD l;
00140 WORD s;
00141 } ls;
00142
00143 Address();
00144 Address(const BYTE * addr);
00145 Address(const Address & addr);
00146 Address(const PString & str);
00147 Address & operator=(const Address & addr);
00148 Address & operator=(const PString & str);
00149
00150 bool operator==(const BYTE * eth) const;
00151 bool operator!=(const BYTE * eth) const;
00152 bool operator==(const Address & eth) const { return ls.l == eth.ls.l && ls.s == eth.ls.s; }
00153 bool operator!=(const Address & eth) const { return ls.l != eth.ls.l || ls.s != eth.ls.s; }
00154
00155 operator PString() const;
00156
00157 friend ostream & operator<<(ostream & s, const Address & a)
00158 { return s << (PString)a; }
00159 };
00160
00163 struct Frame {
00164 Address dst_addr;
00165 Address src_addr;
00166 union {
00167 struct {
00168 WORD type;
00169 BYTE payload[1500];
00170 } ether;
00171 struct {
00172 WORD length;
00173 BYTE dsap;
00174 BYTE ssap;
00175 BYTE ctrl;
00176 BYTE oui[3];
00177 WORD type;
00178 BYTE payload[1492];
00179 } snap;
00180 };
00181
00186 void Parse(
00187 WORD & type,
00188 BYTE * & payload,
00189 PINDEX & length
00190 );
00191 };
00192 #pragma pack()
00193
00201 virtual BOOL Close();
00202
00215 virtual BOOL Read(
00216 void * buf,
00217 PINDEX len
00218 );
00219
00231 virtual BOOL Write(
00232 const void * buf,
00233 PINDEX len
00234 );
00236
00246 virtual BOOL Connect(
00247 const PString & address
00248 );
00249
00256 virtual BOOL Listen(
00257 unsigned queueSize = 5,
00258 WORD port = 0,
00259 Reusability reuse = AddressIsExclusive
00260 );
00262
00263
00276 BOOL EnumInterfaces(
00277 PINDEX idx,
00278 PString & name
00279 );
00280
00281
00287 BOOL GetAddress(
00288 Address & addr
00289 );
00290
00296 BOOL GetIpAddress(
00297 PIPSocket::Address & addr
00298 );
00299
00306 BOOL GetIpAddress(
00307 PIPSocket::Address & addr,
00308 PIPSocket::Address & netMask
00309 );
00310
00320 BOOL EnumIpAddress(
00321 PINDEX idx,
00322 PIPSocket::Address & addr,
00323 PIPSocket::Address & netMask
00324 );
00325
00326
00328 enum MediumTypes {
00330 MediumLoop,
00332 Medium802_3,
00334 MediumWan,
00336 MediumUnknown,
00337 NumMediumTypes
00338 };
00344 MediumTypes GetMedium();
00346
00347
00350
00351 enum EthTypes {
00353 TypeAll = 3,
00355 TypeIP = 0x800,
00357 TypeX25 = 0x805,
00359 TypeARP = 0x806,
00361 TypeAtalk = 0x809B,
00363 TypeAARP = 0x80F3,
00365 TypeIPX = 0x8137,
00367 TypeIPv6 = 0x86DD
00368 };
00369
00371 enum FilterMask {
00373 FilterDirected = 0x01,
00375 FilterMulticast = 0x02,
00377 FilterAllMulticast = 0x04,
00379 FilterBroadcast = 0x08,
00381 FilterPromiscuous = 0x10
00382 };
00383
00395 BOOL GetFilter(
00396 unsigned & mask,
00397 WORD & type
00398 );
00399
00412 BOOL SetFilter(
00413 unsigned mask,
00414 WORD type = TypeAll
00415 );
00417
00418
00423 BOOL ResetAdaptor();
00424
00432 BOOL ReadPacket(
00433 PBYTEArray & buffer,
00434 Address & dest,
00435 Address & src,
00436 WORD & type,
00437 PINDEX & len,
00438 BYTE * & payload
00439 );
00441
00442 protected:
00443 virtual BOOL OpenSocket();
00444 virtual const char * GetProtocolName() const;
00445
00446
00447 WORD filterType;
00448
00449
00450
00451 #ifdef _WIN32
00452 #include "msos/ptlib/ethsock.h"
00453 #else
00454 #include "unix/ptlib/ethsock.h"
00455 #endif
00456 };
00457
00458 #endif
00459
00460