PTLib Version 2.12.9

- Main Page
- Related Pages
- Namespaces
- Classes
- Files

- File List
- File Members

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: 30455 $
30 * $Author: rjongbloed $
31 * $Date: 2013-09-04 15:29:24 +1000 (Wed, 04 Sep 2013) $
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,
171 Reusability reuse = AddressIsExclusive
172 );
174
175
187 static PStringArray EnumInterfaces(
188 bool detailed = true
189 );
190
191
197 MediumType GetMedium();
199
200
205 const PString & GetFilter() const { return m_filter; }
206
213 bool SetFilter(
214 const PString & filter
215 );
217
218
223 class Frame : public PObject
224 {
225 PCLASSINFO(Frame, PObject);
226 public:
227 Frame(
228 PINDEX maxSize = 65536
229 );
230
231 virtual bool Read(
232 PChannel & channel,
233 PINDEX packetSize = P_MAX_INDEX
234 );
235
236 void PreRead();
237
241 int GetDataLink(
242 PBYTEArray & payload
243 );
244 int GetDataLink(
245 PBYTEArray & payload,
246 Address & src,
247 Address & dst
248 );
249
253 int GetIP(
254 PBYTEArray & payload
255 );
256 int GetIP(
257 PBYTEArray & payload,
258 PIPSocket::Address & src,
259 PIPSocket::Address & dst
260 );
261
265 bool GetUDP(
266 PBYTEArray & payload,
267 WORD & srcPort,
268 WORD & dstPort
269 );
270 bool GetUDP(
271 PBYTEArray & payload,
272 PIPSocketAddressAndPort & src,
273 PIPSocketAddressAndPort & dst
274 );
275
279 bool GetTCP(
280 PBYTEArray & payload,
281 WORD & srcPort,
282 WORD & dstPort
283 );
284 bool GetTCP(
285 PBYTEArray & payload,
286 PIPSocketAddressAndPort & src,
287 PIPSocketAddressAndPort & dst
288 );
289
290 const PTime & GetTimestamp() const { return m_timestamp; }
291 bool IsFragmentated() const { return m_fragmentated; }
292
293 protected:
294 PTime m_timestamp;
295 PBYTEArray m_rawData;
296 PINDEX m_rawSize;
297
298 PBYTEArray m_fragments;
299 bool m_fragmentated;
300 unsigned m_fragmentProto;
301 bool m_fragmentProcessed;
302 PIPSocket::Address m_fragmentSrcIP;
303 PIPSocket::Address m_fragmentDstIP;
304 };
305
314 bool ReadFrame(
315 Frame & frame
316 ) { return frame.Read(*this); }
318
319 protected:
320 virtual PBoolean OpenSocket();
321 virtual const char * GetProtocolName() const;
322
323 bool m_promiscuous;
324 unsigned m_snapLength;
325 PString m_filter; // Filter expression
326
327 struct InternalData;
328 InternalData * m_internal;
329 };
330
331
332 class PEthSocketThread : public PObject
333 {
334 PCLASSINFO(PEthSocketThread, PObject);
335 public:
336 #define PDECLARE_EthFrameNotifier(cls, fn) PDECLARE_NOTIFIER2(PEthSocket, cls, fn, PEthSocket::Frame &)
337 typedef PNotifierTemplate<PEthSocket::Frame &> FrameNotifier;
338
339 PEthSocketThread(const FrameNotifier & notifier = NULL);
340 ~PEthSocketThread() { Stop(); }
341
342 virtual bool Start(
343 const PString & device,
344 const PString & filter = PString::Empty()
345 );
346
347 virtual void Stop();
348
349 bool IsRunning() const { return m_running; }
350
351 virtual PEthSocket * CreateEthSocket() const;
352
353 void SetNotifier(
354 const FrameNotifier & notifier
355 ) { m_notifier = notifier; }
356
357 PEthSocket * GetSocket() const { return m_socket; }
358
359 protected:
360 virtual void MainLoop();
361
362 FrameNotifier m_notifier;
363 PThread * m_thread;
364 PEthSocket * m_socket;
365 PEthSocket::Frame m_frame;
366 bool m_running;
367 };
368
369
370 #endif // PTLIB_ETHSOCKET_H
371
372
373 // End Of File ///////////////////////////////////////////////////////////////