PTLib
Version 2.10.4
|
00001 /* 00002 * ethsock.h 00003 * 00004 * Direct Ethernet socket I/O channel class. 00005 * 00006 * Portable Windows Library 00007 * 00008 * Copyright (c) 1993-1998 Equivalence Pty. Ltd. 00009 * 00010 * The contents of this file are subject to the Mozilla Public License 00011 * Version 1.0 (the "License"); you may not use this file except in 00012 * compliance with the License. You may obtain a copy of the License at 00013 * http://www.mozilla.org/MPL/ 00014 * 00015 * Software distributed under the License is distributed on an "AS IS" 00016 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See 00017 * the License for the specific language governing rights and limitations 00018 * under the License. 00019 * 00020 * The Original Code is Portable Windows Library. 00021 * 00022 * The Initial Developer of the Original Code is Equivalence Pty. Ltd. 00023 * 00024 * Portions are Copyright (C) 1993 Free Software Foundation, Inc. 00025 * All Rights Reserved. 00026 * 00027 * Contributor(s): ______________________________________. 00028 * 00029 * $Revision: 24177 $ 00030 * $Author: rjongbloed $ 00031 * $Date: 2010-04-05 06:52:04 -0500 (Mon, 05 Apr 2010) $ 00032 */ 00033 00034 #ifndef PTLIB_ETHSOCKET_H 00035 #define PTLIB_ETHSOCKET_H 00036 00037 #ifdef P_USE_PRAGMA 00038 #pragma interface 00039 #endif 00040 00041 #include <ptlib/socket.h> 00042 00043 #ifdef _WIN32 00044 class PWin32PacketDriver; 00045 class PWin32SnmpLibrary; 00046 class PWin32PacketBuffer; 00047 00048 PARRAY(PWin32PackBufArray, PWin32PacketBuffer); 00049 #endif 00050 00054 class PEthSocket : public PSocket 00055 { 00056 PCLASSINFO(PEthSocket, PSocket); 00057 00058 public: 00064 PEthSocket( 00065 PINDEX nReadBuffers = 8, 00066 PINDEX nWriteBuffers = 1, 00067 PINDEX size = 1514 00068 ); 00069 00071 ~PEthSocket(); 00073 00074 00075 public: 00076 #pragma pack(1) 00077 00079 union Address { 00080 BYTE b[6]; 00081 WORD w[3]; 00082 struct { 00083 DWORD l; 00084 WORD s; 00085 } ls; 00086 00087 Address(); 00088 Address(const BYTE * addr); 00089 Address(const Address & addr); 00090 Address(const PString & str); 00091 Address & operator=(const Address & addr); 00092 Address & operator=(const PString & str); 00093 00094 bool operator==(const BYTE * eth) const; 00095 bool operator!=(const BYTE * eth) const; 00096 bool operator==(const Address & eth) const { return ls.l == eth.ls.l && ls.s == eth.ls.s; } 00097 bool operator!=(const Address & eth) const { return ls.l != eth.ls.l || ls.s != eth.ls.s; } 00098 00099 operator PString() const; 00100 00101 friend ostream & operator<<(ostream & s, const Address & a) 00102 { return s << (PString)a; } 00103 }; 00104 00107 struct Frame { 00108 Address dst_addr; 00109 Address src_addr; 00110 union { 00111 struct { 00112 WORD type; 00113 BYTE payload[1500]; 00114 } ether; 00115 struct { 00116 WORD length; 00117 BYTE dsap; 00118 BYTE ssap; 00119 BYTE ctrl; 00120 BYTE oui[3]; 00121 WORD type; 00122 BYTE payload[1492]; 00123 } snap; 00124 }; 00125 00130 void Parse( 00131 WORD & type, // Type of frame 00132 BYTE * & payload, // Pointer to payload 00133 PINDEX & length // Length of payload (on input is full frame length) 00134 ); 00135 }; 00136 #pragma pack() 00137 00145 virtual PBoolean Close(); 00146 00159 virtual PBoolean Read( 00160 void * buf, 00161 PINDEX len 00162 ); 00163 00175 virtual PBoolean Write( 00176 const void * buf, 00177 PINDEX len 00178 ); 00180 00190 virtual PBoolean Connect( 00191 const PString & address 00192 ); 00193 00200 virtual PBoolean Listen( 00201 unsigned queueSize = 5, 00202 WORD port = 0, 00203 Reusability reuse = AddressIsExclusive 00204 ); 00206 00207 00220 PBoolean EnumInterfaces( 00221 PINDEX idx, 00222 PString & name 00223 ); 00224 00225 00231 PBoolean GetAddress( 00232 Address & addr 00233 ); 00234 00240 PBoolean GetIpAddress( 00241 PIPSocket::Address & addr 00242 ); 00243 00250 PBoolean GetIpAddress( 00251 PIPSocket::Address & addr, 00252 PIPSocket::Address & netMask 00253 ); 00254 00264 PBoolean EnumIpAddress( 00265 PINDEX idx, 00266 PIPSocket::Address & addr, 00267 PIPSocket::Address & netMask 00268 ); 00269 00270 00272 enum MediumTypes { 00274 MediumLoop, 00276 Medium802_3, 00278 MediumWan, 00280 MediumUnknown, 00281 NumMediumTypes 00282 }; 00288 MediumTypes GetMedium(); 00290 00291 00294 00295 enum EthTypes { 00297 TypeAll = 3, 00299 TypeIP = 0x800, 00301 TypeX25 = 0x805, 00303 TypeARP = 0x806, 00305 TypeAtalk = 0x809B, 00307 TypeAARP = 0x80F3, 00309 TypeIPX = 0x8137, 00311 TypeIPv6 = 0x86DD 00312 }; 00313 00315 enum FilterMask { 00317 FilterDirected = 0x01, 00319 FilterMulticast = 0x02, 00321 FilterAllMulticast = 0x04, 00323 FilterBroadcast = 0x08, 00325 FilterPromiscuous = 0x10 00326 }; 00327 00339 PBoolean GetFilter( 00340 unsigned & mask, 00341 WORD & type 00342 ); 00343 00356 PBoolean SetFilter( 00357 unsigned mask, 00358 WORD type = TypeAll 00359 ); 00361 00362 00367 PBoolean ResetAdaptor(); 00368 00376 PBoolean ReadPacket( 00377 PBYTEArray & buffer, 00378 Address & dest, 00379 Address & src, 00380 WORD & type, 00381 PINDEX & len, 00382 BYTE * & payload 00383 ); 00385 00386 protected: 00387 virtual PBoolean OpenSocket(); 00388 virtual const char * GetProtocolName() const; 00389 00390 00391 WORD filterType; // Remember the set filter frame type 00392 00393 00394 // Include platform dependent part of class 00395 #ifdef _WIN32 00396 #include "msos/ptlib/ethsock.h" 00397 #else 00398 #include "unix/ptlib/ethsock.h" 00399 #endif 00400 }; 00401 00402 00403 #endif // PTLIB_ETHSOCKET_H 00404 00405 00406 // End Of File ///////////////////////////////////////////////////////////////