Imported Upstream version 5.18.0.142

Former-commit-id: 7467d4b717762eeaf652d77f1486dd11ffb1ff1f
This commit is contained in:
Xamarin Public Jenkins (auto-signing)
2018-10-09 08:20:59 +00:00
parent e52655b4dc
commit 0abdbe5a7d
1547 changed files with 93792 additions and 47893 deletions

View File

@@ -0,0 +1,50 @@
//
// System.Net.NetworkInformation.IPGlobalProperties
//
// Authors:
// Gonzalo Paniagua Javier (gonzalo@novell.com)
// Atsushi Enomoto (atsushi@ximian.com)
// Miguel de Icaza (miguel@novell.com)
// Eric Butler (eric@extremeboredom.net)
// Marek Habersack (mhabersack@novell.com)
// Marek Safar (marek.safar@gmail.com)
//
// Copyright (c) 2006-2008 Novell, Inc. (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
namespace System.Net.NetworkInformation {
internal static class IPGlobalPropertiesFactoryPal {
public static IPGlobalProperties Create ()
{
var instance = UnixIPGlobalPropertiesFactoryPal.Create ();
#if WIN_PLATFORM
if (instance == null)
instance = Win32IPGlobalPropertiesFactoryPal.Create ();
#endif
if (instance == null)
throw new NotImplementedException ();
return instance;
}
}
}

View File

@@ -1,484 +0,0 @@
//
// System.Net.NetworkInformation.IPInterfaceProperties
//
// Authors:
// Gonzalo Paniagua Javier (gonzalo@novell.com)
// Atsushi Enomoto (atsushi@ximian.com)
//
// Copyright (c) 2006-2007 Novell, Inc. (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
using System.Runtime.InteropServices;
namespace System.Net.NetworkInformation {
abstract class UnixIPInterfaceProperties : IPInterfaceProperties
{
protected IPv4InterfaceProperties ipv4iface_properties;
protected UnixNetworkInterface iface;
List <IPAddress> addresses;
IPAddressCollection dns_servers;
public UnixIPInterfaceProperties (UnixNetworkInterface iface, List <IPAddress> addresses)
{
this.iface = iface;
this.addresses = addresses;
}
public override IPv6InterfaceProperties GetIPv6Properties ()
{
throw new NotImplementedException ();
}
#if MONODROID
[DllImport ("__Internal")]
static extern int _monodroid_get_dns_servers (out IntPtr dns_servers_array);
void GetDNSServersFromOS ()
{
IntPtr dsa;
int len = _monodroid_get_dns_servers (out dsa);
if (len <= 0)
return;
var servers = new IntPtr [len];
Marshal.Copy (dsa, servers, 0, len);
dns_servers = new IPAddressCollection ();
foreach (IntPtr s in servers) {
string server_ip = Marshal.PtrToStringAnsi (s);
Marshal.FreeHGlobal (s);
IPAddress addr;
if (!IPAddress.TryParse (server_ip, out addr))
continue;
dns_servers.InternalAdd (addr);
}
Marshal.FreeHGlobal (dsa);
}
#else
static Regex ns = new Regex (@"\s*nameserver\s+(?<address>.*)");
static Regex search = new Regex (@"\s*search\s+(?<domain>.*)");
string dns_suffix;
DateTime last_parse;
void ParseResolvConf ()
{
try {
DateTime wt = File.GetLastWriteTime ("/etc/resolv.conf");
if (wt <= last_parse)
return;
last_parse = wt;
dns_suffix = "";
dns_servers = new IPAddressCollection ();
using (StreamReader reader = new StreamReader ("/etc/resolv.conf")) {
string str;
string line;
while ((line = reader.ReadLine ()) != null) {
line = line.Trim ();
if (line.Length == 0 || line [0] == '#')
continue;
Match match = ns.Match (line);
if (match.Success) {
try {
str = match.Groups ["address"].Value;
str = str.Trim ();
dns_servers.InternalAdd (IPAddress.Parse (str));
} catch {
}
} else {
match = search.Match (line);
if (match.Success) {
str = match.Groups ["domain"].Value;
string [] parts = str.Split (',');
dns_suffix = parts [0].Trim ();
}
}
}
}
} catch {
}
}
#endif
public override IPAddressInformationCollection AnycastAddresses {
get {
var c = new IPAddressInformationCollection ();
foreach (IPAddress address in addresses) {
c.InternalAdd (new SystemIPAddressInformation (address, false, false));
}
return c;
}
}
[MonoTODO ("Always returns an empty collection.")]
public override IPAddressCollection DhcpServerAddresses {
get {
// There are lots of different DHCP clients
// that all store their configuration differently.
// I'm not sure what to do here.
IPAddressCollection coll = new IPAddressCollection ();
return coll;
}
}
public override IPAddressCollection DnsAddresses {
get {
#if MONODROID
GetDNSServersFromOS ();
#else
ParseResolvConf ();
#endif
return dns_servers;
}
}
public override string DnsSuffix {
get {
#if MONODROID
return String.Empty;
#else
ParseResolvConf ();
return dns_suffix;
#endif
}
}
[MonoTODO ("Always returns true")]
public override bool IsDnsEnabled {
get {
return true;
}
}
[MonoTODO ("Always returns false")]
public override bool IsDynamicDnsEnabled {
get {
return false;
}
}
public override MulticastIPAddressInformationCollection MulticastAddresses {
get {
var multicastAddresses = new MulticastIPAddressInformationCollection ();
foreach (IPAddress address in addresses) {
byte[] addressBytes = address.GetAddressBytes ();
if (addressBytes[0] >= 224 && addressBytes[0] <= 239) {
multicastAddresses.InternalAdd (new SystemMulticastIPAddressInformation (new SystemIPAddressInformation (address, true, false)));
}
}
return multicastAddresses;
}
}
public override UnicastIPAddressInformationCollection UnicastAddresses {
get {
var unicastAddresses = new UnicastIPAddressInformationCollection ();
foreach (IPAddress address in addresses) {
switch (address.AddressFamily) {
case AddressFamily.InterNetwork:
byte top = address.GetAddressBytes () [0];
if (top >= 224 && top <= 239)
continue;
unicastAddresses.InternalAdd (new LinuxUnicastIPAddressInformation (address));
break;
case AddressFamily.InterNetworkV6:
if (address.IsIPv6Multicast)
continue;
unicastAddresses.InternalAdd (new LinuxUnicastIPAddressInformation (address));
break;
}
}
return unicastAddresses;
}
}
[MonoTODO ("Always returns an empty collection.")]
public override IPAddressCollection WinsServersAddresses {
get {
// I do SUPPOSE we could scrape /etc/samba/smb.conf, but.. yeesh.
return new IPAddressCollection ();
}
}
}
class LinuxIPInterfaceProperties : UnixIPInterfaceProperties
{
public LinuxIPInterfaceProperties (LinuxNetworkInterface iface, List <IPAddress> addresses)
: base (iface, addresses)
{
}
public override IPv4InterfaceProperties GetIPv4Properties ()
{
if (ipv4iface_properties == null)
ipv4iface_properties = new LinuxIPv4InterfaceProperties (iface as LinuxNetworkInterface);
return ipv4iface_properties;
}
IPAddressCollection ParseRouteInfo (string iface)
{
var col = new IPAddressCollection ();
try {
using (StreamReader reader = new StreamReader ("/proc/net/route")) {
string line;
reader.ReadLine (); // Ignore first line
while ((line = reader.ReadLine ()) != null) {
line = line.Trim ();
if (line.Length == 0)
continue;
string [] parts = line.Split ('\t');
if (parts.Length < 3)
continue;
string gw_address = parts [2].Trim ();
byte [] ipbytes = new byte [4];
if (gw_address.Length == 8 && iface.Equals (parts [0], StringComparison.OrdinalIgnoreCase)) {
for (int i = 0; i < 4; i++) {
if (!Byte.TryParse (gw_address.Substring (i * 2, 2), NumberStyles.HexNumber, null, out ipbytes [3 - i]))
continue;
}
IPAddress ip = new IPAddress (ipbytes);
if (!ip.Equals (IPAddress.Any) && !col.Contains (ip))
col.InternalAdd (ip);
}
}
}
} catch {
}
return col;
}
public override GatewayIPAddressInformationCollection GatewayAddresses {
get {
return SystemGatewayIPAddressInformation.ToGatewayIpAddressInformationCollection (ParseRouteInfo (this.iface.Name.ToString()));
}
}
}
class MacOsIPInterfaceProperties : UnixIPInterfaceProperties
{
public MacOsIPInterfaceProperties (MacOsNetworkInterface iface, List <IPAddress> addresses)
: base (iface, addresses)
{
}
public override IPv4InterfaceProperties GetIPv4Properties ()
{
if (ipv4iface_properties == null)
ipv4iface_properties = new MacOsIPv4InterfaceProperties (iface as MacOsNetworkInterface);
return ipv4iface_properties;
}
[MethodImplAttribute(MethodImplOptions.InternalCall)]
private extern static bool ParseRouteInfo_internal(string iface, out string[] gw_addr_list);
public override GatewayIPAddressInformationCollection GatewayAddresses {
get {
var gateways = new IPAddressCollection ();
string[] gw_addrlist;
if (!ParseRouteInfo_internal (this.iface.Name.ToString(), out gw_addrlist))
return new GatewayIPAddressInformationCollection ();
for(int i=0; i<gw_addrlist.Length; i++) {
try {
IPAddress ip = IPAddress.Parse(gw_addrlist[i]);
if (!ip.Equals (IPAddress.Any) && !gateways.Contains (ip))
gateways.InternalAdd (ip);
} catch (ArgumentNullException) {
/* Ignore this, as the
* internal call might have
* left some blank entries at
* the end of the array
*/
}
}
return SystemGatewayIPAddressInformation.ToGatewayIpAddressInformationCollection (gateways);
}
}
}
#if WIN_PLATFORM
class Win32IPInterfaceProperties2 : IPInterfaceProperties
{
readonly Win32_IP_ADAPTER_ADDRESSES addr;
readonly Win32_MIB_IFROW mib4, mib6;
public Win32IPInterfaceProperties2 (Win32_IP_ADAPTER_ADDRESSES addr, Win32_MIB_IFROW mib4, Win32_MIB_IFROW mib6)
{
this.addr = addr;
this.mib4 = mib4;
this.mib6 = mib6;
}
public override IPv4InterfaceProperties GetIPv4Properties ()
{
Win32_IP_ADAPTER_INFO v4info = Win32NetworkInterface2.GetAdapterInfoByIndex (mib4.Index);
return new Win32IPv4InterfaceProperties (v4info, mib4);
}
public override IPv6InterfaceProperties GetIPv6Properties ()
{
Win32_IP_ADAPTER_INFO v6info = Win32NetworkInterface2.GetAdapterInfoByIndex (mib6.Index);
return new Win32IPv6InterfaceProperties (mib6);
}
public override IPAddressInformationCollection AnycastAddresses {
get { return Win32FromAnycast (addr.FirstAnycastAddress); }
}
static IPAddressInformationCollection Win32FromAnycast (IntPtr ptr)
{
var c = new IPAddressInformationCollection ();
Win32_IP_ADAPTER_ANYCAST_ADDRESS a;
for (IntPtr p = ptr; p != IntPtr.Zero; p = a.Next) {
a = (Win32_IP_ADAPTER_ANYCAST_ADDRESS) Marshal.PtrToStructure (p, typeof (Win32_IP_ADAPTER_ANYCAST_ADDRESS));
c.InternalAdd (new SystemIPAddressInformation (
a.Address.GetIPAddress (),
a.LengthFlags.IsDnsEligible,
a.LengthFlags.IsTransient));
}
return c;
}
public override IPAddressCollection DhcpServerAddresses {
get {
Win32_IP_ADAPTER_INFO v4info = Win32NetworkInterface2.GetAdapterInfoByIndex (mib4.Index);
// FIXME: should ipv6 DhcpServer be considered?
try {
return new Win32IPAddressCollection (v4info.DhcpServer);
} catch (IndexOutOfRangeException) {
return Win32IPAddressCollection.Empty;
}
}
}
public override IPAddressCollection DnsAddresses {
get { return Win32IPAddressCollection.FromDnsServer (addr.FirstDnsServerAddress); }
}
public override string DnsSuffix {
get { return addr.DnsSuffix; }
}
public override GatewayIPAddressInformationCollection GatewayAddresses {
get {
var col = new GatewayIPAddressInformationCollection ();
try {
Win32_IP_ADAPTER_INFO v4info = Win32NetworkInterface2.GetAdapterInfoByIndex (mib4.Index);
// FIXME: should ipv6 DhcpServer be considered?
var a = v4info.GatewayList;
if (!String.IsNullOrEmpty (a.IpAddress)) {
col.InternalAdd(new SystemGatewayIPAddressInformation(IPAddress.Parse (a.IpAddress)));
AddSubsequently (a.Next, col);
}
} catch (IndexOutOfRangeException) {}
return col;
}
}
static void AddSubsequently (IntPtr head, GatewayIPAddressInformationCollection col)
{
Win32_IP_ADDR_STRING a;
for (IntPtr p = head; p != IntPtr.Zero; p = a.Next) {
a = (Win32_IP_ADDR_STRING) Marshal.PtrToStructure (p, typeof (Win32_IP_ADDR_STRING));
col.InternalAdd (new SystemGatewayIPAddressInformation (IPAddress.Parse (a.IpAddress)));
}
}
public override bool IsDnsEnabled {
get { return Win32NetworkInterface.FixedInfo.EnableDns != 0; }
}
public override bool IsDynamicDnsEnabled {
get { return addr.DdnsEnabled; }
}
public override MulticastIPAddressInformationCollection MulticastAddresses {
get { return Win32FromMulticast (addr.FirstMulticastAddress); }
}
static MulticastIPAddressInformationCollection Win32FromMulticast (IntPtr ptr)
{
var c = new MulticastIPAddressInformationCollection ();
Win32_IP_ADAPTER_MULTICAST_ADDRESS a;
for (IntPtr p = ptr; p != IntPtr.Zero; p = a.Next) {
a = (Win32_IP_ADAPTER_MULTICAST_ADDRESS) Marshal.PtrToStructure (p, typeof (Win32_IP_ADAPTER_MULTICAST_ADDRESS));
c.InternalAdd (new SystemMulticastIPAddressInformation (new SystemIPAddressInformation (
a.Address.GetIPAddress (),
a.LengthFlags.IsDnsEligible,
a.LengthFlags.IsTransient)));
}
return c;
}
public override UnicastIPAddressInformationCollection UnicastAddresses {
get {
try {
Win32_IP_ADAPTER_INFO ai = Win32NetworkInterface2.GetAdapterInfoByIndex (mib4.Index);
// FIXME: should ipv6 DhcpServer be considered?
return Win32FromUnicast (addr.FirstUnicastAddress);
} catch (IndexOutOfRangeException) {
return new UnicastIPAddressInformationCollection ();
}
}
}
static UnicastIPAddressInformationCollection Win32FromUnicast (IntPtr ptr)
{
UnicastIPAddressInformationCollection c = new UnicastIPAddressInformationCollection ();
Win32_IP_ADAPTER_UNICAST_ADDRESS a;
for (IntPtr p = ptr; p != IntPtr.Zero; p = a.Next) {
a = (Win32_IP_ADAPTER_UNICAST_ADDRESS) Marshal.PtrToStructure (p, typeof (Win32_IP_ADAPTER_UNICAST_ADDRESS));
c.InternalAdd (new Win32UnicastIPAddressInformation (a));
}
return c;
}
public override IPAddressCollection WinsServersAddresses {
get {
try {
Win32_IP_ADAPTER_INFO v4info = Win32NetworkInterface2.GetAdapterInfoByIndex (mib4.Index);
// FIXME: should ipv6 DhcpServer be considered?
return new Win32IPAddressCollection (v4info.PrimaryWinsServer, v4info.SecondaryWinsServer);
} catch (IndexOutOfRangeException) {
return Win32IPAddressCollection.Empty;
}
}
}
}
#endif
}

