/// <summary> /// Gets or creates an OsxNetworkInterface, 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> /// <returns>The cached or new OsxNetworkInterface with the given name.</returns> private static OsxNetworkInterface GetOrCreate(Dictionary<string, OsxNetworkInterface> interfaces, string name) { OsxNetworkInterface oni; if (!interfaces.TryGetValue(name, out oni)) { oni = new OsxNetworkInterface(name); interfaces.Add(name, oni); } return oni; }
/// <summary> /// Gets or creates an OsxNetworkInterface, 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> /// <returns>The cached or new OsxNetworkInterface with the given name.</returns> private static OsxNetworkInterface GetOrCreate(Dictionary <string, OsxNetworkInterface> interfaces, string name) { OsxNetworkInterface oni; if (!interfaces.TryGetValue(name, out oni)) { oni = new OsxNetworkInterface(name); interfaces.Add(name, oni); } return(oni); }
private UnicastIPAddressInformationCollection GetUnicastAddresses() { UnicastIPAddressInformationCollection collection = new UnicastIPAddressInformationCollection(); foreach (UnicastIPAddressInformation info in OsxNetworkInterface.GetOsxNetworkInterfaces().SelectMany(oni => oni.GetIPProperties().UnicastAddresses)) { // PERF: Use Interop.Sys.EnumerateInterfaceAddresses directly here. collection.InternalAdd(info); } return(collection); }
public static unsafe NetworkInterface[] GetOsxNetworkInterfaces() { Dictionary <string, OsxNetworkInterface> interfacesByName = new Dictionary <string, OsxNetworkInterface>(); 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, maskAddr) => { try { OsxNetworkInterface oni = GetOrCreate(interfacesByName, name); oni.ProcessIpv4Address(ipAddr, maskAddr); } catch (Exception e) { if (exceptions == null) { exceptions = new List <Exception>(); } exceptions.Add(e); } }, (name, ipAddr, scopeId) => { try { OsxNetworkInterface oni = GetOrCreate(interfacesByName, name); oni.ProcessIpv6Address(ipAddr, *scopeId); } catch (Exception e) { if (exceptions == null) { exceptions = new List <Exception>(); } exceptions.Add(e); } }, (name, llAddr) => { try { OsxNetworkInterface oni = GetOrCreate(interfacesByName, name); 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) { return(interfacesByName.Values.ToArray()); } else { interfacesByName.Clear(); } } throw new NetworkInformationException(SR.net_PInvokeError); }
public OsxIpInterfaceProperties(OsxNetworkInterface oni, int mtu) : base(oni) { _ipv4Properties = new OsxIPv4InterfaceProperties(oni, mtu); _ipv6Properties = new OsxIPv6InterfaceProperties(oni, mtu); _gatewayAddresses = GetGatewayAddresses(oni.Index); }