| 173 | } |
| 174 | |
| 175 | uint16_t DNSClient::BuildRequest(const char* aName) |
| 176 | { |
| 177 | // Build header |
| 178 | // 1 1 1 1 1 1 |
| 179 | // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 |
| 180 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 181 | // | ID | |
| 182 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 183 | // |QR| Opcode |AA|TC|RD|RA| Z | RCODE | |
| 184 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 185 | // | QDCOUNT | |
| 186 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 187 | // | ANCOUNT | |
| 188 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 189 | // | NSCOUNT | |
| 190 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 191 | // | ARCOUNT | |
| 192 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 193 | // As we only support one request at a time at present, we can simplify |
| 194 | // some of this header |
| 195 | iRequestId = millis(); // generate a random ID |
| 196 | uint16_t twoByteBuffer; |
| 197 | |
| 198 | // FIXME We should also check that there's enough space available to write to, rather |
| 199 | // FIXME than assume there's enough space (as the code does at present) |
| 200 | iUdp.write((uint8_t*)&iRequestId, sizeof(iRequestId)); |
| 201 | |
| 202 | twoByteBuffer = htons(QUERY_FLAG | OPCODE_STANDARD_QUERY | RECURSION_DESIRED_FLAG); |
| 203 | iUdp.write((uint8_t*)&twoByteBuffer, sizeof(twoByteBuffer)); |
| 204 | |
| 205 | twoByteBuffer = htons(1); // One question record |
| 206 | iUdp.write((uint8_t*)&twoByteBuffer, sizeof(twoByteBuffer)); |
| 207 | |
| 208 | twoByteBuffer = 0; // Zero answer records |
| 209 | iUdp.write((uint8_t*)&twoByteBuffer, sizeof(twoByteBuffer)); |
| 210 | |
| 211 | iUdp.write((uint8_t*)&twoByteBuffer, sizeof(twoByteBuffer)); |
| 212 | // and zero additional records |
| 213 | iUdp.write((uint8_t*)&twoByteBuffer, sizeof(twoByteBuffer)); |
| 214 | |
| 215 | // Build question |
| 216 | const char* start =aName; |
| 217 | const char* end =start; |
| 218 | uint8_t len; |
| 219 | // Run through the name being requested |
| 220 | while (*end) |
| 221 | { |
| 222 | // Find out how long this section of the name is |
| 223 | end = start; |
| 224 | while (*end && (*end != '.') ) |
| 225 | { |
| 226 | end++; |
| 227 | } |
| 228 | |
| 229 | if (end-start > 0) |
| 230 | { |
| 231 | // Write out the size of this section |
| 232 | len = end-start; |