Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
MechanismDomainCidrSpec.cs
1using System;
2using System.Collections.Generic;
3using System.Net;
4using System.Net.Sockets;
5
7{
13 {
17 protected readonly int ip4Cidr = 32;
18
22 protected readonly int ip6Cidr = 128;
23
31 : base(Term, Qualifier)
32 {
33 if (Term.PeekNextChar() == '/')
34 {
35 Term.NextChar();
36
37 bool HasIp4;
38
39 if (HasIp4 = char.IsDigit(Term.PeekNextChar()))
40 {
41 this.ip4Cidr = Term.NextInteger();
42 if (this.ip4Cidr < 0 || this.ip4Cidr > 32)
43 throw new Exception("Invalid IP4 CIDR");
44 }
45
46 if (Term.PeekNextChar() == '/')
47 {
48 Term.NextChar();
49
50 if (HasIp4 && Term.PeekNextChar() == '/')
51 Term.NextChar();
52
53 if (char.IsDigit(Term.PeekNextChar()))
54 {
55 this.ip6Cidr = Term.NextInteger();
56 if (this.ip6Cidr < 0 || this.ip4Cidr > 128)
57 throw new Exception("Invalid IP6 CIDR");
58 }
59 else if (!HasIp4)
60 throw new Exception("IP4 or IP6 CIDR expected.");
61 }
62 }
63 }
64
68 public override bool DomainRequired => false;
69
78 internal static bool Matches(IPAddress[] Addresses, Term Term, int Cidr)
79 {
80 byte[] Bin1 = Term.ip.GetAddressBytes();
81 int c = Bin1.Length;
82
83 foreach (IPAddress Addr in Addresses)
84 {
85 byte[] Bin2 = Addr.GetAddressBytes();
86 if (Bin2.Length != c)
87 continue;
88
89 int BitsLeft = Cidr;
90 int Pos = 0;
91
92 while (BitsLeft > 0 && Pos < c)
93 {
94 if (BitsLeft >= 8)
95 {
96 if (Bin1[Pos] != Bin2[Pos])
97 break;
98
99 BitsLeft -= 8;
100 }
101 else
102 {
103 byte Mask = (byte)(0xff << (8 - BitsLeft));
104
105 if ((Bin1[Pos] & Mask) != (Bin2[Pos] & Mask))
106 break;
107
108 BitsLeft = 0;
109 }
110
111 Pos++;
112 }
113
114 if (BitsLeft == 0)
115 return true;
116 }
117
118 return false;
119 }
120 }
121}
Abstract base class for SPF mechanisms with a domain specification and an optional CIDR specification...
override bool DomainRequired
If the domain specification is required.
MechanismDomainCidrSpec(Term Term, SpfQualifier Qualifier)
Abstract base class for SPF mechanisms with a domain specification and an optional CIDR specification...
Abstract base class for SPF mechanisms with a domain specification.
abstract Task< SpfResult > Matches()
Checks if the mechamism matches the current request.
SpfQualifier Qualifier
Mechanism qualifier
Definition: Mechanism.cs:36
SpfQualifier
SPF Mechanism qualifier
Definition: Term.cs:12