DwmDns-0.1.0
DwmDnsNameServer.hh
Go to the documentation of this file.
1 //===========================================================================
2 // @(#) $DwmPath: dwm/DwmDns/tags/DwmDns-0.1.0/classes/include/DwmDnsNameServer.hh 10141 $
3 // @(#) $Id: DwmDnsNameServer.hh 10141 2018-01-28 19:10:16Z dwm $
4 //===========================================================================
5 // Copyright (c) Daniel W. McRobb 2017, 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 _DNSNAMESERVER_HH_
43 #define _DNSNAMESERVER_HH_
44 
45 extern "C" {
46  #include <sys/socket.h>
47  #include <sys/select.h>
48  #include <netinet/in.h>
49  #include <unistd.h>
50 }
51 
52 #include <string>
53 #include <tuple>
54 #include <vector>
55 
56 #include "DwmDnsMessage.hh"
57 
58 namespace Dwm {
59 
60  namespace Dns {
61 
62  //------------------------------------------------------------------------
64  //------------------------------------------------------------------------
65  class NameServer
66  {
67  public:
68  //----------------------------------------------------------------------
70  //----------------------------------------------------------------------
71  NameServer(const struct in_addr & addr);
72 
73  //----------------------------------------------------------------------
75  //----------------------------------------------------------------------
76  NameServer(const struct in6_addr & addr);
77 
78  //----------------------------------------------------------------------
82  //----------------------------------------------------------------------
83  NameServer(const std::string & addr);
84 
85  //----------------------------------------------------------------------
87  //----------------------------------------------------------------------
88  NameServer(const NameServer &) = default;
89 
90  //----------------------------------------------------------------------
92  //----------------------------------------------------------------------
93  NameServer & operator = (const NameServer &) = default;
94 
95  //----------------------------------------------------------------------
97  //----------------------------------------------------------------------
98  ~NameServer();
99 
100  typedef std::tuple<sa_family_t,const struct sockaddr *,socklen_t> AddrInfo;
101 
102  //----------------------------------------------------------------------
104  //----------------------------------------------------------------------
105  AddrInfo Address() const;
106 
107  //----------------------------------------------------------------------
110  //----------------------------------------------------------------------
111  bool SendMessage(const Message & message);
112 
113  //----------------------------------------------------------------------
116  //----------------------------------------------------------------------
117  bool WriteMessage(const Message & message);
118 
119  //----------------------------------------------------------------------
122  //----------------------------------------------------------------------
123  bool ReceiveMessage(Message & message);
124 
125  //----------------------------------------------------------------------
128  //----------------------------------------------------------------------
129  bool ReadMessage(Message & message);
130 
131  //----------------------------------------------------------------------
135  //----------------------------------------------------------------------
136  bool AnswerWaiting(const struct timeval & timeout) const;
137 
138  private:
139  sa_family_t _addrFamily;
140  union {
141  struct sockaddr_in in4Addr;
142  struct sockaddr_in6 in6Addr;
143  } _addr;
144  int _udpfd;
145  int _tcpfd;
146 
147  bool Open(bool tcp = false);
148  };
149 
150  } // namespace Dns
151 
152 } // namespace Dwm
153 
154 #endif // _DNSNAMESERVER_HH_
AddrInfo Address() const
Returns the address of the name server.
bool SendMessage(const Message &message)
Sends the given message to the name server via UDP.
bool ReadMessage(Message &message)
Reads a message from the name server via TCP.
Encapsulates a DNS message.
Definition: DwmDnsMessage.hh:68
bool WriteMessage(const Message &message)
Sends the given message to the name server via TCP.
Definition: DwmDnsEtcHosts.hh:60
NameServer(const struct in_addr &addr)
Constructs a name server whose IPv4 address is addr.
bool ReceiveMessage(Message &message)
Receives a message from the name server via UDP.
NameServer & operator=(const NameServer &)=default
Copy operator.
Encapsulates a DNS name server, from the perspective of a DNS client.
Definition: DwmDnsNameServer.hh:65
bool AnswerWaiting(const struct timeval &timeout) const
Returns true if there is a UDP message ready to be read via ReceiveMessage().
Dwm::Dns::Message class definition.
~NameServer()
Destructor.