Skip to content

Instantly share code, notes, and snippets.

@laclefyoshi
Created January 7, 2011 14:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save laclefyoshi/769513 to your computer and use it in GitHub Desktop.
Save laclefyoshi/769513 to your computer and use it in GitHub Desktop.
DNS server with Arduino and EthernetShield
// -*- mode: c++ -*-
/**
Copyright: (c) SAEKI Yoshiyasu
License : MIT-style license
<http://www.opensource.org/licenses/mit-license.php>
last updated: 2011/01/12
**/
#include <SPI.h>
#include <Ethernet.h>
#include <Udp.h>
// setting for EthernetShield
byte mac[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05};
byte ip[] = {192, 168, 254, 100};
byte resIp[] = {192, 168, 0, 100};
unsigned int listenPort = 53;
byte remoteIp[4];
unsigned int remotePort;
#define PACKET_MAX_SIZE 512
char requestBuffer[PACKET_MAX_SIZE];
char responseBuffer[PACKET_MAX_SIZE];
void setup() {
Ethernet.begin(mac, ip);
Udp.begin(listenPort);
Serial.begin(9600);
}
void loop() {
int requestSize = Udp.available();
if(requestSize) {
Udp.readPacket(requestBuffer, PACKET_MAX_SIZE, remoteIp, remotePort);
int type = (requestBuffer[2] >> 3) & 15;
if(type == 0) { // nomal request
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];
}
domain[i++] = '.';
ini += lon + 1;
lon = requestBuffer[ini];
}
domain[i] = '\0';
Serial.println(domain);
if(domain[0] != '\0') { // request exists
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';
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.sendPacket((uint8_t *)responseBuffer, (uint16_t)(resIndex - 1),
remoteIp, remotePort);
}
}
}
// delay(10);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment