Skip to content
This repository has been archived by the owner on Dec 6, 2024. It is now read-only.

Add additional Network.Host methods. #414

Merged
merged 29 commits into from
Feb 22, 2018
Merged
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
70056fa
Initial commit: Network List Manager.
Yomodo Feb 5, 2018
63745e5
Completed.
Yomodo Feb 5, 2018
bfc9041
-Fixed NetworkConnectionInfo for .NET 3.5 build;
Yomodo Feb 7, 2018
54365b7
Replace properties AdapterID, AdapterName and AdapterDescription with…
Yomodo Feb 7, 2018
06f5c9d
Merge branch 'develop' into NetworkConnection
Yomodo Feb 7, 2018
f07a2e5
Merge branch 'develop' into NetworkConnection
Yomodo Feb 7, 2018
bcc103f
NetStatistics
Yomodo Feb 9, 2018
faae479
-Fixed compile error;
Yomodo Feb 9, 2018
31348cd
Implemented ServerStatisticsInfo.
Yomodo Feb 9, 2018
cc73f0a
Code improvement, work in progress.
Yomodo Feb 10, 2018
b21b288
Added class WorkstationStatisticsInfo.
Yomodo Feb 10, 2018
66d358b
Merge branch 'NetStatistics' into NetworkConnection
Yomodo Feb 10, 2018
86543f3
Fixed project file.
Yomodo Feb 10, 2018
f7255b5
Initial commit, work in progress.
Yomodo Feb 10, 2018
e07bd9b
Code improvement, work in progress.
Yomodo Feb 11, 2018
0b29017
Code improvement.
Yomodo Feb 11, 2018
1d07550
Renamed SessionInfoLevel to SessionLevel.
Yomodo Feb 11, 2018
3ded2e5
Merge branch 'develop' into NetworkConnection
Yomodo Feb 16, 2018
84c2c4f
Added ProcessContext static class that determine the context of the c…
Yomodo Feb 16, 2018
6dd2364
Merge branch 'ProcessContext2' into NetworkConnection
Yomodo Feb 16, 2018
0b5e33b
Merge branch 'develop' into ProcessContext2
Yomodo Feb 17, 2018
ed3f988
Merge branch 'ProcessContext2' into NetworkConnection
Yomodo Feb 17, 2018
6ec583f
Merge branch 'develop' into ProcessContext2
Yomodo Feb 19, 2018
c1e017e
Merge branch 'develop' into NetworkConnection
Yomodo Feb 19, 2018
9baebd1
Merge branch 'develop' into ProcessContext2
Yomodo Feb 19, 2018
f1a6956
Merge branch 'ProcessContext2' into NetworkConnection
Yomodo Feb 19, 2018
160c068
Merge branch 'develop' into NetworkConnection
Yomodo Feb 19, 2018
8ce1e9d
Merge branch 'develop' into NetworkConnection
Yomodo Feb 20, 2018
691f2ba
Merge branch 'develop' into NetworkConnection
Yomodo Feb 20, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Code improvement.
  • Loading branch information
Yomodo committed Feb 11, 2018

Verified

This commit was signed with the committer’s verified signature.
rom1504 Romain Beaumont
commit 0b290171534fd8ad4b61b2076cf7406f37afe158
Original file line number Diff line number Diff line change
@@ -32,16 +32,16 @@ public partial class AlphaFS_HostTest
public void AlphaFS_Host_EnumerateSessions_Local_Success()
{
var host = UnitTestConstants.LocalHost;

EnumerateSessions(host, true);
EnumerateSessions(host);
}




