4 * Direct Ethernet socket I/O channel class.
6 * Portable Windows Library
8 * Copyright (c) 1993-1998 Equivalence Pty. Ltd.
10 * The contents of this file are subject to the Mozilla Public License
11 * Version 1.0 (the "License"); you may not use this file except in
12 * compliance with the License. You may obtain a copy of the License at
13 * http://www.mozilla.org/MPL/
15 * Software distributed under the License is distributed on an "AS IS"
16 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
17 * the License for the specific language governing rights and limitations
20 * The Original Code is Portable Windows Library.
22 * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
24 * Portions are Copyright (C) 1993 Free Software Foundation, Inc.
25 * All Rights Reserved.
27 * Contributor(s): ______________________________________.
30 * Revision 1.18 2005/11/25 03:43:47 csoutheren
31 * Fixed function argument comments to be compatible with Doxygen
33 * Revision 1.17 2004/04/18 04:33:36 rjongbloed
34 * Changed all operators that return BOOL to return standard type bool. This is primarily
35 * for improved compatibility with std STL usage removing many warnings.
37 * Revision 1.16 2003/09/17 05:41:58 csoutheren
38 * Removed recursive includes
40 * Revision 1.15 2003/09/17 01:18:02 csoutheren
41 * Removed recursive include file system and removed all references
42 * to deprecated coooperative threading support
44 * Revision 1.14 2002/09/16 01:08:59 robertj
45 * Added #define so can select if #pragma interface/implementation is used on
46 * platform basis (eg MacOS) rather than compiler, thanks Robert Monaghan.
48 * Revision 1.13 2001/10/03 03:15:05 robertj
49 * Changed to allow use of NULL pointer to indicate address of all zeros.
51 * Revision 1.12 2001/05/22 12:49:32 robertj
52 * Did some seriously wierd rewrite of platform headers to eliminate the
53 * stupid GNU compiler warning about braces not matching.
55 * Revision 1.11 1999/03/09 02:59:49 robertj
56 * Changed comments to doc++ compatible documentation.
58 * Revision 1.10 1999/02/16 08:07:11 robertj
59 * MSVC 6.0 compatibility changes.
61 * Revision 1.9 1998/11/20 03:18:24 robertj
62 * Split rad and write buffers to separate pools.
64 * Revision 1.8 1998/11/19 05:18:21 robertj
65 * Added route table manipulation functions to PIPSocket class.
67 * Revision 1.7 1998/10/12 09:34:40 robertj
68 * New method for getting IP addresses of interfaces.
70 * Revision 1.6 1998/09/23 06:20:31 robertj
71 * Added open source copyright license.
73 * Revision 1.5 1998/09/14 12:27:21 robertj
74 * Added function to parse type out of ethernet/802.2 frame.
76 * Revision 1.4 1998/08/25 11:06:34 robertj
77 * Fixed output of PEthSocket::Address variables to streams.
79 * Revision 1.3 1998/08/22 04:07:42 robertj
80 * Fixed GNU problem with structure packing
82 * Revision 1.2 1998/08/21 05:26:34 robertj
83 * Fine tuning of interface.
85 * Revision 1.1 1998/08/20 05:46:45 robertj
97 #include <ptlib/socket.h>
100 class PWin32PacketDriver
;
101 class PWin32SnmpLibrary
;
102 class PWin32PacketBuffer
;
104 PARRAY(PWin32PackBufArray
, PWin32PacketBuffer
);
107 /**This class describes a type of socket that will communicate using
108 raw ethernet packets.
110 class PEthSocket
: public PSocket
112 PCLASSINFO(PEthSocket
, PSocket
);
115 /**@name Constructor */
117 /**Create a new ethernet packet socket. Some platforms require a set of
118 buffers to be allocated to avoid losing frequent packets.
121 PINDEX nReadBuffers
= 8, ///< Number of buffers used for reading.
122 PINDEX nWriteBuffers
= 1, ///< Number of buffers used for writing.
123 PINDEX size
= 1514 ///< Size of each buffer.
133 /** An ethernet MAC Address specification.
144 Address(const BYTE
* addr
);
145 Address(const Address
& addr
);
146 Address(const PString
& str
);
147 Address
& operator=(const Address
& addr
);
148 Address
& operator=(const PString
& str
);
150 bool operator==(const BYTE
* eth
) const;
151 bool operator!=(const BYTE
* eth
) const;
152 bool operator==(const Address
& eth
) const { return ls
.l
== eth
.ls
.l
&& ls
.s
== eth
.ls
.s
; }
153 bool operator!=(const Address
& eth
) const { return ls
.l
!= eth
.ls
.l
|| ls
.s
!= eth
.ls
.s
; }
155 operator PString() const;
157 friend ostream
& operator<<(ostream
& s
, const Address
& a
)
158 { return s
<< (PString
)a
; }
161 /** An ethernet MAC frame.
182 /**Parse the Ethernet Frame to extract the frame type and the address of
183 the payload. The length should be the original bytes read in the frame
184 and may be altered to information contained in the frame, if available.
187 WORD
& type
, // Type of frame
188 BYTE
* & payload
, // Pointer to payload
189 PINDEX
& length
// Length of payload (on input is full frame length)
194 /**@name Overrides from class PChannel */
196 /**Close the channel, shutting down the link to the data source.
199 TRUE if the channel successfully closed.
201 virtual BOOL
Close();
203 /**Low level read from the channel. This function may block until the
204 requested number of characters were read or the read timeout was
205 reached. The GetLastReadCount() function returns the actual number
208 The GetErrorCode() function should be consulted after Read() returns
209 FALSE to determine what caused the failure.
212 TRUE indicates that at least one character was read from the channel.
213 FALSE means no bytes were read due to timeout or some other I/O error.
216 void * buf
, ///< Pointer to a block of memory to receive the read bytes.
217 PINDEX len
///< Maximum number of bytes to read into the buffer.
220 /**Low level write to the channel. This function will block until the
221 requested number of characters are written or the write timeout is
222 reached. The GetLastWriteCount() function returns the actual number
225 The GetErrorCode() function should be consulted after Write() returns
226 FALSE to determine what caused the failure.
229 TRUE if at least len bytes were written to the channel.
232 const void * buf
, ///< Pointer to a block of memory to write.
233 PINDEX len
///< Number of bytes to write.
237 /**@name Overrides from class PSocket */
239 /**Connect a socket to an interface. The first form opens an interface by
240 a name as returned by the EnumInterfaces() function. The second opens
241 the interface that has the specified MAC address.
244 TRUE if the channel was successfully connected to the interface.
246 virtual BOOL
Connect(
247 const PString
& address
///< Name of interface to connect to.
250 /**This function is illegal and will assert if attempted. You must be
251 connected to an interface using Connect() to do I/O on the socket.
254 TRUE if the channel was successfully opened.
257 unsigned queueSize
= 5, ///< Number of pending accepts that may be queued.
258 WORD port
= 0, ///< Port number to use for the connection.
259 Reusability reuse
= AddressIsExclusive
///< Can/Cant listen more than once.
264 /**@name Information functions */
266 /**Enumerate all the interfaces that are capable of being accessed at the
267 ethernet level. Begin with index 0, and increment until the function
268 returns FALSE. The name string returned can be passed, unchanged, to
269 the Connect() function.
271 Note that the driver does not need to be open for this function to work.
274 TRUE if an interface has the index supplied.
277 PINDEX idx
, ///< Index of interface
278 PString
& name
///< Interface name
282 /**Get the low level MAC address of the open interface.
285 TRUE if the address is returned, FALSE on error.
288 Address
& addr
///< Variable to receive the MAC address.
291 /**Get the prime IP number bound to the open interface.
294 TRUE if the address is returned, FALSE on error.
297 PIPSocket::Address
& addr
///< Variable to receive the IP address.
300 /**Get the prime IP number bound to the open interface.
301 This also returns the net mask associated with the open interface.
304 TRUE if the address is returned, FALSE on error.
307 PIPSocket::Address
& addr
, ///< Variable to receive the IP address.
308 PIPSocket::Address
& netMask
///< Variable to receive the net mask.
311 /**Enumerate all of the IP addresses and net masks bound to the open
312 interface. This allows all the addresses to be found on multi-homed
313 hosts. Begin with index 0 and increment until the function returns
314 FALSE to enumerate all the addresses.
317 TRUE if the address is returned, FALSE on error or if there are no more
318 addresses bound to the interface.
321 PINDEX idx
, ///< Index
322 PIPSocket::Address
& addr
, ///< Variable to receive the IP address.
323 PIPSocket::Address
& netMask
///< Variable to receive the net mask.
327 /// Medium types for the open interface.
329 /// A Loopback Network
331 /// An ethernet Network Interface Card (10base2, 10baseT etc)
333 /// A Wide Area Network (modem etc)
339 /**Return the type of the interface.
342 Type enum for the interface, or NumMediumTypes if interface not open.
344 MediumTypes
GetMedium();
348 /**@name Filtering functions */
350 /// Type codes for ethernet frames.
352 /// All frames (3 is value for Linux)
354 /// Internet Protocol
358 /// Address Resolution Protocol
370 /// Mask filter bits for GetFilter() function.
372 /// Packets directed at the interface.
373 FilterDirected
= 0x01,
374 /// Multicast packets directed at the interface.
375 FilterMulticast
= 0x02,
376 /// All multicast packets.
377 FilterAllMulticast
= 0x04,
378 /// Packets with a broadcast address.
379 FilterBroadcast
= 0x08,
381 FilterPromiscuous
= 0x10
384 /**Get the current filtering criteria for receiving packets.
386 A bit-wise OR of the FilterMask values will filter packets so that
387 they do not appear in the Read() function at all.
389 The type is be the specific frame type to accept. A value of TypeAll
390 may be used to match all frame types.
393 A bit mask is returned, a value of 0 indicates an error.
396 unsigned & mask
, ///< Bits for filtering on address
397 WORD
& type
///< Code for filtering on type.
400 /**Set the current filtering criteria for receiving packets. A bit-wise OR
401 of the FilterMask values will filter packets so that they do not appear
402 in the Read() function at all.
404 The type is be the specific frame type to accept. A value of TypeAll
405 may be used to match all frame types.
407 A value of zero for the filter mask is useless and will assert.
410 TRUE if the address is returned, FALSE on error.
413 unsigned mask
, ///< Bits for filtering on address
414 WORD type
= TypeAll
///< Code for filtering on type.
419 /**@name I/O functions */
421 /**Reset the interface.
425 /**Read a packet from the interface and parse out the information
426 specified by the parameters. This will automatically adjust for 802.2
427 and 802.3 ethernet frames.
430 TRUE if the packet read, FALSE on error.
433 PBYTEArray
& buffer
, ///< Buffer to receive the raw packet
434 Address
& dest
, ///< Destination address of packet
435 Address
& src
, ///< Source address of packet
436 WORD
& type
, ///< Packet frame type ID
437 PINDEX
& len
, ///< Length of payload
438 BYTE
* & payload
///< Pointer into #buffer# of payload.
443 virtual BOOL
OpenSocket();
444 virtual const char * GetProtocolName() const;
447 WORD filterType
; // Remember the set filter frame type
450 // Include platform dependent part of class
452 #include "msos/ptlib/ethsock.h"
454 #include "unix/ptlib/ethsock.h"
460 // End Of File ///////////////////////////////////////////////////////////////