251 lines
9.1 KiB
C#
251 lines
9.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Collections;
|
|
using System.Net;
|
|
using System.Net.NetworkInformation;
|
|
|
|
namespace SafeNetLib
|
|
{
|
|
public class RouteManager
|
|
{
|
|
private ArrayList imeiList;
|
|
private static string radioIP;
|
|
private static string radioInterface;
|
|
public static string networkID = "";
|
|
private static bool radioStatus;
|
|
private static bool capacityPlus;
|
|
|
|
|
|
public RouteManager(string _radioIP, ArrayList _imeiList, bool _capacityPlus)
|
|
{
|
|
radioIP = _radioIP;
|
|
imeiList = _imeiList;
|
|
capacityPlus = _capacityPlus;
|
|
|
|
try
|
|
{
|
|
//adding 1 to the IP of the radio to get the interface IP
|
|
string[] arrStr = (radioIP).Split('.');
|
|
int lastPartOfIP = Convert.ToInt32(arrStr[3]);
|
|
arrStr[3] = (lastPartOfIP + 1).ToString();
|
|
string interfaceIP = arrStr[0] + "." + arrStr[1] + "." + arrStr[2] + "." + arrStr[3];
|
|
radioInterface = interfaceIP;
|
|
radioStatus = false;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
string err = "Fatal Error:" + ex.ToString();
|
|
Utils.ConsWrite(DebugMSG_Type.always, err);
|
|
Utils.ConsWrite(DebugMSG_Type.Routes, "Fatal ERROR");
|
|
Console.WriteLine("Press any key to continue...");
|
|
Thread.Sleep(5000);
|
|
System.Environment.Exit(0);
|
|
}
|
|
}
|
|
|
|
public void Start()
|
|
{
|
|
//SM.Debug("Starting ping thread for:" + radioGwList[i].InterfaceIP+ "interval:"+Main.pingInterval);
|
|
PingThread threadWorker = new PingThread(radioInterface, 5);
|
|
threadWorker.OnNewPingComplete += new PingThread.NewPingCompleteDEl(RouteManager_OnNewPingComplete);
|
|
Thread PingThreadObj = new Thread(new ThreadStart(threadWorker.Worker));
|
|
PingThreadObj.IsBackground = true;
|
|
PingThreadObj.Start();
|
|
}
|
|
|
|
void restoreRoutes(string ip_temp)
|
|
{
|
|
try
|
|
{
|
|
|
|
if (imeiList != null)
|
|
{
|
|
if (imeiList.Count > 0)
|
|
{
|
|
foreach (string imei in imeiList)
|
|
{
|
|
//register route
|
|
if (!capacityPlus)
|
|
{
|
|
if (networkID != "")
|
|
{
|
|
RoutingUtils.addRoute(imei, ip_temp, networkID);
|
|
}
|
|
else
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.Routes, "RouteManager.NetworkIDs_Hash does not contain a key for " + ip_temp);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine("**************\nCould not restore routes\n" + e.ToString());
|
|
}
|
|
}
|
|
|
|
void RouteManager_OnNewPingComplete(bool status, string ip)
|
|
{
|
|
//Console.WriteLine("****** OnNewPingComplete ** " + status + " *** " + ip);
|
|
string ip_temp = RadioUtils.GetReverse_Interface(ip);
|
|
|
|
//bool value = radioStatus;
|
|
if (this.OnRadioPing != null)
|
|
{
|
|
this.OnRadioPing(status, ip_temp);
|
|
}
|
|
|
|
if (status) //radio going from OFF->ON
|
|
{
|
|
try
|
|
{
|
|
radioStatus = true;
|
|
//networkID = RadioUtils.GetInterfaceID(ip_temp);
|
|
|
|
//restoreRoutes(ip_temp);
|
|
|
|
if (this.OnRadioStatusUpdate != null)
|
|
{
|
|
this.OnRadioStatusUpdate(status, ip_temp);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.Routes, "!!!!! Could not turn radio ON:" + ex.ToString());
|
|
}
|
|
}
|
|
else //going from ON->OFF
|
|
{
|
|
try
|
|
{
|
|
//Utils.ConsWrite(DebugMSG_Type.Routes, "GW:" + ip_temp + " was turned OFF!!!!");
|
|
//remove from hash
|
|
radioStatus = false;
|
|
networkID = "";
|
|
|
|
if (this.OnRadioStatusUpdate != null)
|
|
{
|
|
this.OnRadioStatusUpdate(status, ip_temp);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.Routes, "!!!!!!! Could not turn radio OFF:" + ex.ToString());
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
public static void RegisterRouteARS(string suid, IPEndPoint interfaceReceived)
|
|
{
|
|
//register route
|
|
if (!capacityPlus)
|
|
{
|
|
|
|
|
|
|
|
//Utils.ConsWrite(DebugMSG_Type.Routes, "ARSThread received ARS from field unit with ID " + suid + " on gateway radio with IP " + radioIP);
|
|
if (interfaceReceived != null)
|
|
{
|
|
String[] ip = interfaceReceived.Address.ToString().Split('.');
|
|
ip[3] = (Convert.ToInt16(ip[3]) - 1) + "";
|
|
|
|
|
|
Utils.WriteLine("GATEWAY ID IS: " + ip[0] + "." + ip[1] + "." + ip[2] + "." + ip[3], ConsoleColor.Red);
|
|
Utils.WriteLine("INTERFACE ID IS: " + interfaceReceived.Address.ToString(), ConsoleColor.Red);
|
|
|
|
int interfaceID = 0;
|
|
|
|
interfaceID = GetInterfaceIndex(interfaceReceived.Address.ToString());
|
|
|
|
if(interfaceID > 0)
|
|
RoutingUtils.addRoute(suid, ip[0] + "." + ip[1] + "." + ip[2] + "." + ip[3], interfaceID + "");
|
|
}
|
|
}
|
|
}
|
|
|
|
private static int GetInterfaceIndex(string InterfaceIP)
|
|
{
|
|
int result = 0;
|
|
|
|
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
|
|
IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
|
|
|
|
/*
|
|
Console.WriteLine("IPv4 interface information for {0}.{1}",
|
|
properties.HostName, properties.DomainName);
|
|
*/
|
|
|
|
foreach (NetworkInterface adapter in nics)
|
|
{
|
|
if (adapter.Supports(NetworkInterfaceComponent.IPv4) == false)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if (adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
|
|
{
|
|
//Console.WriteLine(adapter.Name);
|
|
foreach (UnicastIPAddressInformation ip in adapter.GetIPProperties().UnicastAddresses)
|
|
{
|
|
if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
|
|
{
|
|
//Console.WriteLine(ip.Address.ToString());
|
|
}
|
|
}
|
|
}
|
|
|
|
//Console.WriteLine(adapter.Description);
|
|
IPInterfaceProperties adapterProperties = adapter.GetIPProperties();
|
|
|
|
foreach (UnicastIPAddressInformation ip in adapter.GetIPProperties().UnicastAddresses)
|
|
{
|
|
if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork
|
|
&& ip.Address.ToString().Equals(InterfaceIP))
|
|
{
|
|
Utils.WriteLine("CONTAINS " + ip.Address.ToString(), ConsoleColor.Cyan);
|
|
IPv4InterfaceProperties p = adapterProperties.GetIPv4Properties();
|
|
if (p == null)
|
|
{
|
|
//Console.WriteLine("No information is available for this interface.");
|
|
continue;
|
|
}
|
|
|
|
result = p.Index;
|
|
Console.WriteLine(" Index : {0}", p.Index);
|
|
}
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public static void RegisterRouteARS(string suid)
|
|
{
|
|
//register route
|
|
if (!capacityPlus)
|
|
{
|
|
//Utils.ConsWrite(DebugMSG_Type.Routes, "ARSThread received ARS from field unit with ID " + suid + " on gateway radio with IP " + radioIP);
|
|
if (networkID != "")
|
|
{
|
|
RoutingUtils.addRoute(suid, radioIP, networkID);
|
|
}
|
|
else
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.Routes, "RouteManager does not contain a networkID for " + radioIP);
|
|
}
|
|
}
|
|
}
|
|
|
|
public delegate void RadioStatusUpdateDEl(bool status, string ip);
|
|
public event RadioStatusUpdateDEl OnRadioStatusUpdate;
|
|
|
|
public delegate void RadioPingDEl(bool status, string ip);
|
|
public event RadioPingDEl OnRadioPing;
|
|
}
|
|
}
|