-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
IPAddr.jl
312 lines (274 loc) · 8.59 KB
/
IPAddr.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# This file is a part of Julia. License is MIT: https://julialang.org/license
"""
IPAddr
Abstract supertype for IP addresses. [`IPv4`](@ref) and [`IPv6`](@ref) are subtypes of this.
"""
abstract type IPAddr end
Base.isless(a::T, b::T) where {T<:IPAddr} = isless(a.host, b.host)
(dt::Type{<:Integer})(ip::IPAddr) = dt(ip.host)::dt
# Allow IP addresses to broadcast as unwrapped scalars
Base.Broadcast.broadcastable(ip::IPAddr) = Ref(ip)
struct IPv4 <: IPAddr
host::UInt32
IPv4(host::UInt32) = new(host)
IPv4(a::UInt8,b::UInt8,c::UInt8,d::UInt8) = new(UInt32(a)<<24|
UInt32(b)<<16|
UInt32(c)<<8|
d)
function IPv4(a::Integer,b::Integer,c::Integer,d::Integer)
if !(0<=a<=255 && 0<=b<=255 && 0<=c<=255 && 0<=d<=255)
throw(ArgumentError("IPv4 field out of range (must be 0-255)"))
end
IPv4(UInt8(a),UInt8(b),UInt8(c),UInt8(d))
end
end
"""
IPv4(host::Integer) -> IPv4
Returns an IPv4 object from ip address `host` formatted as an [`Integer`](@ref).
# Examples
```jldoctest
julia> IPv4(3223256218)
ip"192.30.252.154"
```
"""
function IPv4(host::Integer)
if host < 0
throw(ArgumentError("IPv4 address must be positive"))
elseif typemax(typeof(host)) > typemax(UInt32) && host > typemax(UInt32)
throw(ArgumentError("IPv4 address must fit within 32 bits"))
else
return IPv4(UInt32(host))
end
end
# constructor: ("1.2.3.4")
IPv4(str::AbstractString) = parse(IPv4, str)
show(io::IO,ip::IPv4) = print(io,"ip\"",ip,"\"")
print(io::IO,ip::IPv4) = print(io,string((ip.host&(0xFF000000))>>24),".",
string((ip.host&(0xFF0000))>>16),".",
string((ip.host&(0xFF00))>>8),".",
string(ip.host&0xFF))
struct IPv6 <: IPAddr
host::UInt128
IPv6(host::UInt128) = new(host)
IPv6(a::UInt16,b::UInt16,c::UInt16,d::UInt16,
e::UInt16,f::UInt16,g::UInt16,h::UInt16) = new(UInt128(a)<<(7*16)|
UInt128(b)<<(6*16)|
UInt128(c)<<(5*16)|
UInt128(d)<<(4*16)|
UInt128(e)<<(3*16)|
UInt128(f)<<(2*16)|
UInt128(g)<<(1*16)|
h)
function IPv6(a::Integer,b::Integer,c::Integer,d::Integer,
e::Integer,f::Integer,g::Integer,h::Integer)
if !(0<=a<=0xFFFF && 0<=b<=0xFFFF && 0<=c<=0xFFFF && 0<=d<=0xFFFF &&
0<=e<=0xFFFF && 0<=f<=0xFFFF && 0<=g<=0xFFFF && 0<=h<=0xFFFF)
throw(ArgumentError("IPv6 field out of range (must be 0-65535)"))
end
IPv6(UInt16(a),UInt16(b),UInt16(c),UInt16(d),
UInt16(e),UInt16(f),UInt16(g),UInt16(h))
end
end
"""
IPv6(host::Integer) -> IPv6
Returns an IPv6 object from ip address `host` formatted as an [`Integer`](@ref).
# Examples
```jldoctest
julia> IPv6(3223256218)
ip"::c01e:fc9a"
```
"""
function IPv6(host::Integer)
if host < 0
throw(ArgumentError("IPv6 address must be positive"))
# We allow passing bigger integer types, but need to be careful to avoid overflow
# Let's hope promotion rules are sensible
elseif typemax(typeof(host)) > typemax(UInt128) && host > typemax(UInt128)
throw(ArgumentError("IPv6 address must fit within 128 bits"))
else
return IPv6(UInt128(host))
end
end
IPv6(str::AbstractString) = parse(IPv6, str)
# Suppress leading '0's and "0x"
print_ipv6_field(io,field::UInt16) = print(io,string(field, base = 16))
print_ipv6_field(io,ip,i) = print_ipv6_field(io,ipv6_field(ip,i))
function ipv6_field(ip::IPv6,i)
if i < 0 || i > 7
throw(BoundsError())
end
UInt16((ip.host&(UInt128(0xFFFF)<<(i*16))) >> (i*16))
end
show(io::IO, ip::IPv6) = print(io,"ip\"",ip,"\"")
# RFC 5952 compliant show function
# http://tools.ietf.org/html/rfc5952
function print(io::IO,ip::IPv6)
i = 8
m = 0
longest_sub_i = -1
while i!=0
i-=1
field = ipv6_field(ip,i)
if field == 0 && longest_sub_i == -1
# Find longest subsequence of 0
longest_sub_i,j,m,c = i,i,1,1
while j != 0
j-=1
if ipv6_field(ip,j) == 0
c += 1
else
c = 0
end
if c > m
if j+c != longest_sub_i+1
longest_sub_i = j+c-1
end
m = c
end
end
# Prevent single 0 from contracting to :: as required
if m == 1
longest_sub_i = 9
end
end
if i == longest_sub_i
print(io,":")
i -= m-1
if i == 0
print(io,":")
break
end
else
if i != 7
print(io,":")
end
print_ipv6_field(io,field)
end
end
end
# Parsing
const ipv4_leading_zero_error = """
Leading zeros in IPv4 addresses are disallowed due to ambiguity.
If the address is in octal or hexadecimal, convert it to decimal, otherwise remove the leading zero.
"""
function parse(::Type{IPv4}, str::AbstractString)
fields = split(str,'.')
i = 1
ret = 0
for f in fields
if isempty(f)
throw(ArgumentError("empty field in IPv4 address"))
end
if length(f) > 1 && f[1] == '0'
throw(ArgumentError(ipv4_leading_zero_error))
else
r = parse(Int, f, base = 10)
end
if i != length(fields)
if r < 0 || r > 255
throw(ArgumentError("IPv4 field out of range (must be 0-255)"))
end
ret |= UInt32(r) << ((4-i)*8)
else
if r > ((UInt64(1)<<((5-length(fields))*8))-1)
throw(ArgumentError("IPv4 field too large"))
end
ret |= r
end
i+=1
end
IPv4(ret)
end
function parseipv6fields(fields,num_fields)
if length(fields) > num_fields
throw(ArgumentError("too many fields in IPv6 address"))
end
cf = 7
ret = UInt128(0)
for f in fields
if isempty(f)
# ::abc:... and ..:abc::
if cf != 7 && cf != 0
cf -= num_fields-length(fields)
end
cf -= 1
continue
end
ret |= UInt128(parse(Int, f, base = 16))<<(cf*16)
cf -= 1
end
ret
end
parseipv6fields(fields) = parseipv6fields(fields,8)
function parse(::Type{IPv6}, str::AbstractString)
fields = split(str,':')
if length(fields) > 8
throw(ArgumentError("too many fields in IPv6 address"))
elseif length(fields) == 8
return IPv6(parseipv6fields(fields))
elseif in('.',fields[end])
return IPv6((parseipv6fields(fields[1:(end-1)],6))
| parse(IPv4, fields[end]).host )
else
return IPv6(parseipv6fields(fields))
end
end
#
# This supports IP addresses in the common dot (IPv4) or colon (IPv6)
# separated formats. Most other common formats use a standard integer encoding
# of the appropriate size and should use the appropriate constructor
#
function parse(::Type{IPAddr}, str::AbstractString)
if ':' in str
return parse(IPv6, str)
else
return parse(IPv4, str)
end
end
"""
@ip_str str -> IPAddr
Parse `str` as an IP address.
# Examples
```jldoctest
julia> ip"127.0.0.1"
ip"127.0.0.1"
julia> @ip_str "2001:db8:0:0:0:0:2:1"
ip"2001:db8::2:1"
```
"""
macro ip_str(str)
return parse(IPAddr, str)
end
struct InetAddr{T<:IPAddr}
host::T
port::UInt16
end
"""
InetAddr(ip::IPAddr, port) -> InetAddr
Return an `InetAddr` object from ip address `ip` and port number `port`.
# Examples
```jldoctest
julia> Sockets.InetAddr(ip"127.0.0.1", 8000)
Sockets.InetAddr{IPv4}(ip"127.0.0.1", 8000)
```
"""
InetAddr(ip::IPAddr, port) = InetAddr{typeof(ip)}(ip, port)
"""
InetAddr(str::AbstractString, port) -> InetAddr
Return an `InetAddr` object from ip address `str` formatted as [`AbstractString`](@ref)
and port number `port`.
!!! compat "Julia 1.3"
This constructor requires at least Julia 1.3.
# Examples
```jldoctest
julia> Sockets.InetAddr("127.0.0.1", 8000)
Sockets.InetAddr{IPv4}(ip"127.0.0.1", 8000)
```
"""
InetAddr(str::AbstractString, port) = InetAddr(parse(IPAddr, str), port)
function show(io::IO, addr::InetAddr)
show(io, typeof(addr))
print(io, "(")
show(io, addr.host)
print(io, ", ", Int(addr.port), ")")
end