/// <summary> /// Write the record data into the given byte array and return the number of bytes written /// </summary> /// <param name="array">Array.</param> /// <param name="offset">Offset.</param> public override int WriteRecord(byte[] array, int offset) { offset--; int startingOffset = offset; // 2 bytes - TYPE offset += DnsEncoder.WriteUint16(array, (uint)this.Type, offset); // 2 bytes - CLASS We just use the default one here offset += DnsEncoder.WriteUint16(array, (uint)4096, offset); // 2 bytes - Extended TTL: EXTENDED-RCODE offset += DnsEncoder.WriteUint16(array, (uint)0, offset); // 2 bytes - Extended TTL: VERSION offset += DnsEncoder.WriteUint16(array, (uint)0, offset); // 2 bytes - RDLENGTH the length of the response data for this response // This is actually written later, after we write the data with a negative offset // X bytes - Response data var dataLength = this.WriteResponseData(array, offset + 2); // Write the RDLENGTH offset += DnsEncoder.WriteUint16(array, (uint)dataLength, offset); offset += dataLength; //offset += this.ResponseData.Length; return(offset - startingOffset); }
/// <summary> /// Write the response data /// </summary> /// <param name="array"></param> /// <param name="offset"></param> /// <returns></returns> protected override int WriteResponseData(byte[] array, int offset) { int bytesWritten = DnsEncoder.WriteUint16(array, this.Priority, offset); bytesWritten += DnsUtil.WriteHostnameLabel(array, offset + bytesWritten, this.Value); return(bytesWritten); }
/// <summary> /// Write the header data into the given array /// </summary> /// <returns>The header.</returns> /// <param name="array">Array.</param> /// <param name="offset">Offset.</param> public int WriteHeader(byte[] array, int offset) { // HEADER PART 1 // 2 bytes - Message ID array [offset++] = this.MessageId[0]; array [offset++] = this.MessageId[1]; // 1 byte - QR, OPCODE, AA, TC, RD, RA byte header1 = 0; // 1 bit header1 += (byte)((this.IsResponse ? 1 : 0) << 7); // Add QR - Query response bit, since this is a response, it's a 1 // 4 bits - Request type DnsEncoder.Save4BitNumeric(ref header1, (byte)this.RequestType, 1); // 1 bit, this is an athorative answer header1 += (byte)((this.IsAuthorativeMessage ? 1 : 0) << 2); // 1 bit, this message is not truncated header1 += (byte)((this.IsTruncated ? 1 : 0) << 1); // 1 bit, recursion not desired header1 += (byte)((this.RecursionDesired ? 1 : 0) << 0); array[offset++] = header1; // Write into the second byte // 1 bit, recursion not supported array[offset] = (byte)((this.RecursionSupported ? 1 : 0) << 0); // HEADER PART 2 // 3 bits - res1, res2, res3 // Skipped ~ // 4 bits RCODE - Response status code DnsEncoder.Save4BitNumeric(ref array[offset++], (byte)this.StatusCode, 0); // HEADER COUNTS // 2 bytes - question count DnsEncoder.WriteUint16(array, (uint)this.QuestionCount, offset); // 2 bytes - answer count DnsEncoder.WriteUint16(array, this.NormalAnswerCount, offset + 2); // 2 bytes - NS type answer count DnsEncoder.WriteUint16(array, this.NameServerAnswerCount, offset + 4); // 2 bytes - AR type answer count DnsEncoder.WriteUint16(array, this.AdditionalAnswerCount, offset + 6); // A header is always the same size return(DnsMessageHeader.SizeInBytes); }
/// <summary> /// Write the record data into the given byte array and return the number of bytes written /// </summary> /// <param name="array">Array.</param> /// <param name="offset">Offset.</param> public virtual int WriteRecord(byte[] array, int offset) { int startingOffset = offset; // 2 bytes - TYPE offset += DnsEncoder.WriteUint16(array, (uint)this.Type, offset); // 2 bytes - CLASS We just use the default one here offset += DnsEncoder.WriteUint16(array, (uint)1, offset); // 4 bytes - TTL offset += DnsEncoder.WriteInt32(array, this.TTL, offset); // 2 bytes - RDLENGTH the length of the response data for this response // This is actually written later, after we write the data with a negative offset // X bytes - Response data var dataLength = this.WriteResponseData(array, offset + 2); // Write the RDLENGTH offset += DnsEncoder.WriteUint16(array, (uint)dataLength, offset); offset += dataLength; return(offset - startingOffset); }