コード例 #1
0
ファイル: DNSClient.cs プロジェクト: qcjxberin/DC.Framework
        private void Client_Received(Object sender, ReceivedEventArgs e)
        {
            var pk = e.Packet;

            if (pk.Total == 0)
            {
                return;
            }

            var dns = DNSEntity.Read(pk.GetStream(), Client.Local.IsTcp);

            OnReceive(dns);
        }
コード例 #2
0
        /// <summary>接收处理</summary>
        /// <param name="session"></param>
        /// <param name="pk"></param>
        protected override void OnReceive(INetSession session, Packet pk)
        {
            var isTcp = session.Session.Local.IsTcp;

            // 解析
            var request = DNSEntity.Read(pk.GetStream(), isTcp);

            var response = Request(session, request);

            if (response != null)
            {
                response.Header.ID = request.Header.ID;
                Response(session, request, response);
            }

            session.Dispose();
        }
コード例 #3
0
        internal DNSEntity Invoke(DNSEntity request, Boolean isQuery)
        {
            var attempts = 0;

            while (attempts <= _maxRetryAttemps)
            {
                var bytes = request.GetStream(false).ReadBytes();

                if (bytes.Length > 512)
                {
                    throw new ArgumentException("RFC 1035 2.3.4 states that the maximum size of a UDP datagram is 512 octets (bytes).");
                }

                Socket socket = null;

                try
                {
                    socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
                    {
                        ReceiveTimeout = 300
                    };
                    //socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 300);

                    socket.SendTo(bytes, bytes.Length, SocketFlags.None, new IPEndPoint(IPAddress.Parse("192.168.178.255"), 137));

                    if (!isQuery)
                    {
                        return(null);
                    }

                    // Messages carried by UDP are restricted to 512 bytes (not counting the IP
                    // or UDP headers).  Longer messages are truncated and the TC bit is set in
                    // the header. (RFC 1035 4.2.1)
                    var responseMessage = new Byte[512];

                    //int numBytes = socket.Receive(responseMessage);

                    var ep       = (EndPoint) new IPEndPoint(new IPAddress(4294967295), 137);
                    var numBytes = socket.ReceiveFrom(responseMessage, ref ep);

                    if (numBytes == 0 || numBytes > 512)
                    {
                        throw new Exception("RFC 1035 2.3.4 states that the maximum size of a UDP datagram is 512 octets (bytes).");
                    }

                    //DnsReader br = new DnsReader(responseMessage);
                    //DnsResponse res = new DnsResponse(br);
                    var rs = DNSEntity.Read(responseMessage, false);

                    if (request.Header.ID == rs.Header.ID)
                    {
                        return(rs);
                    }

                    attempts++;
                }
                catch
                {
                    attempts++;
                }
                finally
                {
                    socket.Close();
                    socket = null;
                }
            }

            throw new Exception("Could not resolve the query (" + attempts + " attempts).");
        }