<commit_before>﻿#if !NOSOCKET
using NBitcoin.Crypto;
using NBitcoin.Protocol.Behaviors;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using NBitcoin.Logging;

namespace NBitcoin.Protocol
{

	/// <summary>
	/// The AddressManager, keep a set of peers discovered on the network in cache can update their actual states.
	/// Replicate AddressManager of Bitcoin Core, the Buckets and BucketPosition are not guaranteed to be coherent with Bitcoin Core
	/// </summary>
	public class AddressManager : IBitcoinSerializable
	{
		/// <summary>
		/// Will properly convert a endpoint to IPEndpoint
		/// If endpoint is a DNSEndpoint, a DNS resolution will be made and all addresses added
		/// If endpoint is a DNSEndpoint for onion, it will be converted into onioncat address
		/// If endpoint is an IPEndpoint it is added to AddressManager
		/// </summary>
		/// <param name="endpoint">The endpoint to add to the address manager</param>
		/// <param name="source">The source which advertized this endpoint (default: IPAddress.Loopback)</param>
		/// <returns></returns>
		public async Task AddAsync(EndPoint endpoint, IPAddress source = null)
		{
			if (endpoint == null)
				throw new ArgumentNullException(nameof(endpoint));
			if (source == null)
				source = IPAddress.Loopback;
			if (endpoint is IPEndPoint ip)
			{
				Add(new NetworkAddress(ip), IPAddress.Loopback);
			}
			else if (endpoint.AsOnionCatIPEndpoint() is IPEndPoint ip2)
			{
				Add(new NetworkAddress(ip2), IPAddress.Loopback);
			}
			else if (endpoint.AsOnionDNSEndpoint() is DnsEndPoint dns)
			{
				var ips = await Dns.GetHostAddressesAsync(dns.Host);
				foreach (var ip3 in ips)
				{
					Add(new NetworkAddress(new IPEndPoint(ip3, dns.Port)), IPAddress.Loopback);
				}
			}
			else
				throw new NotSupportedException(endpoint.ToString());
		}
		internal class AddressInfo : IBitcoinSerializable
		{
			#region IBitcoinSerializable Members

			public void ReadWrite(BitcoinStream stream)
			{
				stream.ReadWrite(ref _Address);
				stream.ReadWrite(ref source);
				stream.ReadWrite(ref nLastSuccess);
				stream.ReadWrite(ref nAttempts);

			}

			internal int nAttempts;
			internal long nLastSuccess;<commit_msg>Fix AddAsync on dns endpoints<commit_after>﻿#if !NOSOCKET
using NBitcoin.Crypto;
using NBitcoin.Protocol.Behaviors;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using NBitcoin.Logging;

namespace NBitcoin.Protocol
{

	/// <summary>
	/// The AddressManager, keep a set of peers discovered on the network in cache can update their actual states.
	/// Replicate AddressManager of Bitcoin Core, the Buckets and BucketPosition are not guaranteed to be coherent with Bitcoin Core
	/// </summary>
	public class AddressManager : IBitcoinSerializable
	{
		/// <summary>
		/// Will properly convert a endpoint to IPEndpoint
		/// If endpoint is a DNSEndpoint, a DNS resolution will be made and all addresses added
		/// If endpoint is a DNSEndpoint for onion, it will be converted into onioncat address
		/// If endpoint is an IPEndpoint it is added to AddressManager
		/// </summary>
		/// <param name="endpoint">The endpoint to add to the address manager</param>
		/// <param name="source">The source which advertized this endpoint (default: IPAddress.Loopback)</param>
		/// <returns></returns>
		public async Task AddAsync(EndPoint endpoint, IPAddress source = null)
		{
			if (endpoint == null)
				throw new ArgumentNullException(nameof(endpoint));
			if (source == null)
				source = IPAddress.Loopback;
			if (endpoint is IPEndPoint ip)
			{
				Add(new NetworkAddress(ip), IPAddress.Loopback);
			}
			else if (endpoint.AsOnionCatIPEndpoint() is IPEndPoint ip2)
			{
				Add(new NetworkAddress(ip2), IPAddress.Loopback);
			}
			else if (endpoint is DnsEndPoint dns)
			{
				var ips = await Dns.GetHostAddressesAsync(dns.Host);
				foreach (var ip3 in ips)
				{
					Add(new NetworkAddress(new IPEndPoint(ip3, dns.Port)), IPAddress.Loopback);
				}
			}
			else
				throw new NotSupportedException(endpoint.ToString());
		}
		internal class AddressInfo : IBitcoinSerializable
		{
			#region IBitcoinSerializable Members

			public void ReadWrite(BitcoinStream stream)
			{
				stream.ReadWrite(ref _Address);
				stream.ReadWrite(ref source);
				stream.ReadWrite(ref nLastSuccess);
				stream.ReadWrite(ref nAttempts);

			}

			internal int nAttempts;
			internal long nLastSuccess;
{'fraction_non_alphanumeric': 0.09369024856596558, 'fraction_numerical': 0.0016995963458678563, 'mean_word_length': 3.8636363636363638, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, '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': '6627460', 'n_tokens_mistral': 1593, 'n_tokens_neox': 1376, 'n_words': 447}