Skip to content

Commit 50efa67

Browse files
Added support for 6to4 and Teredo.
1 parent 59a79b9 commit 50efa67

4 files changed

Lines changed: 58 additions & 13 deletions

File tree

LICENSE.TXT

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2017 IP2Location.com
3+
Copyright (c) 2019 IP2Location.com
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,4 @@ Use the IPv6 BIN file if you need to query BOTH IPv4 and IPv6 addresses.
8585
Copyright
8686
=========
8787

88-
Copyright (C) 2018 by IP2Location.com, support@ip2location.com
88+
Copyright (C) 2019 by IP2Location.com, support@ip2location.com

dub.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
],
66
"description": "IP2Location D Library",
77
"homepage": "https://www.ip2location.com/developers/d",
8-
"copyright": "Copyright © 2018, IP2Location",
8+
"copyright": "Copyright © 2019, IP2Location",
99
"license": "MIT",
1010
"targetType": "library"
1111
}

source/ip2location-d/ip2location.d

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,26 +72,51 @@ const ubyte[25] MOBILEBRAND_POSITION = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
7272
const ubyte[25] ELEVATION_POSITION = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, 19, 0, 19];
7373
const ubyte[25] USAGETYPE_POSITION = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 20];
7474

75-
protected const string API_VERSION = "8.0.5";
76-
77-
protected const BigInt MAX_IPV4_RANGE = BigInt("4294967295");
75+
protected const string API_VERSION = "8.1.0";
7876

7977
version(X86)
8078
{
81-
8279
// CTFE of BigInt only supported since phobos 2.079
8380
static if (__VERSION__ < 2079)
8481
{
82+
protected const BigInt MAX_IPV4_RANGE;
8583
protected const BigInt MAX_IPV6_RANGE;
84+
protected const BigInt FROM_6TO4;
85+
protected const BigInt TO_6TO4;
86+
protected const BigInt FROM_TEREDO;
87+
protected const BigInt TO_TEREDO;
88+
protected const BigInt LAST_32BITS;
8689

87-
static this()
88-
{
89-
MAX_IPV6_RANGE = BigInt("340282366920938463463374607431768211455");
90-
}
90+
static this()
91+
{
92+
MAX_IPV4_RANGE = BigInt("4294967295");
93+
MAX_IPV6_RANGE = BigInt("340282366920938463463374607431768211455");
94+
FROM_6TO4 = BigInt("42545680458834377588178886921629466624");
95+
TO_6TO4 = BigInt("42550872755692912415807417417958686719");
96+
FROM_TEREDO = BigInt("42540488161975842760550356425300246528");
97+
TO_TEREDO = BigInt("42540488241204005274814694018844196863");
98+
LAST_32BITS = BigInt("4294967295");
99+
}
100+
}
101+
else {
102+
protected const MAX_IPV4_RANGE = BigInt("4294967295");
103+
protected const MAX_IPV6_RANGE = BigInt("340282366920938463463374607431768211455");
104+
protected const FROM_6TO4 = BigInt("42545680458834377588178886921629466624");
105+
protected const TO_6TO4 = BigInt("42550872755692912415807417417958686719");
106+
protected const FROM_TEREDO = BigInt("42540488161975842760550356425300246528");
107+
protected const TO_TEREDO = BigInt("42540488241204005274814694018844196863");
108+
protected const LAST_32BITS = BigInt("4294967295");
91109
}
92-
else protected const MAX_IPV6_RANGE = BigInt("340282366920938463463374607431768211455");
93110
}
94-
else protected const BigInt MAX_IPV6_RANGE = BigInt("340282366920938463463374607431768211455");
111+
else {
112+
protected const BigInt MAX_IPV4_RANGE = BigInt("4294967295");
113+
protected const BigInt MAX_IPV6_RANGE = BigInt("340282366920938463463374607431768211455");
114+
protected const BigInt FROM_6TO4 = BigInt("42545680458834377588178886921629466624");
115+
protected const BigInt TO_6TO4 = BigInt("42550872755692912415807417417958686719");
116+
protected const BigInt FROM_TEREDO = BigInt("42540488161975842760550356425300246528");
117+
protected const BigInt TO_TEREDO = BigInt("42540488241204005274814694018844196863");
118+
protected const BigInt LAST_32BITS = BigInt("4294967295");
119+
}
95120

96121
protected const uint COUNTRYSHORT = 0x00001;
97122
protected const uint COUNTRYLONG = 0x00002;
@@ -330,6 +355,26 @@ class ip2location {
330355
ipdata.ipindex = ((ipno2 >> 16) << 3) + meta.ipv4indexbaseaddr;
331356
}
332357
}
358+
else if (ipdata.ipnum >= FROM_6TO4 && ipdata.ipnum <= TO_6TO4) {
359+
// 6to4 so need to remap to ipv4
360+
ipdata.iptype = 4;
361+
ipdata.ipnum = ipdata.ipnum >> 80;
362+
ipdata.ipnum = ipdata.ipnum & LAST_32BITS;
363+
uint ipno2 = to!uint(ipdata.ipnum);
364+
if (meta.ipv4indexbaseaddr > 0) {
365+
ipdata.ipindex = ((ipno2 >> 16) << 3) + meta.ipv4indexbaseaddr;
366+
}
367+
}
368+
else if (ipdata.ipnum >= FROM_TEREDO && ipdata.ipnum <= TO_TEREDO) {
369+
// Teredo so need to remap to ipv4
370+
ipdata.iptype = 4;
371+
ipdata.ipnum = ~ipdata.ipnum; // bitwise NOT
372+
ipdata.ipnum = ipdata.ipnum & LAST_32BITS;
373+
uint ipno2 = to!uint(ipdata.ipnum);
374+
if (meta.ipv4indexbaseaddr > 0) {
375+
ipdata.ipindex = ((ipno2 >> 16) << 3) + meta.ipv4indexbaseaddr;
376+
}
377+
}
333378
}
334379
}
335380
catch (Exception e) {

0 commit comments

Comments
 (0)