libDwm-0.6.0
DwmIpv4TcpHeader.hh
Go to the documentation of this file.
1 //===========================================================================
2 // @(#) $DwmPath: dwm/libDwm/tags/libDwm-0.6.0/include/DwmIpv4TcpHeader.hh 8421 $
3 // @(#) $Id: DwmIpv4TcpHeader.hh 8421 2016-04-21 04:55:55Z dwm $
4 //===========================================================================
5 // Copyright (c) Daniel W. McRobb 2007, 2016
6 // All rights reserved.
7 //
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions
10 // are met:
11 //
12 // 1. Redistributions of source code must retain the above copyright
13 // notice, this list of conditions and the following disclaimer.
14 // 2. Redistributions in binary form must reproduce the above copyright
15 // notice, this list of conditions and the following disclaimer in the
16 // documentation and/or other materials provided with the distribution.
17 // 3. The names of the authors and copyright holders may not be used to
18 // endorse or promote products derived from this software without
19 // specific prior written permission.
20 //
21 // IN NO EVENT SHALL DANIEL W. MCROBB BE LIABLE TO ANY PARTY FOR
22 // DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
23 // INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE,
24 // EVEN IF DANIEL W. MCROBB HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
25 // DAMAGE.
26 //
27 // THE SOFTWARE PROVIDED HEREIN IS ON AN "AS IS" BASIS, AND
28 // DANIEL W. MCROBB HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
29 // UPDATES, ENHANCEMENTS, OR MODIFICATIONS. DANIEL W. MCROBB MAKES NO
30 // REPRESENTATIONS AND EXTENDS NO WARRANTIES OF ANY KIND, EITHER
31 // IMPLIED OR EXPRESS, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
32 // WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE,
33 // OR THAT THE USE OF THIS SOFTWARE WILL NOT INFRINGE ANY PATENT,
34 // TRADEMARK OR OTHER RIGHTS.
35 //===========================================================================
36 
37 //---------------------------------------------------------------------------
40 //---------------------------------------------------------------------------
41 
42 #ifndef _DWMIPV4TCPHEADER_HH_
43 #define _DWMIPV4TCPHEADER_HH_
44 
45 extern "C" {
46  #include <sys/types.h>
47  #ifdef __linux__
48  #define __FAVOR_BSD 1
49  #endif
50  #include <netinet/tcp.h>
51  #include <inttypes.h>
52 }
53 
54 #include <cassert>
55 
56 #include "DwmIpv4PacketHeader.hh"
57 #include "DwmBZ2Readable.hh"
58 #include "DwmBZ2Writable.hh"
59 #include "DwmGZReadable.hh"
60 #include "DwmGZWritable.hh"
61 #include "DwmReadable.hh"
62 #include "DwmWritable.hh"
63 
64 namespace Dwm {
65 
66  //--------------------------------------------------------------------------
68  //--------------------------------------------------------------------------
70  : public Readable, public Writable,
71  public GZReadable, public GZWritable,
72  public BZ2Readable, public BZ2Writable
73  {
74  public:
75  //------------------------------------------------------------------------
77  //------------------------------------------------------------------------
78  Ipv4TcpHeader();
79 
80  //------------------------------------------------------------------------
84  //------------------------------------------------------------------------
85  Ipv4TcpHeader(const Ipv4PacketHeader & ipHeader);
86 
87  //------------------------------------------------------------------------
89  //------------------------------------------------------------------------
90  Ipv4TcpHeader(const Ipv4TcpHeader & tcpHeader);
91 
92  //------------------------------------------------------------------------
96  //------------------------------------------------------------------------
97  Ipv4TcpHeader(struct tcphdr *tcpHdr);
98 
99  //------------------------------------------------------------------------
101  //------------------------------------------------------------------------
102  Ipv4TcpHeader(const struct tcphdr & tcpHdr);
103 
104  //------------------------------------------------------------------------
106  //------------------------------------------------------------------------
107  Ipv4TcpHeader & operator = (const Ipv4TcpHeader & tcpHeader);
108 
109  //------------------------------------------------------------------------
111  //------------------------------------------------------------------------
112  ~Ipv4TcpHeader();
113 
114  //------------------------------------------------------------------------
116  //------------------------------------------------------------------------
117  uint16_t SourcePort() const;
118 
119  //------------------------------------------------------------------------
121  //------------------------------------------------------------------------
122  uint16_t SourcePort(uint16_t port);
123 
124  //------------------------------------------------------------------------
126  //------------------------------------------------------------------------
127  uint16_t DestinationPort() const;
128 
129  //------------------------------------------------------------------------
131  //------------------------------------------------------------------------
132  uint16_t DestinationPort(uint16_t port);
133 
134  //------------------------------------------------------------------------
136  //------------------------------------------------------------------------
137  uint32_t SequenceNumber() const;
138 
139  //------------------------------------------------------------------------
141  //------------------------------------------------------------------------
142  uint32_t SequenceNumber(uint32_t seq);
143 
144  //------------------------------------------------------------------------
146  //------------------------------------------------------------------------
147  uint32_t AckNumber() const;
148 
149  //------------------------------------------------------------------------
151  //------------------------------------------------------------------------
152  uint32_t AckNumber(uint32_t ack);
153 
154  //------------------------------------------------------------------------
160  //------------------------------------------------------------------------
161  uint8_t DataOffset() const;
162 
163  //------------------------------------------------------------------------
169  //------------------------------------------------------------------------
170  uint8_t DataOffset(uint8_t offset);
171 
172  //------------------------------------------------------------------------
174  //------------------------------------------------------------------------
175  uint8_t Flags() const;
176 
177  //------------------------------------------------------------------------
179  //------------------------------------------------------------------------
180  uint8_t Flags(uint8_t flags);
181 
182  //------------------------------------------------------------------------
184  //------------------------------------------------------------------------
185  uint16_t Window() const;
186 
187  //------------------------------------------------------------------------
189  //------------------------------------------------------------------------
190  uint16_t Window(uint16_t window);
191 
192  //------------------------------------------------------------------------
194  //------------------------------------------------------------------------
195  uint16_t Checksum() const;
196 
197  //------------------------------------------------------------------------
199  //------------------------------------------------------------------------
200  void SetChecksum(const Ipv4PacketHeader & ipHeader,
201  const std::string & data);
202 
203  //------------------------------------------------------------------------
205  //------------------------------------------------------------------------
206  uint16_t UrgentPointer() const;
207 
208  //------------------------------------------------------------------------
210  //------------------------------------------------------------------------
211  uint16_t UrgentPointer(uint16_t urgent);
212 
213  //------------------------------------------------------------------------
215  //------------------------------------------------------------------------
216  std::istream & Read(std::istream & is);
217 
218  //------------------------------------------------------------------------
220  //------------------------------------------------------------------------
221  std::ostream & Write(std::ostream & os) const;
222 
223  //------------------------------------------------------------------------
226  //------------------------------------------------------------------------
227  ssize_t Read(int fd);
228 
229  //------------------------------------------------------------------------
232  //------------------------------------------------------------------------
233  ssize_t Write(int fd) const;
234 
235  //------------------------------------------------------------------------
238  //------------------------------------------------------------------------
239  size_t Read(FILE *f);
240 
241  //------------------------------------------------------------------------
244  //------------------------------------------------------------------------
245  size_t Write(FILE *f) const;
246 
247  //------------------------------------------------------------------------
250  //------------------------------------------------------------------------
251  int Read(gzFile gzf);
252 
253  //------------------------------------------------------------------------
256  //------------------------------------------------------------------------
257  int Write(gzFile gzf) const;
258 
259  //------------------------------------------------------------------------
262  //------------------------------------------------------------------------
263  int BZRead(BZFILE *bzf);
264 
265  //------------------------------------------------------------------------
268  //------------------------------------------------------------------------
269  int BZWrite(BZFILE *bzf) const;
270 
271  //------------------------------------------------------------------------
274  //------------------------------------------------------------------------
275  uint32_t StreamedLength() const;
276 
277  //------------------------------------------------------------------------
279  //------------------------------------------------------------------------
280  bool operator == (const Ipv4TcpHeader & tcpHeader) const;
281 
282  //------------------------------------------------------------------------
285  //------------------------------------------------------------------------
286  std::ostream &
287  PrintXML(std::ostream & os, const std::string & pre = "") const;
288 
289  //------------------------------------------------------------------------
293  //------------------------------------------------------------------------
294  uint8_t *End() const;
295 
296  private:
297  typedef struct __attribute__((packed)) {
298  uint16_t sourcePort;
299  uint16_t destinationPort;
300  uint32_t sequenceNumber;
301  uint32_t ackNumber;
302 #if BYTE_ORDER == LITTLE_ENDIAN
303  uint8_t _x:4,
304  dataOffset:4;
305 #else
306  uint8_t dataOffset:4,
307  _x:4;
308 #endif
309  uint8_t flags;
310  uint16_t window;
311  uint16_t checksum;
312  uint16_t urgentPointer;
313  } Data;
314 
315  Data *_data;
316  uint8_t *_options;
317  bool _ownData;
318  bool _ownOptions;
319 
320  void FreeOptions();
321  void AllocateOptions(uint8_t len);
322  };
323 
324 
325 } // namespace Dwm
326 
327 #endif // _DWMIPV4TCPHEADER_HH_
328 
329 //---------------------------- emacs settings -----------------------------
330 // Local Variables:
331 // mode: C++/la
332 // tab-width: 2
333 // indent-tabs-mode: nil
334 // c-basic-offset: 2
335 // End:
336 //-------------------------------------------------------------------------
This class is a pure virtual class, defining an interface for classes that can write their contents t...
Definition: DwmGZWritable.hh:57
int BZWrite(BZFILE *bzf) const
Writes to the given BZFILE pointer bzf.
Dwm::Writable pure virtual class definition.
Dwm::BZ2Readable pure virtual class definition.
uint8_t * End() const
Returns the address of the end of the TCP header.
Dwm::GZReadable pure virtual class definition.
uint8_t Flags() const
Returns the flags.
Dwm::Readable pure virtual class definition.
Ipv4TcpHeader()
Constructor.
int BZRead(BZFILE *bzf)
Reads from the given BZFILE pointer bzf.
uint16_t UrgentPointer() const
Returns the urgent pointer.
uint16_t Checksum() const
Returns the checksum.
This class is a pure virtual class, defining an interface for classes that can read their contents fr...
Definition: DwmBZ2Readable.hh:57
Dwm::Ipv4PacketHeader class definition.
uint32_t AckNumber() const
Returns the ACK nuber.
std::istream & Read(std::istream &is)
Reads from an istream. Returns the istream.
uint8_t DataOffset() const
The number of 32 bit words in the TCP header.
uint32_t StreamedLength() const
Returns the number of bytes that would be written if one of the Write() members were called...
Dwm::BZ2Writable pure virtual class definition.
std::ostream & Write(std::ostream &os) const
Writes to an ostream. Returns the ostream.
uint32_t SequenceNumber() const
Returns the sequence number.
Definition: DwmBZ2IO.hh:67
This class is a pure virtual class, defining an interface for classes that can read their contents fr...
Definition: DwmGZReadable.hh:57
uint16_t DestinationPort() const
Returns the destination port.
This class defines an interface for classes that can write their contents to an ostream, file descriptor or FILE pointer.
Definition: DwmWritable.hh:58
This class is a pure virtual class, defining an interface for classes that can write their contents t...
Definition: DwmBZ2Writable.hh:57
Dwm::GZWritable pure virtual class definition.
This class defines an interface for classes that can read their contents from an istream, file descriptor or FILE pointer.
Definition: DwmReadable.hh:58
uint16_t SourcePort() const
Returns the source port.
bool operator==(const Ipv4TcpHeader &tcpHeader) const
operator ==
uint16_t Window() const
Returns the window.
Encapsulates a TCP header.
Definition: DwmIpv4TcpHeader.hh:69
std::ostream & PrintXML(std::ostream &os, const std::string &pre="") const
Prints the TCP header to an ostream as pseudo-XML.
Ipv4TcpHeader & operator=(const Ipv4TcpHeader &tcpHeader)
operator =
void SetChecksum(const Ipv4PacketHeader &ipHeader, const std::string &data)
Sets the checksum, for which we need the IP header and the data.
Encapsulates an IPv4 packet header.
Definition: DwmIpv4PacketHeader.hh:65
~Ipv4TcpHeader()
Destructor.