资源简介
有简单的服务器和客户端的默认实现
还有一个简单的DNS代理服务器(有回调函数可以自己加缓存)
支持所有DNS协议报文格式
代码片段和文件信息
using Protocol.DomainNameSystem;
using Protocol.DomainNameSystem.Simple;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace ConsoleApplication1
{
class Program
{
static DnsPacket MyProcess(DnsPacket request)
{
if (request._3_4_OPCODE != DnsPacket.OPCODE_QUERY)
{
return null;
}
for (int i = 0; i < request.QDList.Count; i++)
{
if (request.QDList[i]._2_2_TYPE == DnsPacket.TYPE_A && request.QDList[i]._3_2_CLASS == DnsPacket.CLASS_IN &&
request.QDList[i].ResolveName(request).ToLower().EndsWith(“.user.adsl001.com“))
{
/*
* 根据域名查找IP。
*/
string domian = request.QDList[i].ResolveName(request);
UInt32 ttl = 10; // 域名对应的IP失效时间,单位为秒,超过此时间后客户端应该重新发起查询。
string ip = “192.168.0.25“; // IP等会填到这里
// 你来写这一段,通过域名查找IP,从缓存里面去读,没有时间给你查数据库的。
/*
* 生成应答数据包。
*/
DnsPacket response = new DnsPacket();
response._1_16_ID = request._1_16_ID;
response._2_1_QR = DnsPacket.QR_REPLY;
response._3_4_OPCODE = DnsPacket.OPCODE_QUERY;
response._4_1_AA = DnsPacket.AA_NON_AUTHORITATIVE_ANSWER;
response._5_1_TC = DnsPacket.TC_NON_TRUNCATION;
response._6_1_RD = DnsPacket.RD_NON_RECURSION_DESIRED;
response._7_1_RA = DnsPacket.RA_NON_RECURSION_AVAILABLE;
response._8_3_Z = 0;
response._9_4_RCODE = DnsPacket.RCODE_NO_ERROR;
DnsQuestion question = new DnsQuestion();
question._1_N_NAME = request.ResolveNameBytes(request.QDList[i]._1_N_NAME);
question._2_2_TYPE = request.QDList[i]._2_2_TYPE;
question._3_2_CLASS = request.QDList[i]._3_2_CLASS;
response.QDList.Add(question);
response._10_16_QDCOUNT = (UInt16)response.QDList.Count;
DnsResourceRecord record = new DnsResourceRecord();
record._1_N_NAME = request.ResolveNameBytes(request.QDList[i]._1_N_NAME);
record._2_2_TYPE = DnsPacket.TYPE_A;
record._3_2_CLASS = DnsPacket.CLASS_IN;
record._4_4_TTL = ttl;
record._6_N_RDDATA = DnsHelper.FromIPString(ip);
record._5_2_RDLENGTH = (UInt16)record._6_N_RDDATA.Length;
response.ANList.Add(record);
response._11_16_ANCOUNT = (UInt16)response.ANList.Count;
return response;
}
}
评论
共有 条评论