Пример #1
0
 private static byte[] UpdatePacketFromCache(DnsPacket packet, ushort newId)
 {
     packet.UpdateTtl();
     packet.UpdateTransactionId(newId);
     ConsolePainter.WriteResponse($"MESSAGE FROM CACHE:\n{packet}");
     return(packet.Data);
 }
Пример #2
0
        private byte[] GetAnswerFromBetterServer(byte[] query, Dictionary <string, DnsPacket> subCache)
        {
            using (var client = new UdpClient())
            {
                client.Client.ReceiveTimeout = 2000;
                client.Send(query, query.Length, remoteDns);
                byte[] response;
                try
                {
                    response = client.Receive(ref remoteDns);
                }
                catch (SocketException)
                {
                    ConsolePainter.WriteWarning("Couldn't connect to the upper server. Check internet connection");
                    return(null);
                }

                var responsePacket = new DnsPacket(response);
                ConsolePainter.WriteResponse($"SENDING:\n{responsePacket}");
                subCache[responsePacket.Questions[0].Name] = responsePacket;
                return(response);
            }
        }
Пример #3
0
        private bool TryModifyCache(DnsPacket query, out DnsPacket cached)
        {
            var question = query.Questions[0];

            if (cache[question.Type].TryGetValue(question.Name, out cached))
            {
                ConsolePainter.WriteResponse($"MESSAGE FROM CACHE:\n{cached}");
                return(true);
            }

            if (TryFindRightCachedRecord(query, out cached))
            {
                var gen = DnsPacket.GenerateAnswer(query.TransactionId, query.Questions,
                                                   question.Type == ResourceType.A ? cached.AdditionalRecords : cached.AuthoritiveServers);

                ConsolePainter.WriteResponse($"Modified from cache:\n{gen}");
                cached = gen;
                cache[query.Questions[0].Type].Add(query.Questions[0].Name, gen);
                return(true);
            }

            cached = null;
            return(false);
        }