<commit_before>﻿using System;
using DnsClient.Protocol;

namespace DnsClient
{
    internal class DnsRequestHeader
    {
        public const int HeaderLength = 12;

        private ushort _flags = 0;
        
        public ushort RawFlags => _flags;
        
        internal DnsHeaderFlag HeaderFlags
        {
            get
            {
                return (DnsHeaderFlag)_flags;
            }
            set
            {
                _flags &= (ushort)~(DnsHeaderFlag.IsCheckingDisabled);
                _flags &= (ushort)~(DnsHeaderFlag.IsAuthenticData);
                _flags &= (ushort)~(DnsHeaderFlag.FutureUse);
                _flags &= (ushort)~(DnsHeaderFlag.HasQuery);
                _flags &= (ushort)~(DnsHeaderFlag.HasAuthorityAnswer);
                _flags &= (ushort)~(DnsHeaderFlag.ResultTruncated);
                _flags &= (ushort)~(DnsHeaderFlag.RecursionDesired);
                _flags &= (ushort)~(DnsHeaderFlag.RecursionAvailable);
                _flags |= (ushort)value;
            }
        }

        public int Id { get; set; }

        public DnsOpCode OpCode
        {
            get
            {
                return (DnsOpCode)((DnsHeader.OPCodeMask & _flags) >> DnsHeader.OPCodeShift);
            }
            set
            {
                _flags &= (ushort)~(DnsHeader.OPCodeMask);
                _flags |= (ushort)(((ushort)value << DnsHeader.OPCodeShift) & DnsHeader.OPCodeMask);
            }
        }
        
        public ushort RCode
        {
            get
            {
                return (ushort)(DnsHeader.RCodeMask & _flags);
            }
            set
            {
                _flags &= (ushort)~(DnsHeader.RCodeMask);
                _flags |= (ushort)(value & DnsHeader.RCodeMask);
            }
        }

        public bool UseRecursion
        {
            get { return (HeaderFlags | DnsHeaderFlag.RecursionDesired) != 0; }
            set
            {
                HeaderFlags |= DnsHeaderFlag.RecursionDesired;
            }
        }

        public DnsRequestHeader(int id, DnsOpCode queryKind)
            : this(id, true, queryKind)
        {
        }

        public DnsRequestHeader(int id, bool useRecursion, DnsOpCode queryKind)
        {
            Id = id;
            OpCode = queryKind;
            UseRecursion = useRecursion;            
        }

        public override string ToString()
        {
            return $"{Id} - Qs: {1} Recursion: {UseRecursion} OpCode: {OpCode}";
        }
    }
}<commit_msg>Fix for setting of recursion flag<commit_after>﻿using System;
using DnsClient.Protocol;

namespace DnsClient
{
    internal class DnsRequestHeader
    {
        public const int HeaderLength = 12;

        private ushort _flags = 0;

        public ushort RawFlags => _flags;

        internal DnsHeaderFlag HeaderFlags
        {
            get
            {
                return (DnsHeaderFlag)_flags;
            }
            set
            {
                _flags &= (ushort)~(DnsHeaderFlag.IsCheckingDisabled);
                _flags &= (ushort)~(DnsHeaderFlag.IsAuthenticData);
                _flags &= (ushort)~(DnsHeaderFlag.FutureUse);
                _flags &= (ushort)~(DnsHeaderFlag.HasQuery);
                _flags &= (ushort)~(DnsHeaderFlag.HasAuthorityAnswer);
                _flags &= (ushort)~(DnsHeaderFlag.ResultTruncated);
                _flags &= (ushort)~(DnsHeaderFlag.RecursionDesired);
                _flags &= (ushort)~(DnsHeaderFlag.RecursionAvailable);
                _flags |= (ushort)value;
            }
        }

        public int Id { get; set; }

        public DnsOpCode OpCode
        {
            get
            {
                return (DnsOpCode)((DnsHeader.OPCodeMask & _flags) >> DnsHeader.OPCodeShift);
            }
            set
            {
                _flags &= (ushort)~(DnsHeader.OPCodeMask);
                _flags |= (ushort)(((ushort)value << DnsHeader.OPCodeShift) & DnsHeader.OPCodeMask);
            }
        }

        public ushort RCode
        {
            get
            {
                return (ushort)(DnsHeader.RCodeMask & _flags);
            }
            set
            {
                _flags &= (ushort)~(DnsHeader.RCodeMask);
                _flags |= (ushort)(value & DnsHeader.RCodeMask);
            }
        }

        public bool UseRecursion
        {
            get
            {
                return HeaderFlags.HasFlag(DnsHeaderFlag.RecursionDesired);
            }
            set
            {
                if (value)
                {
                    if (!HeaderFlags.HasFlag(DnsHeaderFlag.RecursionDesired))
                    {
                        HeaderFlags |= DnsHeaderFlag.RecursionDesired;
                    }
                }
                else
                {
                    if (HeaderFlags.HasFlag(DnsHeaderFlag.RecursionDesired))
                    {
                        HeaderFlags |= DnsHeaderFlag.RecursionDesired;
                    }
                }
            }
        }

        public DnsRequestHeader(int id, DnsOpCode queryKind)
            : this(id, true, queryKind)
        {
        }

        public DnsRequestHeader(int id, bool useRecursion, DnsOpCode queryKind)
        {
            Id = id;
            OpCode = queryKind;
            UseRecursion = useRecursion;
        }

        public override string ToString()
        {
            return $"{Id} - Qs: {1} Recursion: {UseRecursion} OpCode: {OpCode}";
        }
    }
}
{'fraction_non_alphanumeric': 0.09154929577464789, 'fraction_numerical': 0.0016251354279523294, 'mean_word_length': 1.302161263507897, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4020327', 'n_tokens_mistral': 1606, 'n_tokens_neox': 1511, 'n_words': 260}