Repository navigation
Expand file tree
/
Copy pathPacketRouter.cs
More file actions
53 lines (48 loc) · 2.02 KB
/
Copy pathPacketRouter.cs
File metadata and controls
53 lines (48 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ModemAPI
{
public class PacketRouter(Address localAddress) : IPacketRouter
{
public Address LocalAddress { get; } = localAddress;
public PacketRouterDecidion Decide(TransmitterReceiverPair txrx, PacketTransmissionDirection dir)
{
string local = LocalAddress.ToString();
if (!IsWithin(txrx.Transmitter.ToString(), local) && dir == PacketTransmissionDirection.FromUpperToLower)
{
ModemAPIDebugger.OutputDebugMessage("address spoofing detected");
return PacketRouterDecidion.AddressSpoofingDetectedRefusedToForward;
}
if (IsWithin(txrx.Receiver.ToString(), local))
{
ModemAPIDebugger.OutputDebugMessage("forwarding in the inner network");
return PacketRouterDecidion.ForwardInInnerNetwork;
}
else
{
return PacketRouterDecidion.ForwardDown;
}
}
/// <summary>
/// Whether the address is the local one or one under it: 7-1 and 7-1-5 are within 7-1, but 7-10-5 isn't. Compared
/// ordinally: addresses are digits and dashes, and the rules of the current culture are ~150 times slower for nothing.
/// An empty local address has everything within it.
/// </summary>
internal static bool IsWithin(string address, string local)
{
return local.Length == 0 ||
(address.StartsWith(local, StringComparison.Ordinal) && (address.Length == local.Length || address[local.Length] == '-'));
}
public Packet? ModifyModifiablePacket(Packet packetWithModifyByRoutersFlag)
{
return null;
}
public void WorkOnReceiveByRouterPacket(Packet packetWithReceiveByRoutersFlag)
{
// nothing there
}
}
}