This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/
UnixNetworkInterface.cs
102 lines (85 loc) · 3.99 KB
/
UnixNetworkInterface.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace System.Net.NetworkInformation
{
internal abstract class UnixNetworkInterface : NetworkInterface
{
protected string _name;
protected int _index;
protected NetworkInterfaceType _networkInterfaceType = NetworkInterfaceType.Unknown;
protected PhysicalAddress _physicalAddress;
protected List<IPAddress> _addresses = new List<IPAddress>();
protected Dictionary<IPAddress, IPAddress> _netMasks = new Dictionary<IPAddress, IPAddress>();
// If this is an ipv6 device, contains the Scope ID.
protected uint? _ipv6ScopeId = null;
private string _id;
protected UnixNetworkInterface(string name)
{
_name = name;
}
public sealed override string Name { get { return _name; } }
public sealed override NetworkInterfaceType NetworkInterfaceType { get { return _networkInterfaceType; } }
public sealed override PhysicalAddress GetPhysicalAddress()
{
Debug.Assert(_physicalAddress != null, "_physicalAddress was never initialized. This means no address with type AF_PACKET was discovered.");
return _physicalAddress;
}
public override string Id { get { return _index.ToString(); } }
public override bool Supports(NetworkInterfaceComponent networkInterfaceComponent)
{
Sockets.AddressFamily family =
(networkInterfaceComponent == NetworkInterfaceComponent.IPv4)
? Sockets.AddressFamily.InterNetwork
: Sockets.AddressFamily.InterNetworkV6;
return _addresses.Any(addr => addr.AddressFamily == family);
}
/// <summary>
/// The system's index for this network device.
/// </summary>
public int Index { get { return _index; } }
/// <summary>
/// Returns a list of all of the interface's IP Addresses.
/// </summary>
public List<IPAddress> Addresses { get { return _addresses; } }
// Adds any IPAddress to this interface's List of addresses.
protected void AddAddress(IPAddress ipAddress)
{
_addresses.Add(ipAddress);
}
public IPAddress GetNetMaskForIPv4Address(IPAddress address)
{
Debug.Assert(address.AddressFamily == Sockets.AddressFamily.InterNetwork);
return _netMasks[address];
}
protected unsafe void ProcessIpv4Address(Interop.Sys.IpAddressInfo* addressInfo, Interop.Sys.IpAddressInfo* netMask)
{
IPAddress ipAddress = IPAddressUtil.GetIPAddressFromNativeInfo(addressInfo);
IPAddress netMaskAddress = IPAddressUtil.GetIPAddressFromNativeInfo(netMask);
AddAddress(ipAddress);
_netMasks[ipAddress] = netMaskAddress;
}
protected unsafe void ProcessIpv6Address(Interop.Sys.IpAddressInfo* addressInfo, uint scopeId)
{
IPAddress address = IPAddressUtil.GetIPAddressFromNativeInfo(addressInfo);
AddAddress(address);
_ipv6ScopeId = scopeId;
}
protected unsafe void ProcessLinkLayerAddress(Interop.Sys.LinkLayerAddressInfo* llAddr)
{
byte[] macAddress = new byte[llAddr->NumAddressBytes];
fixed (byte* macAddressPtr = macAddress)
{
Buffer.MemoryCopy(llAddr->AddressBytes, macAddressPtr, llAddr->NumAddressBytes, llAddr->NumAddressBytes);
}
PhysicalAddress physicalAddress = new PhysicalAddress(macAddress);
_index = llAddr->InterfaceIndex;
_id = _index.ToString();
_physicalAddress = physicalAddress;
_networkInterfaceType = (NetworkInterfaceType)llAddr->HardwareType;
}
}
}