SafeNet/SafeNetLib/CapacityPlus.cs

170 lines
7.0 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
using System.Net.NetworkInformation;
using System.Net;
using System.Diagnostics;
using System.Threading;
namespace SafeNetLib
{
public class CapacityPlus
{
private static void ExecuteCommand(String command)
{
Process p = new Process();
p = new Process();
p.StartInfo.FileName = "cmd.exe";
String arguments = "/c " + command;
p.StartInfo.Arguments = arguments;
p.StartInfo.UseShellExecute = false;
p.Start();
}
private static void DeletePreviousTRBORoutes()
{
Process p = new Process();
p = new Process();
p.StartInfo.FileName = "cmd.exe";
String arguments = "/c route print";
p.StartInfo.Arguments = arguments;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
String output = p.StandardOutput.ReadToEnd();
char[] delimiters = new char[] { '\r', '\n' };
String[] lines = output.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < lines.Length; i++)
{
String currentLine = lines[i].Trim();
if (currentLine.StartsWith("12.") || currentLine.StartsWith("13."))
{
String ip = currentLine.Split(' ')[0];
ExecuteCommand("route delete " + ip);
}
}
}
public static void UpdateRoutes(String masterRadioIP)
{
//Utils.ConsWrite(DebugMSG_Type.CFG, "Step111");
List<GatewayID_IP> gatewayRadios = getAllRadiosIPs();
//Utils.ConsWrite(DebugMSG_Type.CFG, "Gaby UpdateRoutes for " + masterRadioIP);
GatewayID_IP masterRadio = new GatewayID_IP();
//Utils.ConsWrite(DebugMSG_Type.CFG, "Step2");
foreach (GatewayID_IP gatewayRadio in gatewayRadios)
{
if (gatewayRadio.remoteIP != null)
if (gatewayRadio.remoteIP != "" && gatewayRadio.remoteIP.Contains("192.168"))
{
//Utils.ConsWrite(DebugMSG_Type.CFG, "Processing " + gatewayRadio.remoteIP);
try
{
//Utils.ConsWrite(DebugMSG_Type.CFG, "route delete " + gatewayRadio.remoteIP);
ExecuteCommand("route delete " + gatewayRadio.remoteIP);
if (gatewayRadio.remoteIP.Equals(masterRadioIP))
{
masterRadio = gatewayRadio;
}
}
catch (Exception ex)
{
//Utils.ConsWrite(DebugMSG_Type.ALL, "UpdateRoutes Exception: " + ex.ToString());
}
}
}
//Utils.ConsWrite(DebugMSG_Type.CFG, "Before masterRadio.ID != null");
if (masterRadio.ID != null)
{
//adding gateway radio route
//Utils.ConsWrite(DebugMSG_Type.CFG,"Adding master radio route");
String command = "route add " + masterRadio.remoteIP + " mask 255.255.255.255 " + masterRadio.localIP + " metric 40 IF " + masterRadio.ID;
//Utils.ConsWrite(DebugMSG_Type.CFG, command);
ExecuteCommand(command);
//Utils.ConsWrite(DebugMSG_Type.CFG,"Route added, master radio ip is " + masterRadio.remoteIP);
//adding 12.x.x.x route
//Utils.ConsWrite(DebugMSG_Type.CFG,"updating 12.0.0.0 route");
command = "route delete 12.0.0.0";
//Utils.ConsWrite(DebugMSG_Type.CFG, command);
ExecuteCommand(command);
Thread.Sleep(200);
command = "route add 12.0.0.0 mask 255.0.0.0 " + masterRadio.remoteIP + " metric 10 IF " + masterRadio.ID;
//Utils.ConsWrite(DebugMSG_Type.CFG, command);
ExecuteCommand(command);
//Utils.ConsWrite(DebugMSG_Type.CFG,"12.0.0.0 route added");
}
else
{
//Utils.ConsWrite(DebugMSG_Type.CFG,"The master radio IP does not exist in the routing table.\r\n Please make sure the radio is connected and the IP is properly\r\n configured in CPS.");
}
}
public static List<GatewayID_IP> getAllRadiosIPs()
{
List<GatewayID_IP> ipList = new List<GatewayID_IP>();
NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
try
{
foreach (NetworkInterface n in interfaces)
{
if (n.Description.Contains("MOTOTRBO"))
{
GatewayID_IP radioID_IP = new GatewayID_IP();
IPInterfaceProperties ipProperties = n.GetIPProperties();
radioID_IP.ID = (n.GetIPProperties().GetIPv4Properties().Index).ToString();
IPAddressCollection IpCol = ipProperties.DhcpServerAddresses;
foreach (IPAddress ip in IpCol)
{
radioID_IP.remoteIP = ip.ToString();
}
foreach (UnicastIPAddressInformation addr in ipProperties.UnicastAddresses)
{
if (addr.Address.ToString().StartsWith("192."))
{
radioID_IP.localIP = addr.Address.ToString();
}
}
ipList.Add(radioID_IP);
}
}
}
catch (Exception e)
{
Utils.ConsWrite(DebugMSG_Type.ALL,"RadioStuff getAllRadiosIPs Exception: " + e.ToString());
}
return ipList;
}
public static string GetInterface(string IPstr)
{
string strInterface = "";
try
{
string[] arrStr = IPstr.Split('.');
int lastPartOfIP = Convert.ToInt32(arrStr[3]);
arrStr[3] = (lastPartOfIP + 1).ToString();
strInterface = arrStr[0] + "." + arrStr[1] + "." + arrStr[2] + "." + arrStr[3];
}
catch (Exception ex)
{
Utils.ConsWrite(DebugMSG_Type.CFG,"Error GEtting interface" + ex.ToString());
}
return strInterface;
}
}
public struct GatewayID_IP
{
//the remote IP of the gateway radio (192.168.10.31, for example)
public String remoteIP;
//the local IP of the gateway radio (192.168.10.30, for example)
public String localIP;
//the ID of the network card - necessary for route adding
public String ID;
}
}