/**
* <summary>Creates a response from the parameter fields with RData being
* a memory chunk. This is for MDns which supports caching</summary>
* <param name="Name">The name resolved.</param>
* <param name="Type">The query type.</param>
* <param name="Class">The network type.</param>
* <param name="CacheFlush">Flush the dns cache in the client.</param>
* <param name="Ttl">How long to hold the result in the local dns cache.</param>
* <param name="RData">RData in String format.</param>
*/
public Response(string name, DnsPacket.Types type, DnsPacket.Classes class_type,
bool cache_flush, int ttl, String rdata)
{
Name = name;
Class = class_type;
Ttl = ttl;
Type = type;
CacheFlush = cache_flush;
RData = rdata;
if (Type == DnsPacket.Types.A || Type == DnsPacket.Types.AAAA)
{
NameBlob = DnsPacket.HostnameStringToMemBlock(Name);
RDataBlob = DnsPacket.IPStringToMemBlock(RData);
}
else if (Type == DnsPacket.Types.Ptr)
{
if (DnsPacket.StringIsIP(Name))
{
NameBlob = DnsPacket.PtrStringToMemBlock(Name);
}
else
{
NameBlob = DnsPacket.HostnameStringToMemBlock(Name);
}
RDataBlob = DnsPacket.HostnameStringToMemBlock(RData);
}
else
{
throw new Exception("Invalid Query Type: " + Type + "!");
}
RdLength = (short)RDataBlob.Length;
// 2 for Type + 2 for Class + 4 for Ttl + 2 for RdLength
byte[] data = new byte[10];
int idx = 0;
data[idx++] = (byte)((((int)Type) >> 8) & 0xFF);
data[idx++] = (byte)(((int)Type) & 0xFF);
byte cf = 0x80;
if (!CacheFlush)
{
cf = 0x00;
}
data[idx++] = (byte)(((((int)Class) >> 8) & 0x7F) | cf);
data[idx++] = (byte)(((int)Class) & 0xFF);
data[idx++] = (byte)((Ttl >> 24) & 0xFF);
data[idx++] = (byte)((Ttl >> 16) & 0xFF);
data[idx++] = (byte)((Ttl >> 8) & 0xFF);
data[idx++] = (byte)(Ttl & 0xFF);
data[idx++] = (byte)((RdLength >> 8) & 0xFF);
data[idx] = (byte)(RdLength & 0xFF);
_icpacket = new CopyList(NameBlob, MemBlock.Reference(data), RDataBlob);
}