Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Class Members | File Members

ethsock.h

Go to the documentation of this file.
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  * $Log: ethsock.h,v $
00030  * Revision 1.17  2004/04/18 04:33:36  rjongbloed
00031  * Changed all operators that return BOOL to return standard type bool. This is primarily
00032  *   for improved compatibility with std STL usage removing many warnings.
00033  *
00034  * Revision 1.16  2003/09/17 05:41:58  csoutheren
00035  * Removed recursive includes
00036  *
00037  * Revision 1.15  2003/09/17 01:18:02  csoutheren
00038  * Removed recursive include file system and removed all references
00039  * to deprecated coooperative threading support
00040  *
00041  * Revision 1.14  2002/09/16 01:08:59  robertj
00042  * Added #define so can select if #pragma interface/implementation is used on
00043  *   platform basis (eg MacOS) rather than compiler, thanks Robert Monaghan.
00044  *
00045  * Revision 1.13  2001/10/03 03:15:05  robertj
00046  * Changed to allow use of NULL pointer to indicate address of all zeros.
00047  *
00048  * Revision 1.12  2001/05/22 12:49:32  robertj
00049  * Did some seriously wierd rewrite of platform headers to eliminate the
00050  *   stupid GNU compiler warning about braces not matching.
00051  *
00052  * Revision 1.11  1999/03/09 02:59:49  robertj
00053  * Changed comments to doc++ compatible documentation.
00054  *
00055  * Revision 1.10  1999/02/16 08:07:11  robertj
00056  * MSVC 6.0 compatibility changes.
00057  *
00058  * Revision 1.9  1998/11/20 03:18:24  robertj
00059  * Split rad and write buffers to separate pools.
00060  *
00061  * Revision 1.8  1998/11/19 05:18:21  robertj
00062  * Added route table manipulation functions to PIPSocket class.
00063  *
00064  * Revision 1.7  1998/10/12 09:34:40  robertj
00065  * New method for getting IP addresses of interfaces.
00066  *
00067  * Revision 1.6  1998/09/23 06:20:31  robertj
00068  * Added open source copyright license.
00069  *
00070  * Revision 1.5  1998/09/14 12:27:21  robertj
00071  * Added function to parse type out of ethernet/802.2 frame.
00072  *
00073  * Revision 1.4  1998/08/25 11:06:34  robertj
00074  * Fixed output of PEthSocket::Address variables to streams.
00075  *
00076  * Revision 1.3  1998/08/22 04:07:42  robertj
00077  * Fixed GNU problem with structure packing
00078  *
00079  * Revision 1.2  1998/08/21 05:26:34  robertj
00080  * Fine tuning of interface.
00081  *
00082  * Revision 1.1  1998/08/20 05:46:45  robertj
00083  * Initial revision
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,      // Type of frame
00185         BYTE * & payload, // Pointer to payload
00186         PINDEX & length   // Length of payload (on input is full frame 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;  // Remember the set filter frame type
00445 
00446 
00447 // Include platform dependent part of class
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 // End Of File ///////////////////////////////////////////////////////////////

Generated on Sat Dec 22 16:46:50 2007 for PWLib by  doxygen 1.4.2