Skip to content

Instantly share code, notes, and snippets.

@koike koike/dnsd.cs
Last active Dec 14, 2015

Embed
What would you like to do?
inaz2さんのdnsd.py(https://gist.github.com/inaz2/74ee3af6842b7724a827 )をC#でテキトーに書き直したもの
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
namespace dnsd
{
class Program
{
static void Main(string[] args)
{
var ip = "127.0.0.1";
var port = 53;
var local = IPAddress.Parse(ip);
var ep = new IPEndPoint(local, port);
var udp = new UdpClient(ep);
while (true)
{
IPEndPoint remote = null;
var rcv = udp.Receive(ref remote);
Console.WriteLine($"Address:{remote.Address}, Port:{remote.Port}");
Console.WriteLine($"Received.Data => {BitConverter.ToString(rcv).Replace("-", " ")}");
var res = new List<byte>();
//Transaction ID
res.Add(rcv[0]);
res.Add(rcv[1]);
//Flags
//0x8180 is Reply Code: No error (0)
res.Add(0x81);
res.Add(0x80);
//Questions: 1
res.Add(0x00);
res.Add(0x01);
//Answer RRs: 1
res.Add(0x00);
res.Add(0x01);
//Authority RRs: 0
res.Add(0x00);
res.Add(0x00);
//Aditional RRs: 0
res.Add(0x00);
res.Add(0x00);
for (var i = 12; i < rcv.Length - 4; i++)
{
res.Add(rcv[i]);
}
//Type: A => 1
res.Add(0x00);
res.Add(0x01);
//Class: IN => 1
res.Add(0x00);
res.Add(0x01);
//Name: 固定値?
res.Add(0xc0);
res.Add(0x0c);
//Type: A => 1
res.Add(0x00);
res.Add(0x01);
//Class: IN => 1
res.Add(0x00);
res.Add(0x01);
//Time to live: 86400
res.Add(0x00);
res.Add(0x01);
res.Add(0x51);
res.Add(0x80);
//Data length: 4
res.Add(0x00);
res.Add(0x04);
//Address: 127.0.0.1
res.Add(0x7f);
res.Add(0x00);
res.Add(0x00);
res.Add(0x01);
Console.WriteLine($"Response.Data => {BitConverter.ToString(res.ToArray()).Replace("-", " ")}");
udp.Send(res.ToArray(), res.Count, remote.Address.ToString(), remote.Port);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.