SafeNet/SafeNetLib/RoutingUtils.cs

356 lines
14 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Threading;
namespace SafeNetLib
{
public class RoutingUtils
{
#region Capacity plus code
/// <summary>
///
/// </summary>
/// <param name="specific">checks if any route is in table/ checks if an specific ip is in table</param>
/// <param name="ip">IP if specific is set to true / else add it ""</param>
/// <returns></returns>
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, Boolean capacityPlus)
{
List<GatewayID_IP> gatewayRadios = RadioUtils.getAllRadiosIPs();
if (capacityPlus)
{
GatewayID_IP masterRadio = new GatewayID_IP();
try
{
foreach (GatewayID_IP gatewayRadio in gatewayRadios)
{
//Utils.ConsWrite(DebugMSG_Type.Routes, "Route CMD:" + "route delete " + gatewayRadio.remoteIP);
if ((gatewayRadio.remoteIP != "") && (gatewayRadio.remoteIP != " "))
ExecuteCommand("route delete " + gatewayRadio.remoteIP);
if (gatewayRadio.remoteIP.Equals(masterRadioIP))
{
masterRadio = gatewayRadio;
}
}
}
catch (Exception ex)
{
//Utils.ConsWrite(DebugMSG_Type.Routes, "Error on route Delete:" + ex.ToString());
}
if (masterRadio.ID != null)
{
//adding gateway radio route
//Console.WriteLine("Adding master radio route");
String command = "route add " + masterRadio.remoteIP + " mask 255.255.255.255 " + masterRadio.localIP + " metric 40 IF " + masterRadio.ID;
ExecuteCommand(command);
//Console.WriteLine("Route added, master radio ip is " + masterRadio.remoteIP);
//adding 12.x.x.x route
//Console.WriteLine("updating 12.0.0.0 route");
command = "route delete 12.0.0.0";
ExecuteCommand(command);
Thread.Sleep(200);
command = "route add 12.0.0.0 mask 255.0.0.0 " + masterRadio.remoteIP + " metric 10 IF " + masterRadio.ID;
ExecuteCommand(command);
//Console.WriteLine("12.0.0.0 route added");
}
else
{
//Utils.ConsWrite(DebugMSG_Type.Routes, "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.");
}
}
else
{
foreach (GatewayID_IP gatewayRadio in gatewayRadios)
{
String command = "route add " + gatewayRadio.remoteIP + " mask 255.255.255.255 " + gatewayRadio.localIP + " metric 50 IF " + gatewayRadio.ID;
ExecuteCommand(command);
}
}
}
public static void UpdateRoutes_NonCapPlus(string ip, string intIP, string Netowrk_id)
{
String command = "route add " + ip + " mask 255.255.255.255 " + intIP + " metric 50 IF " + Netowrk_id;
//Console.WriteLine("**** Updating route for the new radio\n" + command);
ExecuteCommand(command);
}
#endregion
#region Non-Capacity plus functions
private static RouteTableInfo routeTable;
/// <summary>
///
/// </summary>
/// <param name="specific"></param>
/// <param name="ip"></param>
/// <returns></returns>
public static bool checkRouteTable(string radioIP)
{
bool ret = false;
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/c route print";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
string output = p.StandardOutput.ReadToEnd();
if (output.Contains(radioIP))
{
ret = true;
}
return ret;
}
public static bool checkIfAlreadyPresent(string radioIP, string gwip)
{
bool ret = false;
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/c route print";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
string output = p.StandardOutput.ReadToEnd();
routeTable = new RouteTableInfo();
routeTable.time = DateTime.Now;
routeTable.routeString = output;
if (output.Contains(radioIP + " 255.255.255.255 " + gwip))
{
string temp1 = output.Substring(output.IndexOf(radioIP));
if (temp1.Contains(gwip))
{
string temp2 = temp1.Remove(temp1.IndexOf(gwip) + gwip.Length);
temp2 = temp2.Replace(radioIP, "");
temp2 = temp2.Replace(gwip, "");
temp2 = temp2.Replace("255.255.255.255", "");
temp2 = temp2.Replace(" ", "");
if (temp2.Length == 0)
ret = true;
}
}
return ret;
}
public static bool checkIfAlreadyPresent_withBuffer(string radioIP, string gwip)
{
string output = "";
bool ret = false;
if (routeTable != null)
{
if (routeTable.time.AddMinutes(5) > DateTime.Now)
{
//Utils.ConsWrite(DebugMSG_Type.Routes, "Getting route from buffer. buffer time:" + routeTable.time + " system time:" + DateTime.Now);
output = routeTable.routeString;
}
else
{
//Utils.ConsWrite(DebugMSG_Type.Routes, "Refreshing buffer. old buffer time:" + routeTable.time + " system time:" + DateTime.Now);
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/c route print";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
output = p.StandardOutput.ReadToEnd();
routeTable = new RouteTableInfo();
routeTable.time = DateTime.Now;
routeTable.routeString = output;
}
}
else
{
//Utils.ConsWrite(DebugMSG_Type.Routes, "Creating route table buffer at: " + DateTime.Now);
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/c route print";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
output = p.StandardOutput.ReadToEnd();
routeTable = new RouteTableInfo();
routeTable.time = DateTime.Now;
routeTable.routeString = output;
}
if (output.Contains(radioIP + " 255.255.255.255 " + gwip))
{
string temp1 = output.Substring(output.IndexOf(radioIP));
if (temp1.Contains(gwip))
{
string temp2 = temp1.Remove(temp1.IndexOf(gwip) + gwip.Length);
temp2 = temp2.Replace(radioIP, "");
temp2 = temp2.Replace(gwip, "");
temp2 = temp2.Replace("255.255.255.255", "");
temp2 = temp2.Replace(" ", "");
if (temp2.Length == 0)
ret = true;
}
}
return ret;
}
public static void deleteRoute(string ip)
{
//Console.WriteLine("route delete " + ip);
Process p = new Process();
p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/c route delete " + ip;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
string output = p.StandardOutput.ReadToEnd();
}
public static String radioID2IP(String p_radioID)
{
Int64 radioID = Convert.ToInt64(p_radioID);
if (radioID > 255 * 255 * 255)
throw new Exception("Radio ID out of range");
byte c = (byte)(radioID & 0x0000FF);
byte b = (byte)((radioID & 0x00FF00) >> 8);
byte a = (byte)((radioID & 0xFF0000) >> 16);
string m_ip = "12." + a + "." + b + "." + c;
return m_ip;
}
public static void addRoute(String radioID, String gatewayIP, String gatewayInterfaceID)
{
String radioIP = radioID2IP(radioID);
if (checkIfAlreadyPresent(radioIP, gatewayIP))
{
//Utils.ConsWrite(DebugMSG_Type.always, "Route " + radioIP + " gw " + gatewayIP + " exists");
return;
}
else
{
//Utils.ConsWrite(DebugMSG_Type.always, "Route " + radioIP + " gw " + gatewayIP + " DOSEN'T exist");
}
//Console.WriteLine("---------------------------------------------------------------");
if (checkRouteTable(radioIP))
{
deleteRoute(radioIP);
}
Process p = new Process();
p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/c route add " + radioIP + " mask 255.255.255.255 " + gatewayIP + " metric 40 IF " + gatewayInterfaceID;
Utils.WriteLine(p.StartInfo.Arguments, ConsoleColor.Yellow);
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
string output = p.StandardOutput.ReadToEnd();
if (checkRouteTable(radioIP))
{
//Console.WriteLine("Route added");
}
else
{
//Utils.ConsWrite(DebugMSG_Type.DB, "---------------------------------------------------------------");
//Utils.ConsWrite(DebugMSG_Type.DB, "!!!!! Route adding failed !!!!!");
//Utils.ConsWrite(DebugMSG_Type.DB, "---------------------------------------------------------------");
}
}
public static void addRoute_withBuffer(String radioID, String gatewayIP, String gatewayInterfaceID)
{
String radioIP = radioID2IP(radioID);
if (checkIfAlreadyPresent_withBuffer(radioIP, gatewayIP))
{
//Utils.ConsWrite(DebugMSG_Type.Routes, "Route " + radioIP + " to " + gatewayIP + " already in route table!!!");
return;
}
//Console.WriteLine("---------------------------------------------------------------");
/*
if (checkRouteTable(radioIP))
{
deleteRoute(radioIP);
}
*/
Process p = new Process();
p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/c route add " + radioIP + " mask 255.255.255.255 " + gatewayIP + " metric 40 IF " + gatewayInterfaceID;
//Console.WriteLine("route adding:" + p.StartInfo.Arguments);
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
string output = p.StandardOutput.ReadToEnd();
if (checkRouteTable(radioIP))
{
//Console.WriteLine("Route added");
}
else
{
//Console.WriteLine("Route adding failed");
}
//Console.WriteLine("---------------------------------------------------------------");
}
#endregion
}
public class RouteTableInfo
{
public DateTime time;
public string routeString;
}
}