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

Generated on Fri Mar 7 06:25:02 2008 for PTLib by  doxygen 1.5.1