PTLib  Version 2.18.8
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 
30 #ifndef PTLIB_ETHSOCKET_H
31 #define PTLIB_ETHSOCKET_H
32 
33 #ifdef P_USE_PRAGMA
34 #pragma interface
35 #endif
36 
37 #include <ptlib/ipsock.h>
38 
39 
43 class PEthSocket : public PSocket
44 {
45  PCLASSINFO(PEthSocket, PSocket);
46  public:
51  PEthSocket(
52  bool promiscuous = true,
54  unsigned snapLength = 65536
55  );
56 
58  ~PEthSocket();
60 
61 
62  public:
64  P_DECLARE_STREAMABLE_ENUM(MediumType,
65  MediumLoop,
66  Medium802_3,
67  MediumWan,
68  MediumLinuxSLL,
69  MediumUnknown
70  );
71 
72 #pragma pack(1)
73 
75  union Address {
76  BYTE b[6];
77  WORD w[3];
78  struct {
79  DWORD l;
80  WORD s;
81  } ls;
82 
83  Address();
84  Address(const BYTE * addr);
85  Address(const Address & addr);
86  Address(const PString & str);
87  Address & operator=(const Address & addr);
88  Address & operator=(const PString & str);
89 
90  bool operator==(const BYTE * eth) const;
91  bool operator!=(const BYTE * eth) const;
92  bool operator==(const Address & eth) const { return ls.l == eth.ls.l && ls.s == eth.ls.s; }
93  bool operator!=(const Address & eth) const { return ls.l != eth.ls.l || ls.s != eth.ls.s; }
94 
95  operator PString() const;
96 
97  friend ostream & operator<<(ostream & s, const Address & a)
98  { return s << (PString)a; }
99  };
100 #pragma pack()
101 
109  virtual PString GetName() const { return m_channelName; }
110 
116  virtual PBoolean Close();
117 
130  virtual PBoolean Read(
131  void * buf,
132  PINDEX len
133  );
134 
146  virtual PBoolean Write(
147  const void * buf,
148  PINDEX len
149  );
151 
161  virtual PBoolean Connect(
162  const PString & address
163  );
164 
171  virtual PBoolean Listen(
172  unsigned queueSize = 5,
173  WORD port = 0,
175  );
177 
178 
191  bool detailed = true
192  );
193 
194 
200  MediumType GetMedium();
201 
204  const PTime & GetLastPacketTime() const { return m_lastPacketTime; }
206 
207 
212  const PString & GetFilter() const { return m_filter; }
213 
220  bool SetFilter(
221  const PString & filter
222  );
224 
225 
230  class Frame : public PObject
231  {
232  PCLASSINFO(Frame, PObject);
233  public:
234  Frame(
235  PINDEX maxSize = 65536
236  );
237  Frame(
238  const Frame & frame
239  );
240 
241  virtual bool Write(
242  PChannel & channel
243  ) const;
244 
245  virtual bool Read(
246  PChannel & channel,
247  PINDEX packetSize = P_MAX_INDEX
248  );
249 
250  void PreRead();
251 
255  int GetDataLink(
256  PBYTEArray & payload
257  );
258  int GetDataLink(
259  PBYTEArray & payload,
260  Address & src,
261  Address & dst
262  );
263  BYTE * CreateDataLink(
264  const Address & src,
265  const Address & dst,
266  unsigned proto,
267  PINDEX length
268  );
269 
273  int GetIP(
274  PBYTEArray & payload
275  );
276  int GetIP(
277  PBYTEArray & payload,
278  PIPSocket::Address & src,
279  PIPSocket::Address & dst
280  );
281  BYTE * CreateIP(
282  const PIPSocket::Address & src,
283  const PIPSocket::Address & dst,
284  unsigned proto,
285  PINDEX length
286  );
287 
291  bool GetUDP(
292  PBYTEArray & payload,
293  WORD & srcPort,
294  WORD & dstPort
295  );
296  bool GetUDP(
297  PBYTEArray & payload,
300  );
301  BYTE * CreateUDP(
302  const PIPSocketAddressAndPort & src,
303  const PIPSocketAddressAndPort & dst,
304  PINDEX length,
305  const void * data = NULL
306  );
307 
311  bool GetTCP(
312  PBYTEArray & payload,
313  WORD & srcPort,
314  WORD & dstPort
315  );
316  bool GetTCP(
317  PBYTEArray & payload,
320  );
321  BYTE * CreateTCP(
322  const PIPSocketAddressAndPort & src,
323  const PIPSocketAddressAndPort & dst,
324  PINDEX length,
325  const void * data = NULL
326  );
327 
328  const PTime & GetTimestamp() const { return m_timestamp; }
329  bool IsFragmentated() const { return m_fragmentated; }
330 
331  PINDEX GetSize() const { return m_rawSize; }
332 
333  void SetDataLinkType(unsigned dataLinkType) { m_dataLinkType = dataLinkType; }
334  unsigned GetDataLinkType() const { return m_dataLinkType; }
335 
336  protected:
338  PINDEX m_rawSize;
339  unsigned m_dataLinkType;
340 
343  unsigned m_fragmentProto;
345 
349  };
350 
359  bool ReadFrame(
360  Frame & frame
361  ) { return frame.Read(*this); }
363 
364  protected:
365  virtual PBoolean OpenSocket();
366  virtual const char * GetProtocolName() const;
367 
370  unsigned m_snapLength;
371  PString m_filter; // Filter expression
372 
373  struct InternalData;
374  InternalData * m_internal;
375 
377 };
378 
379 
380 class PEthSocketThread : public PObject
381 {
382  PCLASSINFO(PEthSocketThread, PObject);
383  public:
384  #define PDECLARE_EthFrameNotifier(cls, fn) PDECLARE_NOTIFIER2(PEthSocket, cls, fn, PEthSocket::Frame &)
386 
387  PEthSocketThread(const FrameNotifier & notifier = NULL);
389 
390  virtual bool Start(
391  const PString & device,
392  const PString & filter = PString::Empty(),
393  PThread::Priority priority = PThread::NormalPriority
394  );
395 
396  virtual void Stop();
397 
398  bool IsRunning() const { return m_running; }
399 
400  virtual PEthSocket * CreateEthSocket() const;
401 
403  const FrameNotifier & notifier
404  ) { m_notifier = notifier; }
405 
406  PEthSocket * GetSocket() const { return m_socket; }
407 
408  protected:
409  virtual void MainLoop();
410 
415  bool m_running;
416 };
417 
418 
419 #endif // PTLIB_ETHSOCKET_H
420 
421 
422 // End Of File ///////////////////////////////////////////////////////////////
BYTE b[6]
Definition: ethsock.h:76
FrameNotifier m_notifier
Definition: ethsock.h:411
Definition: socket.h:87
~PEthSocketThread()
Definition: ethsock.h:388
unsigned m_dataLinkType
Definition: ethsock.h:339
void SetDataLinkType(unsigned dataLinkType)
Definition: ethsock.h:333
bool GetTCP(PBYTEArray &payload, WORD &srcPort, WORD &dstPort)
Extract the TCP payload.
PINDEX m_rawSize
Definition: ethsock.h:338
WORD s
Definition: ethsock.h:80
~PEthSocket()
Close the socket.
Definition: ethsock.h:380
BYTE * CreateIP(const PIPSocket::Address &src, const PIPSocket::Address &dst, unsigned proto, PINDEX length)
bool IsFragmentated() const
Definition: ethsock.h:329
virtual PBoolean OpenSocket()
This class defines an absolute time and date.
Definition: ptime.h:49
MediumType GetMedium()
Return the data link of the interface.
BYTE * CreateDataLink(const Address &src, const Address &dst, unsigned proto, PINDEX length)
WORD w[3]
Definition: ethsock.h:77
PEthSocket(bool promiscuous=true, unsigned snapLength=65536)
Create a new ethernet packet socket.
friend ostream & operator<<(ostream &s, const Address &a)
Definition: ethsock.h:97
PIPSocket::Address m_fragmentDstIP
Definition: ethsock.h:348
An ethernet MAC Address specification.
Definition: ethsock.h:75
static PStringArray EnumInterfaces(bool detailed=true)
Enumerate all the interfaces that are capable of being accessed at the ethernet level.
virtual PBoolean Close()
Close the channel, shutting down the link to the data source.
PINDEX GetSize() const
Definition: ethsock.h:331
virtual void Stop()
This is an array collection class of PString objects.
Definition: pstring.h:2365
void SetNotifier(const FrameNotifier &notifier)
Definition: ethsock.h:402
A class describing an IP address and port number combination.
Definition: ipsock.h:278
BYTE * CreateUDP(const PIPSocketAddressAndPort &src, const PIPSocketAddressAndPort &dst, PINDEX length, const void *data=NULL)
PBYTEArray m_fragments
Definition: ethsock.h:341
bool operator==(const BYTE *eth) const
virtual const char * GetProtocolName() const
This function returns the protocol name for the socket type.
Array of unsigned characters.
Definition: array.h:605
virtual bool Start(const PString &device, const PString &filter=PString::Empty(), PThread::Priority priority=PThread::NormalPriority)
Abstract class defining I/O channel semantics.
Definition: channel.h:103
unsigned m_fragmentProto
Definition: ethsock.h:343
PTime m_lastPacketTime
Definition: ethsock.h:376
PEthSocketThread(const FrameNotifier &notifier=NULL)
bool m_fragmentated
Definition: ethsock.h:342
bool PBoolean
Definition: object.h:174
const PTime & GetTimestamp() const
Definition: ethsock.h:328
PThread * m_thread
Definition: ethsock.h:412
bool operator!=(const Address &eth) const
Definition: ethsock.h:93
PString m_filter
Definition: ethsock.h:371
PEthSocket * GetSocket() const
Definition: ethsock.h:406
bool m_promiscuous
Definition: ethsock.h:369
const PTime & GetLastPacketTime() const
Return the capture time of the last read packet.
Definition: ethsock.h:204
#define P_MAX_INDEX
Definition: object.h:80
virtual void MainLoop()
The character string class.
Definition: pstring.h:108
bool operator!=(const BYTE *eth) const
PEthSocket::Frame m_frame
Definition: ethsock.h:414
int GetDataLink(PBYTEArray &payload)
Extract the data link payload.
bool IsRunning() const
Definition: ethsock.h:398
P_DECLARE_STREAMABLE_ENUM(MediumType, MediumLoop, Medium802_3, MediumWan, MediumLinuxSLL, MediumUnknown)
Medium types for the open interface.
const PString & GetFilter() const
Get the current filtering criteria for receiving packets.
Definition: ethsock.h:212
PIPSocket::Address m_fragmentSrcIP
Definition: ethsock.h:347
BYTE * CreateTCP(const PIPSocketAddressAndPort &src, const PIPSocketAddressAndPort &dst, PINDEX length, const void *data=NULL)
This class defines a thread of execution in the system.
Definition: thread.h:66
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.
bool GetUDP(PBYTEArray &payload, WORD &srcPort, WORD &dstPort)
Extract the UDP payload.
static const PString & Empty()
Return an empty string.
A class describing an IP address.
Definition: ipsock.h:59
InternalData * m_internal
Definition: ethsock.h:373
DWORD l
Definition: ethsock.h:79
bool m_running
Definition: ethsock.h:415
virtual PEthSocket * CreateEthSocket() const
PTime m_timestamp
Definition: ethsock.h:346
PBYTEArray m_rawData
Definition: ethsock.h:337
unsigned GetDataLinkType() const
Definition: ethsock.h:334
PString m_channelName
Definition: ethsock.h:368
virtual bool Read(PChannel &channel, PINDEX packetSize=P_MAX_INDEX)
An ethernet MAC frame.
Definition: ethsock.h:230
virtual PString GetName() const
Get the platform and I/O channel type name of the channel.
Definition: ethsock.h:109
bool operator==(const Address &eth) const
Definition: ethsock.h:92
A network communications channel.
Definition: socket.h:59
PNotifierTemplate< PEthSocket::Frame & > FrameNotifier
Definition: ethsock.h:385
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.
int GetIP(PBYTEArray &payload)
Extract the Internet Protocol payload.
Reusability
Flags to reuse of port numbers in Listen() function.
Definition: socket.h:85
Ultimate parent class for all objects in the class library.
Definition: object.h:2204
This class describes a type of socket that will communicate using raw ethernet packets.
Definition: ethsock.h:43
unsigned m_snapLength
Definition: ethsock.h:370
struct PEthSocket::Address::@1 ls
Frame(PINDEX maxSize=65536)
bool SetFilter(const PString &filter)
Set the current filtering criteria for receiving packets.
virtual bool Write(PChannel &channel) const
Address & operator=(const Address &addr)
bool ReadFrame(Frame &frame)
Read a frame from the interface.
Definition: ethsock.h:359
PEthSocket * m_socket
Definition: ethsock.h:413
bool m_fragmentProcessed
Definition: ethsock.h:344