Skip to content

Commit 9f915dd

Browse files
committed
Add project files.
1 parent ebfa2dd commit 9f915dd

File tree

5 files changed

+619
-0
lines changed

5 files changed

+619
-0
lines changed

community-id/CommunityID.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
</PropertyGroup>
6+
7+
</Project>

community-id/CommunityIDGenerator.cs

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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+
}

communityid.sln

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.31105.61
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CommunityID", "community-id\CommunityID.csproj", "{15CD627E-3801-429A-81A8-7607E57AEC98}"
7+
EndProject
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CommunityID_UnitTest", "unitTest\CommunityID_UnitTest.csproj", "{7BB59B43-4E1E-4EA2-8A8E-A434352DCB43}"
9+
EndProject
10+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "CommunityID", "CommunityID", "{D3774E3E-7FE4-45AC-BCE5-7E2D3EF6A945}"
11+
EndProject
12+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "UnitTest", "UnitTest", "{9232D298-5B6E-4344-A975-C3D2C585555B}"
13+
EndProject
14+
Global
15+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
16+
Debug|Any CPU = Debug|Any CPU
17+
Release|Any CPU = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
20+
{15CD627E-3801-429A-81A8-7607E57AEC98}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{15CD627E-3801-429A-81A8-7607E57AEC98}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{15CD627E-3801-429A-81A8-7607E57AEC98}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{15CD627E-3801-429A-81A8-7607E57AEC98}.Release|Any CPU.Build.0 = Release|Any CPU
24+
{7BB59B43-4E1E-4EA2-8A8E-A434352DCB43}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
25+
{7BB59B43-4E1E-4EA2-8A8E-A434352DCB43}.Debug|Any CPU.Build.0 = Debug|Any CPU
26+
{7BB59B43-4E1E-4EA2-8A8E-A434352DCB43}.Release|Any CPU.ActiveCfg = Release|Any CPU
27+
{7BB59B43-4E1E-4EA2-8A8E-A434352DCB43}.Release|Any CPU.Build.0 = Release|Any CPU
28+
EndGlobalSection
29+
GlobalSection(SolutionProperties) = preSolution
30+
HideSolutionNode = FALSE
31+
EndGlobalSection
32+
GlobalSection(NestedProjects) = preSolution
33+
{15CD627E-3801-429A-81A8-7607E57AEC98} = {D3774E3E-7FE4-45AC-BCE5-7E2D3EF6A945}
34+
{7BB59B43-4E1E-4EA2-8A8E-A434352DCB43} = {9232D298-5B6E-4344-A975-C3D2C585555B}
35+
EndGlobalSection
36+
GlobalSection(ExtensibilityGlobals) = postSolution
37+
SolutionGuid = {A6204FBC-A21F-4365-ACDE-FA310300DC74}
38+
EndGlobalSection
39+
EndGlobal

unitTest/CommunityID_UnitTest.csproj

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
6+
<IsPackable>false</IsPackable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="NUnit" Version="3.12.0" />
11+
<PackageReference Include="NUnit3TestAdapter" Version="3.16.1" />
12+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<ProjectReference Include="..\community-id\CommunityID.csproj" />
17+
</ItemGroup>
18+
19+
</Project>

0 commit comments

Comments
 (0)