View File

@@ -0,0 +1,89 @@
//
// System.Net.NetworkInformation.IPInterfaceProperties
//
// Authors:
// Gonzalo Paniagua Javier (gonzalo@novell.com)
// Atsushi Enomoto (atsushi@ximian.com)
//
// Copyright (c) 2006-2007 Novell, Inc. (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.Collections.Generic;
using System.Globalization;
using System.IO;
namespace System.Net.NetworkInformation {
class LinuxIPInterfaceProperties : UnixIPInterfaceProperties
{
public LinuxIPInterfaceProperties (LinuxNetworkInterface iface, List <IPAddress> addresses)
: base (iface, addresses)
{
}
public override IPv4InterfaceProperties GetIPv4Properties ()
{
if (ipv4iface_properties == null)
ipv4iface_properties = new LinuxIPv4InterfaceProperties (iface as LinuxNetworkInterface);
return ipv4iface_properties;
}
IPAddressCollection ParseRouteInfo (string iface)
{
var col = new IPAddressCollection ();
try {
using (StreamReader reader = new StreamReader ("/proc/net/route")) {
string line;
reader.ReadLine (); // Ignore first line
while ((line = reader.ReadLine ()) != null) {
line = line.Trim ();
if (line.Length == 0)
continue;
string [] parts = line.Split ('\t');
if (parts.Length < 3)
continue;
string gw_address = parts [2].Trim ();
byte [] ipbytes = new byte [4];
if (gw_address.Length == 8 && iface.Equals (parts [0], StringComparison.OrdinalIgnoreCase)) {
for (int i = 0; i < 4; i++) {
if (!Byte.TryParse (gw_address.Substring (i * 2, 2), NumberStyles.HexNumber, null, out ipbytes [3 - i]))
continue;
}
IPAddress ip = new IPAddress (ipbytes);
if (!ip.Equals (IPAddress.Any) && !col.Contains (ip))
col.InternalAdd (ip);
}
}
}
} catch {
}
return col;
}
public override GatewayIPAddressInformationCollection GatewayAddresses {
get {
return SystemGatewayIPAddressInformation.ToGatewayIpAddressInformationCollection (ParseRouteInfo (this.iface.Name.ToString()));
}
}
}
}

