public static unsafe void GetAddrInfoAsync(DnsResolveAsyncResult asyncResult)
        {
            GetAddrInfoExContext *context = GetAddrInfoExContext.AllocateContext();

            try
            {
                var state = new GetAddrInfoExState(asyncResult);
                context->QueryStateHandle = state.CreateHandle();
            }
            catch
            {
                GetAddrInfoExContext.FreeContext(context);
                throw;
            }

            AddressInfoEx hints = new AddressInfoEx();

            hints.ai_flags  = AddressInfoHints.AI_CANONNAME;
            hints.ai_family = AddressFamily.Unspecified; // Gets all address families

            SocketError errorCode =
                (SocketError)Interop.Winsock.GetAddrInfoExW(asyncResult.HostName, null, 0 /* NS_ALL*/, IntPtr.Zero, ref hints, out context->Result, IntPtr.Zero, ref context->Overlapped, s_getAddrInfoExCallback, out context->CancelHandle);

            if (errorCode != SocketError.IOPending)
            {
                ProcessResult(errorCode, context);
            }
        }
Exemple #2
0
        private static void ResolveCallback(object context)
        {
            DnsResolveAsyncResult result = (DnsResolveAsyncResult)context;
            IPHostEntry           hostEntry;

            try
            {
                if (result.IpAddress != null)
                {
                    hostEntry = InternalGetHostByAddress(result.IpAddress);
                }
                else
                {
                    hostEntry = InternalGetHostByName(result.HostName);
                }
            }
            catch (OutOfMemoryException)
            {
                throw;
            }
            catch (Exception exception)
            {
                result.InvokeCallback(exception);
                return;
            }

            result.InvokeCallback(hostEntry);
        }
Exemple #3
0
        private static IPHostEntry HostResolutionEndHelper(IAsyncResult asyncResult)
        {
            //
            // parameter validation
            //
            if (asyncResult == null)
            {
                throw new ArgumentNullException(nameof(asyncResult));
            }
            DnsResolveAsyncResult castedResult = asyncResult as DnsResolveAsyncResult;

            if (castedResult == null)
            {
                throw new ArgumentException(SR.net_io_invalidasyncresult, nameof(asyncResult));
            }
            if (castedResult.EndCalled)
            {
                throw new InvalidOperationException(SR.Format(SR.net_io_invalidendcall, nameof(EndResolve)));
            }

            if (NetEventSource.IsEnabled)
            {
                NetEventSource.Info(null);
            }

            castedResult.InternalWaitForCompletion();
            castedResult.EndCalled = true;

            Exception exception = castedResult.Result as Exception;

            if (exception != null)
            {
                ExceptionDispatchInfo.Throw(exception);
            }

            return((IPHostEntry)castedResult.Result);
        }
Exemple #4
0
        private static IAsyncResult HostResolutionBeginHelper(IPAddress address, bool flowContext, AsyncCallback requestCallback, object state)
        {
            if (address == null)
            {
                throw new ArgumentNullException(nameof(address));
            }

            if (address.Equals(IPAddress.Any) || address.Equals(IPAddress.IPv6Any))
            {
                throw new ArgumentException(SR.net_invalid_ip_addr, nameof(address));
            }

            if (NetEventSource.IsEnabled)
            {
                NetEventSource.Info(null, address);
            }

            // Set up the context, possibly flow.
            DnsResolveAsyncResult asyncResult = new DnsResolveAsyncResult(address, null, state, requestCallback);

            if (flowContext)
            {
                asyncResult.StartPostingAsyncOp(false);
            }

            // Start the resolve.
            Task.Factory.StartNew(
                s => ResolveCallback(s),
                asyncResult,
                CancellationToken.None,
                TaskCreationOptions.DenyChildAttach,
                TaskScheduler.Default);

            // Finish the flowing, maybe it completed?  This does nothing if we didn't initiate the flowing above.
            asyncResult.FinishPostingAsyncOp();
            return(asyncResult);
        }
 internal static void GetAddrInfoAsync(DnsResolveAsyncResult asyncResult)
 {
     throw new NotSupportedException();
 }
 public GetAddrInfoExState(DnsResolveAsyncResult asyncResult)
 {
     _asyncResult = asyncResult;
 }
Exemple #7
0
        // Helpers for async GetHostByName, ResolveToAddresses, and Resolve - they're almost identical
        // If hostName is an IPString and justReturnParsedIP==true then no reverse lookup will be attempted, but the original address is returned.
        private static IAsyncResult HostResolutionBeginHelper(string hostName, bool justReturnParsedIp, bool throwOnIIPAny, AsyncCallback requestCallback, object state)
        {
            if (hostName == null)
            {
                throw new ArgumentNullException(nameof(hostName));
            }

            if (NetEventSource.IsEnabled)
            {
                NetEventSource.Info(null, hostName);
            }

            // See if it's an IP Address.
            IPAddress             ipAddress;
            DnsResolveAsyncResult asyncResult;

            if (IPAddress.TryParse(hostName, out ipAddress))
            {
                if (throwOnIIPAny && (ipAddress.Equals(IPAddress.Any) || ipAddress.Equals(IPAddress.IPv6Any)))
                {
                    throw new ArgumentException(SR.net_invalid_ip_addr, nameof(hostName));
                }

                asyncResult = new DnsResolveAsyncResult(ipAddress, null, state, requestCallback);

                if (justReturnParsedIp)
                {
                    IPHostEntry hostEntry = NameResolutionUtilities.GetUnresolvedAnswer(ipAddress);
                    asyncResult.StartPostingAsyncOp(false);
                    asyncResult.InvokeCallback(hostEntry);
                    asyncResult.FinishPostingAsyncOp();
                    return(asyncResult);
                }
            }
            else
            {
                asyncResult = new DnsResolveAsyncResult(hostName, null, state, requestCallback);
            }

            // Set up the context, possibly flow.
            asyncResult.StartPostingAsyncOp(false);

            // If the OS supports it and 'hostName' is not an IP Address, resolve the name asynchronously
            // instead of calling the synchronous version in the ThreadPool.
            if (NameResolutionPal.SupportsGetAddrInfoAsync && ipAddress == null)
            {
                ValidateHostName(hostName);
                NameResolutionPal.GetAddrInfoAsync(asyncResult);
            }
            else
            {
                // Start the resolve.
                Task.Factory.StartNew(
                    s => ResolveCallback(s),
                    asyncResult,
                    CancellationToken.None,
                    TaskCreationOptions.DenyChildAttach,
                    TaskScheduler.Default);
            }

            // Finish the flowing, maybe it completed?  This does nothing if we didn't initiate the flowing above.
            asyncResult.FinishPostingAsyncOp();
            return(asyncResult);
        }