PTLib  Version 2.14.3
 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  * $Revision: 32368 $
30  * $Author: rjongbloed $
31  * $Date: 2014-07-17 10:59:29 +1000 (Thu, 17 Jul 2014) $
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/ipsock.h>
42 
43 
47 class PEthSocket : public PSocket
48 {
49  PCLASSINFO(PEthSocket, PSocket);
50  public:
55  PEthSocket(
56  bool promiscuous = true,
58  unsigned snapLength = 65536
59  );
60 
62  ~PEthSocket();
64 
65 
66  public:
68  P_DECLARE_STREAMABLE_ENUM(MediumType,
69  MediumLoop,
70  Medium802_3,
71  MediumWan,
72  MediumLinuxSLL,
73  MediumUnknown
74  );
75 
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 #pragma pack()
105 
113  virtual PBoolean Close();
114 
127  virtual PBoolean Read(
128  void * buf,
129  PINDEX len
130  );
131 
143  virtual PBoolean Write(
144  const void * buf,
145  PINDEX len
146  );
148 
158  virtual PBoolean Connect(
159  const PString & address
160  );
161 
168  virtual PBoolean Listen(
169  unsigned queueSize = 5,
170  WORD port = 0,
172  );
174 
175 
188  bool detailed = true
189  );
190 
191 
197  MediumType GetMedium();
198 
201  const PTime & GetLastPacketTime() const { return m_lastPacketTime; }
203 
204 
209  const PString & GetFilter() const { return m_filter; }
210 
217  bool SetFilter(
218  const PString & filter
219  );
221 
222 
227  class Frame : public PObject
228  {
229  PCLASSINFO(Frame, PObject);
230  public:
231  Frame(
232  PINDEX maxSize = 65536
233  );
234  Frame(
235  const Frame & frame
236  );
237 
238  virtual bool Write(
239  PChannel & channel
240  ) const;
241 
242  virtual bool Read(
243  PChannel & channel,
244  PINDEX packetSize = P_MAX_INDEX
245  );
246 
247  void PreRead();
248 
252  int GetDataLink(
253  PBYTEArray & payload
254  );
255  int GetDataLink(
256  PBYTEArray & payload,
257  Address & src,
258  Address & dst
259  );
260  BYTE * CreateDataLink(
261  const Address & src,
262  const Address & dst,
263  unsigned proto,
264  PINDEX length
265  );
266 
270  int GetIP(
271  PBYTEArray & payload
272  );
273  int GetIP(
274  PBYTEArray & payload,
275  PIPSocket::Address & src,
276  PIPSocket::Address & dst
277  );
278  BYTE * CreateIP(
279  const PIPSocket::Address & src,
280  const PIPSocket::Address & dst,
281  unsigned proto,
282  PINDEX length
283  );
284 
288  bool GetUDP(
289  PBYTEArray & payload,
290  WORD & srcPort,
291  WORD & dstPort
292  );
293  bool GetUDP(
294  PBYTEArray & payload,
297  );
298  BYTE * CreateUDP(
299  const PIPSocketAddressAndPort & src,
300  const PIPSocketAddressAndPort & dst,
301  PINDEX length
302  );
303 
307  bool GetTCP(
308  PBYTEArray & payload,
309  WORD & srcPort,
310  WORD & dstPort
311  );
312  bool GetTCP(
313  PBYTEArray & payload,
316  );
317  BYTE * CreateTCP(
318  const PIPSocketAddressAndPort & src,
319  const PIPSocketAddressAndPort & dst,
320  PINDEX length
321  );
322 
323  const PTime & GetTimestamp() const { return m_timestamp; }
324  bool IsFragmentated() const { return m_fragmentated; }
325 
326  PINDEX GetSize() const { return m_rawSize; }
327 
328  protected:
330  PINDEX m_rawSize;
331 
334  unsigned m_fragmentProto;
336 
340  };
341 
350  bool ReadFrame(
351  Frame & frame
352  ) { return frame.Read(*this); }
354 
355  protected:
356  virtual PBoolean OpenSocket();
357  virtual const char * GetProtocolName() const;
358 
360  unsigned m_snapLength;
361  PString m_filter; // Filter expression
362 
363  struct InternalData;
364  InternalData * m_internal;
365 
367 };
368 
369 
370 class PEthSocketThread : public PObject
371 {
372  PCLASSINFO(PEthSocketThread, PObject);
373  public:
374  #define PDECLARE_EthFrameNotifier(cls, fn) PDECLARE_NOTIFIER2(PEthSocket, cls, fn, PEthSocket::Frame &)
376 
377  PEthSocketThread(const FrameNotifier & notifier = NULL);
379 
380  virtual bool Start(
381  const PString & device,
382  const PString & filter = PString::Empty(),
384  );
385 
386  virtual void Stop();
387 
388  bool IsRunning() const { return m_running; }
389 
390  virtual PEthSocket * CreateEthSocket() const;
391 
393  const FrameNotifier & notifier
394  ) { m_notifier = notifier; }
395 
396  PEthSocket * GetSocket() const { return m_socket; }
397 
398  protected:
399  virtual void MainLoop();
400 
405  bool m_running;
406 };
407 
408 
409 #endif // PTLIB_ETHSOCKET_H
410 
411 
412 // End Of File ///////////////////////////////////////////////////////////////