Code:
/ Dotnetfx_Vista_SP2 / Dotnetfx_Vista_SP2 / 8.0.50727.4016 / DEVDIV / depot / DevDiv / releases / whidbey / NetFxQFE / ndp / fx / src / Net / System / Net / NetworkInformation / SystemIPAddressInformation.cs / 1 / SystemIPAddressInformation.cs
///
/// Provides support for ip configuation information and statistics.
///
///
namespace System.Net.NetworkInformation {
using System.Net;
using System.Net.Sockets;
using System;
using System.Runtime.InteropServices;
using System.Collections;
using System.ComponentModel;
using System.Security.Permissions;
using Microsoft.Win32;
//this is the main addressinformation class that contains the ipaddress
//and other properties
internal class SystemIPAddressInformation:IPAddressInformation{
IPAddress address;
internal bool transient = false;
internal bool dnsEligible = true;
/*
// Consider removing.
internal SystemIPAddressInformation(){}
*/
internal SystemIPAddressInformation(IPAddress address) {
this.address = address;
if (address.AddressFamily == AddressFamily.InterNetwork) {
//DngEligible is true except for 169.254.x.x addresses (the APIPA addresses )
dnsEligible = !((address.m_Address & 0x0000FEA9)>0);
}
}
internal SystemIPAddressInformation(IpAdapterUnicastAddress adapterAddress,IPAddress address) {
this.address = address;
transient = (adapterAddress.flags & AdapterAddressFlags.Transient)>0;
dnsEligible = (adapterAddress.flags & AdapterAddressFlags.DnsEligible)>0;
}
internal SystemIPAddressInformation(IpAdapterAddress adapterAddress,IPAddress address) {
this.address = address;
transient = (adapterAddress.flags & AdapterAddressFlags.Transient)>0;
dnsEligible = (adapterAddress.flags & AdapterAddressFlags.DnsEligible)>0;
}
public override IPAddress Address{get {return address;}}
/// The address is a cluster address and shouldn't be used by most applications.
public override bool IsTransient{
get {
return (transient);
}
}
/// This address can be used for DNS.
public override bool IsDnsEligible{
get {
return (dnsEligible);
}
}
//helper method that marshals the addressinformation into the classes
internal static IPAddressCollection ToAddressCollection(IntPtr ptr,IPVersion versionSupported) {
//we don't know the number of addresses up front, so we create an arraylist
//to temporarily store them.
IPAddressCollection addressList = new IPAddressCollection();
//if there is no address, just return;
if (ptr == IntPtr.Zero)
return addressList;
//get the first address
IpAdapterAddress addr = (IpAdapterAddress)Marshal.PtrToStructure(ptr,typeof(IpAdapterAddress));
//determine the address family used to create the IPAddress
AddressFamily family = (addr.address.addressLength > 16)?AddressFamily.InterNetworkV6:AddressFamily.InterNetwork;
SocketAddress sockAddress = new SocketAddress(family,(int)addr.address.addressLength);
Marshal.Copy(addr.address.address,sockAddress.m_Buffer,0,addr.address.addressLength);
//unfortunately, the only way to currently create an ipaddress is through IPEndPoint
IPEndPoint ep;
if (family == AddressFamily.InterNetwork )
ep = (IPEndPoint)IPEndPoint.Any.Create(sockAddress);
else
ep = (IPEndPoint)IPEndPoint.IPv6Any.Create(sockAddress);
//add the ipaddress to the arraylist
addressList.InternalAdd(ep.Address);
//repeat for all of the addresses
while ( addr.next != IntPtr.Zero ) {
addr = (IpAdapterAddress)Marshal.PtrToStructure(addr.next,typeof(IpAdapterAddress));
//determine the address family used to create the IPAddress
family = (addr.address.addressLength > 16)?AddressFamily.InterNetworkV6:AddressFamily.InterNetwork;
//only add addresses that are the same type as what is supported by the interface
//this fixes a bug in iphelper regarding dns addresses
if (((family == AddressFamily.InterNetwork) && ((versionSupported & IPVersion.IPv4) > 0))
|| ((family == AddressFamily.InterNetworkV6) && ((versionSupported & IPVersion.IPv6) > 0))) {
sockAddress = new SocketAddress(family,(int)addr.address.addressLength);
Marshal.Copy(addr.address.address,sockAddress.m_Buffer,0,addr.address.addressLength);
//use the endpoint to create the ipaddress
if (family == AddressFamily.InterNetwork )
ep = (IPEndPoint)IPEndPoint.Any.Create(sockAddress);
else
ep = (IPEndPoint)IPEndPoint.IPv6Any.Create(sockAddress);
addressList.InternalAdd(ep.Address);
}
}
return addressList;
}
//helper method that marshals the addressinformation into the classes
internal static IPAddressInformationCollection ToAddressInformationCollection(IntPtr ptr,IPVersion versionSupported) {
//we don't know the number of addresses up front, so we create an arraylist
//to temporarily store them.
IPAddressInformationCollection addressList = new IPAddressInformationCollection();
//if there is no address, just return;
if (ptr == IntPtr.Zero)
return addressList;
//get the first address
IpAdapterAddress addr = (IpAdapterAddress)Marshal.PtrToStructure(ptr,typeof(IpAdapterAddress));
//determine the address family used to create the IPAddress
AddressFamily family = (addr.address.addressLength > 16)?AddressFamily.InterNetworkV6:AddressFamily.InterNetwork;
SocketAddress sockAddress = new SocketAddress(family,(int)addr.address.addressLength);
Marshal.Copy(addr.address.address,sockAddress.m_Buffer,0,addr.address.addressLength);
//unfortunately, the only way to currently create an ipaddress is through IPEndPoint
IPEndPoint ep;
if (family == AddressFamily.InterNetwork )
ep = (IPEndPoint)IPEndPoint.Any.Create(sockAddress);
else
ep = (IPEndPoint)IPEndPoint.IPv6Any.Create(sockAddress);
//add the ipaddress to the arraylist
addressList.InternalAdd(new SystemIPAddressInformation(addr,ep.Address));
//repeat for all of the addresses
while ( addr.next != IntPtr.Zero ) {
addr = (IpAdapterAddress)Marshal.PtrToStructure(addr.next,typeof(IpAdapterAddress));
//determine the address family used to create the IPAddress
family = (addr.address.addressLength > 16)?AddressFamily.InterNetworkV6:AddressFamily.InterNetwork;
//only add addresses that are the same type as what is supported by the interface
//this fixes a bug in iphelper regarding dns addresses
if (((family == AddressFamily.InterNetwork) && ((versionSupported & IPVersion.IPv4) > 0))
|| ((family == AddressFamily.InterNetworkV6) && ((versionSupported & IPVersion.IPv6) > 0))) {
sockAddress = new SocketAddress(family,(int)addr.address.addressLength);
Marshal.Copy(addr.address.address,sockAddress.m_Buffer,0,addr.address.addressLength);
//use the endpoint to create the ipaddress
if (family == AddressFamily.InterNetwork )
ep = (IPEndPoint)IPEndPoint.Any.Create(sockAddress);
else
ep = (IPEndPoint)IPEndPoint.IPv6Any.Create(sockAddress);
addressList.InternalAdd(new SystemIPAddressInformation(addr,ep.Address));
}
}
return addressList;
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
///
/// Provides support for ip configuation information and statistics.
///
///
namespace System.Net.NetworkInformation {
using System.Net;
using System.Net.Sockets;
using System;
using System.Runtime.InteropServices;
using System.Collections;
using System.ComponentModel;
using System.Security.Permissions;
using Microsoft.Win32;
//this is the main addressinformation class that contains the ipaddress
//and other properties
internal class SystemIPAddressInformation:IPAddressInformation{
IPAddress address;
internal bool transient = false;
internal bool dnsEligible = true;
/*
// Consider removing.
internal SystemIPAddressInformation(){}
*/
internal SystemIPAddressInformation(IPAddress address) {
this.address = address;
if (address.AddressFamily == AddressFamily.InterNetwork) {
//DngEligible is true except for 169.254.x.x addresses (the APIPA addresses )
dnsEligible = !((address.m_Address & 0x0000FEA9)>0);
}
}
internal SystemIPAddressInformation(IpAdapterUnicastAddress adapterAddress,IPAddress address) {
this.address = address;
transient = (adapterAddress.flags & AdapterAddressFlags.Transient)>0;
dnsEligible = (adapterAddress.flags & AdapterAddressFlags.DnsEligible)>0;
}
internal SystemIPAddressInformation(IpAdapterAddress adapterAddress,IPAddress address) {
this.address = address;
transient = (adapterAddress.flags & AdapterAddressFlags.Transient)>0;
dnsEligible = (adapterAddress.flags & AdapterAddressFlags.DnsEligible)>0;
}
public override IPAddress Address{get {return address;}}
/// The address is a cluster address and shouldn't be used by most applications.
public override bool IsTransient{
get {
return (transient);
}
}
/// This address can be used for DNS.
public override bool IsDnsEligible{
get {
return (dnsEligible);
}
}
//helper method that marshals the addressinformation into the classes
internal static IPAddressCollection ToAddressCollection(IntPtr ptr,IPVersion versionSupported) {
//we don't know the number of addresses up front, so we create an arraylist
//to temporarily store them.
IPAddressCollection addressList = new IPAddressCollection();
//if there is no address, just return;
if (ptr == IntPtr.Zero)
return addressList;
//get the first address
IpAdapterAddress addr = (IpAdapterAddress)Marshal.PtrToStructure(ptr,typeof(IpAdapterAddress));
//determine the address family used to create the IPAddress
AddressFamily family = (addr.address.addressLength > 16)?AddressFamily.InterNetworkV6:AddressFamily.InterNetwork;
SocketAddress sockAddress = new SocketAddress(family,(int)addr.address.addressLength);
Marshal.Copy(addr.address.address,sockAddress.m_Buffer,0,addr.address.addressLength);
//unfortunately, the only way to currently create an ipaddress is through IPEndPoint
IPEndPoint ep;
if (family == AddressFamily.InterNetwork )
ep = (IPEndPoint)IPEndPoint.Any.Create(sockAddress);
else
ep = (IPEndPoint)IPEndPoint.IPv6Any.Create(sockAddress);
//add the ipaddress to the arraylist
addressList.InternalAdd(ep.Address);
//repeat for all of the addresses
while ( addr.next != IntPtr.Zero ) {
addr = (IpAdapterAddress)Marshal.PtrToStructure(addr.next,typeof(IpAdapterAddress));
//determine the address family used to create the IPAddress
family = (addr.address.addressLength > 16)?AddressFamily.InterNetworkV6:AddressFamily.InterNetwork;
//only add addresses that are the same type as what is supported by the interface
//this fixes a bug in iphelper regarding dns addresses
if (((family == AddressFamily.InterNetwork) && ((versionSupported & IPVersion.IPv4) > 0))
|| ((family == AddressFamily.InterNetworkV6) && ((versionSupported & IPVersion.IPv6) > 0))) {
sockAddress = new SocketAddress(family,(int)addr.address.addressLength);
Marshal.Copy(addr.address.address,sockAddress.m_Buffer,0,addr.address.addressLength);
//use the endpoint to create the ipaddress
if (family == AddressFamily.InterNetwork )
ep = (IPEndPoint)IPEndPoint.Any.Create(sockAddress);
else
ep = (IPEndPoint)IPEndPoint.IPv6Any.Create(sockAddress);
addressList.InternalAdd(ep.Address);
}
}
return addressList;
}
//helper method that marshals the addressinformation into the classes
internal static IPAddressInformationCollection ToAddressInformationCollection(IntPtr ptr,IPVersion versionSupported) {
//we don't know the number of addresses up front, so we create an arraylist
//to temporarily store them.
IPAddressInformationCollection addressList = new IPAddressInformationCollection();
//if there is no address, just return;
if (ptr == IntPtr.Zero)
return addressList;
//get the first address
IpAdapterAddress addr = (IpAdapterAddress)Marshal.PtrToStructure(ptr,typeof(IpAdapterAddress));
//determine the address family used to create the IPAddress
AddressFamily family = (addr.address.addressLength > 16)?AddressFamily.InterNetworkV6:AddressFamily.InterNetwork;
SocketAddress sockAddress = new SocketAddress(family,(int)addr.address.addressLength);
Marshal.Copy(addr.address.address,sockAddress.m_Buffer,0,addr.address.addressLength);
//unfortunately, the only way to currently create an ipaddress is through IPEndPoint
IPEndPoint ep;
if (family == AddressFamily.InterNetwork )
ep = (IPEndPoint)IPEndPoint.Any.Create(sockAddress);
else
ep = (IPEndPoint)IPEndPoint.IPv6Any.Create(sockAddress);
//add the ipaddress to the arraylist
addressList.InternalAdd(new SystemIPAddressInformation(addr,ep.Address));
//repeat for all of the addresses
while ( addr.next != IntPtr.Zero ) {
addr = (IpAdapterAddress)Marshal.PtrToStructure(addr.next,typeof(IpAdapterAddress));
//determine the address family used to create the IPAddress
family = (addr.address.addressLength > 16)?AddressFamily.InterNetworkV6:AddressFamily.InterNetwork;
//only add addresses that are the same type as what is supported by the interface
//this fixes a bug in iphelper regarding dns addresses
if (((family == AddressFamily.InterNetwork) && ((versionSupported & IPVersion.IPv4) > 0))
|| ((family == AddressFamily.InterNetworkV6) && ((versionSupported & IPVersion.IPv6) > 0))) {
sockAddress = new SocketAddress(family,(int)addr.address.addressLength);
Marshal.Copy(addr.address.address,sockAddress.m_Buffer,0,addr.address.addressLength);
//use the endpoint to create the ipaddress
if (family == AddressFamily.InterNetwork )
ep = (IPEndPoint)IPEndPoint.Any.Create(sockAddress);
else
ep = (IPEndPoint)IPEndPoint.IPv6Any.Create(sockAddress);
addressList.InternalAdd(new SystemIPAddressInformation(addr,ep.Address));
}
}
return addressList;
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- RoleGroupCollection.cs
- QueryResponse.cs
- ParameterInfo.cs
- WindowsGraphicsCacheManager.cs
- PagesSection.cs
- MatrixValueSerializer.cs
- OdbcConnectionOpen.cs
- Comparer.cs
- MonitorWrapper.cs
- UDPClient.cs
- RtfControlWordInfo.cs
- Latin1Encoding.cs
- WindowsSpinner.cs
- Label.cs
- LayoutManager.cs
- BaseCollection.cs
- CompensationTokenData.cs
- FixedTextContainer.cs
- ValueTypeFixupInfo.cs
- SubstitutionList.cs
- NamespaceQuery.cs
- ProcessHostFactoryHelper.cs
- RegexWriter.cs
- FileInfo.cs
- InputProcessorProfilesLoader.cs
- XmlAnyElementAttributes.cs
- NativeMethods.cs
- ExpressionPrefixAttribute.cs
- HijriCalendar.cs
- FontConverter.cs
- InputProcessorProfilesLoader.cs
- AffineTransform3D.cs
- DiscoveryRequestHandler.cs
- Trace.cs
- XmlSchemaAttribute.cs
- PropertyBuilder.cs
- EntityViewGenerationConstants.cs
- DbDeleteCommandTree.cs
- PreservationFileReader.cs
- Rfc2898DeriveBytes.cs
- GridLengthConverter.cs
- ToolStrip.cs
- IResourceProvider.cs
- TypeConverterHelper.cs
- Stackframe.cs
- View.cs
- Renderer.cs
- StreamingContext.cs
- XmlWhitespace.cs
- MultitargetingHelpers.cs
- DataGridViewColumnHeaderCell.cs
- KeyInstance.cs
- JavascriptCallbackBehaviorAttribute.cs
- Monitor.cs
- Maps.cs
- TypeForwardedToAttribute.cs
- VScrollProperties.cs
- XPathBinder.cs
- MbpInfo.cs
- SystemIPv4InterfaceProperties.cs
- PropertyChangedEventArgs.cs
- XmlUtil.cs
- odbcmetadatafactory.cs
- relpropertyhelper.cs
- ReceiveMessageRecord.cs
- StylusPlugInCollection.cs
- JoinGraph.cs
- FlatButtonAppearance.cs
- UrlMappingsSection.cs
- WorkflowPrinting.cs
- DataContractSerializerFaultFormatter.cs
- PropertyConverter.cs
- BinaryObjectWriter.cs
- RelationshipConstraintValidator.cs
- WebServiceMethodData.cs
- XmlSchemaAttributeGroup.cs
- PieceNameHelper.cs
- BamlTreeUpdater.cs
- StorageAssociationTypeMapping.cs
- configsystem.cs
- MenuItemStyleCollection.cs
- ThicknessConverter.cs
- IdnMapping.cs
- DesignerForm.cs
- DomNameTable.cs
- LiteralControl.cs
- ViewStateException.cs
- HttpCacheParams.cs
- TdsParserSessionPool.cs
- StateChangeEvent.cs
- RetrieveVirtualItemEventArgs.cs
- XmlSchemaFacet.cs
- SignatureToken.cs
- ContextStack.cs
- MatrixStack.cs
- WorkflowRuntime.cs
- UIHelper.cs
- SortableBindingList.cs
- InputReport.cs
- Emitter.cs