View File

@@ -0,0 +1,73 @@
//
// System.Net.NetworkInformation.IPv4InterfaceProperties
//
// Authors:
// Gonzalo Paniagua Javier (gonzalo@novell.com)
// Atsushi Enomoto (atsushi@ximian.com)
// Marek Habersack (mhabersack@novell.com)
//
// Copyright (c) 2006-2007 Novell, Inc. (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.IO;
namespace System.Net.NetworkInformation {
sealed class LinuxIPv4InterfaceProperties : UnixIPv4InterfaceProperties
{
public LinuxIPv4InterfaceProperties (LinuxNetworkInterface iface)
: base (iface)
{
}
public override bool IsForwardingEnabled {
get {
string iface_path = "/proc/sys/net/ipv4/conf/" + iface.Name + "/forwarding";
if (File.Exists (iface_path)) {
string val = LinuxNetworkInterface.ReadLine (iface_path);
return val != "0";
}
return false;
}
}
public override int Mtu {
get {
string iface_path = (iface as LinuxNetworkInterface).IfacePath + "mtu";
int ret = 0;
if (File.Exists (iface_path)) {
string val = LinuxNetworkInterface.ReadLine (iface_path);
try {
ret = Int32.Parse (val);
} catch {
}
}
return ret;
}
}
}
}

View File

