PTLib  Version 2.10.11
ethsock.h
Go to the documentation of this file.
1 /*
2  * ethsock.h
3  *
4  * Direct Ethernet socket I/O channel class.
5  *
6  * Portable Windows Library
7  *
8  * Copyright (c) 1993-1998 Equivalence Pty. Ltd.
9  *
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/
14  *
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
18  * under the License.
19  *
20  * The Original Code is Portable Windows Library.
21  *
22  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
23  *
24  * Portions are Copyright (C) 1993 Free Software Foundation, Inc.
25  * All Rights Reserved.
26  *
27  * Contributor(s): ______________________________________.
28  *
29  * $Revision: 24177 $
30  * $Author: rjongbloed $
31  * $Date: 2010-04-05 06:52:04 -0500 (Mon, 05 Apr 2010) $
32  */
33 
34 #ifndef PTLIB_ETHSOCKET_H
35 #define PTLIB_ETHSOCKET_H
36 
37 #ifdef P_USE_PRAGMA
38 #pragma interface
39 #endif
40 
41 #include <ptlib/socket.h>
42 
43 #ifdef _WIN32
44 class PWin32PacketDriver;
45 class PWin32SnmpLibrary;
46 class PWin32PacketBuffer;
47 
48 PARRAY(PWin32PackBufArray, PWin32PacketBuffer);
49 #endif
50 
54 class PEthSocket : public PSocket
55 {
56  PCLASSINFO(PEthSocket, PSocket);
57 
58  public:
64  PEthSocket(
65  PINDEX nReadBuffers = 8,
66  PINDEX nWriteBuffers = 1,
67  PINDEX size = 1514
68  );
69 
71  ~PEthSocket();
73 
74 
75  public:
76 #pragma pack(1)
77 
79  union Address {
80  BYTE b[6];
81  WORD w[3];
82  struct {
83  DWORD l;
84  WORD s;
85  } ls;
86 
87  Address();
88  Address(const BYTE * addr);
89  Address(const Address & addr);
90  Address(const PString & str);
91  Address & operator=(const Address & addr);
92  Address & operator=(const PString & str);
93 
94  bool operator==(const BYTE * eth) const;
95  bool operator!=(const BYTE * eth) const;
96  bool operator==(const Address & eth) const { return ls.l == eth.ls.l && ls.s == eth.ls.s; }
97  bool operator!=(const Address & eth) const { return ls.l != eth.ls.l || ls.s != eth.ls.s; }
98 
99  operator PString() const;
100 
101  friend ostream & operator<<(ostream & s, const Address & a)
102  { return s << (PString)a; }
103  };
104 
107  struct Frame {
110  union {
111  struct {
112  WORD type;
113  BYTE payload[1500];
114  } ether;
115  struct {
116  WORD length;
117  BYTE dsap;
118  BYTE ssap;
119  BYTE ctrl;
120  BYTE oui[3];
121  WORD type;
122  BYTE payload[1492];
123  } snap;
124  };
125 
130  void Parse(
131  WORD & type, // Type of frame
132  BYTE * & payload, // Pointer to payload
133  PINDEX & length // Length of payload (on input is full frame length)
134  );
135  };
136 #pragma pack()
137 
145  virtual PBoolean Close();
146 
159  virtual PBoolean Read(
160  void * buf,
161  PINDEX len
162  );
163 
175  virtual PBoolean Write(
176  const void * buf,
177  PINDEX len
178  );
180 
190  virtual PBoolean Connect(
191  const PString & address
192  );
193 
200  virtual PBoolean Listen(
201  unsigned queueSize = 5,
202  WORD port = 0,
204  );
206 
207 
221  PINDEX idx,
222  PString & name
223  );
224 
225 
232  Address & addr
233  );
234 
241  PIPSocket::Address & addr
242  );
243 
251  PIPSocket::Address & addr,
252  PIPSocket::Address & netMask
253  );
254 
265  PINDEX idx,
266  PIPSocket::Address & addr,
267  PIPSocket::Address & netMask
268  );
269 
270 
272  enum MediumTypes {
282  };
290 
291 
294  enum EthTypes {
297  TypeAll = 3,
299  TypeIP = 0x800,
301  TypeX25 = 0x805,
303  TypeARP = 0x806,
305  TypeAtalk = 0x809B,
307  TypeAARP = 0x80F3,
309  TypeIPX = 0x8137,
311  TypeIPv6 = 0x86DD
312  };
313 
315  enum FilterMask {
317  FilterDirected = 0x01,
326  };
327 
340  unsigned & mask,
341  WORD & type
342  );
343 
357  unsigned mask,
358  WORD type = TypeAll
359  );
361 
362 
368 
377  PBYTEArray & buffer,
378  Address & dest,
379  Address & src,
380  WORD & type,
381  PINDEX & len,
382  BYTE * & payload
383  );
385 
386  protected:
387  virtual PBoolean OpenSocket();
388  virtual const char * GetProtocolName() const;
389 
390 
391  WORD filterType; // Remember the set filter frame type
392 
393 
394 // Include platform dependent part of class
395 #ifdef _WIN32
396 #include "msos/ptlib/ethsock.h"
397 #else
398 #include "unix/ptlib/ethsock.h"
399 #endif
400 };
401 
402 
403 #endif // PTLIB_ETHSOCKET_H
404 
405 
406 // End Of File ///////////////////////////////////////////////////////////////
Packets directed at the interface.
Definition: ethsock.h:317
BYTE b[6]
Definition: ethsock.h:80
Definition: socket.h:86
BYTE ctrl
Definition: ethsock.h:119
Packets with a broadcast address.
Definition: ethsock.h:323
WORD s
Definition: ethsock.h:84
BYTE ssap
Definition: ethsock.h:118
~PEthSocket()
Close the socket.
Appletalk DDP.
Definition: ethsock.h:305
struct PEthSocket::Address::@0 ls
virtual PBoolean OpenSocket()
MediumTypes GetMedium()
Return the type of the interface.
WORD w[3]
Definition: ethsock.h:81
MediumTypes
Medium types for the open interface.
Definition: ethsock.h:272
Multicast packets directed at the interface.
Definition: ethsock.h:319
friend ostream & operator<<(ostream &s, const Address &a)
Definition: ethsock.h:101
WORD length
Definition: ethsock.h:116
PBoolean GetFilter(unsigned &mask, WORD &type)
Get the current filtering criteria for receiving packets.
An ethernet MAC Address specification.
Definition: ethsock.h:79
PBoolean SetFilter(unsigned mask, WORD type=TypeAll)
Set the current filtering criteria for receiving packets.
PBoolean ReadPacket(PBYTEArray &buffer, Address &dest, Address &src, WORD &type, PINDEX &len, BYTE *&payload)
Read a packet from the interface and parse out the information specified by the parameters.
virtual PBoolean Close()
Close the channel, shutting down the link to the data source.
PBoolean ResetAdaptor()
Reset the interface.
WORD port
Port to be used by the socket when opening the channel.
Definition: socket.h:444
A Wide Area Network (modem etc)
Definition: ethsock.h:278
X.25.
Definition: ethsock.h:301
FilterMask
Mask filter bits for GetFilter() function.
Definition: ethsock.h:315
BOOL PBoolean
Definition: object.h:102
bool operator==(const BYTE *eth) const
All frames (3 is value for Linux)
Definition: ethsock.h:297
virtual const char * GetProtocolName() const
This function returns the protocol name for the socket type.
Bluebook IPv6.
Definition: ethsock.h:311
Array of unsigned characters.
Definition: array.h:670
An ethernet Network Interface Card (10base2, 10baseT etc)
Definition: ethsock.h:276
Address Resolution Protocol.
Definition: ethsock.h:303
bool operator!=(const Address &eth) const
Definition: ethsock.h:97
Something else.
Definition: ethsock.h:280
#define PARRAY(cls, T)
Declare an array to a specific type of object.
Definition: array.h:1084
The character string class.
Definition: pstring.h:108
PBoolean GetAddress(Address &addr)
Get the low level MAC address of the open interface.
EthTypes
Type codes for ethernet frames.
Definition: ethsock.h:295
Internet Protocol.
Definition: ethsock.h:299
bool operator!=(const BYTE *eth) const
PBoolean GetIpAddress(PIPSocket::Address &addr)
Get the prime IP number bound to the open interface.
Address src_addr
Definition: ethsock.h:109
Definition: ethsock.h:281
WORD type
Definition: ethsock.h:112
WORD filterType
Definition: ethsock.h:391
An ethernet MAC frame.
Definition: ethsock.h:107
Appletalk AARP.
Definition: ethsock.h:307
PEthSocket(PINDEX nReadBuffers=8, PINDEX nWriteBuffers=1, PINDEX size=1514)
Create a new ethernet packet socket.
virtual PBoolean Connect(const PString &address)
Connect a socket to an interface.
virtual PBoolean Write(const void *buf, PINDEX len)
Low level write to the channel.
A class describing an IP address.
Definition: ipsock.h:75
DWORD l
Definition: ethsock.h:83
PBoolean EnumIpAddress(PINDEX idx, PIPSocket::Address &addr, PIPSocket::Address &netMask)
Enumerate all of the IP addresses and net masks bound to the open interface.
Address dst_addr
Definition: ethsock.h:108
bool operator==(const Address &eth) const
Definition: ethsock.h:96
A network communications channel.
Definition: socket.h:58
All multicast packets.
Definition: ethsock.h:321
PBoolean EnumInterfaces(PINDEX idx, PString &name)
Enumerate all the interfaces that are capable of being accessed at the ethernet level.
All packets.
Definition: ethsock.h:325
virtual PBoolean Read(void *buf, PINDEX len)
Low level read from the channel.
virtual PBoolean Listen(unsigned queueSize=5, WORD port=0, Reusability reuse=AddressIsExclusive)
This function is illegal and will assert if attempted.
Reusability
Flags to reuse of port numbers in Listen() function.
Definition: socket.h:84
Novell IPX.
Definition: ethsock.h:309
This class describes a type of socket that will communicate using raw ethernet packets.
Definition: ethsock.h:54
BYTE dsap
Definition: ethsock.h:117
A Loopback Network.
Definition: ethsock.h:274
Address & operator=(const Address &addr)