Exemple #1
0
        /// <summary>
        /// Gets or creates an BsdNetworkInterface, based on whether it already exists in the given Dictionary.
        /// If created, it is added to the Dictionary.
        /// </summary>
        /// <param name="interfaces">The Dictionary of existing interfaces.</param>
        /// <param name="name">The name of the interface.</param>
        /// <param name="index">Interface index of the interface.</param>
        /// <returns>The cached or new BsdNetworkInterface with the given name.</returns>
        private static BsdNetworkInterface GetOrCreate(Dictionary <string, BsdNetworkInterface> interfaces, string name, int index)
        {
            BsdNetworkInterface?oni;

            if (!interfaces.TryGetValue(name, out oni))
            {
                oni = new BsdNetworkInterface(name, index);
                interfaces.Add(name, oni);
            }

            return(oni);
        }
            /// <summary>
            /// Gets or creates an BsdNetworkInterface, based on whether it already exists in the given Dictionary.
            /// If created, it is added to the Dictionary.
            /// </summary>
            /// <param name="pName">The name of the interface.</param>
            /// <param name="index">Interface index of the interface.</param>
            /// <returns>The cached or new BsdNetworkInterface with the given name.</returns>
            internal unsafe BsdNetworkInterface GetOrCreate(byte *pName, int index)
            {
                string name = new string((sbyte *)pName);

                BsdNetworkInterface?oni;

                if (!_interfaces.TryGetValue(name, out oni))
                {
                    oni = new BsdNetworkInterface(name, index);
                    _interfaces.Add(name, oni);
                }
                return(oni);
            }
Exemple #3
0
 public BsdIpInterfaceProperties(BsdNetworkInterface oni, int mtu) : base(oni)
 {
     _ipv4Properties   = new BsdIPv4InterfaceProperties(oni, mtu);
     _ipv6Properties   = new BsdIPv6InterfaceProperties(oni, mtu);
     _gatewayAddresses = GetGatewayAddresses(oni.Index);
 }
Exemple #4
0
        public static unsafe NetworkInterface[] GetBsdNetworkInterfaces()
        {
            Dictionary <string, BsdNetworkInterface> interfacesByName = new Dictionary <string, BsdNetworkInterface>();
            List <Exception>?exceptions = null;
            const int        MaxTries   = 3;

            for (int attempt = 0; attempt < MaxTries; attempt++)
            {
                // Because these callbacks are executed in a reverse-PInvoke, we do not want any exceptions
                // to propogate out, because they will not be catchable. Instead, we track all the exceptions
                // that are thrown in these callbacks, and aggregate them at the end.
                int result = Interop.Sys.EnumerateInterfaceAddresses(
                    (name, ipAddr) =>
                {
                    try
                    {
                        BsdNetworkInterface oni = GetOrCreate(interfacesByName, name, ipAddr->InterfaceIndex);
                        oni.ProcessIpv4Address(ipAddr);
                    }
                    catch (Exception e)
                    {
                        if (exceptions == null)
                        {
                            exceptions = new List <Exception>();
                        }
                        exceptions.Add(e);
                    }
                },
                    (name, ipAddr, scopeId) =>
                {
                    try
                    {
                        BsdNetworkInterface oni = GetOrCreate(interfacesByName, name, ipAddr->InterfaceIndex);
                        oni.ProcessIpv6Address(ipAddr, *scopeId);
                    }
                    catch (Exception e)
                    {
                        if (exceptions == null)
                        {
                            exceptions = new List <Exception>();
                        }
                        exceptions.Add(e);
                    }
                },
                    (name, llAddr) =>
                {
                    try
                    {
                        BsdNetworkInterface oni = GetOrCreate(interfacesByName, name, llAddr->InterfaceIndex);
                        oni.ProcessLinkLayerAddress(llAddr);
                    }
                    catch (Exception e)
                    {
                        if (exceptions == null)
                        {
                            exceptions = new List <Exception>();
                        }
                        exceptions.Add(e);
                    }
                });
                if (exceptions != null)
                {
                    throw new NetworkInformationException(SR.net_PInvokeError, new AggregateException(exceptions));
                }
                else if (result == 0)
                {
                    var results = new BsdNetworkInterface[interfacesByName.Count];
                    int i       = 0;
                    foreach (KeyValuePair <string, BsdNetworkInterface> item in interfacesByName)
                    {
                        results[i++] = item.Value;
                    }
                    return(results);
                }
                else
                {
                    interfacesByName.Clear();
                }
            }

            throw new NetworkInformationException(SR.net_PInvokeError);
        }
 public BsdIPv6InterfaceProperties(BsdNetworkInterface oni, int mtu)
     : base(oni)
 {
     _mtu = mtu;
 }