DwmDns-0.1.0
DwmDnsMessage.hh
Go to the documentation of this file.
1 //===========================================================================
2 // @(#) $DwmPath: dwm/DwmDns/tags/DwmDns-0.1.0/classes/include/DwmDnsMessage.hh 10136 $
3 // @(#) $Id: DwmDnsMessage.hh 10136 2018-01-28 06:01:09Z dwm $
4 //===========================================================================
5 // Copyright (c) Daniel W. McRobb 2000, 2016, 2018
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 _DWMDNSMESSAGE_HH_
43 #define _DWMDNSMESSAGE_HH_
44 
45 extern "C" {
46  #include <unistd.h>
47 }
48 
49 #include <cstdint>
50 #include <vector>
51 
52 #include "DwmDnsMessageHeader.hh"
53 #include "DwmDnsMessageQuestion.hh"
54 #include "DwmDnsResourceRecord.hh"
55 
56 namespace Dwm {
57 
58  namespace Dns {
59 
60  //------------------------------------------------------------------------
62  //------------------------------------------------------------------------
63 
64  //------------------------------------------------------------------------
67  //------------------------------------------------------------------------
68  class Message
69  {
70  public:
71  //----------------------------------------------------------------------
73  //----------------------------------------------------------------------
74  Message();
75 
76  //----------------------------------------------------------------------
78  //----------------------------------------------------------------------
79  bool operator == (const Message & message) const;
80 
81  //----------------------------------------------------------------------
83  //----------------------------------------------------------------------
84  void Clear();
85 
86  //----------------------------------------------------------------------
88  //----------------------------------------------------------------------
89  const MessageHeader & Header() const
90  {
91  return _header;
92  }
93 
94  //----------------------------------------------------------------------
96  //----------------------------------------------------------------------
98  {
99  return _header;
100  }
101 
102  //----------------------------------------------------------------------
104  //----------------------------------------------------------------------
105  const std::vector<MessageQuestion> & Questions() const
106  {
107  return _questions;
108  }
109 
110  //----------------------------------------------------------------------
112  //----------------------------------------------------------------------
113  std::vector<MessageQuestion> & Questions()
114  {
115  return _questions;
116  }
117 
118  //----------------------------------------------------------------------
121  //----------------------------------------------------------------------
122  const std::vector<ResourceRecord> & Answers() const
123  {
124  return _answers;
125  }
126 
127  //----------------------------------------------------------------------
130  //----------------------------------------------------------------------
131  std::vector<ResourceRecord> & Answers()
132  {
133  return _answers;
134  }
135 
136  //----------------------------------------------------------------------
139  //----------------------------------------------------------------------
140  const std::vector<ResourceRecord> & Authorities() const
141  {
142  return _authorities;
143  }
144 
145  //----------------------------------------------------------------------
148  //----------------------------------------------------------------------
149  std::vector<ResourceRecord> & Authorities()
150  {
151  return _authorities;
152  }
153 
154  //----------------------------------------------------------------------
157  //----------------------------------------------------------------------
158  const std::vector<ResourceRecord> & Additional() const
159  {
160  return _additional;
161  }
162 
163  //----------------------------------------------------------------------
166  //----------------------------------------------------------------------
167  std::vector<ResourceRecord> & Additional()
168  {
169  return _additional;
170  }
171 
172  //----------------------------------------------------------------------
177  //----------------------------------------------------------------------
178  uint8_t *Encode(uint8_t *pkt, uint8_t *ptr, uint16_t pktlen) const;
179 
180  //----------------------------------------------------------------------
186  //----------------------------------------------------------------------
187  const uint8_t *Decode(const uint8_t *pkt, const uint8_t *ptr,
188  uint16_t pktlen);
189 
190  //----------------------------------------------------------------------
195  //----------------------------------------------------------------------
196  ssize_t SendTo(int sockFd, int flags, const struct sockaddr *to,
197  int tolen) const;
198 
199  //----------------------------------------------------------------------
204  //----------------------------------------------------------------------
205  ssize_t Write(int fd) const;
206 
207  //----------------------------------------------------------------------
214  //----------------------------------------------------------------------
215  ssize_t RecvFrom(int sockFd, int flags, sockaddr *from,
216  socklen_t *fromlen);
217 
218  //----------------------------------------------------------------------
223  //----------------------------------------------------------------------
224  ssize_t Read(int fd);
225 
226  //----------------------------------------------------------------------
229  //----------------------------------------------------------------------
230  void EnableEDNS(uint16_t pduSize, bool doDnssec);
231 
232  private:
233  mutable MessageHeader _header;
234  std::vector<MessageQuestion> _questions;
235  std::vector<ResourceRecord> _answers;
236  std::vector<ResourceRecord> _authorities;
237  std::vector<ResourceRecord> _additional;
238  };
239 
240  } // namespace Dns
241 
242 } // namespace Dwm
243 
244 #endif // _DWMDNSMESSAGE_HH_
std::vector< ResourceRecord > & Answers()
Returns a mutable reference to the records from the answers section of the message.
Definition: DwmDnsMessage.hh:131
ssize_t SendTo(int sockFd, int flags, const struct sockaddr *to, int tolen) const
A wrapper for sendto().
void Clear()
Clears the message contents.
Dwm::Dns::MessageHeader class definition.
ssize_t Read(int fd)
Reads the Message from a connected TCP socket fd.
const std::vector< ResourceRecord > & Answers() const
Returns a const reference to the records from the answers section of the message. ...
Definition: DwmDnsMessage.hh:122
ssize_t RecvFrom(int sockFd, int flags, sockaddr *from, socklen_t *fromlen)
A wrapper for recvfrom().
const uint8_t * Decode(const uint8_t *pkt, const uint8_t *ptr, uint16_t pktlen)
Decodes the Message in pkt of length pktlen, starting at ptr.
const std::vector< MessageQuestion > & Questions() const
Returns a const reference to the questions in the message.
Definition: DwmDnsMessage.hh:105
Encapsulates a DNS message.
Definition: DwmDnsMessage.hh:68
void EnableEDNS(uint16_t pduSize, bool doDnssec)
Enable EDNS by setting the maximum packet size to pduSize and enable or disable DNSSEC.
ssize_t Write(int fd) const
Writes the message to a connected TCP socket fd.
Dwm::Dns::ResourceRecord class definition.
MessageHeader & Header()
Returns a mutable reference to the message header.
Definition: DwmDnsMessage.hh:97
Message()
Constructor.
std::vector< MessageQuestion > & Questions()
Returns a mutable reference to the questions in the message.
Definition: DwmDnsMessage.hh:113
std::vector< ResourceRecord > & Additional()
Returns a mutable reference to the records from the additional section of the message.
Definition: DwmDnsMessage.hh:167
Definition: DwmDnsEtcHosts.hh:60
const std::vector< ResourceRecord > & Additional() const
Returns a const reference to the records from the additional section of the message.
Definition: DwmDnsMessage.hh:158
Encapsulates a DNS message header.
Definition: DwmDnsMessageHeader.hh:55
const MessageHeader & Header() const
Returns a const reference to the message header.
Definition: DwmDnsMessage.hh:89
std::vector< ResourceRecord > & Authorities()
Returns a mutable reference to the records from the authority section of the message.
Definition: DwmDnsMessage.hh:149
const std::vector< ResourceRecord > & Authorities() const
Returns a const reference to the records from the authority section of the message.
Definition: DwmDnsMessage.hh:140
bool operator==(const Message &message) const
Equal-to operator, mostly useful for unit tests.
uint8_t * Encode(uint8_t *pkt, uint8_t *ptr, uint16_t pktlen) const
Encode the Message in pkt of length pktlen, starting at ptr.
Dwm::Dns::MessageQuestion class definition.