This blog is moved to
http://amalhashim.wordpress.com

Monday, March 8, 2010

Disabling Network using C#

In C# there are several ways to interact with network interfaces. Which include WMI, NetworkInformation Namespace, Win32 API’s etc. In this article I am going to explain, how easily we can query network information's and play with it.
///Check whether network is available or not
bool isNwUp = System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable();

///Get all network cards and display status
System.Net.NetworkInformation.NetworkInterface[] networkCards = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces();

foreach (System.Net.NetworkInformation.NetworkInterface ni in networkCards)
{
Console.WriteLine(ni.Name + ": " + ni.OperationalStatus.ToString());
}


Now let’s see how we can enable/disable a network connection. I have written a generic class wrapping all required methods.

/// <summary>
/// This is a generic class for disconnecting TCP connections.
/// This class can be used to
/// 1. Get a list of all connections.
/// 2. Cloas a connection
/// </summary>
public static class DisconnectWrapper
{
/// <summary>
/// Enumeration of connection states
/// </summary>
public enum ConnectionState
{
All = 0,
Closed = 1,
Listen = 2,
Syn_Sent = 3,
Syn_Rcvd = 4,
Established = 5,
Fin_Wait1 = 6,
Fin_Wait2 = 7,
Close_Wait = 8,
Closing = 9,
Last_Ack = 10,
Time_Wait = 11,
Delete_TCB = 12
}

/// <summary>
/// Connection information
/// </summary>
private struct ConnectionInfo
{
public int dwState;
public int dwLocalAddr;
public int dwLocalPort;
public int dwRemoteAddr;
public int dwRemotePort;
}

/// <summary>
/// Win 32 API for get all connection
/// </summary>
/// <param name="pTcpTable">Pointer to TCP table</param>
/// <param name="pdwSize">Size</param>
/// <param name="bOrder">Order</param>
/// <returns>Number</returns>
[DllImport("iphlpapi.dll")]
private static extern int GetTcpTable(IntPtr pTcpTable, ref int pdwSize, bool bOrder);

/// <summary>
/// Set the connection state
/// </summary>
/// <param name="pTcprow">Pointer to TCP table row</param>
/// <returns>Status</returns>
[DllImport("iphlpapi.dll")]
private static extern int SetTcpEntry(IntPtr pTcprow);

/// <summary>
/// Convert 16-bit value from network to host byte order
/// </summary>
/// <param name="netshort">network host</param>
/// <returns>host byte order</returns>
[DllImport("wsock32.dll")]
private static extern int ntohs(int netshort);

/// <summary>
/// //Convert 16-bit value back again
/// </summary>
/// <param name="netshort"></param>
/// <returns></returns>
[DllImport("wsock32.dll")]
private static extern int htons(int netshort);

/// <summary>
/// Close all connection to the remote IP
/// </summary>
/// <param name="IP">IP to close</param>
public static void CloseRemoteIP(string IP)
{
ConnectionInfo[] rows = getTcpTable();
for (int i = 0; i < rows.Length; i++)
{
if (rows[i].dwRemoteAddr == IPStringToInt(IP))
{
rows[i].dwState = (int)ConnectionState.Delete_TCB;
IntPtr ptr = GetPtrToNewObject(rows[i]);
int ret = SetTcpEntry(ptr);
}
}
}

/// <summary>
/// Close all connections at current local IP
/// </summary>
/// <param name="IP">IP to close</param>
public static void CloseLocalIP(string IP)
{
ConnectionInfo[] rows = getTcpTable();
for (int i = 0; i < rows.Length; i++)
{
if (rows[i].dwLocalAddr == IPStringToInt(IP))
{
rows[i].dwState = (int)ConnectionState.Delete_TCB;
IntPtr ptr = GetPtrToNewObject(rows[i]);
int ret = SetTcpEntry(ptr);
}
}
}

/// <summary>
/// //Closes all connections to the remote port
/// </summary>
/// <param name="port">Port to close</param>
public static void CloseRemotePort(int port)
{
ConnectionInfo[] rows = getTcpTable();
for (int i = 0; i < rows.Length; i++)
{
if (port == ntohs(rows[i].dwRemotePort))
{
rows[i].dwState = (int)ConnectionState.Delete_TCB;
IntPtr ptr = GetPtrToNewObject(rows[i]);
int ret = SetTcpEntry(ptr);
}
}
}

/// <summary>
/// //Closes all connections to the local port
/// </summary>
/// <param name="port">Local port</param>
public static void CloseLocalPort(int port)
{
ConnectionInfo[] rows = getTcpTable();
for (int i = 0; i < rows.Length; i++)
{
if (port == ntohs(rows[i].dwLocalPort))
{
rows[i].dwState = (int)ConnectionState.Delete_TCB;
IntPtr ptr = GetPtrToNewObject(rows[i]);
int ret = SetTcpEntry(ptr);
}
}
}

/// <summary>
/// Close a connection by returning the connectionstring
/// </summary>
/// <param name="connectionstring">Connection to close</param>
public static void CloseConnection(string connectionstring)
{
try
{
//Split the string to its subparts
string[] parts = connectionstring.Split('-');
if (parts.Length != 4) throw new Exception("Invalid connectionstring - use the one provided by Connections.");
string[] loc = parts[0].Split(':');
string[] rem = parts[1].Split(':');
string[] locaddr = loc[0].Split('.');
string[] remaddr = rem[0].Split('.');
//Fill structure with data
ConnectionInfo row = new ConnectionInfo();
row.dwState = 12;
byte[] bLocAddr = new byte[] { byte.Parse(locaddr[0]), byte.Parse(locaddr[1]), byte.Parse(locaddr[2]), byte.Parse(locaddr[3]) };
byte[] bRemAddr = new byte[] { byte.Parse(remaddr[0]), byte.Parse(remaddr[1]), byte.Parse(remaddr[2]), byte.Parse(remaddr[3]) };
row.dwLocalAddr = BitConverter.ToInt32(bLocAddr, 0);
row.dwRemoteAddr = BitConverter.ToInt32(bRemAddr, 0);
row.dwLocalPort = htons(int.Parse(loc[1]));
row.dwRemotePort = htons(int.Parse(rem[1]));
//Make copy of the structure into memory and use the pointer to call SetTcpEntry
IntPtr ptr = GetPtrToNewObject(row);
int ret = SetTcpEntry(ptr);
if (ret == -1) throw new Exception("Unsuccessful");
if (ret == 65) throw new Exception("User has no sufficient privilege to execute this API successfully");
if (ret == 87) throw new Exception("Specified port is not in state to be closed down");
if (ret != 0) throw new Exception("Unknown error (" + ret + ")");
}
catch (Exception ex)
{
throw new Exception("CloseConnection failed (" + connectionstring + ")! [" + ex.GetType().ToString() + "," + ex.Message + "]");
}
}

/// <summary>
/// Get all connection
/// </summary>
/// <returns>Array of connection string</returns>
public static string[] Connections()
{
return Connections(ConnectionState.All);
}

/// <summary>
/// Get connections based on the state
/// </summary>
/// <param name="state"></param>
/// <returns></returns>
public static string[] Connections(ConnectionState state)
{
ConnectionInfo[] rows = getTcpTable();

ArrayList arr = new ArrayList();

foreach (ConnectionInfo row in rows)
{
if (state == ConnectionState.All || state == (ConnectionState)row.dwState)
{
string localaddress = IPIntToString(row.dwLocalAddr) + ":" + ntohs(row.dwLocalPort);
string remoteaddress = IPIntToString(row.dwRemoteAddr) + ":" + ntohs(row.dwRemotePort);
arr.Add(localaddress + "-" + remoteaddress + "-" + ((ConnectionState)row.dwState).ToString() + "-" + row.dwState);
}
}

return (string[])arr.ToArray(typeof(System.String));
}

/// <summary>
/// The function that fills the ConnectionInfo array with connectioninfos
/// </summary>
/// <returns>ConnectionInfo</returns>
private static ConnectionInfo[] getTcpTable()
{
IntPtr buffer = IntPtr.Zero; bool allocated = false;
try
{
int iBytes = 0;
GetTcpTable(IntPtr.Zero, ref iBytes, false); //Getting size of return data
buffer = Marshal.AllocCoTaskMem(iBytes); //allocating the datasize

allocated = true;
GetTcpTable(buffer, ref iBytes, false); //Run it again to fill the memory with the data
int structCount = Marshal.ReadInt32(buffer); // Get the number of structures
IntPtr buffSubPointer = buffer; //Making a pointer that will point into the buffer
buffSubPointer = (IntPtr)((int)buffer + 4); //Move to the first data (ignoring dwNumEntries from the original MIB_TCPTABLE struct)
ConnectionInfo[] tcpRows = new ConnectionInfo[structCount]; //Declaring the array
//Get the struct size
ConnectionInfo tmp = new ConnectionInfo();
int sizeOfTCPROW = Marshal.SizeOf(tmp);
//Fill the array 1 by 1
for (int i = 0; i < structCount; i++)
{
tcpRows[i] = (ConnectionInfo)Marshal.PtrToStructure(buffSubPointer, typeof(ConnectionInfo)); //copy struct data
buffSubPointer = (IntPtr)((int)buffSubPointer + sizeOfTCPROW); //move to next structdata
}

return tcpRows;
}
catch (Exception ex)
{
throw new Exception("getTcpTable failed! [" + ex.GetType().ToString() + "," + ex.Message + "]");
}
finally
{
if (allocated) Marshal.FreeCoTaskMem(buffer); //Free the allocated memory
}
}

/// <summary>
/// Object pointer
/// </summary>
/// <param name="obj"></param>
/// <returns>Pointer</returns>
private static IntPtr GetPtrToNewObject(object obj)
{
IntPtr ptr = Marshal.AllocCoTaskMem(Marshal.SizeOf(obj));
Marshal.StructureToPtr(obj, ptr, false);
return ptr;
}

/// <summary>
/// IP to Int
/// </summary>
/// <param name="IP">IP Address</param>
/// <returns>Integer</returns>
private static int IPStringToInt(string IP)
{
if (IP.IndexOf(".") < 0) throw new Exception("Invalid IP address");
string[] addr = IP.Split('.');
if (addr.Length != 4) throw new Exception("Invalid IP address");
byte[] bytes = new byte[] { byte.Parse(addr[0]), byte.Parse(addr[1]), byte.Parse(addr[2]), byte.Parse(addr[3]) };
return BitConverter.ToInt32(bytes, 0);
}

/// <summary>
/// IP int to String
/// </summary>
/// <param name="IP">IP</param>
/// <returns>String</returns>
private static string IPIntToString(int IP)
{
byte[] addr = System.BitConverter.GetBytes(IP);
return addr[0] + "." + addr[1] + "." + addr[2] + "." + addr[3];
}
}

Add the class. For getting all connections use the following code

string[] connections = DisconnectWrapper.Connections();

Once you get the connection, you can disconnect it as follows
DisconnectWrapper.CloseConnection(connection[0]);

You can get connection based on the state

string openConnections[] = DisconnectWrapper.Connections(DisconnectWrapper.ConnectionState.Established);

Hope this helps you.

4 comments:

Ismael said...

Hi Hashim,

I was watching your code and is very useful, but I need to open a connection network, and my question is how can I the SetTcpEntry method to get this?

Can you help me??

Thanks in advance.

A.m.a.L Hashim said...

The SetTcpEntry function sets the state of a TCP connection. What you mean by open a connection network?

rohit arora said...

hi Hashim,
is there any way to identify which network is wifi and which one is wired LAN.
I am in desperate need to figure out this..
Thanks
Rohit

mdasgar said...

hi Hashim,
it was very useful for me, now i can close my network adapter connection. but how can i open it after close, in fact i wanna reset the connection.
is there a solution?