/usr/share/julia/base/socket.jl is in julia-common 0.4.7-6.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 | # This file is a part of Julia. License is MIT: http://julialang.org/license
## IP ADDRESS HANDLING ##
abstract IPAddr
Base.isless{T<:IPAddr}(a::T, b::T) = isless(a.host, b.host)
Base.convert{T<:Integer}(dt::Type{T}, ip::IPAddr) = dt(ip.host)
immutable 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
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(ipstr::AbstractString) = parseipv4(ipstr)
show(io::IO,ip::IPv4) = print(io,"ip\"",ip,"\"")
print(io::IO,ip::IPv4) = print(io,dec((ip.host&(0xFF000000))>>24),".",
dec((ip.host&(0xFF0000))>>16),".",
dec((ip.host&(0xFF00))>>8),".",
dec(ip.host&0xFF))
immutable 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
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(ipstr::AbstractString) = parseipv6(ipstr)
# Suppress leading '0's and "0x"
print_ipv6_field(io,field::UInt16) = print(io,hex(field))
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
function parseipv4(str)
fields = split(str,'.')
i = 1
ret = 0
for f in fields
if length(f) == 0
throw(ArgumentError("empty field in IPv4 address"))
end
if f[1] == '0'
if length(f) >= 2 && f[2] == 'x'
if length(f) > 8 # 2+(3*2) - prevent parseint from overflowing on 32bit
throw(ArgumentError("IPv4 field too large"))
end
r = parse(Int,f[3:end],16)
else
if length(f) > 9 # 1+8 - prevent parseint from overflowing on 32bit
throw(ArgumentError("IPv4 field too large"))
end
r = parse(Int,f,8)
end
else
r = parse(Int,f,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 f == ""
# ::abc:... and ..:abc::
if cf != 7 && cf != 0
cf -= num_fields-length(fields)
end
cf -= 1
continue
end
ret |= UInt128(parse(Int,f,16))<<(cf*16)
cf -= 1
end
ret
end
parseipv6fields(fields) = parseipv6fields(fields,8)
function parseipv6(str)
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))
| parseipv4(fields[end]).host )
else
return IPv6(parseipv6fields(fields))
end
end
#
# This support IPv4 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 parseip(str)
if in(':',str)
# IPv6 Address
return parseipv6(str)
else
# IPv4 Address
return parseipv4(str)
end
end
macro ip_str(str)
return parseip(str)
end
type InetAddr
host::IPAddr
port::UInt16
function InetAddr(host, port::Integer)
if !(0 <= port <= typemax(UInt16))
throw(ArgumentError("port out of range, must be 0 ≤ port ≤ 65535, got $port"))
end
new(host,UInt16(port))
end
end
## SOCKETS ##
type TCPSocket <: LibuvStream
handle::Ptr{Void}
status::Int
line_buffered::Bool
buffer::IOBuffer
readcb::Callback
readnotify::Condition
ccb::Callback
connectnotify::Condition
closecb::Callback
closenotify::Condition
sendbuf::Nullable{IOBuffer}
lock::ReentrantLock
throttle::Int
TCPSocket(handle) = new(
handle,
StatusUninit,
true,
PipeBuffer(),
false, Condition(),
false, Condition(),
false, Condition(),
nothing,
ReentrantLock(),
DEFAULT_READ_BUFFER_SZ
)
end
function TCPSocket()
this = TCPSocket(Libc.malloc(_sizeof_uv_tcp))
associate_julia_struct(this.handle,this)
finalizer(this,uvfinalize)
err = ccall(:uv_tcp_init,Cint,(Ptr{Void},Ptr{Void}),
eventloop(),this.handle)
if err != 0
#TODO: this codepath is not currently tested
Libc.free(this.handle)
this.handle = C_NULL
throw(UVError("failed to create tcp socket",err))
end
this.status = StatusInit
this
end
type TCPServer <: LibuvServer
handle::Ptr{Void}
status::Int
ccb::Callback
connectnotify::Condition
closecb::Callback
closenotify::Condition
TCPServer(handle) = new(
handle,
StatusUninit,
false, Condition(),
false, Condition()
)
end
function TCPServer()
this = TCPServer(Libc.malloc(_sizeof_uv_tcp))
associate_julia_struct(this.handle, this)
finalizer(this,uvfinalize)
err = ccall(:uv_tcp_init,Cint,(Ptr{Void},Ptr{Void}),
eventloop(),this.handle)
if err != 0
#TODO: this codepath is not currently tested
Libc.free(this.handle)
this.handle = C_NULL
throw(UVError("failed to create tcp server",err))
end
this.status = StatusInit
this
end
isreadable(io::TCPSocket) = isopen(io) || nb_available(io) > 0
iswritable(io::TCPSocket) = isopen(io) && io.status != StatusClosing
## VARIOUS METHODS TO BE MOVED TO BETTER LOCATION
_jl_connect_raw(sock::TCPSocket,sockaddr::Ptr{Void}) =
ccall(:jl_connect_raw,Int32,(Ptr{Void},Ptr{Void},Ptr{Void}),sock.handle,sockaddr,uv_jl_connectcb::Ptr{Void})
_jl_sockaddr_from_addrinfo(addrinfo::Ptr{Void}) =
ccall(:jl_sockaddr_from_addrinfo,Ptr{Void},(Ptr{Void},),addrinfo)
_jl_sockaddr_set_port(ptr::Ptr{Void},port::UInt16) =
ccall(:jl_sockaddr_set_port,Void,(Ptr{Void},UInt16),ptr,port)
accept(server::TCPServer) = accept(server, TCPSocket())
# Libuv will internally reset the readable and writable flags on
# this pipe after it has successfully accepted the connection, to
# remember that before that this is an invalid pipe
accept(server::PipeServer) = accept(server, init_pipe!(PipeEndpoint();
readable=false, writable=false, julia_only=true))
##
bind(sock::TCPServer, addr::InetAddr) = bind(sock,addr.host,addr.port)
_bind(sock::TCPServer, host::IPv4, port::UInt16) = ccall(:jl_tcp_bind, Int32, (Ptr{Void}, UInt16, UInt32, Cuint),
sock.handle, hton(port), hton(host.host), 0)
_bind(sock::TCPServer, host::IPv6, port::UInt16) = ccall(:jl_tcp_bind6, Int32, (Ptr{Void}, UInt16, Ptr{UInt128}, Cuint),
sock.handle, hton(port), &hton(host.host), 0)
# UDP
type UDPSocket <: LibuvStream
handle::Ptr{Void}
status::Int
recvnotify::Condition
sendnotify::Condition
closenotify::Condition
UDPSocket(handle::Ptr) = new(
handle,
StatusUninit,
Condition(),
Condition(),
Condition()
)
end
function UDPSocket()
this = UDPSocket(Libc.malloc(_sizeof_uv_udp))
associate_julia_struct(this.handle, this)
err = ccall(:uv_udp_init,Cint,(Ptr{Void},Ptr{Void}),
eventloop(),this.handle)
finalizer(this, uvfinalize)
if err != 0
#TODO: this codepath is not currently tested
Libc.free(this.handle)
this.handle = C_NULL
throw(UVError("failed to create udp socket",err))
end
this.status = StatusInit
this
end
show(io::IO, stream::UDPSocket) = print(io, typeof(stream), "(", uv_status_string(stream), ")")
function uvfinalize(uv::Union{TTY,PipeEndpoint,PipeServer,TCPServer,TCPSocket,UDPSocket})
if (uv.status != StatusUninit && uv.status != StatusInit)
close(uv)
end
disassociate_julia_struct(uv)
uv.handle = C_NULL
end
function _uv_hook_close(sock::UDPSocket)
sock.handle = C_NULL
sock.status = StatusClosed
notify(sock.closenotify)
notify(sock.sendnotify)
notify_error(sock.recvnotify,EOFError())
end
# Disables dual stack mode. Only available when using ipv6 binf
const UV_UDP_IPV6ONLY = 1
# Indicates message was truncated because read buffer was too small. The
# remainder was discarded by the OS.
const UV_UDP_PARTIAL = 2
function bind(sock::Union{TCPServer,UDPSocket}, host::IPv4, port::Integer)
if sock.status != StatusInit
error("$(typeof(sock)) is not initialized")
end
err = _bind(sock,host,UInt16(port))
if err < 0
if err != UV_EADDRINUSE && err != UV_EACCES
#TODO: this codepath is not currently tested
throw(UVError("bind",err))
else
return false
end
end
sock.status = StatusOpen
true
end
_bind(sock::UDPSocket, host::IPv4, port::UInt16) = ccall(:jl_udp_bind, Int32, (Ptr{Void}, UInt16, UInt32, UInt32),
sock.handle, hton(port), hton(host.host), 0)
_bind(sock::UDPSocket, host::IPv6, port::UInt16, flags::UInt32 = UInt32(0)) = ccall(:jl_udp_bind6, Int32, (Ptr{Void}, UInt16, Ptr{UInt128}, UInt32),
sock.handle, hton(port), &hton(host.host), flags)
function bind(sock::UDPSocket, host::IPv6, port::UInt16; ipv6only = false)
if sock.status != StatusInit
error("UDPSocket is not initialized")
end
err = _bind(sock,host,port, UInt32(ipv6only ? UV_UDP_IPV6ONLY : 0))
if err < 0
if err != UV_EADDRINUSE && err != UV_EACCES
#TODO: this codepath is not currently tested
throw(UVError("bind",err))
else
return false
end
end
sock.status = StatusOpen
true
end
function setopt(sock::UDPSocket; multicast_loop = nothing, multicast_ttl=nothing, enable_broadcast=nothing, ttl=nothing)
if sock.status == StatusUninit
error("Cannot set options on uninitialized socket")
end
if multicast_loop !== nothing
uv_error("multicast_loop",ccall(:uv_udp_set_multicast_loop,Cint,(Ptr{Void},Cint),sock.handle,multicast_loop) < 0)
end
if multicast_ttl !== nothing
uv_error("multicast_ttl",ccall(:uv_udp_set_multicast_ttl,Cint,(Ptr{Void},Cint),sock.handle,multicast_ttl))
end
if enable_broadcast !== nothing
uv_error("enable_broadcast",ccall(:uv_udp_set_broadcast,Cint,(Ptr{Void},Cint),sock.handle,enable_broadcast))
end
if ttl !== nothing
uv_error("ttl",ccall(:uv_udp_set_ttl,Cint,(Ptr{Void},Cint),sock.handle,ttl))
end
end
alloc_buf_hook(sock::UDPSocket,size::UInt) = (Libc.malloc(size),size)
function _recv_start(sock::UDPSocket)
if ccall(:uv_is_active,Cint,(Ptr{Void},),sock.handle) == 0
uv_error("recv_start",ccall(:uv_udp_recv_start,Cint,(Ptr{Void},Ptr{Void},Ptr{Void}),
sock.handle,uv_jl_alloc_buf::Ptr{Void},uv_jl_recvcb::Ptr{Void}))
end
end
_recv_stop(sock::UDPSocket) = uv_error("recv_stop",ccall(:uv_udp_recv_stop,Cint,(Ptr{Void},),sock.handle))
function recv(sock::UDPSocket)
addr, data = recvfrom(sock)
data
end
function recvfrom(sock::UDPSocket)
# If the socket has not been bound, it will be bound implicitly to ::0 and a random port
if sock.status != StatusInit && sock.status != StatusOpen
error("UDPSocket is not initialized and open")
end
_recv_start(sock)
stream_wait(sock,sock.recvnotify)::Tuple{Union{IPv4, IPv6}, Vector{UInt8}}
end
function uv_recvcb(handle::Ptr{Void}, nread::Cssize_t, buf::Ptr{Void}, addr::Ptr{Void}, flags::Cuint)
sock = @handle_as handle UDPSocket
buf_addr = ccall(:jl_uv_buf_base, Ptr{Void}, (Ptr{Void},), buf)
buf_size = ccall(:jl_uv_buf_len, Csize_t, (Ptr{Void},), buf)
# C signature documented as (*uv_udp_recv_cb)(...)
if flags & UV_UDP_PARTIAL > 0
Libc.free(buf_addr)
notify_error(sock.recvnotify,"Partial message received")
end
# need to check the address type in order to convert to a Julia IPAddr
addrout = if (addr == C_NULL)
IPv4(0)
elseif ccall(:jl_sockaddr_in_is_ip4, Cint, (Ptr{Void},), addr) == 1
IPv4(ntoh(ccall(:jl_sockaddr_host4, UInt32, (Ptr{Void},), addr)))
else
tmp = [UInt128(0)]
ccall(:jl_sockaddr_host6, UInt32, (Ptr{Void}, Ptr{UInt8}), addr, pointer(tmp))
IPv6(ntoh(tmp[1]))
end
buf = pointer_to_array(convert(Ptr{UInt8},buf_addr),Int(buf_size),true)
notify(sock.recvnotify,(addrout,buf[1:nread]))
nothing
end
function _send(sock::UDPSocket,ipaddr::IPv4,port::UInt16,buf)
ccall(:jl_udp_send, Cint, (Ptr{Void},UInt16,UInt32,Ptr{UInt8},Csize_t,Ptr{Void}),
sock.handle,hton(port),hton(ipaddr.host),buf,sizeof(buf),uv_jl_sendcb::Ptr{Void})
end
function _send(sock::UDPSocket,ipaddr::IPv6,port::UInt16,buf)
ccall(:jl_udp_send6, Cint, (Ptr{Void},UInt16,Ptr{UInt128},Ptr{UInt8},Csize_t,Ptr{Void}),
sock.handle,hton(port),&hton(ipaddr.host),buf,sizeof(buf),uv_jl_sendcb::Ptr{Void})
end
function send(sock::UDPSocket,ipaddr,port,msg)
# If the socket has not been bound, it will be bound implicitly to ::0 and a random port
if sock.status != StatusInit && sock.status != StatusOpen
error("UDPSocket is not initialized and open")
end
uv_error("send",_send(sock,ipaddr,UInt16(port),msg))
stream_wait(sock,sock.sendnotify)
nothing
end
function uv_sendcb(handle::Ptr{Void}, status::Cint)
sock = @handle_as handle UDPSocket
if status < 0
notify_error(sock.sendnotify,UVError("UDP send failed",status))
end
notify(sock.sendnotify)
Libc.free(handle)
nothing
end
##
callback_dict = ObjectIdDict()
function uv_getaddrinfocb(req::Ptr{Void}, status::Cint, addrinfo::Ptr{Void})
data = ccall(:jl_uv_getaddrinfo_data, Ptr{Void}, (Ptr{Void},), req)
data == C_NULL && return
cb = unsafe_pointer_to_objref(data)::Function
pop!(callback_dict,cb) # using pop forces an error if cb not in callback_dict
if status != 0 || addrinfo == C_NULL
cb(UVError("getaddrinfo callback",status))
else
freeaddrinfo = addrinfo
while addrinfo != C_NULL
sockaddr = ccall(:jl_sockaddr_from_addrinfo,Ptr{Void},(Ptr{Void},),addrinfo)
if ccall(:jl_sockaddr_is_ip4,Int32,(Ptr{Void},),sockaddr) == 1
cb(IPv4(ntoh(ccall(:jl_sockaddr_host4,UInt32,(Ptr{Void},),sockaddr))))
break
#elseif ccall(:jl_sockaddr_is_ip6,Int32,(Ptr{Void},),sockaddr) == 1
# host = Array(UInt128,1)
# scope_id = ccall(:jl_sockaddr_host6,UInt32,(Ptr{Void},Ptr{UInt128}),sockaddr,host)
# cb(IPv6(ntoh(host[1])))
# break
end
addrinfo = ccall(:jl_next_from_addrinfo,Ptr{Void},(Ptr{Void},),addrinfo)
end
ccall(:uv_freeaddrinfo,Void,(Ptr{Void},),freeaddrinfo)
end
Libc.free(req)
nothing
end
function getaddrinfo(cb::Function, host::ASCIIString)
callback_dict[cb] = cb
uv_error("getaddrinfo",ccall(:jl_getaddrinfo, Int32, (Ptr{Void}, Cstring, Ptr{UInt8}, Any, Ptr{Void}),
eventloop(), host, C_NULL, cb, uv_jl_getaddrinfocb::Ptr{Void}))
end
getaddrinfo(cb::Function, host::AbstractString) = getaddrinfo(cb,ascii(host))
function getaddrinfo(host::ASCIIString)
c = Condition()
getaddrinfo(host) do IP
notify(c,IP)
end
ip = wait(c)
isa(ip,UVError) && throw(ip)
return ip::IPAddr
end
getaddrinfo(host::AbstractString) = getaddrinfo(ascii(host))
const _sizeof_uv_interface_address = ccall(:jl_uv_sizeof_interface_address,Int32,())
function getipaddr()
addr = Array(Ptr{UInt8},1)
addr[1] = C_NULL
count = zeros(Int32,1)
lo_present = false
err = ccall(:jl_uv_interface_addresses,Int32,(Ptr{Ptr{UInt8}},Ptr{Int32}),addr,count)
addr, count = addr[1],count[1]
if err != 0
ccall(:uv_free_interface_addresses,Void,(Ptr{UInt8},Int32),addr,count)
throw(UVError("getlocalip",err))
end
for i = 0:(count-1)
current_addr = addr + i*_sizeof_uv_interface_address
if 1 == ccall(:jl_uv_interface_address_is_internal,Int32,(Ptr{UInt8},),current_addr)
lo_present = true
continue
end
sockaddr = ccall(:jl_uv_interface_address_sockaddr,Ptr{Void},(Ptr{UInt8},),current_addr)
if ccall(:jl_sockaddr_in_is_ip4,Int32,(Ptr{Void},),sockaddr) == 1
rv = IPv4(ntoh(ccall(:jl_sockaddr_host4,UInt32,(Ptr{Void},),sockaddr)))
ccall(:uv_free_interface_addresses,Void,(Ptr{UInt8},Int32),addr,count)
return rv
# Uncomment to enbable IPv6
#elseif ccall(:jl_sockaddr_in_is_ip6,Int32,(Ptr{Void},),sockaddr) == 1
# host = Array(UInt128,1)
# ccall(:jl_sockaddr_host6,UInt32,(Ptr{Void},Ptr{UInt128}),sockaddrr,host)
# return IPv6(ntoh(host[1]))
end
end
ccall(:uv_free_interface_addresses,Void,(Ptr{UInt8},Int32),addr,count)
lo_present ? ip"127.0.0.1" : error("No networking interface available")
end
##
function connect!(sock::TCPSocket, host::IPv4, port::Integer)
if sock.status != StatusInit
error("TCPSocket is not initialized")
end
if !(0 <= port <= typemax(UInt16))
throw(ArgumentError("port out of range, must be 0 ≤ port ≤ 65535, got $port"))
end
uv_error("connect",ccall(:jl_tcp4_connect,Int32,(Ptr{Void},UInt32,UInt16,Ptr{Void}),
sock.handle,hton(host.host),hton(UInt16(port)),uv_jl_connectcb::Ptr{Void}))
sock.status = StatusConnecting
end
function connect!(sock::TCPSocket, host::IPv6, port::Integer)
if sock.status != StatusInit
error("TCPSocket is not initialized")
end
if !(0 <= port <= typemax(UInt16))
throw(ArgumentError("port out of range, must be 0 ≤ port ≤ 65535, got $port"))
end
uv_error("connect",ccall(:jl_tcp6_connect,Int32,(Ptr{Void},Ptr{UInt128},UInt16,Ptr{Void}),
sock.handle,&hton(host.host),hton(UInt16(port)),uv_jl_connectcb::Ptr{Void}))
sock.status = StatusConnecting
end
# Default Host to localhost
connect(sock::TCPSocket, port::Integer) = connect(sock,IPv4(127,0,0,1),port)
connect(port::Integer) = connect(IPv4(127,0,0,1),port)
# Valid connect signatures for TCP
connect(host::AbstractString, port::Integer) = connect(TCPSocket(),host,port)
connect(addr::IPAddr, port::Integer) = connect(TCPSocket(),addr,port)
connect(addr::InetAddr) = connect(TCPSocket(),addr)
default_connectcb(sock,status) = nothing
function connect!(sock::TCPSocket, host::AbstractString, port::Integer)
if sock.status != StatusInit
error("TCPSocket is not initialized")
end
ipaddr = getaddrinfo(host)
sock.status = StatusInit
connect!(sock,ipaddr,port)
sock.status = StatusConnecting
sock
end
##
listen(sock::LibuvServer; backlog::Integer=BACKLOG_DEFAULT) = (uv_error("listen",_listen(sock;backlog=backlog)); sock)
function listen(addr; backlog::Integer=BACKLOG_DEFAULT)
sock = TCPServer()
!bind(sock,addr) && error("cannot bind to port; may already be in use or access denied")
uv_error("listen",_listen(sock;backlog=backlog))
sock
end
listen(port::Integer; backlog::Integer=BACKLOG_DEFAULT) = listen(IPv4(UInt32(0)),port;backlog=backlog)
listen(host::IPAddr, port::Integer; backlog::Integer=BACKLOG_DEFAULT) = listen(InetAddr(host,port);backlog=backlog)
listen(cb::Callback,args...; backlog::Integer=BACKLOG_DEFAULT) = (sock=listen(args...;backlog=backlog);sock.ccb=cb;sock)
listen(cb::Callback,sock::Union{TCPSocket,UDPSocket}; backlog::Integer=BACKLOG_DEFAULT) = (sock.ccb=cb;listen(sock;backlog=backlog))
##
function accept_nonblock(server::TCPServer,client::TCPSocket)
if client.status != StatusInit
error("client TCPSocket is not initialized")
end
err = ccall(:uv_accept,Int32,(Ptr{Void},Ptr{Void}),server.handle,client.handle)
if err == 0
client.status = StatusOpen
end
err
end
function accept_nonblock(server::TCPServer)
client = TCPSocket()
uv_error("accept", accept_nonblock(server, client))
client
end
## Utility functions
function listenany(default_port)
addr = InetAddr(IPv4(UInt32(0)),default_port)
while true
sock = TCPServer()
if bind(sock,addr) && _listen(sock) == 0
return (addr.port,sock)
end
close(sock)
addr.port += 1
if addr.port==default_port
error("no ports available")
end
end
end
|