SafeNet/SafeNetLib/RadioUtils.cs

186 lines
6.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
using System.Net.NetworkInformation;
using System.Net;
namespace SafeNetLib
{
public class RadioUtils
{
public static List<GatewayID_IP> getAllRadiosIPs()
{
List<GatewayID_IP> ipList = new List<GatewayID_IP>();
NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
try
{
foreach (NetworkInterface n in interfaces)
{
//SM.Debug("Descriport name:" + n.Description);
//old code if (n.Description.Contains("MOTOTRBO"))
if ((n.Description.ToUpper().Contains("MOTOTRBO")) || (n.Description.ToUpper().Contains("BLUETOOTH")))
{
GatewayID_IP radioID_IP = new GatewayID_IP();
IPInterfaceProperties ipProperties = n.GetIPProperties();
radioID_IP.ID = (n.GetIPProperties().GetIPv4Properties().Index).ToString();
IPAddressCollection IpCol = ipProperties.DhcpServerAddresses;
//Utils.ConsWrite(DebugMSG_Type.Routes, "In mototrbo " + IpCol.Count);
if (IpCol.Count > 0)
{
foreach (IPAddress ip in IpCol)
{
//Console.WriteLine("IP's: " + ip.ToString());
radioID_IP.remoteIP = ip.ToString();
}
}
foreach (UnicastIPAddressInformation addr in ipProperties.UnicastAddresses)
{
//SM.Debug("Radio localIP: " + addr.Address);
if (addr.Address.ToString().StartsWith("192."))
{
radioID_IP.localIP = addr.Address.ToString();
}
}
//SM.Debug("Radio RemoteIP:"+radioID_IP.remoteIP);
ipList.Add(radioID_IP);
}
}
}
catch (Exception e)
{
Utils.ConsWrite(DebugMSG_Type.Routes, "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)
{
Console.WriteLine("Error GEtting interface");
Console.WriteLine(ex.ToString());
}
return strInterface;
}
public static string GetReverse_Interface(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)
{
Console.WriteLine("Error GEtting interface");
Console.WriteLine(ex.ToString());
}
return strInterface;
}
public static string GetInterfaceID(string ip)
{
string id = "";
DateTime time = DateTime.Now;
NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
Utils.ConsWrite(DebugMSG_Type.Routes, "GetInterfaceID time to complete:" + (DateTime.Now - time));
foreach (NetworkInterface n in interfaces)
{
//old code if (n.Description.Contains("MOTOTRBO"))
if ((n.Description.ToUpper().Contains("MOTOTRBO")) || (n.Description.ToUpper().Contains("BLUETOOTH")))
{
IPInterfaceProperties ipProperties = n.GetIPProperties();
string temp_id = (n.GetIPProperties().GetIPv4Properties().Index).ToString();
foreach (UnicastIPAddressInformation addr in ipProperties.UnicastAddresses)
{
if (addr.Address.ToString().StartsWith("192."))
{
string trboRemoteIP = GetInterface(ip);
if (trboRemoteIP == addr.Address.ToString())
{
id = temp_id;
}
}
}
}
}
return id;
}
}
public class RadioGateway
{
private int id;
public int Id
{
get { return id; }
set { id = value; }
}
private string ip;
public string Ip
{
get { return ip; }
set { ip = value; }
}
private string interfaceIP;
public string InterfaceIP
{
get { return interfaceIP; }
set { interfaceIP = value; }
}
private int imei;
public int Imei
{
get { return imei; }
set { imei = value; }
}
private int gw_id;
public int Gw_id
{
get { return gw_id; }
set { gw_id = value; }
}
private int gw_voice;
public int Gw_voice
{
get { return gw_voice; }
set { gw_voice = value; }
}
private int subnetwork;
public int Subnetwork
{
get { return subnetwork; }
set { subnetwork = value; }
}
public RadioGateway(int id, string ip, int imei, int gw_id, int gw_voice, int subnetwork)
{
this.id = id;
this.ip = ip;
this.gw_id = gw_id;
this.imei = imei;
this.gw_voice = gw_voice;
this.subnetwork = subnetwork;
}
}
}