private void EnumerateSessions(string host, bool isNetwork)
private void EnumerateSessions(string host)
{
UnitTestConstants.PrintUnitTestHeader(isNetwork);
UnitTestConstants.PrintUnitTestHeader(false);

Console.WriteLine("\nInput Host: [{0}]", host);

@@ -52,7 +52,7 @@ private void EnumerateSessions(string host, bool isNetwork)
{
//Console.WriteLine("\n\t#{0:000}\tShare: [{1}]", ++cnt, shareInfo);

if (UnitTestConstants.Dump(sessionsInfo, -18))
if (UnitTestConstants.Dump(sessionsInfo, -16))
sessionCount++;

Console.WriteLine();
Original file line number Diff line number Diff line change
@@ -29,26 +29,26 @@ public partial class AlphaFS_HostTest
// Pattern: <class>_<function>_<scenario>_<expected result>

[TestMethod]
public void AlphaFS_Host_EnumerateOpenConnections_Network_Success()
public void AlphaFS_Host_EnumerateOpenConnections_Local_Success()
{
var host = "localhost"; //UnitTestConstants.LocalHost;
var host = UnitTestConstants.LocalHost;
var share = UnitTestConstants.SysDrive[0] + Alphaleonis.Win32.Filesystem.Path.NetworkDriveSeparator;

// Create an active connection to the "remote" host.
var currentDir = System.IO.Directory.GetCurrentDirectory();
System.IO.Directory.SetCurrentDirectory(@"\\" + host + @"\" + share);
System.IO.Directory.SetCurrentDirectory(Alphaleonis.Win32.Filesystem.Path.UncPrefix + host + Alphaleonis.Win32.Filesystem.Path.DirectorySeparator + share);

EnumerateOpenConnections(host, share, true);
EnumerateOpenConnections(host, share);

System.IO.Directory.SetCurrentDirectory(currentDir);
}




private void EnumerateOpenConnections(string host, string share, bool isNetwork)
private void EnumerateOpenConnections(string host, string share)
{
UnitTestConstants.PrintUnitTestHeader(isNetwork);
UnitTestConstants.PrintUnitTestHeader(false);

if (!UnitTestConstants.IsAdmin())
Assert.Inconclusive();
Original file line number Diff line number Diff line change
@@ -29,27 +29,19 @@ public partial class AlphaFS_HostTest
// Pattern: <class>_<function>_<scenario>_<expected result>

[TestMethod]
public void AlphaFS_Host_EnumerateOpenResources_Network_Success()
public void AlphaFS_Host_EnumerateOpenResources_Local_Success()
{
var host = "localhost"; //UnitTestConstants.LocalHost;
var share = UnitTestConstants.SysDrive[0] + Alphaleonis.Win32.Filesystem.Path.NetworkDriveSeparator;
var host = UnitTestConstants.LocalHost;

// Create an active connection to the "remote" host.
var currentDir = System.IO.Directory.GetCurrentDirectory();
System.IO.Directory.SetCurrentDirectory(@"\\" + host + @"\" + share);

//EnumerateOpenResources(UnitTestConstants.LocalHost, true);
EnumerateOpenResources(host, true);

System.IO.Directory.SetCurrentDirectory(currentDir);
EnumerateOpenResources(host);
}




private void EnumerateOpenResources(string host, bool isNetwork)
private void EnumerateOpenResources(string host)
{
UnitTestConstants.PrintUnitTestHeader(isNetwork);
UnitTestConstants.PrintUnitTestHeader(false);

if (!UnitTestConstants.IsAdmin())
Assert.Inconclusive();
Original file line number Diff line number Diff line change
@@ -29,19 +29,19 @@ public partial class AlphaFS_HostTest
// Pattern: <class>_<function>_<scenario>_<expected result>

[TestMethod]
public void AlphaFS_Host_EnumerateShares_Network_Success()
public void AlphaFS_Host_EnumerateShares_Local_Success()
{
var host = UnitTestConstants.LocalHost;

EnumerateShares(host, true);
EnumerateShares(host);
}




private void EnumerateShares(string host, bool isNetwork)
private void EnumerateShares(string host)
{
UnitTestConstants.PrintUnitTestHeader(isNetwork);
UnitTestConstants.PrintUnitTestHeader(false);

Console.WriteLine("\nInput Host: [{0}]", host);

Original file line number Diff line number Diff line change
@@ -37,7 +37,7 @@ public void AlphaFS_Host_GetUncName_Network_Success()
Console.WriteLine("Host.GetUncName(): [{0}]", hostUncName);


Assert.IsTrue(hostUncName.StartsWith(@"\\"));
Assert.IsTrue(hostUncName.StartsWith(Alphaleonis.Win32.Filesystem.Path.UncPrefix));

Assert.AreEqual(Alphaleonis.Win32.Filesystem.Path.UncPrefix + Environment.MachineName, hostUncName);
}
47 changes: 37 additions & 10 deletions AlphaFS/Network/Host Class/Host.SMB.EnumerateSessions.cs
Original file line number Diff line number Diff line change
@@ -40,20 +40,45 @@ public static IEnumerable<SessionInfo> EnumerateSessions()

/// <summary>[AlphaFS] Enumerates sessions established on the specified <paramref name="hostName"/>.</summary>
/// <returns>An <see cref="IEnumerable{SessionInfo}"/> collection from the specified <paramref name="hostName"/>.</returns>
/// <param name="hostName">The DNS or NetBIOS name of the specified host. If this parameter is NULL, the local Computer is used.</param>
/// <param name="hostName">The DNS or NetBIOS name of the specified host. If this parameter is <c>null</c>, the local Computer is used.</param>
[SecurityCritical]
public static IEnumerable<SessionInfo> EnumerateSessions(string hostName)
{
return EnumerateSessionsCore(hostName, null, null);
}


/// <summary>[AlphaFS] Enumerates sessions established on the specified <paramref name="hostName"/>.</summary>
/// <returns>An <see cref="IEnumerable{SessionInfo}"/> collection from the specified <paramref name="hostName"/>.</returns>
/// <param name="hostName">The DNS or NetBIOS name of the specified host. If this parameter is <c>null</c>, the local Computer is used.</param>
/// <param name="clientName">The name of the Computer session for which information is to be returned. If this parameter is <c>null</c>, information for all Computer sessions on the server is returned.</param>
[SecurityCritical]
public static IEnumerable<SessionInfo> EnumerateSessions(string hostName, string clientName)
{
return EnumerateSessionsCore(hostName, clientName, null);
}


/// <summary>[AlphaFS] Enumerates sessions established on the specified <paramref name="hostName"/>.</summary>
/// <returns>An <see cref="IEnumerable{SessionInfo}"/> collection from the specified <paramref name="hostName"/>.</returns>
/// <param name="hostName">The DNS or NetBIOS name of the specified host. If this parameter is <c>null</c>, the local Computer is used.</param>
/// <param name="clientName">The name of the Computer session for which information is to be returned. If this parameter is <c>null</c>, information for all Computer sessions on the server is returned.</param>
/// <param name="userName">The name of the user for which information is to be returned. If this parameter is <c>null</c>, information for all users is returned.</param>
[SecurityCritical]
public static IEnumerable<SessionInfo> EnumerateSessions(string hostName, string clientName, string userName)
{
return EnumerateSessionsCore(hostName, clientName, userName);
}




/// <summary>[AlphaFS] Enumerates sessions established on the specified <paramref name="hostName"/>.</summary>
/// <returns>An <see cref="IEnumerable{SessionInfo}"/> collection from the specified <paramref name="hostName"/>.</returns>
/// <exception cref="NetworkInformationException"/>
/// <param name="hostName">The DNS or NetBIOS name of the specified host. If this parameter is NULL, the local Computer is used.</param>
/// <param name="clientName">The name of the Computer session for which information is to be returned. If this parameter is NULL, NetSessionEnum returns information for all Computer sessions on the server.</param>
/// <param name="userName">The name of the user for which information is to be returned. If this parameter is NULL, NetSessionEnum returns information for all users.</param>
/// <param name="hostName">The DNS or NetBIOS name of the specified host. If this parameter is <c>null</c>, the local Computer is used.</param>
/// <param name="clientName">The name of the Computer session for which information is to be returned. If this parameter is <c>null</c>, information for all Computer sessions on the server is returned.</param>
/// <param name="userName">The name of the user for which information is to be returned. If this parameter is <c>null</c>, information for all users is returned.</param>
[SecurityCritical]
internal static IEnumerable<SessionInfo> EnumerateSessionsCore(string hostName, string clientName, string userName)
{
@@ -62,13 +87,15 @@ internal static IEnumerable<SessionInfo> EnumerateSessionsCore(string hostName,


// hostName is allowed to be null.
// clientName is allowed to be null.
// userName is allowed to be null.

var stripUnc = !Utils.IsNullOrWhiteSpace(hostName) ? Path.GetRegularPathCore(hostName, GetFullPathOptions.CheckInvalidPathChars, false).Replace(Path.UncPrefix, string.Empty) : null;


// Try SESSION_INFO_502 structure.
// Start with SESSION_INFO_502 structure.

foreach (var sessionInfo in EnumerateNetworkObjectCore(fd, (NativeMethods.SESSION_INFO_502 structure, SafeGlobalMemoryBufferHandle buffer) => new SessionInfo(SessionInfoLevel.Info502, structure),
foreach (var sessionInfo in EnumerateNetworkObjectCore(fd, (NativeMethods.SESSION_INFO_502 structure, SafeGlobalMemoryBufferHandle buffer) => new SessionInfo(hostName, SessionInfoLevel.Info502, structure),

(FunctionData functionData, out SafeGlobalMemoryBufferHandle buffer, int prefMaxLen, out uint entriesRead, out uint totalEntries, out uint resumeHandle) =>

@@ -84,7 +111,7 @@ internal static IEnumerable<SessionInfo> EnumerateSessionsCore(string hostName,

// Fallback on SESSION_INFO_2 structure.

foreach (var sessionInfo in EnumerateNetworkObjectCore(fd, (NativeMethods.SESSION_INFO_2 structure, SafeGlobalMemoryBufferHandle buffer) => new SessionInfo(SessionInfoLevel.Info2, structure),
foreach (var sessionInfo in EnumerateNetworkObjectCore(fd, (NativeMethods.SESSION_INFO_2 structure, SafeGlobalMemoryBufferHandle buffer) => new SessionInfo(hostName, SessionInfoLevel.Info2, structure),

(FunctionData functionData, out SafeGlobalMemoryBufferHandle buffer, int prefMaxLen, out uint entriesRead, out uint totalEntries, out uint resumeHandle) =>

@@ -100,7 +127,7 @@ internal static IEnumerable<SessionInfo> EnumerateSessionsCore(string hostName,

// Fallback on SHARE_INFO_1 structure.

foreach (var sessionInfo in EnumerateNetworkObjectCore(fd, (NativeMethods.SHARE_INFO_1 structure, SafeGlobalMemoryBufferHandle buffer) => new SessionInfo(SessionInfoLevel.Info1, structure),
foreach (var sessionInfo in EnumerateNetworkObjectCore(fd, (NativeMethods.SHARE_INFO_1 structure, SafeGlobalMemoryBufferHandle buffer) => new SessionInfo(hostName, SessionInfoLevel.Info1, structure),

(FunctionData functionData, out SafeGlobalMemoryBufferHandle buffer, int prefMaxLen, out uint entriesRead, out uint totalEntries, out uint resumeHandle) =>

@@ -116,7 +143,7 @@ internal static IEnumerable<SessionInfo> EnumerateSessionsCore(string hostName,

// Fallback on SESSION_INFO_10 structure.

foreach (var sessionInfo in EnumerateNetworkObjectCore(fd, (NativeMethods.SESSION_INFO_10 structure, SafeGlobalMemoryBufferHandle buffer) => new SessionInfo(SessionInfoLevel.Info10, structure),
foreach (var sessionInfo in EnumerateNetworkObjectCore(fd, (NativeMethods.SESSION_INFO_10 structure, SafeGlobalMemoryBufferHandle buffer) => new SessionInfo(hostName, SessionInfoLevel.Info10, structure),

(FunctionData functionData, out SafeGlobalMemoryBufferHandle buffer, int prefMaxLen, out uint entriesRead, out uint totalEntries, out uint resumeHandle) =>

@@ -132,7 +159,7 @@ internal static IEnumerable<SessionInfo> EnumerateSessionsCore(string hostName,

// Fallback on SESSION_INFO_0 structure.

foreach (var sessionInfo in EnumerateNetworkObjectCore(fd, (NativeMethods.SESSION_INFO_0 structure, SafeGlobalMemoryBufferHandle buffer) => new SessionInfo(SessionInfoLevel.Info0, structure),
foreach (var sessionInfo in EnumerateNetworkObjectCore(fd, (NativeMethods.SESSION_INFO_0 structure, SafeGlobalMemoryBufferHandle buffer) => new SessionInfo(hostName, SessionInfoLevel.Info0, structure),

(FunctionData functionData, out SafeGlobalMemoryBufferHandle buffer, int prefMaxLen, out uint entriesRead, out uint totalEntries, out uint resumeHandle) =>

36 changes: 30 additions & 6 deletions AlphaFS/Network/OpenConnectionInfo.cs
Original file line number Diff line number Diff line change
@@ -29,23 +29,33 @@ namespace Alphaleonis.Win32.Network
[Serializable]
public sealed class OpenConnectionInfo
{
#region Private Fields

private string _netName;

#endregion // Private Fields


#region Constructor

/// <summary>Create an OpenConnectionInfo instance.</summary>
internal OpenConnectionInfo(string host, NativeMethods.CONNECTION_INFO_1 connectionInfo)
internal OpenConnectionInfo(string hostName, NativeMethods.CONNECTION_INFO_1 connectionInfo)
{
Host = host;
Host = hostName;
HostName = hostName;
Id = connectionInfo.coni1_id;
ShareType = connectionInfo.coni1_type;
TotalOpenFiles = connectionInfo.coni1_num_opens;
TotalUsers = connectionInfo.coni1_num_users;
ConnectedSeconds = connectionInfo.coni1_time;
ConnectedTime = TimeSpan.FromSeconds(connectionInfo.coni1_time);
UserName = connectionInfo.coni1_username;
NetName = connectionInfo.oni1_netname.Replace(Path.LongPathUncPrefix, string.Empty).Replace(Path.UncPrefix, string.Empty);
NetName = connectionInfo.oni1_netname;
}

#endregion // Constructor


#region Methods

/// <summary>Returns the full path to the share.</summary>
@@ -57,11 +67,16 @@ public override string ToString()

#endregion // Methods


#region Properties

/// <summary>The local or remote Host.</summary>
[Obsolete("Use HostName")]
public string Host { get; private set; }

/// <summary>The host name of this connection information.</summary>
public string HostName { get; private set; }

/// <summary>Specifies a connection identification number.</summary>
public long Id { get; private set; }

@@ -75,13 +90,22 @@ public override string ToString()
public long TotalUsers { get; private set; }

/// <summary>Specifies the number of seconds that the connection has been established.</summary>
[Obsolete("Use ConnectedTime property.")]
public long ConnectedSeconds { get; private set; }

/// <summary>If the server sharing the resource is running with user-level security, the UserName member describes which user made the connection. If the server is running with share-level security, coni1_username describes which computer (computername) made the connection.</summary>
/// <summary>Specifies duration that the connection has been established.</summary>
public TimeSpan ConnectedTime { get; private set; }

/// <summary>If the server sharing the resource is running with user-level security, the UserName member describes which user made the connection. If the server is running with share-level security, UserName describes which Computer (Computer name) made the connection.</summary>
public string UserName { get; private set; }

/// <summary>Specifies either the server's shared resource name or the Computer name or IP address of the client. The value of this member depends on which name was specified as the qualifier parameter to the function.</summary>
public string NetName
{
get { return _netName; }

/// <summary>String that specifies either the share name of the server's shared resource or the computername of the client. The value of this member depends on which name was specified as the qualifier parameter to the NetConnectionEnum function.</summary>
public string NetName { get; private set; }
set { _netName = null != value ? value.Replace(Path.LongPathUncPrefix, string.Empty).Replace(Path.UncPrefix, string.Empty).Trim('[', ']') : null; }
}

#endregion // Properties
}
22 changes: 16 additions & 6 deletions AlphaFS/Network/OpenResourceInfo.cs
Original file line number Diff line number Diff line change
@@ -21,6 +21,7 @@

using System;
using System.Globalization;
using Alphaleonis.Win32.Filesystem;

namespace Alphaleonis.Win32.Network
{
@@ -30,28 +31,32 @@ public sealed class OpenResourceInfo
{
#region Constructor

/// <summary>Create a OpenResourceInfo instance.</summary>
internal OpenResourceInfo(string host, NativeMethods.FILE_INFO_3 fileInfo)
/// <summary>Create an OpenResourceInfo instance.</summary>
internal OpenResourceInfo(string hostName, NativeMethods.FILE_INFO_3 fileInfo)
{
Host = host;
Host = hostName;
HostName = hostName;
Id = fileInfo.fi3_id;
Permissions = fileInfo.fi3_permissions;
TotalLocks = fileInfo.fi3_num_locks;
PathName = fileInfo.fi3_pathname.Replace(@"\\", @"\");
PathName = fileInfo.fi3_pathname.Replace(Path.UncPrefix, Path.DirectorySeparator);
UserName = fileInfo.fi3_username;
}

#endregion // Constructor


#region Methods

/// <summary>Forces the open resource to close.</summary>
/// <remarks>You should this method with caution because it does not write data cached on the client system to the file before closing the file.</remarks>
public void Close()
{
uint lastError = NativeMethods.NetFileClose(Host, (uint) Id);
var lastError = NativeMethods.NetFileClose(HostName, (uint) Id);

if (lastError != Win32Errors.NERR_Success && lastError != Win32Errors.NERR_FileIdNotFound)
NativeError.ThrowException(lastError, Host, PathName);

NativeError.ThrowException(lastError, HostName, PathName);
}

/// <summary>Returns the full path to the share.</summary>
@@ -63,11 +68,16 @@ public override string ToString()

#endregion // Methods


#region Properties

/// <summary>The local or remote Host.</summary>
[Obsolete("Use HostName")]
public string Host { get; private set; }

/// <summary>The host name of this resource information.</summary>
public string HostName { get; private set; }

/// <summary>The identification number assigned to the resource when it is opened.</summary>
public long Id { get; private set; }

4 changes: 2 additions & 2 deletions AlphaFS/Network/ServerStatisticsInfo.cs
Original file line number Diff line number Diff line change
@@ -133,7 +133,7 @@ public int FilesOpened


/// <summary>The host name from where the statistics are gathered.</summary>
public string HostName { get; }
public string HostName { get; private set; }


/// <summary>The number of server print jobs spooled.</summary>
@@ -241,7 +241,7 @@ public override int GetHashCode()
/// <returns>The result of the operator.</returns>
public static bool operator ==(ServerStatisticsInfo left, ServerStatisticsInfo right)
{
return left is null && right is null || !(left is null) && !(right is null) && left.Equals(right);
return ReferenceEquals(left, null) && ReferenceEquals(right, null) || !ReferenceEquals(left, null) && !ReferenceEquals(right, null) && left.Equals(right);
}


Loading