Code:
/ Net / Net / 3.5.50727.3053 / DEVDIV / depot / DevDiv / releases / whidbey / netfxsp / ndp / fx / src / Net / System / Net / NetworkInformation / SystemIPGlobalProperties.cs / 1 / SystemIPGlobalProperties.cs
namespace System.Net.NetworkInformation {
using System.Net;
using System.Net.Sockets;
using System.Security.Permissions;
using System;
using System.Runtime.InteropServices;
using System.Collections;
using System.ComponentModel;
using System.Threading;
internal class SystemIPGlobalProperties:IPGlobalProperties {
private FixedInfo fixedInfo;
private bool fixedInfoInitialized = false;
//changing these require a reboot, so we'll cache them instead.
private static string hostName = null;
private static string domainName = null;
static object syncObject = new object();
internal SystemIPGlobalProperties() {
}
internal static FixedInfo GetFixedInfo(){
uint size = 0;
SafeLocalFree buffer = null;
FixedInfo fixedInfo = new FixedInfo();
//first we need to get the size of the buffer
uint result = UnsafeNetInfoNativeMethods.GetNetworkParams(SafeLocalFree.Zero,ref size);
while (result == IpHelperErrors.ErrorBufferOverflow) {
try {
//now we allocate the buffer and read the network parameters.
buffer = SafeLocalFree.LocalAlloc((int)size);
result = UnsafeNetInfoNativeMethods.GetNetworkParams(buffer,ref size);
if ( result == IpHelperErrors.Success ) {
fixedInfo = new FixedInfo( (FIXED_INFO)Marshal.PtrToStructure(buffer.DangerousGetHandle(),typeof(FIXED_INFO)));
}
}
finally {
if(buffer != null){
buffer.Close();
}
}
}
//if the result include there being no information, we'll still throw
if (result != IpHelperErrors.Success) {
throw new NetworkInformationException((int)result);
}
return fixedInfo;
}
internal FixedInfo FixedInfo {
get {
if (!fixedInfoInitialized) {
lock(this){
if (!fixedInfoInitialized) {
fixedInfo = GetFixedInfo();
fixedInfoInitialized = true;
}
}
}
return fixedInfo;
}
}
/// Specifies the host name for the local computer.
public override string HostName{
get {
if(hostName == null){
lock(syncObject){
if(hostName == null){
hostName = FixedInfo.HostName;
domainName = FixedInfo.DomainName;
}
}
}
return hostName;
}
}
/// Specifies the domain in which the local computer is registered.
public override string DomainName{
get {
if(domainName == null){
lock(syncObject){
if(domainName == null){
hostName = FixedInfo.HostName;
domainName = FixedInfo.DomainName;
}
}
}
return domainName;
}
}
///
/// The type of node.
///
///
/// The exact mechanism by which NetBIOS names are resolved to IP addresses
/// depends on the node's configured NetBIOS Node Type. Broadcast - uses broadcast
/// NetBIOS Name Queries for name registration and resolution.
/// PeerToPeer - uses a NetBIOS name server (NBNS), such as Windows Internet
/// Name Service (WINS), to resolve NetBIOS names.
/// Mixed - uses Broadcast then PeerToPeer.
/// Hybrid - uses PeerToPeer then Broadcast.
///
public override NetBiosNodeType NodeType{get {
return (NetBiosNodeType)FixedInfo.NodeType;}
}
/// Specifies the DHCP scope name.
public override string DhcpScopeName{get {
return FixedInfo.ScopeId;}
}
/// Specifies whether the local computer is acting as an WINS proxy.
public override bool IsWinsProxy{get {
return (FixedInfo.EnableProxy);}
}
public override TcpConnectionInformation[] GetActiveTcpConnections(){
ArrayList list = new ArrayList();
TcpConnectionInformation[] connections = GetAllTcpConnections();
foreach(TcpConnectionInformation connection in connections){
if(connection.State != TcpState.Listen){
list.Add(connection);
}
}
connections = new TcpConnectionInformation[list.Count];
for (int i=0; i< list.Count;i++) {
connections[i]=(TcpConnectionInformation)list[i];
}
return connections;
}
public override IPEndPoint[] GetActiveTcpListeners (){
ArrayList list = new ArrayList();
TcpConnectionInformation[] connections = GetAllTcpConnections();
foreach(TcpConnectionInformation connection in connections){
if(connection.State == TcpState.Listen){
list.Add(connection.LocalEndPoint);
}
}
IPEndPoint[] endPoints = new IPEndPoint[list.Count];
for (int i=0; i< list.Count;i++) {
endPoints[i]=(IPEndPoint)list[i];
}
return endPoints;
}
///
/// Gets the active tcp connections. Uses the native GetTcpTable api.
private TcpConnectionInformation[] GetAllTcpConnections(){
uint size = 0;
SafeLocalFree buffer = null;
SystemTcpConnectionInformation[] tcpConnections = null;
//get the size of buffer needed
uint result = UnsafeNetInfoNativeMethods.GetTcpTable(SafeLocalFree.Zero,ref size,true);
while (result == IpHelperErrors.ErrorInsufficientBuffer) {
try {
//allocate the buffer and get the tcptable
buffer = SafeLocalFree.LocalAlloc((int)size);
result = UnsafeNetInfoNativeMethods.GetTcpTable(buffer,ref size,true);
if ( result == IpHelperErrors.Success ) {
//the table info just gives us the number of rows.
IntPtr newPtr = buffer.DangerousGetHandle();
MibTcpTable tcpTableInfo = (MibTcpTable)Marshal.PtrToStructure(newPtr,typeof(MibTcpTable));
if (tcpTableInfo.numberOfEntries > 0) {
tcpConnections = new SystemTcpConnectionInformation[tcpTableInfo.numberOfEntries];
//we need to skip over the tableinfo to get the inline rows
newPtr = (IntPtr)((long)newPtr + Marshal.SizeOf(tcpTableInfo.numberOfEntries));
for (int i=0;iGets the active udp listeners. Uses the native GetUdpTable api.
public override IPEndPoint[] GetActiveUdpListeners(){
uint size = 0;
SafeLocalFree buffer = null;
IPEndPoint[] udpListeners = null;
//get the size of buffer needed
uint result = UnsafeNetInfoNativeMethods.GetUdpTable(SafeLocalFree.Zero,ref size,true);
while (result == IpHelperErrors.ErrorInsufficientBuffer) {
try {
//allocate the buffer and get the udptable
buffer = SafeLocalFree.LocalAlloc((int)size);
result = UnsafeNetInfoNativeMethods.GetUdpTable(buffer,ref size,true);
if ( result == IpHelperErrors.Success) {
//the table info just gives us the number of rows.
IntPtr newPtr = buffer.DangerousGetHandle();
MibUdpTable udpTableInfo = (MibUdpTable)Marshal.PtrToStructure(newPtr,typeof(MibUdpTable));
if (udpTableInfo.numberOfEntries > 0) {
udpListeners = new IPEndPoint[udpTableInfo.numberOfEntries];
//we need to skip over the tableinfo to get the inline rows
newPtr = (IntPtr)((long)newPtr + Marshal.SizeOf(udpTableInfo.numberOfEntries));
for (int i=0;iSpecifies the host name for the local computer.
public override string HostName{
get {
if(hostName == null){
lock(syncObject){
if(hostName == null){
hostName = FixedInfo.HostName;
domainName = FixedInfo.DomainName;
}
}
}
return hostName;
}
}
/// Specifies the domain in which the local computer is registered.
public override string DomainName{
get {
if(domainName == null){
lock(syncObject){
if(domainName == null){
hostName = FixedInfo.HostName;
domainName = FixedInfo.DomainName;
}
}
}
return domainName;
}
}
///
/// The type of node.
///
///
/// The exact mechanism by which NetBIOS names are resolved to IP addresses
/// depends on the node's configured NetBIOS Node Type. Broadcast - uses broadcast
/// NetBIOS Name Queries for name registration and resolution.
/// PeerToPeer - uses a NetBIOS name server (NBNS), such as Windows Internet
/// Name Service (WINS), to resolve NetBIOS names.
/// Mixed - uses Broadcast then PeerToPeer.
/// Hybrid - uses PeerToPeer then Broadcast.
///
public override NetBiosNodeType NodeType{get {
return (NetBiosNodeType)FixedInfo.NodeType;}
}
/// Specifies the DHCP scope name.
public override string DhcpScopeName{get {
return FixedInfo.ScopeId;}
}
/// Specifies whether the local computer is acting as an WINS proxy.
public override bool IsWinsProxy{get {
return (FixedInfo.EnableProxy);}
}
public override TcpConnectionInformation[] GetActiveTcpConnections(){
ArrayList list = new ArrayList();
TcpConnectionInformation[] connections = GetAllTcpConnections();
foreach(TcpConnectionInformation connection in connections){
if(connection.State != TcpState.Listen){
list.Add(connection);
}
}
connections = new TcpConnectionInformation[list.Count];
for (int i=0; i< list.Count;i++) {
connections[i]=(TcpConnectionInformation)list[i];
}
return connections;
}
public override IPEndPoint[] GetActiveTcpListeners (){
ArrayList list = new ArrayList();
TcpConnectionInformation[] connections = GetAllTcpConnections();
foreach(TcpConnectionInformation connection in connections){
if(connection.State == TcpState.Listen){
list.Add(connection.LocalEndPoint);
}
}
IPEndPoint[] endPoints = new IPEndPoint[list.Count];
for (int i=0; i< list.Count;i++) {
endPoints[i]=(IPEndPoint)list[i];
}
return endPoints;
}
///
/// Gets the active tcp connections. Uses the native GetTcpTable api.
private TcpConnectionInformation[] GetAllTcpConnections(){
uint size = 0;
SafeLocalFree buffer = null;
SystemTcpConnectionInformation[] tcpConnections = null;
//get the size of buffer needed
uint result = UnsafeNetInfoNativeMethods.GetTcpTable(SafeLocalFree.Zero,ref size,true);
while (result == IpHelperErrors.ErrorInsufficientBuffer) {
try {
//allocate the buffer and get the tcptable
buffer = SafeLocalFree.LocalAlloc((int)size);
result = UnsafeNetInfoNativeMethods.GetTcpTable(buffer,ref size,true);
if ( result == IpHelperErrors.Success ) {
//the table info just gives us the number of rows.
IntPtr newPtr = buffer.DangerousGetHandle();
MibTcpTable tcpTableInfo = (MibTcpTable)Marshal.PtrToStructure(newPtr,typeof(MibTcpTable));
if (tcpTableInfo.numberOfEntries > 0) {
tcpConnections = new SystemTcpConnectionInformation[tcpTableInfo.numberOfEntries];
//we need to skip over the tableinfo to get the inline rows
newPtr = (IntPtr)((long)newPtr + Marshal.SizeOf(tcpTableInfo.numberOfEntries));
for (int i=0;iGets the active udp listeners. Uses the native GetUdpTable api.
public override IPEndPoint[] GetActiveUdpListeners(){
uint size = 0;
SafeLocalFree buffer = null;
IPEndPoint[] udpListeners = null;
//get the size of buffer needed
uint result = UnsafeNetInfoNativeMethods.GetUdpTable(SafeLocalFree.Zero,ref size,true);
while (result == IpHelperErrors.ErrorInsufficientBuffer) {
try {
//allocate the buffer and get the udptable
buffer = SafeLocalFree.LocalAlloc((int)size);
result = UnsafeNetInfoNativeMethods.GetUdpTable(buffer,ref size,true);
if ( result == IpHelperErrors.Success) {
//the table info just gives us the number of rows.
IntPtr newPtr = buffer.DangerousGetHandle();
MibUdpTable udpTableInfo = (MibUdpTable)Marshal.PtrToStructure(newPtr,typeof(MibUdpTable));
if (udpTableInfo.numberOfEntries > 0) {
udpListeners = new IPEndPoint[udpTableInfo.numberOfEntries];
//we need to skip over the tableinfo to get the inline rows
newPtr = (IntPtr)((long)newPtr + Marshal.SizeOf(udpTableInfo.numberOfEntries));
for (int i=0;i
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- IERequestCache.cs
- BitmapPalettes.cs
- Color.cs
- PagesSection.cs
- _NetRes.cs
- OleDbCommandBuilder.cs
- WorkflowInstanceSuspendedRecord.cs
- UrlMappingCollection.cs
- HtmlInputText.cs
- PrincipalPermissionMode.cs
- RowUpdatedEventArgs.cs
- MenuEventArgs.cs
- FrugalList.cs
- GridViewCancelEditEventArgs.cs
- xmlfixedPageInfo.cs
- ApplicationSettingsBase.cs
- AxisAngleRotation3D.cs
- AttributeProviderAttribute.cs
- XmlDataLoader.cs
- WebPartConnectVerb.cs
- FilteredReadOnlyMetadataCollection.cs
- ICspAsymmetricAlgorithm.cs
- DataControlReference.cs
- Int64Converter.cs
- BitmapInitialize.cs
- DataGridViewComboBoxEditingControl.cs
- PresentationTraceSources.cs
- ButtonBaseAdapter.cs
- StoreContentChangedEventArgs.cs
- ManagementObjectCollection.cs
- Fonts.cs
- FtpRequestCacheValidator.cs
- EntityCommandCompilationException.cs
- IRCollection.cs
- PageAsyncTask.cs
- TableParaClient.cs
- PtsCache.cs
- StrongTypingException.cs
- SqlCacheDependencyDatabase.cs
- RangeValuePatternIdentifiers.cs
- EntityDataSourceDataSelection.cs
- TableRow.cs
- WebPartConnectionsEventArgs.cs
- ObjectDataSourceFilteringEventArgs.cs
- DataGridColumnHeaderAutomationPeer.cs
- OleDbSchemaGuid.cs
- ProviderConnectionPointCollection.cs
- XmlPreloadedResolver.cs
- ExtractedStateEntry.cs
- XmlElement.cs
- InvalidOleVariantTypeException.cs
- DataChangedEventManager.cs
- PermissionSetEnumerator.cs
- WebPartActionVerb.cs
- AssemblyLoader.cs
- XmlEventCache.cs
- Misc.cs
- ValidationErrorCollection.cs
- BooleanAnimationUsingKeyFrames.cs
- ThumbButtonInfoCollection.cs
- ProtocolsConfigurationEntry.cs
- IisTraceListener.cs
- WebRequestModulesSection.cs
- IfAction.cs
- CompilerGeneratedAttribute.cs
- HandleCollector.cs
- StrokeCollectionDefaultValueFactory.cs
- XamlRtfConverter.cs
- UrlPropertyAttribute.cs
- TraceRecords.cs
- TextEditorThreadLocalStore.cs
- TableRow.cs
- _RequestCacheProtocol.cs
- TdsRecordBufferSetter.cs
- WebRequestModulesSection.cs
- WorkflowServiceHostFactory.cs
- XMLSyntaxException.cs
- LinearGradientBrush.cs
- ShowExpandedMultiValueConverter.cs
- ColorBlend.cs
- ExternalFile.cs
- WorkflowHostingEndpoint.cs
- DataControlExtensions.cs
- ScrollChrome.cs
- CodeFieldReferenceExpression.cs
- DbgCompiler.cs
- NativeRightsManagementAPIsStructures.cs
- MetadataCache.cs
- HtmlInputFile.cs
- CompatibleIComparer.cs
- MsmqIntegrationOutputChannel.cs
- RoutedEventValueSerializer.cs
- XmlSchemaException.cs
- SafeNativeMethods.cs
- GraphicsContainer.cs
- MsmqIntegrationValidationBehavior.cs
- GlobalProxySelection.cs
- LowerCaseStringConverter.cs
- Mouse.cs
- SocketStream.cs