2015-08-01 19:58:53 +02:00
using System ;
using System.Collections.Generic ;
using System.Linq ;
using System.Text ;
using System.Threading.Tasks ;
using System.Runtime.Serialization.Formatters.Binary ;
using System.Runtime.Serialization ;
using System.IO ;
using System.Xml ;
2017-02-25 08:33:56 +01:00
using System.Windows.Forms ;
2018-04-09 14:16:56 +02:00
using System.Text.RegularExpressions ;
2015-08-01 19:58:53 +02:00
namespace EduNetworkBuilder
{
[Serializable]
public class NetworkCard
{
public string MAC = NB . GenerateMACAddress ( ) ; //Technically we should make sure it is unique
2018-05-16 16:14:18 +02:00
public List < NetworkInterface > interfaces = new List < NetworkInterface > ( ) ;
2015-08-01 19:58:53 +02:00
public bool UsesDHCP = false ;
public bool CanUseDHCP = false ;
public bool MustUseDHCP = false ;
2018-05-16 16:14:18 +02:00
public NicType myNicType = NicType . eth ;
2015-08-01 19:58:53 +02:00
public HostNicID myID ;
public int ConnectedLink = - 1 ; //The link that is connected to this nic.
2018-05-16 16:14:18 +02:00
public int UniqueIdentifier = NB . GetUniqueIdentifier ( ) ;
public string _nic_name = "" ;
2018-03-09 22:47:33 +01:00
public NB_IPAddress TunnelEndpoint ;
2015-08-01 19:58:53 +02:00
public string EncryptionKey ;
2015-08-19 21:42:04 +02:00
public string SSID ;
public string WirelessKey
{
get { return EncryptionKey ; }
set { EncryptionKey = value ; }
}
2015-08-01 19:58:53 +02:00
2016-10-04 02:49:05 +02:00
public int IFCount { get { return interfaces . Count ; } }
2018-05-14 15:05:56 +02:00
public NetworkCard ( ) { }
2015-08-01 19:58:53 +02:00
public NetworkCard ( int index , int HostID , string hostname , NicType theType = NicType . eth )
{
myNicType = theType ;
_nic_name = myNicType . ToString ( ) + index . ToString ( ) ;
2016-10-31 17:57:25 +01:00
myID = new HostNicID ( HostID , UniqueIdentifier , hostname , _nic_name ) ;
2015-08-01 19:58:53 +02:00
NetworkInterface nInterface = new NetworkInterface ( NicName ( ) , NB . ZeroIPString , NB . ZeroIPString , myID ) ;
if ( theType = = NicType . lo )
nInterface = new NetworkInterface ( NicName ( ) , "127.0.0.1" , "255.0.0.0" , myID ) ;
interfaces . Add ( nInterface ) ;
ApplyNicRules ( ) ;
SetIPForDHCP ( ) ;
}
2018-04-09 14:16:56 +02:00
public NetworkCard ( NetworkCard old , bool duplicateID = false )
{
if ( old = = null ) return ;
MAC = old . MAC ;
foreach ( NetworkInterface one in old . interfaces )
{
NetworkInterface nInterface = new NetworkInterface ( one ) ;
interfaces . Add ( nInterface ) ;
}
UsesDHCP = old . UsesDHCP ;
CanUseDHCP = old . CanUseDHCP ;
MustUseDHCP = old . MustUseDHCP ;
myNicType = old . myNicType ;
myID = old . myID ;
if ( duplicateID = = false )
myID = new HostNicID ( old . myID . HostID , UniqueIdentifier , old . myID . HostName , old . NicName ( ) ) ;
ConnectedLink = old . ConnectedLink ;
if ( duplicateID = = false )
ConnectedLink = - 1 ; //It should be a new card, not connected to anything.
TunnelEndpoint = old . TunnelEndpoint ;
EncryptionKey = old . EncryptionKey ;
SSID = old . SSID ;
}
2015-08-01 19:58:53 +02:00
public NetworkCard ( XmlNode theNode )
{
foreach ( XmlNode Individual in theNode . ChildNodes )
{
XmlNodeType myNodetype = Individual . NodeType ;
if ( myNodetype = = XmlNodeType . Element )
{
switch ( Individual . Name . ToLower ( ) )
{
case "nictype" :
myNicType = NB . ParseEnum < NicType > ( Individual . InnerText ) ;
break ;
case "nicname" :
_nic_name = Individual . InnerText ;
break ;
case "myid" :
myID = new HostNicID ( Individual ) ;
break ;
case "usesdhcp" :
bool . TryParse ( Individual . InnerText , out UsesDHCP ) ;
break ;
case "uniqueidentifier" :
int . TryParse ( Individual . InnerText , out UniqueIdentifier ) ;
break ;
case "interface" :
NetworkInterface newIF = new NetworkInterface ( Individual , myID ) ;
interfaces . Add ( newIF ) ;
break ;
case "mac" :
string tmac = Individual . InnerText ;
if ( tmac . Length = = MAC . Length )
MAC = tmac . ToUpper ( ) ; //Make sure it is all uppercase
break ;
case "tunnelendpoint" :
2018-03-09 22:47:33 +01:00
TunnelEndpoint = new NB_IPAddress ( Individual ) ;
2015-08-01 19:58:53 +02:00
break ;
case "encryptionkey" :
EncryptionKey = Individual . InnerText ;
break ;
2015-08-22 22:51:27 +02:00
case "ssid" :
SSID = Individual . InnerText ;
break ;
2015-08-01 19:58:53 +02:00
}
}
}
ApplyNicRules ( ) ;
SetIPForDHCP ( ) ;
}
2018-04-09 14:16:56 +02:00
public void ChangeType ( NicType newtype )
{
NicType oldType = myNicType ;
myNicType = newtype ;
string index = Regex . Match ( _nic_name , @"\d+$" ) . Value ;
if ( index = = "" ) index = "0" ;
string OldName = oldType . ToString ( ) + index . ToString ( ) ;
_nic_name = myNicType . ToString ( ) + index . ToString ( ) ;
foreach ( NetworkInterface NI in interfaces )
{
//NI.nic_name = Regex.Replace(NI.nic_name, OldName, _nic_name);
NI . nic_name = _nic_name ; //This does not work when we have eth0:1...
NI . AttachedToHostNic . NicName = _nic_name ;
}
myID . NicName = _nic_name ;
if ( newtype = = NicType . port | | newtype = = NicType . wport )
interfaces . Clear ( ) ;
}
2015-08-01 19:58:53 +02:00
2018-03-16 20:17:57 +01:00
public bool Equals ( NetworkCard compareWith )
{
if ( MAC ! = compareWith . MAC ) return false ;
//List<NetworkInterface> interfaces = new List<NetworkInterface>();
2018-03-16 20:35:12 +01:00
if ( interfaces . Count ! = compareWith . interfaces . Count ) return false ;
for ( int i = 0 ; i < interfaces . Count ; i + + )
if ( ! interfaces [ i ] . Equals ( compareWith . interfaces [ i ] ) ) return false ;
2018-03-16 20:17:57 +01:00
if ( UsesDHCP ! = compareWith . UsesDHCP ) return false ;
if ( CanUseDHCP ! = compareWith . CanUseDHCP ) return false ;
if ( MustUseDHCP ! = compareWith . MustUseDHCP ) return false ;
if ( myNicType ! = compareWith . myNicType ) return false ;
if ( myID . HostID ! = compareWith . myID . HostID ) return false ;
if ( ConnectedLink ! = compareWith . ConnectedLink ) return false ;
if ( UniqueIdentifier ! = compareWith . UniqueIdentifier ) return false ;
if ( _nic_name ! = compareWith . _nic_name ) return false ;
if ( TunnelEndpoint ! = compareWith . TunnelEndpoint ) return false ;
if ( EncryptionKey ! = compareWith . EncryptionKey ) return false ;
if ( SSID ! = compareWith . SSID ) return false ;
return true ;
}
2015-08-01 19:58:53 +02:00
private void ApplyNicRules ( )
{
//These are the defaults for the various nic types. They can be overridden by the device.
if ( myNicType = = NicType . eth | | myNicType = = NicType . management_interface | | myNicType = = NicType . wlan )
{
CanUseDHCP = true ;
}
else
{
CanUseDHCP = false ;
}
}
public void Save ( XmlWriter writer )
{
writer . WriteStartElement ( "nic" ) ;
writer . WriteElementString ( "nictype" , myNicType . ToString ( ) ) ;
writer . WriteElementString ( "nicname" , _nic_name ) ;
myID . Save ( writer , "myid" ) ;
writer . WriteElementString ( "nictype" , myNicType . ToString ( ) ) ;
writer . WriteElementString ( "uniqueidentifier" , UniqueIdentifier . ToString ( ) ) ;
writer . WriteElementString ( "usesdhcp" , UsesDHCP . ToString ( ) ) ;
if ( EncryptionKey ! = "" )
writer . WriteElementString ( "encryptionkey" , EncryptionKey ) ;
2015-08-22 22:51:27 +02:00
if ( SSID ! = "" )
writer . WriteElementString ( "ssid" , SSID ) ;
2015-08-01 19:58:53 +02:00
if ( TunnelEndpoint ! = null )
{
TunnelEndpoint . Save ( writer , "tunnelendpoint" ) ;
}
foreach ( NetworkInterface nIF in interfaces )
{
nIF . Save ( writer ) ;
}
writer . WriteEndElement ( ) ;
}
public int GetUniqueIdentifier
{
get { return UniqueIdentifier ; }
}
public NicType GetNicType
{
get { return myNicType ; }
}
2015-08-22 19:57:27 +02:00
public bool isWireless ( )
{
if ( myNicType = = NicType . wport | | myNicType = = NicType . wlan )
return true ;
return false ;
}
2015-08-01 19:58:53 +02:00
public bool HasIP ( UInt32 IP )
{
if ( myNicType = = NicType . port ) return false ;
if ( myNicType = = NicType . none ) return false ;
foreach ( NetworkInterface IF in interfaces )
{
if ( IF . myIP . Equals ( IP ) )
return true ;
}
return false ;
}
public string NicName ( )
{
return _nic_name ;
}
public string NicString ( int index )
{
string connected = " " ;
if ( isConnected ( true ) ) connected = "*" ;
2015-08-10 04:09:17 +02:00
if ( myNicType = = NicType . port | | myNicType = = NicType . wport )
2016-10-06 16:17:27 +02:00
{
NetworkDevice ND = ConnectedTo ( ) ;
if ( ND ! = null )
return NicName ( ) + connected + " -> " + ND . hostname ;
2015-08-01 19:58:53 +02:00
return NicName ( ) + connected ;
2016-10-06 16:17:27 +02:00
}
2015-08-01 19:58:53 +02:00
return NicName ( ) + connected + " " + MAC ;
}
public List < string > NICRouteStrings ( string GW )
{
List < string > thestrings = new List < string > ( ) ;
if ( myNicType = = NicType . port ) return thestrings ;
if ( myNicType = = NicType . none ) return thestrings ;
foreach ( NetworkInterface iface in interfaces )
{
thestrings . Add ( iface . RouteString ( _nic_name , GW ) ) ;
}
return thestrings ;
}
public List < string > IPAddresses ( bool UseCidr = false )
{
List < string > theIPs = new List < string > ( ) ;
if ( myNicType = = NicType . port ) return theIPs ;
2015-08-22 22:57:25 +02:00
if ( myNicType = = NicType . wport ) return theIPs ;
2015-08-01 19:58:53 +02:00
if ( myNicType = = NicType . none ) return theIPs ;
string DHCPString = "" ;
if ( UsesDHCP & & CanUseDHCP ) DHCPString = "DHCP: " ;
foreach ( NetworkInterface iface in interfaces )
{
theIPs . Add ( DHCPString + iface . InterfaceString ( UseCidr ) ) ;
}
return theIPs ;
}
2018-03-09 22:47:33 +01:00
public List < NB_IPAddress > IPAddressList ( )
2015-08-01 19:58:53 +02:00
{
2018-03-09 22:47:33 +01:00
List < NB_IPAddress > theIPs = new List < NB_IPAddress > ( ) ;
2015-08-01 19:58:53 +02:00
if ( myNicType = = NicType . port ) return theIPs ;
if ( myNicType = = NicType . none ) return theIPs ;
foreach ( NetworkInterface iface in interfaces )
{
theIPs . Add ( iface . myIP ) ;
}
return theIPs ;
}
2018-03-09 22:47:33 +01:00
public bool HasIPAddresses ( NB_IPAddress dest )
2015-08-01 19:58:53 +02:00
{
if ( myNicType = = NicType . port ) return false ;
if ( myNicType = = NicType . none ) return false ;
if ( dest = = null ) return false ;
foreach ( NetworkInterface iface in interfaces )
{
if ( iface . myIP . GetIP = = dest . GetIP )
return true ;
}
return false ;
}
2015-09-20 14:16:27 +02:00
2018-03-09 22:47:33 +01:00
public bool HasBroadcastAddresses ( NB_IPAddress dest )
2015-08-01 19:58:53 +02:00
{
if ( myNicType = = NicType . port ) return false ;
if ( myNicType = = NicType . none ) return false ;
2015-09-20 14:16:27 +02:00
if ( myNicType = = NicType . lo ) return false ;
2015-08-01 19:58:53 +02:00
if ( dest = = null ) return false ;
foreach ( NetworkInterface iface in interfaces )
{
2015-09-20 23:23:31 +02:00
if ( iface . myIP . BroadcastAddress = = 0 ) continue ; //a netmask of 0.0.0.0 causes grief
2015-08-01 19:58:53 +02:00
if ( iface . myIP . BroadcastAddress = = dest . GetIP )
return true ; //If they are pinging the broadcast IP
2015-08-27 01:14:34 +02:00
if ( iface . myIP . GetIPString = = NB . BroadcastIPString )
return true ;
2015-08-01 19:58:53 +02:00
}
return false ;
}
2017-02-25 08:33:56 +01:00
public void EditInterface ( int index , Form ParentForm )
2015-08-01 19:58:53 +02:00
{
if ( index < 0 | | index > interfaces . Count ( ) )
return ;
2017-02-25 08:33:56 +01:00
interfaces [ index ] . EditAddress ( ParentForm ) ;
2015-08-01 19:58:53 +02:00
}
public void DeleteInterface ( int index )
{
if ( index < 0 | | index > interfaces . Count ( ) )
return ;
if ( interfaces . Count < 2 )
return ; //We cannot delete the sole remaining interface
interfaces . RemoveAt ( index ) ;
}
2016-10-10 01:03:00 +02:00
private int NextInterfaceNum ( )
{
string NICName = NicName ( ) ;
string testname = "" ;
bool foundone = true ;
int which = 0 ;
while ( foundone )
{
foundone = false ;
testname = NICName ;
if ( which ! = 0 )
testname = testname + ":" + which . ToString ( ) ;
foreach ( NetworkInterface oneif in interfaces )
{
if ( oneif . nic_name = = testname )
{
foundone = true ;
break ;
}
}
if ( foundone )
which + + ;
}
return which ;
}
2015-08-01 19:58:53 +02:00
public void AddInterface ( )
{
2016-10-10 01:03:00 +02:00
string NICName = NicName ( ) ;
int Plus = NextInterfaceNum ( ) ;
if ( Plus ! = 0 ) NICName + = ":" + Plus . ToString ( ) ;
NetworkInterface iface = new NetworkInterface ( NICName , NB . ZeroIPString , NB . ZeroIPString , myID ) ;
2015-08-01 19:58:53 +02:00
interfaces . Add ( iface ) ;
}
public NetworkInterface GetInterface ( int index )
{
if ( index < 0 | | index > interfaces . Count ( ) )
return null ;
return interfaces [ index ] ;
}
public bool isConnected ( bool CheckForBrokenLink )
{
if ( ConnectedLink ! = - 1 )
{
if ( ! CheckForBrokenLink ) return true ; //We only care if there is any link
Network myNet = NB . GetNetwork ( ) ;
NetworkLink NL = myNet . GetLinkFromID ( ConnectedLink ) ;
if ( NL ! = null )
{
if ( NL . theLinkType ! = LinkType . broken ) return true ;
else return false ;
}
}
return false ;
}
2016-10-06 16:17:27 +02:00
public NetworkDevice ConnectedTo ( )
{
if ( ConnectedLink ! = - 1 )
{
Network myNet = NB . GetNetwork ( ) ;
NetworkLink NL = myNet . GetLinkFromID ( ConnectedLink ) ;
if ( NL ! = null )
{
if ( NL . IsSource ( myID . HostID ) ) return myNet . GetDeviceFromID ( NL . Dst ) ;
else return myNet . GetDeviceFromID ( NL . Src ) ;
}
}
return null ;
}
2015-08-01 19:58:53 +02:00
public void Edit ( )
{
NetworkCardEditor nce = new NetworkCardEditor ( this ) ;
nce . ShowDialog ( ) ;
//Now. update the interfaces if we need to do so.
SetIPForDHCP ( ) ;
}
public void SetIPForDHCP ( )
{
if ( UsesDHCP & & CanUseDHCP )
{
//remove any extra interfaces.
//set the one interface to 0.0.0.0
switch ( myNicType )
{
case NicType . eth :
case NicType . wlan :
interfaces . Clear ( ) ; //empty out all interfaces
//Give it an interface with an empty IP
NetworkInterface nInterface = new NetworkInterface ( NicName ( ) , NB . ZeroIPString , NB . ZeroIPString , myID ) ;
interfaces . Add ( nInterface ) ;
break ;
}
}
}
2018-03-09 22:47:33 +01:00
public void SetIPForDHCP ( NB_IPAddress newIP )
2015-08-01 19:58:53 +02:00
{
if ( UsesDHCP & & CanUseDHCP )
{
2015-08-27 01:14:34 +02:00
bool doIt = true ;
if ( interfaces . Count > 0 & & interfaces [ 0 ] . myIP . GetIPString = = NB . ZeroIPString )
{
doIt = true ;
}
else
{
if ( interfaces . Count > 0 )
{
//The IP address is not zero. This means it already has an IP.
Random rnd = NB . GetRandom ( ) ;
int chance = rnd . Next ( 6 ) ;
if ( chance > 3 )
doIt = false ;
}
}
if ( doIt & & interfaces . Count > 0 )
{
2015-08-01 19:58:53 +02:00
interfaces [ 0 ] . myIP = newIP ;
2015-08-27 01:14:34 +02:00
}
2015-08-01 19:58:53 +02:00
}
}
2018-03-09 22:47:33 +01:00
public bool HasLocalInterface ( NB_IPAddress theIP )
2015-08-01 19:58:53 +02:00
{
foreach ( NetworkInterface nIF in interfaces )
{
if ( nIF . isLocal ( theIP ) )
return true ;
}
return false ;
}
/// <summary>
/// Return the interface that is considered "local" to the IP address we are trying to reach
/// </summary>
/// <param name="theIP">An IP address we are trying to send out</param>
/// <returns>null if no interface is local. Otherwise, it returns the one that matches the packet</returns>
2018-03-09 22:47:33 +01:00
public NetworkInterface LocalInterface ( NB_IPAddress theIP , PacketMessage Tracker )
2015-08-01 19:58:53 +02:00
{
2016-10-10 01:03:00 +02:00
if ( myNicType = = NicType . port )
{
if ( interfaces . Count = = 1 ) return interfaces [ 0 ] ;
return null ; //ports have no local interfaces
}
2015-08-01 19:58:53 +02:00
foreach ( NetworkInterface nIF in interfaces )
{
if ( nIF . isLocal ( theIP ) )
{
if ( Tracker ! = null )
2015-11-16 23:02:03 +01:00
Tracker . AddMessage ( DebugLevel . routing , myID . HostName , string . Format ( NB . Translate ( "NC_FoundLocalIF" ) , nIF . myIP . GetIP . ToIpString ( ) , nIF . myIP . GetMask . ToIpString ( ) ) ) ;
2015-11-16 22:26:28 +01:00
if ( Tracker ! = null & & theIP ! = null )
2015-11-16 23:02:03 +01:00
Tracker . AddMessage ( DebugLevel . routing , myID . HostName , " " + string . Format ( NB . Translate ( "NC_IfOfLocal" ) , theIP . GetIPString , theIP . GetGateway . ToIpString ( ) ) ) ;
2015-08-01 19:58:53 +02:00
return nIF ;
}
}
return null ;
}
2015-09-19 17:09:51 +02:00
public NetworkInterface PrimaryInterface ( )
{
if ( interfaces . Count = = 1 )
return interfaces [ 0 ] ;
return null ;
}
2015-08-01 19:58:53 +02:00
2016-10-20 21:53:23 +02:00
public bool NicCanDoVLANs ( )
{
if ( myNicType = = NicType . eth ) return true ;
if ( myNicType = = NicType . port ) return true ;
if ( myNicType = = NicType . wan ) return true ;
return false ;
}
2015-08-01 19:58:53 +02:00
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
public bool SendPacketOutNIC ( Packet tPacket )
{
bool madeprogress = false ;
Packet nPacket = null ;
Network myNet = NB . GetNetwork ( ) ;
NetworkLink nl ;
if ( NB . GetComponentType ( tPacket . WhereAmI ) ! = GeneralComponentType . device ) return false ; //we cannot do this.
NetworkDevice WhereFrom = ( NetworkDevice ) tPacket . WhereAmI ;
2015-09-01 19:07:23 +02:00
NicType what = GetNicType ;
2015-09-02 01:19:31 +02:00
if ( ! tPacket . isFresh & & WhereFrom . IsWirelessForwarder ( ) & & ( what = = NicType . wlan | | ( WhereFrom . GetNetType ( ) = = NetworkComponentType . wap & & what = = NicType . eth ) ) )
2015-09-01 19:07:23 +02:00
what = NicType . wport ;
2018-04-04 12:04:31 +02:00
if ( myNet . ItemHasTest ( WhereFrom . hostname , NicName ( ) , NetTestType . DeviceNICSprays ) )
{
2018-04-05 15:40:42 +02:00
if ( ! myNet . ItemTestIsComplete ( WhereFrom . hostname , NicName ( ) , NetTestType . DeviceNICSprays ) )
{
if ( tPacket . MyType = = PacketType . bad_packet ) return false ;
//We should drop the current packet.
tPacket . AddMessage ( DebugLevel . info , NB . Translate ( "N_ProssShouldContinTime" ) ) ;
tPacket . Tracking . Status = NB . Translate ( "N_ProssShouldContinTime" ) ;
tPacket . MyStatus = PacketStatus . finished_failed ;
//Then, we make a bad packet go out from here
WhereFrom . BadSprayCount = NB . NumBadPackets ;
WhereFrom . BadPacketFromHere ( ) ;
return false ;
}
2018-04-04 12:04:31 +02:00
}
2015-09-01 19:07:23 +02:00
switch ( what )
2015-08-01 19:58:53 +02:00
{
case NicType . lo :
case NicType . management_interface :
case NicType . none :
break ; //Do nothing
case NicType . eth :
2015-08-10 00:33:00 +02:00
case NicType . wlan :
2015-08-01 19:58:53 +02:00
//see if it the packet dest is local to this nic
2016-12-11 01:47:14 +01:00
//if (tPacket.MyType == PacketType.dhcp_answer)
// Console.WriteLine("DHCP Answer");
2015-08-01 19:58:53 +02:00
foreach ( NetworkInterface nf in interfaces . ToList ( ) )
{
2015-09-06 20:59:41 +02:00
if ( tPacket . InboundNic ! = null & & tPacket . InboundNic = = this & &
tPacket . InboundInterface ! = null & & tPacket . InboundInterface = = nf )
continue ; //skip sending it out this interface if it came in this interface.
2016-10-28 17:17:13 +02:00
if ( tPacket . MyType = = PacketType . dhcp_answer & & tPacket . isFresh & & tPacket . payloadIP ! = null & & ! nf . isLocal ( tPacket . payloadIP ) )
continue ; //If it is a dhcp broadcast, only send it out the interface it belongs to
2015-08-01 19:58:53 +02:00
if ( tPacket . MyType = = PacketType . arp_request & & ! nf . isLocal ( tPacket . destIP ) )
continue ; //only send out arp requests on local networks
nPacket = new Packet ( tPacket ) ; //Creates a new packet but sets isfresh=false
2016-11-08 03:31:07 +01:00
nPacket . OutboundIF = nf ;
2016-12-11 01:47:14 +01:00
if ( nf ! = null )
nPacket . TsourceIP = nf . myIP ;
2016-11-08 03:31:07 +01:00
nPacket . InboundInterface = tPacket . InboundInterface ;
2016-10-10 01:03:00 +02:00
nf . ProcessOutboundPacket ( nPacket ) ;
if ( nPacket . MyStatus = = PacketStatus . finished | | nPacket . MyStatus = = PacketStatus . finished_failed | | nPacket . MyStatus = = PacketStatus . finished_ok )
continue ; //If the packet cannot be sent out (VLAN stuff)
2015-09-20 14:16:27 +02:00
if ( tPacket . OutboundIP = = null | | ( nf . isLocal ( tPacket . OutboundIP ) | | ( tPacket . OutboundIP . GetIPString = = NB . BroadcastIPString & & tPacket . isFresh ) ) )
2015-08-01 19:58:53 +02:00
{
if ( ( nf ! = null & & nf . myIP . GetIPString ! = NB . ZeroIPString ) | | nPacket . MyType = = PacketType . dhcp_request )
{
//this means we have a local interface to send it out of
2015-09-19 21:43:32 +02:00
//if(nPacket.sourceMAC == null || nPacket.sourceMAC == "" || nPacket.isFresh)
2015-08-01 19:58:53 +02:00
nPacket . sourceMAC = MAC ;
//If the source IP is empty then it is a new packet. We set the source to be us
if ( nPacket . sourceIP = = null | | nPacket . sourceIP . GetIPString = = NB . ZeroIPString )
nPacket . sourceIP = nf . myIP ;
nPacket . TsourceIP = nf . myIP ;
if ( nPacket . destMAC = = null | | nPacket . destMAC = = "" )
{
nPacket . destMAC = WhereFrom . LookupArpFromIP ( tPacket . OutboundIP . GetIPString ) ;
if ( nPacket . MyType = = PacketType . arp_request )
{
nPacket . destMAC = NB . BroadcastMACString ;
}
if ( nPacket . destMAC = = "" )
{
2017-06-28 17:53:19 +02:00
nPacket . AddMessage ( DebugLevel . info , string . Format ( NB . Translate ( "NC_NoIPOnSubStr" ) , nPacket . destIP . GetIPString ) ) ;
2015-08-01 19:58:53 +02:00
Network mynet = NB . GetNetwork ( ) ;
NetworkDevice nd = mynet . GetDeviceFromID ( myID ) ;
2015-11-16 22:26:28 +01:00
string hostname = NB . Translate ( "NC_NoHost" ) ;
2015-08-01 19:58:53 +02:00
if ( nd ! = null ) hostname = nd . hostname ;
2015-11-21 23:55:03 +01:00
nPacket . Tracking . Status = NB . LeftPad ( hostname ) + string . Format ( NB . Translate ( "NC_NoIPOnSubStr" ) , hostname , nPacket . destIP . GetIPString ) ;
2017-06-28 17:53:19 +02:00
nPacket . Tracking . Finished = true ;
2015-08-01 19:58:53 +02:00
nPacket . MyStatus = PacketStatus . finished_failed ;
return false ;
}
}
nl = myNet . GetLinkFromID ( ConnectedLink ) ;
nPacket . StartOnLink ( nl , WhereFrom ) ; //This sends the packet down the link.
//Store outbound information here - som we expect the returning packet
WhereFrom . StoreOutgoingPacketInfo ( nPacket ) ;
myNet . addPacket ( nPacket ) ;
nPacket . PacketDump ( myID . HostName + "-" + _nic_name , DebugPausePoint . packet_out ) ;
madeprogress = true ;
}
}
}
break ;
case NicType . wan :
//see if it the packet dest is local to this nic
if ( tPacket . MyType = = PacketType . dhcp_answer & & tPacket . isFresh )
break ; //We do not broadcast out the WAN port
foreach ( NetworkInterface nf in interfaces . ToList ( ) )
{
nPacket = new Packet ( tPacket ) ; //Creates a new packet but sets isfresh=false
2016-11-08 03:31:07 +01:00
nPacket . OutboundIF = nf ;
nPacket . InboundInterface = tPacket . InboundInterface ;
2016-12-11 01:47:14 +01:00
if ( nf ! = null )
nPacket . TsourceIP = nf . myIP ;
2016-11-08 03:31:07 +01:00
2016-10-10 01:03:00 +02:00
nf . ProcessOutboundPacket ( nPacket ) ;
if ( nPacket . MyStatus = = PacketStatus . finished | | nPacket . MyStatus = = PacketStatus . finished_failed | | nPacket . MyStatus = = PacketStatus . finished_ok )
continue ; //If the packet cannot be sent out (VLAN stuff)
2015-08-28 22:58:27 +02:00
if ( tPacket . OutboundIP ! = null & & ( nf . isLocal ( tPacket . OutboundIP ) | | tPacket . OutboundIP . GetIPString = = NB . BroadcastIPString ) )
2015-08-01 19:58:53 +02:00
{
if ( ( nf ! = null & & nf . myIP ! = null & & nf . myIP . GetIPString ! = NB . ZeroIPString ) | | nPacket . MyType = = PacketType . dhcp_request )
{
//this means we have a local interface to send it out of
nPacket . sourceMAC = MAC ;
//If the source IP is empty then it originated from here. We set the source to be us
if ( nPacket . sourceIP = = null | | nPacket . sourceIP . GetIPString = = NB . ZeroIPString )
{
nPacket . sourceIP = nf . myIP ;
WhereFrom . StoreOutgoingPacketInfo ( nPacket ) ; //the packet is not masqueraded, just accepted
}
else
{
//When we leave the WAN port, we are masqueraded. Track that.
WhereFrom . StoreOutgoingPacketInfo ( nPacket , ResponseToPacket . masq ) ;
2017-08-27 02:05:44 +02:00
tPacket . TraversalInformation . AddPath ( WhereFrom . hostname , TraversalTechnology . masquerade , "" ) ; //We just masqueraded
2015-08-01 19:58:53 +02:00
//Now, we masquerade the packet so it looks like it comes fromhere
2015-11-19 20:11:13 +01:00
nPacket . Tracking . AddMessage ( DebugLevel . natting , WhereFrom . hostname , string . Format ( NB . Translate ( "NC_ChangeIPStr" ) , nf . myIP . GetIPString ) ) ;
2015-08-01 19:58:53 +02:00
nPacket . sourceIP = nf . myIP ;
}
nPacket . TsourceIP = nf . myIP ;
if ( nPacket . destMAC = = null | | nPacket . destMAC = = "" )
{
nPacket . destMAC = WhereFrom . LookupArpFromIP ( tPacket . OutboundIP . GetIPString ) ;
if ( nPacket . destMAC = = "" )
{
2015-11-19 19:30:40 +01:00
nPacket . AddMessage ( DebugLevel . debug , string . Format ( NB . Translate ( "NC_NoIPOnSubStr" ) , nPacket . destIP . GetIPString ) ) ;
2015-08-01 19:58:53 +02:00
Network mynet = NB . GetNetwork ( ) ;
NetworkDevice nd = mynet . GetDeviceFromID ( myID ) ;
2015-11-16 22:26:28 +01:00
string hostname = NB . Translate ( "NC_NoHost" ) ;
2015-08-01 19:58:53 +02:00
if ( nd ! = null ) hostname = nd . hostname ;
2015-11-21 23:55:03 +01:00
nPacket . Tracking . Status = NB . LeftPad ( hostname ) + string . Format ( NB . Translate ( "NC_NoIPOnSubStr" ) , nPacket . destIP . GetIPString ) ;
2015-08-01 19:58:53 +02:00
nPacket . MyStatus = PacketStatus . finished_failed ;
return false ;
}
}
nl = myNet . GetLinkFromID ( ConnectedLink ) ;
nPacket . StartOnLink ( nl , WhereFrom ) ; //This sends the packet down the link.
//Store outbound information here - som we expect the returning packet
WhereFrom . StoreOutgoingPacketInfo ( nPacket ) ;
myNet . addPacket ( nPacket ) ;
nPacket . PacketDump ( myID . HostName + "-" + _nic_name , DebugPausePoint . packet_out ) ;
madeprogress = true ;
}
}
}
break ;
case NicType . tun :
case NicType . vpn :
foreach ( NetworkInterface nf in interfaces . ToList ( ) )
{
2017-08-31 16:43:44 +02:00
//make sure the firewall allows this.
if ( tPacket . WhereAmI ! = null & & tPacket . WhereAmI is NetworkDevice )
{
NetworkDevice ND = ( NetworkDevice ) tPacket . WhereAmI ;
if ( tPacket . InboundInterface ! = null & & nf ! = null & & ! ND . FirewallAllows ( tPacket . InboundInterface . nic_name , nf . nic_name ) )
{
//The firewall might block it. Check to see if it is a response packet
ResponseToPacket rtp = ND . HowToRespondToPacket ( tPacket ) ;
if ( rtp ! = ResponseToPacket . accept )
{
//If we are here, the packet is rejected.
string message = string . Format ( NB . Translate ( "P_FirewallDropped" ) , ND . hostname ) ;
tPacket . AddMessage ( DebugLevel . filtering , message ) ;
tPacket . Tracking . Status = message ;
tPacket . Tracking . AddMessage ( DebugLevel . info , ND , message ) ;
tPacket . AddMessage ( DebugLevel . info , message ) ;
tPacket . MyStatus = PacketStatus . finished_ok ;
break ;
}
}
}
2019-02-23 18:03:28 +01:00
if ( nf ! = null & & nf . myIP ! = null & & nf . myIP . GetIPString ! = NB . ZeroIPString )
{
//If the source IP is empty then it originated from here. We set the source to be us
if ( tPacket . sourceIP = = null | | tPacket . sourceIP . GetIPString = = NB . ZeroIPString )
{
tPacket . sourceIP = nf . myIP ;
WhereFrom . StoreOutgoingPacketInfo ( tPacket ) ; //the packet is not yet tunneled
}
}
2015-09-01 22:52:08 +02:00
if ( nf . isLocal ( tPacket . OutboundIP , false ) )
2015-08-01 19:58:53 +02:00
{
//We need to tell the original packet that it is inside another packet
tPacket . MyStatus = PacketStatus . encapsulated ;
2017-08-27 02:05:44 +02:00
tPacket . TraversalInformation . AddPath ( WhereFrom . hostname , TraversalTechnology . vpn , NicName ( ) ) ; //We just started a VPN
2015-08-01 19:58:53 +02:00
tPacket . TsourceIP = nf . myIP ;
tPacket . destMAC = WhereFrom . LookupArpFromIP ( tPacket . OutboundIP . GetIPString ) ;
2016-12-11 01:47:14 +01:00
if ( nf ! = null )
tPacket . TsourceIP = nf . myIP ;
2015-08-01 19:58:53 +02:00
//We need to make a new, tunnel packet
if ( myNicType = = NicType . tun )
EncryptionKey = "" ;
Packet rnPacket = new Packet ( tPacket ) ;
WhereFrom . TunnelPacketFromHere ( TunnelEndpoint , rnPacket , EncryptionKey ) ;
//We need to send the new packet on (pass it back to the device to process)
madeprogress = true ;
2017-08-25 17:37:20 +02:00
if ( EncryptionKey ! = "" )
2017-08-27 02:05:44 +02:00
tPacket . TraversalInformation . AddPath ( WhereFrom . hostname , TraversalTechnology . vpn_encryption , NicName ( ) ) ; //We the packet is "encrypted"
2015-08-01 19:58:53 +02:00
}
}
break ;
case NicType . port :
2015-08-10 00:33:00 +02:00
case NicType . wport :
2015-08-27 02:20:26 +02:00
if ( tPacket . InboundNic = = this )
break ; //This is the port we came in on. Do not sent it back out this port
2016-10-10 01:03:00 +02:00
foreach ( NetworkInterface nf in interfaces . ToList ( ) )
2015-09-01 22:52:08 +02:00
{
2016-10-10 01:03:00 +02:00
nPacket = new Packet ( tPacket ) ;
2016-11-08 03:31:07 +01:00
nPacket . OutboundIF = nf ;
nPacket . InboundInterface = tPacket . InboundInterface ;
2016-10-10 01:03:00 +02:00
nf . ProcessOutboundPacket ( nPacket ) ;
if ( nPacket . MyStatus = = PacketStatus . finished | | nPacket . MyStatus = = PacketStatus . finished_failed | | nPacket . MyStatus = = PacketStatus . finished_ok )
continue ; //If the packet cannot be sent out (VLAN stuff)
if ( ( tPacket . InboundNic ! = null & & tPacket . InboundNic . GetNicType = = NicType . wan ) | | tPacket . InboundNic = = null | | tPacket . destMAC = = "" )
2015-09-01 22:52:08 +02:00
{
2016-10-10 01:03:00 +02:00
//We need to find destination MAC and set source MAC
if ( nPacket . sourceMAC = = null | | nPacket . sourceMAC = = "" | | nPacket . isFresh )
nPacket . sourceMAC = MAC ;
//Update the MAC
string getMAC = "" ;
if ( tPacket . OutboundIP ! = null )
getMAC = WhereFrom . LookupArpFromIP ( tPacket . OutboundIP . GetIPString ) ;
else if ( tPacket . destIP ! = null )
getMAC = WhereFrom . LookupArpFromIP ( tPacket . destIP . GetIPString ) ;
if ( getMAC ! = "" ) nPacket . destMAC = getMAC ;
if ( nPacket . MyType = = PacketType . arp_request )
{
nPacket . destMAC = NB . BroadcastMACString ;
}
2015-09-01 22:52:08 +02:00
2016-10-10 01:03:00 +02:00
if ( nPacket . destMAC = = "" )
{
nPacket . AddMessage ( DebugLevel . debug , String . Format ( NB . Translate ( "NC_NoIPOnSubStr" ) , nPacket . destIP . GetIPString ) ) ;
Network mynet = NB . GetNetwork ( ) ;
NetworkDevice nd = mynet . GetDeviceFromID ( myID ) ;
string hostname = NB . Translate ( "NC_NoHost" ) ;
if ( nd ! = null ) hostname = nd . hostname ;
nPacket . Tracking . Status = NB . LeftPad ( hostname ) + String . Format ( NB . Translate ( "NC_NoIPOnSubStr" ) , nPacket . destIP . GetIPString ) ;
nPacket . MyStatus = PacketStatus . finished_failed ;
return false ;
}
}
if ( HasBroadcastAddresses ( tPacket . destIP ) )
2015-09-01 22:52:08 +02:00
{
2016-10-10 01:03:00 +02:00
//Broadcast packets will go to everything and we want a response from all of them.
nPacket . Tracking = PacketMessage . Clone ( tPacket . Tracking ) ;
2015-09-01 22:52:08 +02:00
}
2016-10-10 01:03:00 +02:00
if ( nPacket . sourceMAC = = null | | nPacket . sourceMAC = = "" )
{
//Need to find the managament interface MAC
nPacket . sourceMAC = WhereFrom . HubManagementMAC ( ) ;
}
if ( ( nPacket . sourceIP = = null | | nPacket . sourceIP . GetIPString = = NB . ZeroIPString ) & & nPacket . MyType ! = PacketType . dhcp_request )
{
//set it to be the ip of management interface
nPacket . sourceIP = WhereFrom . HubManagementIP ( ) ;
nPacket . sourceMAC = MAC ;
}
if ( nPacket . destMAC = = null | | nPacket . destMAC = = "" & & tPacket . OutboundIP ! = null )
{
nPacket . destMAC = WhereFrom . LookupArpFromIP ( tPacket . OutboundIP . GetIPString ) ;
}
if ( nPacket . TsourceIP = = null )
nPacket . TsourceIP = WhereFrom . HubManagementIP ( ) ;
nl = myNet . GetLinkFromID ( ConnectedLink ) ;
2016-12-11 01:47:14 +01:00
if ( nl = = null )
break ;
2017-10-05 18:09:31 +02:00
if ( ( nPacket . sourceIP = = null | | nPacket . sourceIP . GetIPString = = NB . ZeroIPString ) & & tPacket . MyType ! = PacketType . dhcp_request ) return false ; //We still have no IP. Do not send the packet out.
2016-10-10 01:03:00 +02:00
nPacket . StartOnLink ( nl , WhereFrom ) ; //This sends the packet down the link.
myNet . addPacket ( nPacket ) ;
if ( tPacket . isFresh )
WhereFrom . StoreOutgoingPacketInfo ( nPacket ) ; //if it originated from here...
madeprogress = true ;
nPacket . PacketDump ( myID . HostName , DebugPausePoint . packet_out ) ;
2015-09-01 22:52:08 +02:00
}
2015-08-01 19:58:53 +02:00
break ;
}
return madeprogress ;
}
2016-10-10 01:03:00 +02:00
2015-08-01 19:58:53 +02:00
public void ProcessInboundPacket ( Packet tPacket )
{
Network mynet ;
NetworkDevice nd ;
//We make sure the MAC matches.
2015-08-28 22:58:27 +02:00
mynet = NB . GetNetwork ( ) ;
nd = mynet . GetDeviceFromID ( myID ) ;
2018-04-04 11:57:58 +02:00
if ( mynet . ItemHasTest ( nd . hostname , NicName ( ) , NetTestType . DeviceNICSprays ) )
{
2018-04-05 15:40:42 +02:00
if ( ! mynet . ItemTestIsComplete ( nd . hostname , NicName ( ) , NetTestType . DeviceNICSprays ) )
{
//We should drop the current packet.
tPacket . AddMessage ( DebugLevel . info , NB . Translate ( "N_ProssShouldContinTime" ) ) ;
tPacket . Tracking . Status = NB . Translate ( "N_ProssShouldContinTime" ) ;
tPacket . MyStatus = PacketStatus . finished_failed ;
//Then, we make a bad packet go out from here
nd . BadPacketFromHere ( ) ;
nd . BadSprayCount = NB . NumBadPackets ;
}
2018-04-04 11:57:58 +02:00
}
if ( mynet . ItemHasTest ( nd . hostname , NetTestType . DeviceIsFrozen ) )
2018-02-13 19:03:01 +01:00
{
2018-02-13 21:34:10 +01:00
if ( ! mynet . ItemTestIsComplete ( nd . hostname , NetTestType . DeviceIsFrozen ) )
2018-02-13 19:03:01 +01:00
{
//the device is busted and nonfunctional. No response.
2018-02-15 10:56:43 +01:00
tPacket . AddMessage ( DebugLevel . info , NB . Translate ( "N_ProssShouldContinTimeout" ) ) ;
tPacket . Tracking . Status = NB . Translate ( "N_ProssShouldContinTimeout" ) ;
2018-02-13 19:03:01 +01:00
tPacket . MyStatus = PacketStatus . finished_failed ;
}
}
2018-02-14 17:38:30 +01:00
if ( nd . IsBurned )
{
//the device is busted and nonfunctional. No response.
tPacket . AddMessage ( DebugLevel . info , NB . Translate ( "NC_Burned" ) ) ;
tPacket . Tracking . Status = NB . Translate ( "NC_Burned" ) ;
tPacket . MyStatus = PacketStatus . finished_failed ;
}
2018-02-13 19:03:01 +01:00
2015-08-27 02:20:26 +02:00
if ( tPacket = = null ) return ;
tPacket . InboundNic = this ; //track which nic we came in on.
2015-09-02 01:19:31 +02:00
if ( myNicType = = NicType . port | | myNicType = = NicType . wport | | ( nd . IsWirelessForwarder ( ) & & ( myNicType = = NicType . wlan | |
( myNicType = = NicType . eth & & nd . GetNetType ( ) = = NetworkComponentType . wap ) ) ) )
2015-08-01 19:58:53 +02:00
{
//Try tracking the arp if we can
if ( tPacket . TsourceIP = = null ) tPacket . TsourceIP = tPacket . sourceIP ;
nd . StoreArp ( tPacket . sourceMAC , tPacket . TsourceIP . GetIP . ToIpString ( ) , myID ) ;
//If it is a return DHCP packet. We should try to update the MAC
if ( tPacket . MyType = = PacketType . dhcp_answer )
{
HostNicID otherid = nd . NicIDFromArp ( tPacket . destMAC ) ;
if ( otherid . HostID ! = - 1 )
2015-09-19 17:09:51 +02:00
{
string ipstring = NB . ZeroIPString ;
if ( tPacket . payloadIP ! = null )
ipstring = tPacket . payloadIP . GetIPString ;
nd . StoreArp ( tPacket . destMAC , ipstring , otherid ) ;
}
2015-08-01 19:58:53 +02:00
}
return ;
}
if ( myNicType = = NicType . wan )
{
mynet = NB . GetNetwork ( ) ;
nd = mynet . GetDeviceFromID ( myID ) ;
2017-06-08 17:19:57 +02:00
if ( nd . HowToRespondToPacket ( tPacket ) = = ResponseToPacket . masq )
2015-08-01 19:58:53 +02:00
{
2018-03-09 22:47:33 +01:00
NB_IPAddress oAddress = nd . PacketMasqueradeSource ( tPacket ) ;
2015-08-01 19:58:53 +02:00
if ( oAddress ! = null )
{
2015-11-19 19:30:40 +01:00
tPacket . Tracking . AddMessage ( DebugLevel . natting , nd . hostname , string . Format ( NB . Translate ( "NC_ChangeIPBackStr" ) , oAddress . GetIPString ) ) ;
2015-08-01 19:58:53 +02:00
tPacket . destIP = oAddress ;
}
}
else if ( ! HasIP ( tPacket . destIP . GetIP ) )
{
2015-11-18 21:11:16 +01:00
tPacket . AddMessage ( DebugLevel . routing , NB . Translate ( "NC_ProssInPackReject" ) ) ;
tPacket . AddMessage ( DebugLevel . debug , NB . Translate ( "NC_ProssInPackExpect" ) ) ;
2015-08-01 19:58:53 +02:00
mynet = NB . GetNetwork ( ) ;
nd = mynet . GetDeviceFromID ( myID ) ;
2015-11-16 22:26:28 +01:00
string hostname = NB . Translate ( "NC_NoHost" ) ;
2015-08-01 19:58:53 +02:00
if ( nd ! = null ) hostname = nd . hostname ;
2015-11-19 19:30:40 +01:00
tPacket . Tracking . Status = string . Format ( NB . Translate ( "NC_PackRejectStr" ) , hostname ) ;
2015-08-01 19:58:53 +02:00
tPacket . MyStatus = PacketStatus . finished_failed ;
}
}
if ( tPacket . destMAC = = MAC | | tPacket . destMAC = = NB . BroadcastMACString | | myNicType = = NicType . port )
{
//It matches. We are ok. Anything to do?
//If the NIC is a vpn, do that here.
}
else
{
2015-11-16 22:26:28 +01:00
tPacket . AddMessage ( DebugLevel . routing , NB . Translate ( "NC_ProcessInboundPacket_DifferentMachine1" ) ) ;
tPacket . AddMessage ( DebugLevel . debug , string . Format ( " " + NB . Translate ( "NC_ProcessInboundPacket_DifferentMachine2" ) , MAC , tPacket . destMAC ) ) ;
2015-08-01 19:58:53 +02:00
mynet = NB . GetNetwork ( ) ;
nd = mynet . GetDeviceFromID ( myID ) ;
2015-11-16 22:26:28 +01:00
string hostname = NB . Translate ( "NC_NoHost" ) ;
2015-08-01 19:58:53 +02:00
if ( nd ! = null ) hostname = nd . hostname ;
2015-11-21 23:55:03 +01:00
tPacket . Tracking . Status = NB . LeftPad ( hostname ) + " " + NB . Translate ( "NC_ProcessInboundPacket_DifferentMachine3" ) ;
2015-08-01 19:58:53 +02:00
tPacket . MyStatus = PacketStatus . finished_failed ;
}
}
public void ClearIPs ( )
{
foreach ( NetworkInterface nf in interfaces )
{
2016-11-02 22:27:43 +01:00
if ( myNicType ! = NicType . lo )
{
2018-10-17 21:38:42 +02:00
if ( myNicType ! = NicType . port )
nf . myIP = new NB_IPAddress ( NB . ZeroIPString ) ; //do not do this for ports (have no IP)
2016-11-02 22:27:43 +01:00
nf . SetVLANTag ( 1 , VLANTagType . Untagged ) ;
}
2015-08-01 19:58:53 +02:00
}
}
2018-10-17 22:23:30 +02:00
public void UpdateAllNicsVLANInfoAfterClone ( )
{
foreach ( NetworkInterface nf in interfaces )
{
nf . UpdateVLANsAfterClone ( ) ;
}
}
2017-08-28 00:22:26 +02:00
public void LockUsOutOfCard ( )
{
foreach ( NetworkInterface nf in interfaces )
{
if ( myNicType ! = NicType . lo )
{
2018-03-09 22:47:33 +01:00
nf . myIP = new NB_IPAddress ( NB . ZeroIPString ) ;
2017-08-28 00:22:26 +02:00
nf . LockOutVLANInterface ( ) ;
}
}
}
2016-10-20 21:53:23 +02:00
public NetworkInterface InterfaceFromVlanTag ( Packet tPacket )
{
int ID = tPacket . VLANID ;
2016-11-02 22:27:43 +01:00
return InterfaceFromVlanTag ( ID ) ;
}
public NetworkInterface InterfaceFromVlanTag ( int ID )
{
foreach ( NetworkInterface oneIF in interfaces )
2016-10-20 21:53:23 +02:00
{
if ( oneIF . GetVLANTag ( ID ) ! = VLANTagType . Forbidden )
return oneIF ;
}
return null ;
}
2016-10-25 20:22:33 +02:00
public NetworkInterface InterfaceFromName ( string InterfaceName )
{
foreach ( NetworkInterface oneIF in interfaces )
{
if ( oneIF . nic_name = = InterfaceName )
return oneIF ;
}
return null ;
}
2024-02-06 20:00:11 +01:00
public string AllInterfacesString ( bool UseCidr = false )
{
string thestring = "" ;
foreach ( NetworkInterface oneIF in interfaces )
{
if ( thestring ! = "" ) thestring + = "," ;
thestring + = oneIF . InterfaceString ( UseCidr ) ;
}
return thestring ;
}
2018-03-09 22:47:33 +01:00
public NB_IPAddress FirstIP ( )
2015-08-01 19:58:53 +02:00
{
2018-03-09 22:47:33 +01:00
List < NB_IPAddress > addresses = IPAddressList ( ) ;
2015-08-01 19:58:53 +02:00
if ( addresses . Count > 0 )
return addresses [ 0 ] ;
else
2018-03-09 22:47:33 +01:00
return new NB_IPAddress ( NB . ZeroIPString ) ;
2015-08-01 19:58:53 +02:00
}
public static T Clone < T > ( T source )
{
if ( ! typeof ( T ) . IsSerializable )
{
2015-11-19 21:49:14 +01:00
throw new ArgumentException ( NB . Translate ( "NC_CloneSerialzable" ) , NB . Translate ( "_source" ) ) ;
2015-08-01 19:58:53 +02:00
}
// Don't serialize a null object, simply return the default for that object
if ( Object . ReferenceEquals ( source , null ) )
{
return default ( T ) ;
}
IFormatter formatter = new BinaryFormatter ( ) ;
Stream stream = new MemoryStream ( ) ;
using ( stream )
{
formatter . Serialize ( stream , source ) ;
stream . Seek ( 0 , SeekOrigin . Begin ) ;
return ( T ) formatter . Deserialize ( stream ) ;
}
}
}
}