I have been attempting to get this fake DNS server to function within my Hamnet mesh network to point to my NTP server. For days I thought it was the NTP server failing to respond until I finally did some packet sniffing with WireShark. The problem appears to be that the return address for the NTP server from the DNS server is being truncated as it is the last part of the returned packet to a DNS request.

This is using an UNO R3 with an Arduino ethernet shield. When I debug print the responseBuffer after ending the UDP write, everything is there! So maybe someone has some idea why the sent responseBuffer truncates the IP address.
Thanks
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
// setting for EthernetShield
byte mac[] = {
0x74, 0xE5, 0x43, 0xE8, 0xCE, 0xFC};
byte ip[] = { // IP of DNS Server
10, 87, 28, 146};
byte resIp[] = { // IP response
10, 87, 28, 147};
unsigned int listenPort = 53;
IPAddress remoteIp;
unsigned int remotePort;
EthernetUDP Udp; // An Ethernet UDP instance
#define PACKET_MAX_SIZE 512
char requestBuffer[PACKET_MAX_SIZE];
char responseBuffer[PACKET_MAX_SIZE];
String readString;
void setup() {
Ethernet.begin(mac, ip);
Udp.begin(listenPort);
Serial.begin(19200);
}
void loop() {
int requestSize = Udp.parsePacket();
if(requestSize) {
Udp.read(requestBuffer, PACKET_MAX_SIZE);
remoteIp = Udp.remoteIP();
remotePort = Udp.remotePort();
int type = (requestBuffer[2] >> 3) & 15;
if(type == 0) { // Standard Query
readString = "";
int ini = 12;
int lon = requestBuffer[ini];
char domain[64];
int i = 0;
while(lon != 0) {
for(int j = 0; j < lon; j++) {
domain[i++] = requestBuffer[ini + j + 1];
readString += requestBuffer[ini + j + 1];
}
domain[i++] = '.';
ini += lon + 1;
lon = requestBuffer[ini];
}
domain[i] = '\0';
if(readString.indexOf('us pool ntp') > 0) { // NTP request exists
Serial.print(remoteIp);
Serial.print(" ");
Serial.println(domain);
int resIndex = 0;
for(int k = 0; k < 2; k++) { // identification
responseBuffer[resIndex++] = requestBuffer[k];
}
responseBuffer[resIndex++] = '\x81'; // response
// recursion desired
responseBuffer[resIndex++] = '\x80'; // recursive
// no error
for(int k = 4; k < 6; k++) { // question
responseBuffer[resIndex++] = requestBuffer[k];
}
for(int k = 4; k < 6; k++) { // answer
responseBuffer[resIndex++] = requestBuffer[k];
}
for(int k = 0; k < 4; k++) { // authority, addition
responseBuffer[resIndex++] = '\x00';
}
for(int k = 12; k < requestSize - 8; k++) { // question
responseBuffer[resIndex++] = requestBuffer[k];
}
responseBuffer[resIndex++] = '\xc0'; // Offset to domain name
responseBuffer[resIndex++] = '\x0c';
responseBuffer[resIndex++] = '\x00'; // type
responseBuffer[resIndex++] = '\x01';
responseBuffer[resIndex++] = '\x00'; // class
responseBuffer[resIndex++] = '\x01';
responseBuffer[resIndex++] = '\x00'; // ttl
responseBuffer[resIndex++] = '\x00';
responseBuffer[resIndex++] = '\x00';
responseBuffer[resIndex++] = '\x3c';
responseBuffer[resIndex++] = '\x00'; // ip length
responseBuffer[resIndex++] = '\x04';
responseBuffer[resIndex++] = resIp[0]; // ip
responseBuffer[resIndex++] = resIp[1];
responseBuffer[resIndex++] = resIp[2];
responseBuffer[resIndex++] = resIp[3];
responseBuffer[resIndex++] = '\x00'; // end
Udp.beginPacket(remoteIp, remotePort);
Udp.write((uint8_t *)responseBuffer, (uint8_t)(resIndex-1));
Udp.endPacket();
}
}
}
// delay(10);
}
