interface_darwin.odin 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package net
  2. #+build darwin
  3. /*
  4. Package net implements cross-platform Berkeley Sockets, DNS resolution and associated procedures.
  5. For other protocols and their features, see subdirectories of this package.
  6. */
  7. /*
  8. Copyright 2022 Tetralux <[email protected]>
  9. Copyright 2022 Colin Davidson <[email protected]>
  10. Copyright 2022 Jeroen van Rijn <[email protected]>.
  11. Copyright 2024 Feoramund <[email protected]>.
  12. Made available under Odin's BSD-3 license.
  13. List of contributors:
  14. Tetralux: Initial implementation
  15. Colin Davidson: Linux platform code, OSX platform code, Odin-native DNS resolver
  16. Jeroen van Rijn: Cross platform unification, code style, documentation
  17. Feoramund: FreeBSD platform code
  18. */
  19. import "core:os"
  20. import "core:strings"
  21. @(private)
  22. _enumerate_interfaces :: proc(allocator := context.allocator) -> (interfaces: []Network_Interface, err: Network_Error) {
  23. context.allocator = allocator
  24. head: ^os.ifaddrs
  25. if res := os._getifaddrs(&head); res < 0 {
  26. return {}, .Unable_To_Enumerate_Network_Interfaces
  27. }
  28. /*
  29. Unlike Windows, *nix regrettably doesn't return all it knows about an interface in one big struct.
  30. We're going to have to iterate over a list and coalesce information as we go.
  31. */
  32. ifaces: map[string]^Network_Interface
  33. defer delete(ifaces)
  34. for ifaddr := head; ifaddr != nil; ifaddr = ifaddr.next {
  35. adapter_name := string(ifaddr.name)
  36. /*
  37. Check if we have seen this interface name before so we can reuse the `Network_Interface`.
  38. Else, create a new one.
  39. */
  40. if adapter_name not_in ifaces {
  41. ifaces[adapter_name] = new(Network_Interface)
  42. ifaces[adapter_name].adapter_name = strings.clone(adapter_name)
  43. }
  44. iface := ifaces[adapter_name]
  45. address: Address
  46. netmask: Netmask
  47. if ifaddr.address != nil {
  48. switch int(ifaddr.address.family) {
  49. case os.AF_INET, os.AF_INET6:
  50. address = _sockaddr_basic_to_endpoint(ifaddr.address).address
  51. }
  52. }
  53. if ifaddr.netmask != nil {
  54. switch int(ifaddr.netmask.family) {
  55. case os.AF_INET, os.AF_INET6:
  56. netmask = Netmask(_sockaddr_basic_to_endpoint(ifaddr.netmask).address)
  57. }
  58. }
  59. if ifaddr.broadcast_or_dest != nil && .BROADCAST in ifaddr.flags {
  60. switch int(ifaddr.broadcast_or_dest.family) {
  61. case os.AF_INET, os.AF_INET6:
  62. broadcast := _sockaddr_basic_to_endpoint(ifaddr.broadcast_or_dest).address
  63. append(&iface.multicast, broadcast)
  64. }
  65. }
  66. if address != nil {
  67. lease := Lease{
  68. address = address,
  69. netmask = netmask,
  70. }
  71. append(&iface.unicast, lease)
  72. }
  73. /*
  74. TODO: Refine this based on the type of adapter.
  75. */
  76. state := Link_State{}
  77. if .UP in ifaddr.flags {
  78. state += {.Up}
  79. }
  80. /*if .DORMANT in ifaddr.flags {
  81. state |= {.Dormant}
  82. }*/
  83. if .LOOPBACK in ifaddr.flags {
  84. state += {.Loopback}
  85. }
  86. iface.link.state = state
  87. }
  88. /*
  89. Free the OS structures.
  90. */
  91. os._freeifaddrs(head)
  92. /*
  93. Turn the map into a slice to return.
  94. */
  95. _interfaces := make([dynamic]Network_Interface, 0, allocator)
  96. for _, iface in ifaces {
  97. append(&_interfaces, iface^)
  98. free(iface)
  99. }
  100. return _interfaces[:], {}
  101. }