@@ -28,71 +28,10 @@
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
namespace System.Net.NetworkInformation {
#if WIN_PLATFORM
class Win32IPv4InterfaceStatistics : IPv4InterfaceStatistics
{
Win32_MIB_IFROW info;
public Win32IPv4InterfaceStatistics (Win32_MIB_IFROW info)
{
this.info = info;
}
public override long BytesReceived {
get { return info.InOctets; }
}
public override long BytesSent {
get { return info.OutOctets; }
}
public override long IncomingPacketsDiscarded {
get { return info.InDiscards; }
}
public override long IncomingPacketsWithErrors {
get { return info.InErrors; }
}
public override long IncomingUnknownProtocolPackets {
get { return info.InUnknownProtos; }
}
public override long NonUnicastPacketsReceived {
get { return info.InNUcastPkts; }
}
public override long NonUnicastPacketsSent {
get { return info.OutNUcastPkts; }
}
public override long OutgoingPacketsDiscarded {
get { return info.OutDiscards; }
}
public override long OutgoingPacketsWithErrors {
get { return info.OutErrors; }
}
public override long OutputQueueLength {
get { return info.OutQLen; }
}
public override long UnicastPacketsReceived {
get { return info.InUcastPkts; }
}
public override long UnicastPacketsSent {
get { return info.OutUcastPkts; }
}
}
#endif
class LinuxIPv4InterfaceStatistics : IPv4InterfaceStatistics
{
LinuxNetworkInterface linux;
public LinuxIPv4InterfaceStatistics (LinuxNetworkInterface parent)
{
linux = parent;
@@ -106,7 +45,7 @@ namespace System.Net.NetworkInformation {
return 0;
}
}
public override long BytesReceived {
get {
return Read ("statistics/rx_bytes");
@@ -182,65 +121,4 @@ namespace System.Net.NetworkInformation {
}
}
}
// dummy class
class MacOsIPv4InterfaceStatistics : IPv4InterfaceStatistics
{
//MacOsNetworkInterface macos;
public MacOsIPv4InterfaceStatistics (MacOsNetworkInterface parent)
{
//macos = parent;
}
public override long BytesReceived {
get { return 0; }
}
public override long BytesSent {
get { return 0; }
}
public override long IncomingPacketsDiscarded {
get { return 0; }
}
public override long IncomingPacketsWithErrors {
get { return 0; }
}
public override long IncomingUnknownProtocolPackets {
get { return 0; }
}
public override long NonUnicastPacketsReceived {
get { return 0; }
}
public override long NonUnicastPacketsSent {
get { return 0; }
}
public override long OutgoingPacketsDiscarded {
get { return 0; }
}
public override long OutgoingPacketsWithErrors {
get { return 0; }
}
public override long OutputQueueLength {
get { return 0; }
}
public override long UnicastPacketsReceived {
get { return 0; }
}
public override long UnicastPacketsSent {
get { return 0; }
}
}
}

View File

@@ -0,0 +1,385 @@
//
// System.Net.NetworkInformation.NetworkInterface
//
// Authors:
// Gonzalo Paniagua Javier (gonzalo@novell.com)
// Atsushi Enomoto (atsushi@ximian.com)
// Miguel de Icaza (miguel@novell.com)
// Eric Butler (eric@extremeboredom.net)
// Marek Habersack (mhabersack@novell.com)
// Marek Safar (marek.safar@gmail.com)
//
// Copyright (c) 2006-2008 Novell, Inc. (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.IO;
using System.Globalization;
namespace System.Net.NetworkInformation {
internal class LinuxNetworkInterfaceAPI : UnixNetworkInterfaceAPI
{
const int AF_INET = 2;
const int AF_INET6 = 10;
const int AF_PACKET = 17;
static void FreeInterfaceAddresses (IntPtr ifap)
{
#if MONODROID
AndroidPlatform.FreeInterfaceAddresses (ifap);
#else
freeifaddrs (ifap);
#endif
}
static int GetInterfaceAddresses (out IntPtr ifap)
{
#if MONODROID
return AndroidPlatform.GetInterfaceAddresses (out ifap);
#else
return getifaddrs (out ifap);
#endif
}
public override NetworkInterface [] GetAllNetworkInterfaces ()
{
var interfaces = new Dictionary <string, LinuxNetworkInterface> ();
IntPtr ifap;
if (GetInterfaceAddresses (out ifap) != 0)
throw new SystemException ("getifaddrs() failed");
try {
IntPtr next = ifap;
while (next != IntPtr.Zero) {
ifaddrs addr = (ifaddrs) Marshal.PtrToStructure (next, typeof (ifaddrs));
IPAddress address = IPAddress.None;
string name = addr.ifa_name;
int index = -1;
byte[] macAddress = null;
NetworkInterfaceType type = NetworkInterfaceType.Unknown;
int nullNameCount = 0;
if (addr.ifa_addr != IntPtr.Zero) {
sockaddr_in sockaddr = (sockaddr_in) Marshal.PtrToStructure (addr.ifa_addr, typeof (sockaddr_in));
if (sockaddr.sin_family == AF_INET6) {
sockaddr_in6 sockaddr6 = (sockaddr_in6) Marshal.PtrToStructure (addr.ifa_addr, typeof (sockaddr_in6));
address = new IPAddress (sockaddr6.sin6_addr.u6_addr8, sockaddr6.sin6_scope_id);
} else if (sockaddr.sin_family == AF_INET) {
address = new IPAddress (sockaddr.sin_addr);
} else if (sockaddr.sin_family == AF_PACKET) {
sockaddr_ll sockaddrll = (sockaddr_ll) Marshal.PtrToStructure (addr.ifa_addr, typeof (sockaddr_ll));
if (((int)sockaddrll.sll_halen) > sockaddrll.sll_addr.Length){
next = addr.ifa_next;
continue;
}
macAddress = new byte [(int) sockaddrll.sll_halen];
Array.Copy (sockaddrll.sll_addr, 0, macAddress, 0, macAddress.Length);
index = sockaddrll.sll_ifindex;
int hwtype = (int)sockaddrll.sll_hatype;
if (Enum.IsDefined (typeof (LinuxArpHardware), hwtype)) {
switch ((LinuxArpHardware)hwtype) {
case LinuxArpHardware.EETHER:
goto case LinuxArpHardware.ETHER;
case LinuxArpHardware.ETHER:
type = NetworkInterfaceType.Ethernet;
break;
case LinuxArpHardware.PRONET:
type = NetworkInterfaceType.TokenRing;
break;
case LinuxArpHardware.ATM:
type = NetworkInterfaceType.Atm;
break;
case LinuxArpHardware.SLIP:
case LinuxArpHardware.CSLIP:
case LinuxArpHardware.SLIP6:
case LinuxArpHardware.CSLIP6:
type = NetworkInterfaceType.Slip;
break;
case LinuxArpHardware.PPP:
type = NetworkInterfaceType.Ppp;
break;
case LinuxArpHardware.LOOPBACK:
type = NetworkInterfaceType.Loopback;
macAddress = null;
break;
case LinuxArpHardware.FDDI:
type = NetworkInterfaceType.Fddi;
break;
case LinuxArpHardware.SIT:
case LinuxArpHardware.IPDDP:
case LinuxArpHardware.IPGRE:
case LinuxArpHardware.IP6GRE:
case LinuxArpHardware.TUNNEL6:
case LinuxArpHardware.TUNNEL:
type = NetworkInterfaceType.Tunnel;
break;
}
}
}
}
LinuxNetworkInterface iface = null;
if (String.IsNullOrEmpty (name))
name = "\0" + (++nullNameCount).ToString ();
if (!interfaces.TryGetValue (name, out iface)) {
iface = new LinuxNetworkInterface (name);
interfaces.Add (name, iface);
}
if (!address.Equals (IPAddress.None))
iface.AddAddress (address);
if (macAddress != null || type == NetworkInterfaceType.Loopback) {
if (type == NetworkInterfaceType.Ethernet) {
if (Directory.Exists(iface.IfacePath + "wireless")) {
type = NetworkInterfaceType.Wireless80211;
}
}
iface.SetLinkLayerInfo (index, macAddress, type);
}
next = addr.ifa_next;
}
} finally {
FreeInterfaceAddresses (ifap);
}
NetworkInterface [] result = new NetworkInterface [interfaces.Count];
int x = 0;
foreach (NetworkInterface thisInterface in interfaces.Values) {
result [x] = thisInterface;
x++;
}
return result;
}
public override int GetLoopbackInterfaceIndex ()
{
return if_nametoindex ("lo");
}
public override IPAddress GetNetMask (IPAddress address)
{
foreach (ifaddrs networkInteface in GetNetworkInterfaces()) {
if (networkInteface.ifa_addr == IntPtr.Zero)
continue;
var sockaddr = (sockaddr_in)Marshal.PtrToStructure(networkInteface.ifa_addr, typeof(sockaddr_in));
if (sockaddr.sin_family != AF_INET)
continue;
if (!address.Equals(new IPAddress(sockaddr.sin_addr)))
continue;
var netmask = (sockaddr_in)Marshal.PtrToStructure(networkInteface.ifa_netmask, typeof(sockaddr_in));
return new IPAddress(netmask.sin_addr);
}
return null;
}
private static IEnumerable<ifaddrs> GetNetworkInterfaces()
{
IntPtr ifap = IntPtr.Zero;
try {
if (GetInterfaceAddresses(out ifap) != 0)
yield break;
var next = ifap;
while (next != IntPtr.Zero) {
var addr = (ifaddrs)Marshal.PtrToStructure(next, typeof(ifaddrs));
yield return addr;
next = addr.ifa_next;
}
} finally {
if (ifap != IntPtr.Zero)
FreeInterfaceAddresses(ifap);
}
}
}
//
// This class needs support from the libsupport.so library to fetch the
// data using arch-specific ioctls.
//
// For this to work, we have to create this on the factory above.
//
sealed class LinuxNetworkInterface : UnixNetworkInterface
{
//NetworkInterfaceType type;
string iface_path;
string iface_operstate_path;
string iface_flags_path;
#if MONODROID
[DllImport ("__Internal")]
static extern int _monodroid_get_android_api_level ();
[DllImport ("__Internal")]
static extern bool _monodroid_get_network_interface_up_state (string ifname, ref bool is_up);
[DllImport ("__Internal")]
static extern bool _monodroid_get_network_interface_supports_multicast (string ifname, ref bool supports_multicast);
bool android_use_java_api;
#endif
internal string IfacePath {
get { return iface_path; }
}
internal LinuxNetworkInterface (string name)
: base (name)
{
iface_path = "/sys/class/net/" + name + "/";
iface_operstate_path = iface_path + "operstate";
iface_flags_path = iface_path + "flags";
#if MONODROID
android_use_java_api = _monodroid_get_android_api_level () >= 24;
#endif
}
public override IPInterfaceProperties GetIPProperties ()
{
if (ipproperties == null)
ipproperties = new LinuxIPInterfaceProperties (this, addresses);
return ipproperties;
}
public override IPv4InterfaceStatistics GetIPv4Statistics ()
{
if (ipv4stats == null)
ipv4stats = new LinuxIPv4InterfaceStatistics (this);
return ipv4stats;
}
public override OperationalStatus OperationalStatus {
get {
#if MONODROID
if (android_use_java_api) {
// Starting from API 24 (Android 7 "Nougat") Android restricts access to many
// files in the /sys filesystem (see https://code.google.com/p/android/issues/detail?id=205565
// for more information) and therefore we are forced to call into Java API in
// order to get the information. Alas, what we can obtain in this way is quite
// limited. In the case of OperationalStatus we can only determine whether the
// interface is up or down. There is a way to get more detailed information but
// it requires an instance of the Android Context class which is not available
// to us here.
bool is_up = false;
if (_monodroid_get_network_interface_up_state (Name, ref is_up))
return is_up ? OperationalStatus.Up : OperationalStatus.Down;
else
return OperationalStatus.Unknown;
}
#endif
if (!Directory.Exists (iface_path))
return OperationalStatus.Unknown;
try {
string s = ReadLine (iface_operstate_path);
switch (s){
case "unknown":
return OperationalStatus.Unknown;
case "notpresent":
return OperationalStatus.NotPresent;
case "down":
return OperationalStatus.Down;
case "lowerlayerdown":
return OperationalStatus.LowerLayerDown;
case "testing":
return OperationalStatus.Testing;
case "dormant":
return OperationalStatus.Dormant;
case "up":
return OperationalStatus.Up;
}
} catch {
}
return OperationalStatus.Unknown;
}
}
public override bool SupportsMulticast {
get {
#if MONODROID
if (android_use_java_api) {
// Starting from API 24 (Android 7 "Nougat") Android restricts access to many
// files in the /sys filesystem (see https://code.google.com/p/android/issues/detail?id=205565
// for more information) and therefore we are forced to call into Java API in
// order to get the information.
bool supports_multicast = false;
_monodroid_get_network_interface_supports_multicast (Name, ref supports_multicast);
return supports_multicast;
}
#endif
if (!Directory.Exists (iface_path))
return false;
try {
string s = ReadLine (iface_flags_path);
if (s.Length > 2 && s [0] == '0' && s [1] == 'x')
s = s.Substring (2);
ulong f = UInt64.Parse (s, NumberStyles.HexNumber);
// Hardcoded, only useful for Linux.
return ((f & 0x1000) == 0x1000);
} catch {
return false;
}
}
}
internal static string ReadLine (string path)
{
using (FileStream fs = File.OpenRead (path)){
using (StreamReader sr = new StreamReader (fs)){
return sr.ReadLine ();
}
}
}
}
}

View File

@@ -25,7 +25,6 @@
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Runtime.InteropServices;
namespace System.Net.NetworkInformation {

View File

@@ -0,0 +1,98 @@
//
// System.Net.NetworkInformation.UnicastIPAddressInformation
//
// Authors:
// Gonzalo Paniagua Javier (gonzalo@novell.com)
// Atsushi Enomoto (atsushi@ximian.com)
// Eric Butler (eric@extremeboredom.net)
//
// Copyright (c) 2006-2007 Novell, Inc. (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.Net.Sockets;
namespace System.Net.NetworkInformation {
class LinuxUnicastIPAddressInformation : UnicastIPAddressInformation
{
IPAddress address;
IPAddress ipv4Mask;
public LinuxUnicastIPAddressInformation (IPAddress address)
{
this.address = address;
}
public override IPAddress Address {
get { return address; }
}
public override bool IsDnsEligible {
get {
byte[] addressBytes = address.GetAddressBytes ();
return !(addressBytes[0] == 169 && addressBytes[1] == 254);
}
}
[MonoTODO("Always returns false")]
public override bool IsTransient {
get { return false; }
}
// UnicastIPAddressInformation members
public override long AddressPreferredLifetime {
get { throw new NotImplementedException (); }
}
public override long AddressValidLifetime {
get { throw new NotImplementedException (); }
}
public override long DhcpLeaseLifetime {
get { throw new NotImplementedException (); }
}
public override DuplicateAddressDetectionState DuplicateAddressDetectionState {
get { throw new NotImplementedException (); }
}
public override IPAddress IPv4Mask {
get {
// The IPv6 equivilant (for .net compatibility)
if (Address.AddressFamily != AddressFamily.InterNetwork)
return IPAddress.Any;
if (ipv4Mask == null)
ipv4Mask = SystemNetworkInterface.GetNetMask (address);
return ipv4Mask;
}
}
public override PrefixOrigin PrefixOrigin {
get { throw new NotImplementedException (); }
}
public override SuffixOrigin SuffixOrigin {
get { throw new NotImplementedException (); }
}
}
}

View File

@@ -0,0 +1,78 @@
//
// System.Net.NetworkInformation.IPInterfaceProperties
//
// Authors:
// Gonzalo Paniagua Javier (gonzalo@novell.com)
// Atsushi Enomoto (atsushi@ximian.com)
//
// Copyright (c) 2006-2007 Novell, Inc. (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace System.Net.NetworkInformation {
class MacOsIPInterfaceProperties : UnixIPInterfaceProperties
{
public MacOsIPInterfaceProperties (MacOsNetworkInterface iface, List <IPAddress> addresses)
: base (iface, addresses)
{
}
public override IPv4InterfaceProperties GetIPv4Properties ()
{
if (ipv4iface_properties == null)
ipv4iface_properties = new MacOsIPv4InterfaceProperties (iface as MacOsNetworkInterface);
return ipv4iface_properties;
}
[MethodImplAttribute(MethodImplOptions.InternalCall)]
private extern static bool ParseRouteInfo_internal(string iface, out string[] gw_addr_list);
public override GatewayIPAddressInformationCollection GatewayAddresses {
get {
var gateways = new IPAddressCollection ();
string[] gw_addrlist;
if (!ParseRouteInfo_internal (this.iface.Name.ToString(), out gw_addrlist))
return new GatewayIPAddressInformationCollection ();
for(int i=0; i<gw_addrlist.Length; i++) {
try {
IPAddress ip = IPAddress.Parse(gw_addrlist[i]);
if (!ip.Equals (IPAddress.Any) && !gateways.Contains (ip))
gateways.InternalAdd (ip);
} catch (ArgumentNullException) {
/* Ignore this, as the
* internal call might have
* left some blank entries at
* the end of the array
*/
}
}
return SystemGatewayIPAddressInformation.ToGatewayIpAddressInformationCollection (gateways);
}
}
}
}

View File

@@ -0,0 +1,49 @@
//
// System.Net.NetworkInformation.IPv4InterfaceProperties
//
// Authors:
// Gonzalo Paniagua Javier (gonzalo@novell.com)
// Atsushi Enomoto (atsushi@ximian.com)
// Marek Habersack (mhabersack@novell.com)
//
// Copyright (c) 2006-2007 Novell, Inc. (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
namespace System.Net.NetworkInformation {
sealed class MacOsIPv4InterfaceProperties : UnixIPv4InterfaceProperties
{
public MacOsIPv4InterfaceProperties (MacOsNetworkInterface iface)
: base (iface)
{
}
// dummy
public override bool IsForwardingEnabled {
get { return false; }
}
// dummy
public override int Mtu {
get { return 0; }
}
}
}

View File

@@ -0,0 +1,89 @@
//
// System.Net.NetworkInformation.IPv4InterfaceStatistics
//
// Authors:
// Gonzalo Paniagua Javier (gonzalo@novell.com)
// Atsushi Enomoto (atsushi@ximian.com)
// Miguel de Icaza (miguel@ximian.com)
//
// Copyright (c) 2006-2008 Novell, Inc. (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
namespace System.Net.NetworkInformation {
// dummy class
class MacOsIPv4InterfaceStatistics : IPv4InterfaceStatistics
{
//MacOsNetworkInterface macos;
public MacOsIPv4InterfaceStatistics (MacOsNetworkInterface parent)
{
//macos = parent;
}
public override long BytesReceived {
get { return 0; }
}
public override long BytesSent {
get { return 0; }
}
public override long IncomingPacketsDiscarded {
get { return 0; }
}
public override long IncomingPacketsWithErrors {
get { return 0; }
}
public override long IncomingUnknownProtocolPackets {
get { return 0; }
}
public override long NonUnicastPacketsReceived {
get { return 0; }
}
public override long NonUnicastPacketsSent {
get { return 0; }
}
public override long OutgoingPacketsDiscarded {
get { return 0; }
}
public override long OutgoingPacketsWithErrors {
get { return 0; }
}
public override long OutputQueueLength {
get { return 0; }
}
public override long UnicastPacketsReceived {
get { return 0; }
}
public override long UnicastPacketsSent {
get { return 0; }
}
}
}

View File

@@ -0,0 +1,220 @@
//
// System.Net.NetworkInformation.NetworkInterface
//
// Authors:
// Gonzalo Paniagua Javier (gonzalo@novell.com)
// Atsushi Enomoto (atsushi@ximian.com)
// Miguel de Icaza (miguel@novell.com)
// Eric Butler (eric@extremeboredom.net)
// Marek Habersack (mhabersack@novell.com)
// Marek Safar (marek.safar@gmail.com)
//
// Copyright (c) 2006-2008 Novell, Inc. (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace System.Net.NetworkInformation {
internal class MacOsNetworkInterfaceAPI : UnixNetworkInterfaceAPI
{
const int AF_INET = 2;
const int AF_INET6 = 30;
const int AF_LINK = 18;
public override NetworkInterface [] GetAllNetworkInterfaces ()
{
var interfaces = new Dictionary <string, MacOsNetworkInterface> ();
IntPtr ifap;
if (getifaddrs (out ifap) != 0)
throw new SystemException ("getifaddrs() failed");
try {
IntPtr next = ifap;
while (next != IntPtr.Zero) {
MacOsStructs.ifaddrs addr = (MacOsStructs.ifaddrs) Marshal.PtrToStructure (next, typeof (MacOsStructs.ifaddrs));
IPAddress address = IPAddress.None;
string name = addr.ifa_name;
int index = -1;
byte[] macAddress = null;
NetworkInterfaceType type = NetworkInterfaceType.Unknown;
if (addr.ifa_addr != IntPtr.Zero) {
// optain IPAddress
MacOsStructs.sockaddr sockaddr = (MacOsStructs.sockaddr) Marshal.PtrToStructure (addr.ifa_addr, typeof (MacOsStructs.sockaddr));
if (sockaddr.sa_family == AF_INET6) {
MacOsStructs.sockaddr_in6 sockaddr6 = (MacOsStructs.sockaddr_in6) Marshal.PtrToStructure (addr.ifa_addr, typeof (MacOsStructs.sockaddr_in6));
address = new IPAddress (sockaddr6.sin6_addr.u6_addr8, sockaddr6.sin6_scope_id);
} else if (sockaddr.sa_family == AF_INET) {
MacOsStructs.sockaddr_in sockaddrin = (MacOsStructs.sockaddr_in) Marshal.PtrToStructure (addr.ifa_addr, typeof (MacOsStructs.sockaddr_in));
address = new IPAddress (sockaddrin.sin_addr);
} else if (sockaddr.sa_family == AF_LINK) {
MacOsStructs.sockaddr_dl sockaddrdl = new MacOsStructs.sockaddr_dl ();
sockaddrdl.Read (addr.ifa_addr);
macAddress = new byte [(int) sockaddrdl.sdl_alen];
// copy mac address from sdl_data field starting at last index pos of interface name into array macaddress, starting
// at index 0
Array.Copy (sockaddrdl.sdl_data, sockaddrdl.sdl_nlen, macAddress, 0, Math.Min (macAddress.Length, sockaddrdl.sdl_data.Length - sockaddrdl.sdl_nlen));
index = sockaddrdl.sdl_index;
int hwtype = (int) sockaddrdl.sdl_type;
if (Enum.IsDefined (typeof (MacOsArpHardware), hwtype)) {
switch ((MacOsArpHardware) hwtype) {
case MacOsArpHardware.ETHER:
type = NetworkInterfaceType.Ethernet;
break;
case MacOsArpHardware.ATM:
type = NetworkInterfaceType.Atm;
break;
case MacOsArpHardware.SLIP:
type = NetworkInterfaceType.Slip;
break;
case MacOsArpHardware.PPP:
type = NetworkInterfaceType.Ppp;
break;
case MacOsArpHardware.LOOPBACK:
type = NetworkInterfaceType.Loopback;
macAddress = null;
break;
case MacOsArpHardware.FDDI:
type = NetworkInterfaceType.Fddi;
break;
}
}
}
}
MacOsNetworkInterface iface = null;
// create interface if not already present
if (!interfaces.TryGetValue (name, out iface)) {
iface = new MacOsNetworkInterface (name, addr.ifa_flags);
interfaces.Add (name, iface);
}
// if a new address has been found, add it
if (!address.Equals (IPAddress.None))
iface.AddAddress (address);
// set link layer info, if iface has macaddress or is loopback device
if (macAddress != null || type == NetworkInterfaceType.Loopback)
iface.SetLinkLayerInfo (index, macAddress, type);
next = addr.ifa_next;
}
} finally {
freeifaddrs (ifap);
}
NetworkInterface [] result = new NetworkInterface [interfaces.Count];
int x = 0;
foreach (NetworkInterface thisInterface in interfaces.Values) {
result [x] = thisInterface;
x++;
}
return result;
}
public override int GetLoopbackInterfaceIndex ()
{
return if_nametoindex ("lo0");
}
public override IPAddress GetNetMask (IPAddress address)
{
IntPtr ifap;
if (getifaddrs (out ifap) != 0)
throw new SystemException ("getifaddrs() failed");
try {
IntPtr next = ifap;
while (next != IntPtr.Zero) {
MacOsStructs.ifaddrs addr = (MacOsStructs.ifaddrs) Marshal.PtrToStructure (next, typeof (MacOsStructs.ifaddrs));
if (addr.ifa_addr != IntPtr.Zero) {
// optain IPAddress
MacOsStructs.sockaddr sockaddr = (MacOsStructs.sockaddr) Marshal.PtrToStructure (addr.ifa_addr, typeof (MacOsStructs.sockaddr));
if (sockaddr.sa_family == AF_INET) {
MacOsStructs.sockaddr_in sockaddrin = (MacOsStructs.sockaddr_in) Marshal.PtrToStructure (addr.ifa_addr, typeof (MacOsStructs.sockaddr_in));
var saddress = new IPAddress (sockaddrin.sin_addr);
if (address.Equals (saddress))
return new IPAddress(((sockaddr_in)Marshal.PtrToStructure(addr.ifa_netmask, typeof(sockaddr_in))).sin_addr);
}
}
next = addr.ifa_next;
}
} finally {
freeifaddrs (ifap);
}
return null;
}
}
sealed class MacOsNetworkInterface : UnixNetworkInterface
{
private uint _ifa_flags;
internal MacOsNetworkInterface (string name, uint ifa_flags)
: base (name)
{
_ifa_flags = ifa_flags;
}
public override IPInterfaceProperties GetIPProperties ()
{
if (ipproperties == null)
ipproperties = new MacOsIPInterfaceProperties (this, addresses);
return ipproperties;
}
public override IPv4InterfaceStatistics GetIPv4Statistics ()
{
if (ipv4stats == null)
ipv4stats = new MacOsIPv4InterfaceStatistics (this);
return ipv4stats;
}
public override OperationalStatus OperationalStatus {
get {
if(((MacOsInterfaceFlags)_ifa_flags & MacOsInterfaceFlags.IFF_UP) == MacOsInterfaceFlags.IFF_UP){
return OperationalStatus.Up;
}
return OperationalStatus.Unknown;
}
}
public override bool SupportsMulticast {
get {
return ((MacOsInterfaceFlags)_ifa_flags & MacOsInterfaceFlags.IFF_MULTICAST) == MacOsInterfaceFlags.IFF_MULTICAST;
}
}
}
}

