|
| 1 | +using System; |
| 2 | +using System.Net; |
| 3 | +using System.Security.Cryptography; |
| 4 | + |
| 5 | +namespace CommunityID |
| 6 | +{ |
| 7 | + public class CommunityIDGenerator |
| 8 | + { |
| 9 | + const string v1 = "1:"; |
| 10 | + public enum Protocol { |
| 11 | + ICMP = 1, |
| 12 | + TCP = 6, |
| 13 | + UDP = 17, |
| 14 | + RSVP = 46, |
| 15 | + ICMP6 = 58, |
| 16 | + SCTP = 132 |
| 17 | + } |
| 18 | + |
| 19 | + public string community_id_v1(IPAddress saddr, IPAddress daddr, UInt16? sport, UInt16? dport, Protocol proto, UInt16 seed = 0) { |
| 20 | + |
| 21 | + byte[] tuple; |
| 22 | + |
| 23 | + if ((sport == null && dport != null) || (sport != null && dport == null)) |
| 24 | + { |
| 25 | + throw new ArgumentNullException("Source and destination port values must be both either valid or null"); |
| 26 | + } |
| 27 | + |
| 28 | + //If source address is larger switch order |
| 29 | + if (ipCompare(saddr,daddr) > 0) |
| 30 | + { |
| 31 | + IPAddress tmpAddr = saddr; |
| 32 | + saddr = daddr; |
| 33 | + daddr = tmpAddr; |
| 34 | + |
| 35 | + if (sport != null && dport != null) |
| 36 | + { |
| 37 | + UInt16 tmpPort = (UInt16)sport; |
| 38 | + sport = dport; |
| 39 | + dport = tmpPort; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + //convert params to bytes |
| 44 | + byte[] byteseed = BitConverter.GetBytes(seed); |
| 45 | + byte[] bytesaddr = saddr.GetAddressBytes(); |
| 46 | + byte[] bytedaddr = daddr.GetAddressBytes(); |
| 47 | + |
| 48 | + if (sport is null || dport is null) |
| 49 | + { |
| 50 | + tuple = new byte[bytesaddr.Length + bytedaddr.Length + 4]; |
| 51 | + } |
| 52 | + else |
| 53 | + { |
| 54 | + byte[] bytesport = BitConverter.GetBytes((UInt16)sport); |
| 55 | + Array.Reverse(bytesport); |
| 56 | + byte[] bytesdport = BitConverter.GetBytes((UInt16)dport); |
| 57 | + Array.Reverse(bytesdport); |
| 58 | + |
| 59 | + tuple = new byte[bytesaddr.Length + bytedaddr.Length + 8]; |
| 60 | + Buffer.BlockCopy(bytesport, 0, tuple, bytesaddr.Length + bytedaddr.Length + 4, 2); |
| 61 | + Buffer.BlockCopy(bytesdport, 0, tuple, bytesaddr.Length + bytedaddr.Length + 6, 2); |
| 62 | + } |
| 63 | + |
| 64 | + Buffer.BlockCopy(byteseed, 0, tuple, 0, 2); |
| 65 | + Buffer.BlockCopy(bytesaddr, 0, tuple, 2, bytesaddr.Length); |
| 66 | + Buffer.BlockCopy(bytedaddr, 0, tuple, bytesaddr.Length + 2, bytedaddr.Length); |
| 67 | + Buffer.BlockCopy(new byte[] { (byte)proto }, 0, tuple, bytesaddr.Length + bytedaddr.Length + 2, 1); |
| 68 | + Buffer.BlockCopy(new byte[] { 0 }, 0, tuple, bytesaddr.Length + bytedaddr.Length + 3, 1); |
| 69 | + |
| 70 | + return v1 + Convert.ToBase64String((getHash(tuple))); |
| 71 | + |
| 72 | + } |
| 73 | + |
| 74 | + public string community_id_v1(IPAddress saddr, IPAddress daddr, int sport, int dport, int proto, int seed = 0) |
| 75 | + { |
| 76 | + Protocol innerProtocol; |
| 77 | + try |
| 78 | + { |
| 79 | + innerProtocol = (CommunityIDGenerator.Protocol)proto; |
| 80 | + }catch(Exception ex) |
| 81 | + { |
| 82 | + throw new ArgumentException("protocol value is unsupported", ex); |
| 83 | + } |
| 84 | + return community_id_v1(saddr, daddr, ConvertToPortNumber( sport), ConvertToPortNumber( dport), innerProtocol, ConvertToPortNumber( seed )); |
| 85 | + } |
| 86 | + |
| 87 | + |
| 88 | + public string community_id_v1(String saddr, String daddr, UInt16 sport, UInt16 dport, Protocol proto, UInt16 seed = 0) |
| 89 | + { |
| 90 | + return community_id_v1(IPAddress.Parse(saddr), IPAddress.Parse(daddr), sport, dport, proto, seed); |
| 91 | + } |
| 92 | + |
| 93 | + private byte[] getHash(byte[] tuple) |
| 94 | + { |
| 95 | + using (SHA1Managed sha1 = new SHA1Managed()) |
| 96 | + { |
| 97 | + var hash = sha1.ComputeHash(tuple); |
| 98 | + return hash; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + private int ipCompare(IPAddress ip1, IPAddress ip2) |
| 103 | + { |
| 104 | + if (ip1.AddressFamily.CompareTo(ip2.AddressFamily )== 0){ |
| 105 | + byte[] tmp1 = ip1.GetAddressBytes(); |
| 106 | + byte[] tmp2 = ip2.GetAddressBytes(); |
| 107 | + for (int i = 0; i < tmp1.Length; i++) |
| 108 | + { |
| 109 | + if (tmp1[i] > tmp2[i]) return 1; |
| 110 | + if (tmp1[i] < tmp2[i]) return -1; |
| 111 | + } |
| 112 | + return 0; |
| 113 | + } |
| 114 | + else |
| 115 | + { |
| 116 | + if (ip1.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6) return 1; |
| 117 | + } |
| 118 | + return -1; |
| 119 | + } |
| 120 | + |
| 121 | + private ushort ConvertToPortNumber(int number) |
| 122 | + { |
| 123 | + if (number < 0) |
| 124 | + { |
| 125 | + throw new ArgumentException("port value must be a positive int"); |
| 126 | + } |
| 127 | + |
| 128 | + ushort result; |
| 129 | + try |
| 130 | + { |
| 131 | + result = Convert.ToUInt16(number); |
| 132 | + } |
| 133 | + catch (OverflowException) |
| 134 | + { |
| 135 | + throw new OverflowException("Parameter value out of range: " + number); |
| 136 | + } |
| 137 | + return result; |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments