示例#1
0
            static void FormatSection(IReadOnlyList <DnsResourceRecord>?secton, ref DnsFormatContext context)
            {
                if (secton is null)
                {
                    return;
                }

                foreach (var record in secton)
                {
                    context.WriteDomainName(record.Name ?? throw new InvalidOperationException("RR must have name"));
                    context.WriteUInt16((ushort)record.Type);
                    context.WriteUInt16((ushort)record.EndpointClass);
                    context.WriteInt32(record.AliveSeconds);
                    var lengthSpan = context.AvailableSpan;
                    context.BytesWritten += 2;
                    int dataLength = record.WriteData(ref context);
                    BinaryPrimitives.WriteUInt16BigEndian(lengthSpan, checked ((ushort)dataLength));
                }
            }
示例#2
0
        public static int FormatMessage(DnsMessage message, Span <byte> destination, bool enableNameCompression = false)
        {
            var context = new DnsFormatContext(destination, enableNameCompression);

            ushort flags = 0;

            if (message.IsResponse)
            {
                flags |= ResponseFlag;
            }
            flags |= (ushort)((ushort)message.Operation << OpCodeShift);
            if (message.IsAuthoritativeAnswer)
            {
                flags |= AuthoritativeMask;
            }
            //if (message.IsTruncated)
            //    flags |= TruncationMask;
            if (message.IsRecursionDesired)
            {
                flags |= RecursionDesiredMask;
            }
            if (message.IsRecursionAvailable)
            {
                flags |= RecursionAvailableMask;
            }
            flags |= (ushort)message.ResponseCode;

            context.WriteUInt16(message.QueryId);
            context.WriteUInt16(flags);
            context.WriteUInt16(checked ((ushort)message.Queries.Count));
            context.WriteUInt16(checked ((ushort)(message.Answers?.Count ?? 0)));
            context.WriteUInt16(checked ((ushort)(message.NameServerAuthorities?.Count ?? 0)));
            context.WriteUInt16(checked ((ushort)(message.AdditionalRecords?.Count ?? 0)));