View File

@@ -1,4 +1,3 @@
using System;
using System.Runtime.InteropServices;
namespace System.Net.NetworkInformation {

View File

@@ -0,0 +1,52 @@
//
// System.Net.NetworkInformation.NetworkInterface
//
// Authors:
// Gonzalo Paniagua Javier (gonzalo@novell.com)
// Atsushi Enomoto (atsushi@ximian.com)
// Miguel de Icaza (miguel@novell.com)
// Eric Butler (eric@extremeboredom.net)
// Marek Habersack (mhabersack@novell.com)
// Marek Safar (marek.safar@gmail.com)
//
// Copyright (c) 2006-2008 Novell, Inc. (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
namespace System.Net.NetworkInformation {
internal static class NetworkInterfaceFactoryPal
{
public static NetworkInterfaceFactory Create ()
{
var instance = UnixNetworkInterfaceFactoryPal.Create ();
#if WIN_PLATFORM
if (instance == null)
instance = Win32NetworkInterfaceFactoryPal.Create ();
#endif
if (instance == null)
throw new NotImplementedException ();
return instance;
}
}
}

View File

@@ -0,0 +1,393 @@
//
// System.Net.NetworkInformation.IPGlobalProperties
//
// Authors:
// Gonzalo Paniagua Javier (gonzalo@novell.com)
// Atsushi Enomoto (atsushi@ximian.com)
// Marek Safar (marek.safar@gmail.com)
//
// Copyright (c) 2006-2007 Novell, Inc. (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
namespace System.Net.NetworkInformation {
abstract class CommonUnixIPGlobalProperties : IPGlobalProperties
{
[DllImport ("libc")]
static extern int gethostname ([MarshalAs (UnmanagedType.LPArray, SizeParamIndex = 1)] byte [] name, int len);
#if !ORBIS
[DllImport ("libc")]
static extern int getdomainname ([MarshalAs (UnmanagedType.LPArray, SizeParamIndex = 1)] byte [] name, int len);
#else
static int getdomainname ([MarshalAs (UnmanagedType.LPArray, SizeParamIndex = 1)] byte [] name, int len)
{
throw new PlatformNotSupportedException ();
}
#endif
public override string DhcpScopeName {
get { return String.Empty; }
}
public override string DomainName {
get {
byte [] bytes = new byte [256];
if (getdomainname (bytes, 256) != 0)
throw new NetworkInformationException ();
int len = Array.IndexOf<byte> (bytes, 0);
return Encoding.ASCII.GetString (bytes, 0, len < 0 ? 256 : len);
}
}
public override string HostName {
get {
byte [] bytes = new byte [256];
if (gethostname (bytes, 256) != 0)
throw new NetworkInformationException ();
int len = Array.IndexOf<byte> (bytes, 0);
return Encoding.ASCII.GetString (bytes, 0, len < 0 ? 256 : len);
}
}
public override bool IsWinsProxy {
get { return false; } // no WINS
}
public override NetBiosNodeType NodeType {
get { return NetBiosNodeType.Unknown; } // no NetBios
}
}
class UnixIPGlobalProperties : CommonUnixIPGlobalProperties
{
public override TcpConnectionInformation [] GetActiveTcpConnections ()
{
throw new NotImplementedException ();
}
public override IPEndPoint [] GetActiveTcpListeners ()
{
throw new NotImplementedException ();
}
public override IPEndPoint [] GetActiveUdpListeners ()
{
throw new NotImplementedException ();
}
public override IcmpV4Statistics GetIcmpV4Statistics ()
{
throw new NotImplementedException ();
}
public override IcmpV6Statistics GetIcmpV6Statistics ()
{
throw new NotImplementedException ();
}
public override IPGlobalStatistics GetIPv4GlobalStatistics ()
{
throw new NotImplementedException ();
}
public override IPGlobalStatistics GetIPv6GlobalStatistics ()
{
throw new NotImplementedException ();
}
public override TcpStatistics GetTcpIPv4Statistics ()
{
throw new NotImplementedException ();
}
public override TcpStatistics GetTcpIPv6Statistics ()
{
throw new NotImplementedException ();
}
public override UdpStatistics GetUdpIPv4Statistics ()
{
throw new NotImplementedException ();
}
public override UdpStatistics GetUdpIPv6Statistics ()
{
throw new NotImplementedException ();
}
}
#if MONODROID
sealed class AndroidIPGlobalProperties : UnixIPGlobalProperties
{
public override string DomainName {
get {
return String.Empty;
}
}
}
#endif
// It expects /proc/net/snmp (or /usr/compat/linux/proc/net/snmp),
// formatted like:
// http://www.linuxdevcenter.com/linux/2000/11/16/example5.html
// http://www.linuxdevcenter.com/linux/2000/11/16/example2.html
class MibIPGlobalProperties : UnixIPGlobalProperties
{
public const string ProcDir = "/proc";
public const string CompatProcDir = "/usr/compat/linux/proc";
public readonly string StatisticsFile, StatisticsFileIPv6, TcpFile, Tcp6File, UdpFile, Udp6File;
public MibIPGlobalProperties (string procDir)
{
StatisticsFile = Path.Combine (procDir, "net/snmp");
StatisticsFileIPv6 = Path.Combine (procDir, "net/snmp6");
TcpFile = Path.Combine (procDir,"net/tcp");
Tcp6File = Path.Combine (procDir,"net/tcp6");
UdpFile = Path.Combine (procDir,"net/udp");
Udp6File = Path.Combine (procDir,"net/udp6");
}
StringDictionary GetProperties4 (string item)
{
string file = StatisticsFile;
string head = item + ": ";
using (StreamReader sr = new StreamReader (file, Encoding.ASCII)) {
string [] keys = null;
string [] values = null;
string s = String.Empty;
do {
s = sr.ReadLine ();
if (String.IsNullOrEmpty (s))
continue;
if (s.Length <= head.Length || String.CompareOrdinal (s, 0, head, 0, head.Length) != 0)
continue;
if (keys == null)
keys = s.Substring (head.Length).Split (' ');
else if (values != null)
// hmm, there may be better error type...
throw CreateException (file, String.Format ("Found duplicate line for values for the same item '{0}'", item));
else {
values = s.Substring (head.Length).Split (' ');
break;
}
} while (!sr.EndOfStream);
if (values == null)
throw CreateException (file, String.Format ("No corresponding line was not found for '{0}'", item));
if (keys.Length != values.Length)
throw CreateException (file, String.Format ("The counts in the header line and the value line do not match for '{0}'", item));
StringDictionary dic = new StringDictionary ();
for (int i = 0; i < keys.Length; i++)
dic [keys [i]] = values [i];
return dic;
}
}
StringDictionary GetProperties6 (string item)
{
if (!File.Exists (StatisticsFileIPv6))
throw new NetworkInformationException ();
string file = StatisticsFileIPv6;
string head = item;
using (StreamReader sr = new StreamReader (file, Encoding.ASCII)) {
StringDictionary dic = new StringDictionary ();
string s = String.Empty;
do {
s = sr.ReadLine ();
if (String.IsNullOrEmpty (s))
continue;
if (s.Length <= head.Length || String.CompareOrdinal (s, 0, head, 0, head.Length) != 0)
continue;
int idx = s.IndexOfAny (wsChars, head.Length);
if (idx < 0)
throw CreateException (file, null);
dic [s.Substring (head.Length, idx - head.Length)] = s.Substring (idx + 1).Trim (wsChars);
} while (!sr.EndOfStream);
return dic;
}
}
static readonly char [] wsChars = new char [] {' ', '\t'};
Exception CreateException (string file, string msg)
{
return new InvalidOperationException (String.Format ("Unsupported (unexpected) '{0}' file format. ", file) + msg);
}
IPEndPoint [] GetLocalAddresses (List<string []> list)
{
IPEndPoint [] ret = new IPEndPoint [list.Count];
for (int i = 0; i < ret.Length; i++)
ret [i] = ToEndpoint (list [i] [1]);
return ret;
}
IPEndPoint ToEndpoint (string s)
{
int idx = s.IndexOf (':');
int port = int.Parse (s.Substring (idx + 1), NumberStyles.HexNumber);
if (s.Length == 13)
return new IPEndPoint (long.Parse (s.Substring (0, idx), NumberStyles.HexNumber), port);
else {
byte [] bytes = new byte [16];
for (int i = 0; (i << 1) < idx; i++)
bytes [i] = byte.Parse (s.Substring (i << 1, 2), NumberStyles.HexNumber);
return new IPEndPoint (new IPAddress (bytes), port);
}
}
void GetRows (string file, List<string []> list)
{
if (!File.Exists (file))
return;
using (StreamReader sr = new StreamReader (file, Encoding.ASCII)) {
sr.ReadLine (); // skip first line
while (!sr.EndOfStream) {
string [] item = sr.ReadLine ().Split (wsChars, StringSplitOptions.RemoveEmptyEntries);
if (item.Length < 4)
throw CreateException (file, null);
list.Add (item);
}
}
}
public override TcpConnectionInformation [] GetActiveTcpConnections ()
{
List<string []> list = new List<string []> ();
GetRows (TcpFile, list);
GetRows (Tcp6File, list);
TcpConnectionInformation [] ret = new TcpConnectionInformation [list.Count];
for (int i = 0; i < ret.Length; i++) {
// sl local_address rem_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout inode
IPEndPoint local = ToEndpoint (list [i] [1]);
IPEndPoint remote = ToEndpoint (list [i] [2]);
TcpState state = (TcpState) int.Parse (list [i] [3], NumberStyles.HexNumber);
ret [i] = new SystemTcpConnectionInformation (local, remote, state);
}
return ret;
}
public override IPEndPoint [] GetActiveTcpListeners ()
{
List<string []> list = new List<string []> ();
GetRows (TcpFile, list);
GetRows (Tcp6File, list);
return GetLocalAddresses (list);
}
public override IPEndPoint [] GetActiveUdpListeners ()
{
List<string []> list = new List<string []> ();
GetRows (UdpFile, list);
GetRows (Udp6File, list);
return GetLocalAddresses (list);
}
public override IcmpV4Statistics GetIcmpV4Statistics ()
{
return new MibIcmpV4Statistics (GetProperties4 ("Icmp"));
}
public override IcmpV6Statistics GetIcmpV6Statistics ()
{
return new MibIcmpV6Statistics (GetProperties6 ("Icmp6"));
}
public override IPGlobalStatistics GetIPv4GlobalStatistics ()
{
return new MibIPGlobalStatistics (GetProperties4 ("Ip"));
}
public override IPGlobalStatistics GetIPv6GlobalStatistics ()
{
return new MibIPGlobalStatistics (GetProperties6 ("Ip6"));
}
public override TcpStatistics GetTcpIPv4Statistics ()
{
return new MibTcpStatistics (GetProperties4 ("Tcp"));
}
public override TcpStatistics GetTcpIPv6Statistics ()
{
// There is no TCP info in /proc/net/snmp,
// so it is shared with IPv4 info.
return new MibTcpStatistics (GetProperties4 ("Tcp"));
}
public override UdpStatistics GetUdpIPv4Statistics ()
{
return new MibUdpStatistics (GetProperties4 ("Udp"));
}
public override UdpStatistics GetUdpIPv6Statistics ()
{
return new MibUdpStatistics (GetProperties6 ("Udp6"));
}
}
internal static class UnixIPGlobalPropertiesFactoryPal {
public static IPGlobalProperties Create ()
{
#if MONODROID
return new AndroidIPGlobalProperties ();
#elif MONOTOUCH || XAMMAC
return new UnixIPGlobalProperties ();
#elif MONO
switch (Environment.OSVersion.Platform) {
case PlatformID.Unix:
MibIPGlobalProperties impl = null;
if (Directory.Exists (MibIPGlobalProperties.ProcDir)) {
impl = new MibIPGlobalProperties (MibIPGlobalProperties.ProcDir);
if (File.Exists (impl.StatisticsFile))
return impl;
}
if (Directory.Exists (MibIPGlobalProperties.CompatProcDir)) {
impl = new MibIPGlobalProperties (MibIPGlobalProperties.CompatProcDir);
if (File.Exists (impl.StatisticsFile))
return impl;
}
return new UnixIPGlobalProperties ();
default:
#if !WIN_PLATFORM
return new UnixIPGlobalProperties ();
#endif
return null;
}
#else
(new NetworkInformationPermission (NetworkInformationAccess.Read)).Demand ();
return new SystemIPGlobalProperties ();
#endif
}
}
}

View File

@@ -0,0 +1,114 @@
//
// System.Net.NetworkInformation.IPGlobalProperties
//
// Authors:
// Gonzalo Paniagua Javier (gonzalo@novell.com)
// Atsushi Enomoto (atsushi@ximian.com)
//
// Copyright (c) 2006-2007 Novell, Inc. (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.Collections.Specialized;
using System.Globalization;
namespace System.Net.NetworkInformation {
class MibIPGlobalStatistics : IPGlobalStatistics
{
StringDictionary dic;
public MibIPGlobalStatistics (StringDictionary dic)
{
this.dic = dic;
}
long Get (string name)
{
return dic [name] != null ? long.Parse (dic [name], NumberFormatInfo.InvariantInfo) : 0;
}
public override int DefaultTtl {
get { return (int) Get ("DefaultTTL"); }
}
public override bool ForwardingEnabled {
get { return Get ("Forwarding") != 0; }
}
public override int NumberOfInterfaces {
get { return (int) Get ("NumIf"); }
}
public override int NumberOfIPAddresses {
get { return (int) Get ("NumAddr"); }
}
public override int NumberOfRoutes {
get { return (int) Get ("NumRoutes"); }
}
public override long OutputPacketRequests {
get { return Get ("OutRequests"); }
}
public override long OutputPacketRoutingDiscards {
get { return Get ("RoutingDiscards"); }
}
public override long OutputPacketsDiscarded {
get { return Get ("OutDiscards"); }
}
public override long OutputPacketsWithNoRoute {
get { return Get ("OutNoRoutes"); }
}
public override long PacketFragmentFailures {
get { return Get ("FragFails"); }
}
public override long PacketReassembliesRequired {
get { return Get ("ReasmReqds"); }
}
public override long PacketReassemblyFailures {
get { return Get ("ReasmFails"); }
}
public override long PacketReassemblyTimeout {
get { return Get ("ReasmTimeout"); }
}
public override long PacketsFragmented {
get { return Get ("FragOks"); }
}
public override long PacketsReassembled {
get { return Get ("ReasmOks"); }
}
public override long ReceivedPackets {
get { return Get ("InReceives"); }
}
public override long ReceivedPacketsDelivered {
get { return Get ("InDelivers"); }
}
public override long ReceivedPacketsDiscarded {
get { return Get ("InDiscards"); }
}
public override long ReceivedPacketsForwarded {
get { return Get ("ForwDatagrams"); }
}
public override long ReceivedPacketsWithAddressErrors {
get { return Get ("InAddrErrors"); }
}
public override long ReceivedPacketsWithHeadersErrors {
get { return Get ("InHdrErrors"); }
}
public override long ReceivedPacketsWithUnknownProtocol {
get { return Get ("InUnknownProtos"); }
}
}
}

View File

@@ -0,0 +1,226 @@
//
// System.Net.NetworkInformation.IPInterfaceProperties
//
// Authors:
// Gonzalo Paniagua Javier (gonzalo@novell.com)
// Atsushi Enomoto (atsushi@ximian.com)
//
// Copyright (c) 2006-2007 Novell, Inc. (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.Collections.Generic;
using System.Net.Sockets;
using System.IO;
using System.Text.RegularExpressions;
using System.Runtime.InteropServices;
namespace System.Net.NetworkInformation {
abstract class UnixIPInterfaceProperties : IPInterfaceProperties
{
protected IPv4InterfaceProperties ipv4iface_properties;
protected UnixNetworkInterface iface;
List <IPAddress> addresses;
IPAddressCollection dns_servers;
public UnixIPInterfaceProperties (UnixNetworkInterface iface, List <IPAddress> addresses)
{
this.iface = iface;
this.addresses = addresses;
}
public override IPv6InterfaceProperties GetIPv6Properties ()
{
throw new NotImplementedException ();
}
#if MONODROID
[DllImport ("__Internal")]
static extern int _monodroid_get_dns_servers (out IntPtr dns_servers_array);
void GetDNSServersFromOS ()
{
IntPtr dsa;
int len = _monodroid_get_dns_servers (out dsa);
if (len <= 0)
return;
var servers = new IntPtr [len];
Marshal.Copy (dsa, servers, 0, len);
dns_servers = new IPAddressCollection ();
foreach (IntPtr s in servers) {
string server_ip = Marshal.PtrToStringAnsi (s);
Marshal.FreeHGlobal (s);
IPAddress addr;
if (!IPAddress.TryParse (server_ip, out addr))
continue;
dns_servers.InternalAdd (addr);
}
Marshal.FreeHGlobal (dsa);
}
#else
static Regex ns = new Regex (@"\s*nameserver\s+(?<address>.*)");
static Regex search = new Regex (@"\s*search\s+(?<domain>.*)");
string dns_suffix;
DateTime last_parse;
void ParseResolvConf ()
{
try {
DateTime wt = File.GetLastWriteTime ("/etc/resolv.conf");
if (wt <= last_parse)
return;
last_parse = wt;
dns_suffix = "";
dns_servers = new IPAddressCollection ();
using (StreamReader reader = new StreamReader ("/etc/resolv.conf")) {
string str;
string line;
while ((line = reader.ReadLine ()) != null) {
line = line.Trim ();
if (line.Length == 0 || line [0] == '#')
continue;
Match match = ns.Match (line);
if (match.Success) {
try {
str = match.Groups ["address"].Value;
str = str.Trim ();
dns_servers.InternalAdd (IPAddress.Parse (str));
} catch {
}
} else {
match = search.Match (line);
if (match.Success) {
str = match.Groups ["domain"].Value;
string [] parts = str.Split (',');
dns_suffix = parts [0].Trim ();
}
}
}
}
} catch {
}
}
#endif
public override IPAddressInformationCollection AnycastAddresses {
get {
var c = new IPAddressInformationCollection ();
foreach (IPAddress address in addresses) {
c.InternalAdd (new SystemIPAddressInformation (address, false, false));
}
return c;
}
}
[MonoTODO ("Always returns an empty collection.")]
public override IPAddressCollection DhcpServerAddresses {
get {
// There are lots of different DHCP clients
// that all store their configuration differently.
// I'm not sure what to do here.
IPAddressCollection coll = new IPAddressCollection ();
return coll;
}
}
public override IPAddressCollection DnsAddresses {
get {
#if MONODROID
GetDNSServersFromOS ();
#else
ParseResolvConf ();
#endif
return dns_servers;
}
}
public override string DnsSuffix {
get {
#if MONODROID
return String.Empty;
#else
ParseResolvConf ();
return dns_suffix;
#endif
}
}
[MonoTODO ("Always returns true")]
public override bool IsDnsEnabled {
get {
return true;
}
}
[MonoTODO ("Always returns false")]
public override bool IsDynamicDnsEnabled {
get {
return false;
}
}
public override MulticastIPAddressInformationCollection MulticastAddresses {
get {
var multicastAddresses = new MulticastIPAddressInformationCollection ();
foreach (IPAddress address in addresses) {
byte[] addressBytes = address.GetAddressBytes ();
if (addressBytes[0] >= 224 && addressBytes[0] <= 239) {
multicastAddresses.InternalAdd (new SystemMulticastIPAddressInformation (new SystemIPAddressInformation (address, true, false)));
}
}
return multicastAddresses;
}
}
public override UnicastIPAddressInformationCollection UnicastAddresses {
get {
var unicastAddresses = new UnicastIPAddressInformationCollection ();
foreach (IPAddress address in addresses) {
switch (address.AddressFamily) {
case AddressFamily.InterNetwork:
byte top = address.GetAddressBytes () [0];
if (top >= 224 && top <= 239)
continue;
unicastAddresses.InternalAdd (new LinuxUnicastIPAddressInformation (address));
break;
case AddressFamily.InterNetworkV6:
if (address.IsIPv6Multicast)
continue;
unicastAddresses.InternalAdd (new LinuxUnicastIPAddressInformation (address));
break;
}
}
return unicastAddresses;
}
}
[MonoTODO ("Always returns an empty collection.")]
public override IPAddressCollection WinsServersAddresses {
get {
// I do SUPPOSE we could scrape /etc/samba/smb.conf, but.. yeesh.
return new IPAddressCollection ();
}
}
}
}

View File

@@ -0,0 +1,63 @@
//
// System.Net.NetworkInformation.IPv4InterfaceProperties
//
// Authors:
// Gonzalo Paniagua Javier (gonzalo@novell.com)
// Atsushi Enomoto (atsushi@ximian.com)
// Marek Habersack (mhabersack@novell.com)
//
// Copyright (c) 2006-2007 Novell, Inc. (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
namespace System.Net.NetworkInformation {
abstract class UnixIPv4InterfaceProperties : IPv4InterfaceProperties
{
protected UnixNetworkInterface iface;
public UnixIPv4InterfaceProperties (UnixNetworkInterface iface)
{
this.iface = iface;
}
public override int Index {
get { return iface.NameIndex; }
}
// TODO: how to discover that?
public override bool IsAutomaticPrivateAddressingActive {
get { return false; }
}
// TODO: how to discover that?
public override bool IsAutomaticPrivateAddressingEnabled {
get { return false; }
}
// TODO: how to discover that? The only way is distribution-specific...
public override bool IsDhcpEnabled {
get { return false; }
}
public override bool UsesWins {
get { return false; }
}
}
}

View File

@@ -0,0 +1,126 @@
//
// System.Net.NetworkInformation.IcmpV4Statistics
//
// Authors:
// Gonzalo Paniagua Javier (gonzalo@novell.com)
// Atsushi Enomoto (atsushi@ximian.com)
//
// Copyright (c) 2006-2007 Novell, Inc. (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.Collections.Specialized;
using System.Globalization;
namespace System.Net.NetworkInformation {
class MibIcmpV4Statistics : IcmpV4Statistics
{
StringDictionary dic;
public MibIcmpV4Statistics (StringDictionary dic)
{
this.dic = dic;
}
long Get (string name)
{
return dic [name] != null ? long.Parse (dic [name], NumberFormatInfo.InvariantInfo) : 0;
}
public override long AddressMaskRepliesReceived {
get { return Get ("InAddrMaskReps"); }
}
public override long AddressMaskRepliesSent {
get { return Get ("OutAddrMaskReps"); }
}
public override long AddressMaskRequestsReceived {
get { return Get ("InAddrMasks"); }
}
public override long AddressMaskRequestsSent {
get { return Get ("OutAddrMasks"); }
}
public override long DestinationUnreachableMessagesReceived {
get { return Get ("InDestUnreachs"); }
}
public override long DestinationUnreachableMessagesSent {
get { return Get ("OutDestUnreachs"); }
}
public override long EchoRepliesReceived {
get { return Get ("InEchoReps"); }
}
public override long EchoRepliesSent {
get { return Get ("OutEchoReps"); }
}
public override long EchoRequestsReceived {
get { return Get ("InEchos"); }
}
public override long EchoRequestsSent {
get { return Get ("OutEchos"); }
}
public override long ErrorsReceived {
get { return Get ("InErrors"); }
}
public override long ErrorsSent {
get { return Get ("OutErrors"); }
}
public override long MessagesReceived {
get { return Get ("InMsgs"); }
}
public override long MessagesSent {
get { return Get ("OutMsgs"); }
}
public override long ParameterProblemsReceived {
get { return Get ("InParmProbs"); }
}
public override long ParameterProblemsSent {
get { return Get ("OutParmProbs"); }
}
public override long RedirectsReceived {
get { return Get ("InRedirects"); }
}
public override long RedirectsSent {
get { return Get ("OutRedirects"); }
}
public override long SourceQuenchesReceived {
get { return Get ("InSrcQuenchs"); }
}
public override long SourceQuenchesSent {
get { return Get ("OutSrcQuenchs"); }
}
public override long TimeExceededMessagesReceived {
get { return Get ("InTimeExcds"); }
}
public override long TimeExceededMessagesSent {
get { return Get ("OutTimeExcds"); }
}
public override long TimestampRepliesReceived {
get { return Get ("InTimestampReps"); }
}
public override long TimestampRepliesSent {
get { return Get ("OutTimestampReps"); }
}
public override long TimestampRequestsReceived {
get { return Get ("InTimestamps"); }
}
public override long TimestampRequestsSent {
get { return Get ("OutTimestamps"); }
}
}
}

Some files were not shown because too many files have changed in this diff Show More