Skip to content

Commit ce4a95e

Browse files
Working copy of a parser for the APNIC published data
0 parents  commit ce4a95e

File tree

6 files changed

+406
-0
lines changed

6 files changed

+406
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
*.suo
2+
*.user
3+
bin/
4+
obj/

apnicparser.sln

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 2013
4+
VisualStudioVersion = 12.0.21005.1
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "apnicparser", "apnicparser\apnicparser.csproj", "{8404C19F-D9BC-4932-80B8-12F13159B118}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{8404C19F-D9BC-4932-80B8-12F13159B118}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{8404C19F-D9BC-4932-80B8-12F13159B118}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{8404C19F-D9BC-4932-80B8-12F13159B118}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{8404C19F-D9BC-4932-80B8-12F13159B118}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal

apnicparser/App.config

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
5+
</startup>
6+
</configuration>

apnicparser/Program.cs

+280
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,280 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Data.SqlClient;
4+
using System.Diagnostics;
5+
using System.IO;
6+
using System.Linq;
7+
using System.Net;
8+
using System.Net.NetworkInformation;
9+
using System.Runtime.InteropServices;
10+
using System.Security.Permissions;
11+
using System.Text;
12+
using System.Threading.Tasks;
13+
14+
namespace apnicparser
15+
{
16+
class Program
17+
{
18+
static StringBuilder sb = new StringBuilder();
19+
20+
public class Range
21+
{
22+
public uint Start;
23+
public uint End;
24+
25+
public uint Total { get { return End - Start + 1; } }
26+
27+
public Range()
28+
{
29+
30+
}
31+
32+
public Range(uint start, uint end)
33+
{
34+
Start = start;
35+
End = end;
36+
}
37+
38+
public static Range ExtendRangeOrNew(Range existing, uint start, uint end)
39+
{
40+
if(existing != null && existing.End == start - 1)
41+
{
42+
existing.End = end;
43+
}
44+
else
45+
{
46+
return new Range(start, end);
47+
}
48+
return null;
49+
}
50+
}
51+
52+
public class RangeGaps
53+
{
54+
public readonly List<Range> FilledRanges = new List<Range>();
55+
public readonly List<Range> MissingRanges = new List<Range>();
56+
57+
public Range CurrentRange = null;
58+
59+
public void AddNewRange(uint start, uint end)
60+
{
61+
var newRange = Range.ExtendRangeOrNew(CurrentRange, start, end);
62+
63+
if (CurrentRange == null)
64+
{
65+
FilledRanges.Add(newRange);
66+
CurrentRange = newRange;
67+
}
68+
else if( newRange != null)
69+
{
70+
var missingRange = new Range(CurrentRange.End + 1, newRange.Start - 1);
71+
MissingRanges.Add(missingRange);
72+
FilledRanges.Add(newRange);
73+
CurrentRange = newRange;
74+
}
75+
}
76+
}
77+
78+
static void Main(string[] args)
79+
{
80+
//Downloaded from ftp://ftp.apnic.net/public/apnic/stats/apnic/delegated-apnic-extended-latest
81+
string file = @"c:\temp\delegated-apnic-extended-latest";
82+
string[] limitLocaions = { "au", };
83+
string[] limitTypes = { "ipv4", };
84+
85+
if (args.Length > 0)
86+
{
87+
file = args[0];
88+
}
89+
90+
if (args.Length > 1)
91+
{
92+
var limitLocation = args[1].ToLower();
93+
limitLocaions = limitLocation.Split(new[] { ',', ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
94+
}
95+
96+
if (args.Length > 2)
97+
{
98+
var limitType = args[2].ToLower();
99+
limitTypes = limitType.Split(new[] { ',', ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
100+
}
101+
102+
if (!File.Exists(file))
103+
{
104+
Console.WriteLine("{0} doesn't exist, usage is\r\n [filename [location,location,location [type,type,type]]]", file);
105+
Console.ReadKey();
106+
return;
107+
}
108+
109+
var lines = File.ReadAllLines(file);
110+
lines = lines.Where(l => !l.StartsWith("#") && !l.Contains("*") && !l.Contains("-") && !l.Contains("+")).ToArray();
111+
112+
RangeGaps ranges = new RangeGaps();
113+
foreach (var line in lines)
114+
{
115+
var sections = line.Split('|');
116+
var offset = -1;
117+
var registry = sections[++offset];
118+
var place = sections[++offset];
119+
var type = sections[++offset];
120+
var rangeStartStr = sections[++offset];
121+
var numberAssignedStr = sections[++offset];
122+
var dateAssignedStr = sections[++offset];
123+
var status = sections[++offset];
124+
var instances = sections[++offset];
125+
126+
127+
if (limitLocaions.Length > 0 && !limitLocaions.Contains(place.ToLower()))
128+
{
129+
continue;
130+
}
131+
132+
if (limitTypes.Length > 0 && !limitTypes.Contains(type.ToLower()))
133+
{
134+
continue;
135+
}
136+
137+
138+
Write(place + '|');
139+
Write(type + '|');
140+
141+
var numberAssigned = uint.Parse(numberAssignedStr);
142+
var numberAssignedMinus1 = numberAssigned - 1;
143+
144+
var significantBitsReverse = (int)Math.Log(numberAssigned, 2);
145+
146+
var originalBits = (int)Math.Pow(2, significantBitsReverse);
147+
var significantBits = 32 - significantBitsReverse;
148+
149+
150+
if (type.Equals("ipv4", StringComparison.OrdinalIgnoreCase))
151+
{
152+
if (originalBits != numberAssigned)
153+
{
154+
Debugger.Break();
155+
}
156+
var ipAddressParts = rangeStartStr.Split('.');
157+
byte[] parts = new byte[4];
158+
for (int index = 0; index < ipAddressParts.Length; index++)
159+
{
160+
var ipAddressPart = ipAddressParts[index];
161+
parts[index] = byte.Parse(ipAddressPart);
162+
}
163+
164+
var ipAddress = new IPAddress(parts);
165+
166+
var ipAddressAsInt = ((uint)parts[3] << 24 | (uint)parts[2] << 16 | (uint)parts[1] << 8 | (uint)parts[0]);
167+
168+
var endPower2 = ReverseBytes(numberAssignedMinus1);
169+
170+
var startPower2 = ~endPower2;
171+
172+
173+
var startPower = (uint)Math.Pow(2, significantBits) - 1;
174+
var endPower = ~startPower;
175+
var start = startPower2 & ipAddressAsInt;
176+
var end = start | endPower2;
177+
178+
var startReversed = ReverseBytes(start);
179+
var endReversed = ReverseBytes(end);
180+
181+
ranges.AddNewRange(startReversed, endReversed);
182+
183+
var startIp = new IPAddress(start);
184+
var endIp = new IPAddress(end);
185+
186+
Write(rangeStartStr + "/" + significantBits + "|");
187+
188+
Write(startIp + "|");
189+
Write(endIp + "|");
190+
191+
Write(numberAssignedStr + '|');
192+
193+
194+
195+
if (rangeStartStr != startIp.ToString())
196+
{
197+
Debugger.Break();
198+
}
199+
}
200+
else
201+
{
202+
Write(rangeStartStr + '|');
203+
Write(significantBits + "|");
204+
Write(numberAssignedStr + '|');
205+
}
206+
207+
WriteLine();
208+
}
209+
210+
WriteLine("Filled Ranges");
211+
foreach (var range in ranges.FilledRanges)
212+
{
213+
var total = range.Total;
214+
var startIp = new IPAddress(ReverseBytes(range.Start));
215+
var endIp = new IPAddress(ReverseBytes(range.End));
216+
217+
WriteLine(startIp + "-" + endIp + "-" + total);
218+
}
219+
220+
WriteLine("Missing Ranges");
221+
foreach (var range in ranges.MissingRanges)
222+
{
223+
var total = range.Total;
224+
var startIp = new IPAddress(ReverseBytes(range.Start));
225+
var endIp = new IPAddress(ReverseBytes(range.End));
226+
227+
WriteLine(startIp + "-" + endIp + "-" + total);
228+
}
229+
230+
SetClipboard(sb.ToString());
231+
232+
Console.ReadKey();
233+
}
234+
235+
private static uint ReverseBytes(uint value)
236+
{
237+
return (value & 0x000000FFU) << 24 | (value & 0x0000FF00U) << 8 |
238+
(value & 0x00FF0000U) >> 8 | (value & 0xFF000000U) >> 24;
239+
}
240+
241+
242+
private static void WriteLine()
243+
{
244+
sb.AppendLine();
245+
Console.WriteLine();
246+
}
247+
248+
private static void WriteLine(string value)
249+
{
250+
sb.AppendLine(value);
251+
Console.WriteLine(value);
252+
}
253+
254+
private static void Write(string value)
255+
{
256+
sb.Append(value);
257+
Console.Write(value);
258+
}
259+
260+
261+
[DllImport("user32.dll")]
262+
internal static extern bool OpenClipboard(IntPtr hWndNewOwner);
263+
264+
[DllImport("user32.dll")]
265+
internal static extern bool CloseClipboard();
266+
267+
[DllImport("user32.dll")]
268+
internal static extern bool SetClipboardData(uint uFormat, IntPtr data);
269+
270+
static void SetClipboard(string value)
271+
{
272+
OpenClipboard(IntPtr.Zero);
273+
var ptr = Marshal.StringToHGlobalUni(value);
274+
SetClipboardData(13, ptr);
275+
CloseClipboard();
276+
Marshal.FreeHGlobal(ptr);
277+
}
278+
279+
}
280+
}
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("apnicparser")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("apnicparser")]
13+
[assembly: AssemblyCopyright("Copyright © MV 2016")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("c40e2742-aca6-4327-b34a-0f5d232b22ab")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

0 commit comments

Comments
 (0)