COSA
An Object-Oriented Platform for Arduino Programming
DNS.hh
Go to the documentation of this file.
1 
21 #ifndef COSA_INET_DNS_HH
22 #define COSA_INET_DNS_HH
23 
24 #include "Cosa/Types.h"
25 #include "Cosa/Socket.hh"
26 
31 class DNS {
32 public:
34  static const uint16_t PORT = 53;
35 
40  DNS() {}
41 
48  DNS(Socket* sock, uint8_t server[4])
49  {
50  begin(sock, server);
51  }
52 
56  ~DNS()
57  {
58  end();
59  }
60 
68  bool begin(Socket* sock, uint8_t server[4]);
69 
74  bool end();
75 
83  int gethostbyname(const char* hostname, uint8_t ip[4])
84  __attribute__((always_inline))
85  {
86  return (gethostbyname(hostname, ip, false));
87  }
88 
96  int gethostbyname_P(str_P hostname, uint8_t ip[4])
97  __attribute__((always_inline))
98  {
99  return (gethostbyname((const char*) hostname, ip, true));
100  }
101 
102 private:
106  enum {
107  QUERY_FLAG = (0),
108  RESPONSE_FLAG = (1<<15),
109  QUERY_RESPONSE_MASK = (1<<15),
110  OPCODE_STANDARD_QUERY = (0),
111  OPCODE_INVERSE_QUERY = (1<<11),
112  OPCODE_STATUS_REQUEST = (2<<11),
113  OPCODE_MASK = (15<<11),
114  AUTHORITATIVE_FLAG = (1<<10),
115  TRUNCATION_FLAG = (1<<9),
116  RECURSION_DESIRED_FLAG = (1<<8),
117  RECURSION_AVAILABLE_FLAG = (1<<7),
118  RESP_NO_ERROR = (0),
119  RESP_FORMAT_ERROR = (1),
120  RESP_SERVER_FAILURE = (2),
121  RESP_NAME_ERROR = (3),
122  RESP_NOT_IMPLEMENTED = (4),
123  RESP_REFUSED = (5),
124  RESP_MASK = (15),
125  TYPE_A = (0x0001),
126  CLASS_IN = (0x0001),
127  LABEL_COMPRESSION_MASK = (0xC0)
128  };
129 
131  struct header_t {
132  uint16_t ID;
133  uint16_t FC;
134  uint16_t QC;
135  uint16_t ANC;
136  uint16_t NSC;
137  uint16_t ARC;
138  };
139 
143  struct attr_t {
144  uint16_t TYPE;
145  uint16_t CLASS;
146  };
147 
151  struct rec_t {
152  uint16_t TYPE;
153  uint16_t CLASS;
154  uint32_t TTL;
155  uint16_t RDL;
156  uint8_t RD[];
157  };
158 
159  static const uint16_t TIMEOUT = 300;
160  static const uint8_t RETRY_MAX = 8;
161  static const uint16_t ID = 0xC05AU;
162  uint8_t m_server[4];
163  Socket* m_sock;
164 
173  int gethostbyname(const char* hostname, uint8_t ip[4], bool progmem);
174 };
175 #endif
Definition: Socket.hh:31
Definition: DNS.hh:31
bool begin(Socket *sock, uint8_t server[4])
Definition: DNS.cpp:26
DNS()
Definition: DNS.hh:40
DNS(Socket *sock, uint8_t server[4])
Definition: DNS.hh:48
~DNS()
Definition: DNS.hh:56
const class prog_str * str_P
Definition: Types.h:187
int gethostbyname_P(str_P hostname, uint8_t ip[4])
Definition: DNS.hh:96
static const uint16_t PORT
Definition: DNS.hh:34
bool end()
Definition: DNS.cpp:34
int gethostbyname(const char *hostname, uint8_t ip[4])
Definition: DNS.hh:83