|
| 1 | +## This is a minimal version of NetworkInterfaceControllers.jl, licensed under MIT |
| 2 | + |
| 3 | +# uv_interface_address_t has a few fields, but we don't support accessing all of |
| 4 | +# them because `name` is the first field and it's a pointer: |
| 5 | +# https://docs.libuv.org/en/v1.x/misc.html#c.uv_interface_address_t |
| 6 | +# |
| 7 | +# To safely access the other fields we would have to account for their |
| 8 | +# offset changing on 32/64bit platforms, which we are too lazy to do (and |
| 9 | +# don't need anyway since we only want the name). |
| 10 | +const uv_interface_address_t = Cvoid |
| 11 | + |
| 12 | +const sizeof_uv_interface_address_t = @ccall jl_uv_sizeof_interface_address()::Cint |
| 13 | + |
| 14 | +function uv_interface_addresses(addresses, count) |
| 15 | + @ccall jl_uv_interface_addresses(addresses::Ptr{Ptr{uv_interface_address_t}}, count::Ptr{Cint})::Cint |
| 16 | +end |
| 17 | + |
| 18 | +function uv_free_interface_addresses(addresses, count) |
| 19 | + @ccall uv_free_interface_addresses(addresses::Ptr{uv_interface_address_t}, count::Cint)::Cvoid |
| 20 | +end |
| 21 | + |
| 22 | +function _next(r::Base.RefValue{Ptr{uv_interface_address_t}}) |
| 23 | + next_addr = r[] + sizeof_uv_interface_address_t |
| 24 | + Ref(Ptr{uv_interface_address_t}(next_addr)) |
| 25 | +end |
| 26 | + |
| 27 | +_is_loopback(addr) = 1 == @ccall jl_uv_interface_address_is_internal(addr::Ptr{uv_interface_address_t})::Cint |
| 28 | + |
| 29 | +_sockaddr(addr) = @ccall jl_uv_interface_address_sockaddr(addr::Ptr{uv_interface_address_t})::Ptr{Cvoid} |
| 30 | + |
| 31 | +_sockaddr_is_ip4(sockaddr::Ptr{Cvoid}) = 1 == @ccall jl_sockaddr_is_ip4(sockaddr::Ptr{Cvoid})::Cint |
| 32 | + |
| 33 | +_sockaddr_is_ip6(sockaddr::Ptr{Cvoid}) = 1 == @ccall jl_sockaddr_is_ip6(sockaddr::Ptr{Cvoid})::Cint |
| 34 | + |
| 35 | +_sockaddr_to_ip4(sockaddr::Ptr{Cvoid}) = IPv4(ntoh(@ccall jl_sockaddr_host4(sockaddr::Ptr{Cvoid})::Cuint)) |
| 36 | + |
| 37 | +function _sockaddr_to_ip6(sockaddr::Ptr{Cvoid}) |
| 38 | + addr6 = Ref{UInt128}() |
| 39 | + @ccall jl_sockaddr_host6(sockaddr::Ptr{Cvoid}, addr6::Ptr{UInt128})::Cuint |
| 40 | + IPv6(ntoh(addr6[])) |
| 41 | +end |
| 42 | + |
| 43 | +# Define a selection of hardware types that we're interested in. Values taken from: |
| 44 | +# https://github.com/torvalds/linux/blob/28eb75e178d389d325f1666e422bc13bbbb9804c/include/uapi/linux/if_arp.h#L29 |
| 45 | +@enum ARPHardware begin |
| 46 | + ARPHardware_Ethernet = 1 |
| 47 | + ARPHardware_Infiniband = 32 |
| 48 | + ARPHardware_Loopback = 772 |
| 49 | +end |
| 50 | + |
| 51 | +struct Interface |
| 52 | + name::String |
| 53 | + version::Symbol |
| 54 | + ip::IPAddr |
| 55 | + |
| 56 | + # These two fields are taken from the sysfs /type and /speed files if available: |
| 57 | + # https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-class-net |
| 58 | + type::Union{ARPHardware, Nothing} |
| 59 | + speed::Union{Float64, Nothing} |
| 60 | +end |
| 61 | + |
| 62 | +function _get_interfaces( |
| 63 | + ::Type{T}=IPAddr; loopback::Bool=false |
| 64 | +) where T <: IPAddr |
| 65 | + addr_ref = Ref{Ptr{uv_interface_address_t}}(C_NULL) |
| 66 | + count_ref = Ref{Int32}(1) |
| 67 | + |
| 68 | + err = uv_interface_addresses(addr_ref, count_ref) |
| 69 | + if err != 0 |
| 70 | + error("Call to uv_interface_addresses() to list network interfaces failed: $(err)") |
| 71 | + end |
| 72 | + |
| 73 | + interface_data = Interface[] |
| 74 | + current_addr = addr_ref |
| 75 | + for i = 0:(count_ref[]-1) |
| 76 | + # Skip loopback devices, if so required |
| 77 | + if (!loopback) && _is_loopback(current_addr[]) |
| 78 | + # Don't don't forget to iterate the address pointer though! |
| 79 | + current_addr = _next(current_addr) |
| 80 | + continue |
| 81 | + end |
| 82 | + |
| 83 | + # Interface name string. The name is the first field of the struct so we |
| 84 | + # just cast the struct pointer to a Ptr{Cstring} and load it. |
| 85 | + name_ptr = unsafe_load(Ptr{Cstring}(current_addr[])) |
| 86 | + name = unsafe_string(name_ptr) |
| 87 | + |
| 88 | + # Sockaddr used to load IPv4, or IPv6 addresses |
| 89 | + sockaddr = _sockaddr(current_addr[]) |
| 90 | + |
| 91 | + # Load IP addresses |
| 92 | + (ip_type, ip_address) = if IPv4 <: T && _sockaddr_is_ip4(sockaddr) |
| 93 | + (:v4, _sockaddr_to_ip4(sockaddr)) |
| 94 | + elseif IPv6 <: T && _sockaddr_is_ip6(sockaddr) |
| 95 | + (:v6, _sockaddr_to_ip6(sockaddr)) |
| 96 | + else |
| 97 | + (:skip, nothing) |
| 98 | + end |
| 99 | + |
| 100 | + type = nothing |
| 101 | + speed = nothing |
| 102 | + |
| 103 | + @static if Sys.isunix() |
| 104 | + # Load sysfs info |
| 105 | + sysfs_path = "/sys/class/net/$(name)" |
| 106 | + type_path = "$(sysfs_path)/type" |
| 107 | + speed_path = "$(sysfs_path)/speed" |
| 108 | + |
| 109 | + if isfile(type_path) |
| 110 | + try |
| 111 | + type_code = parse(Int, read(type_path, String)) |
| 112 | + if type_code in Int.(instances(ARPHardware)) |
| 113 | + type = ARPHardware(type_code) |
| 114 | + end |
| 115 | + catch |
| 116 | + # Do nothing on any failure to read or parse the file |
| 117 | + end |
| 118 | + end |
| 119 | + |
| 120 | + if isfile(speed_path) |
| 121 | + try |
| 122 | + reported_speed = parse(Float64, read(speed_path, String)) |
| 123 | + if reported_speed > 0 |
| 124 | + speed = reported_speed |
| 125 | + end |
| 126 | + catch |
| 127 | + end |
| 128 | + end |
| 129 | + end |
| 130 | + |
| 131 | + # Append to data vector and iterate address pointer |
| 132 | + if ip_type != :skip |
| 133 | + push!(interface_data, Interface(name, ip_type, ip_address, type, speed)) |
| 134 | + end |
| 135 | + current_addr = _next(current_addr) |
| 136 | + end |
| 137 | + |
| 138 | + uv_free_interface_addresses(addr_ref[], count_ref[]) |
| 139 | + |
| 140 | + return interface_data |
| 141 | +end |
0 commit comments