interface_linux.odin 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package net
  2. //+build linux
  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. Made available under Odin's BSD-3 license.
  12. List of contributors:
  13. Tetralux: Initial implementation
  14. Colin Davidson: Linux platform code, OSX platform code, Odin-native DNS resolver
  15. Jeroen van Rijn: Cross platform unification, code style, documentation
  16. This file uses `getifaddrs` libc call to enumerate interfaces.
  17. TODO: When we have raw sockets, split off into its own file for Linux so we can use the NETLINK protocol and bypass libc.
  18. */
  19. //import "core:strings"
  20. // TODO(flysand): regression
  21. // NOTE(flysand): https://man7.org/linux/man-pages/man7/netlink.7.html
  22. // apparently musl libc uses this to enumerate network interfaces
  23. @(private)
  24. _enumerate_interfaces :: proc(allocator := context.allocator) -> (interfaces: []Network_Interface, err: Network_Error) {
  25. context.allocator = allocator
  26. // head: ^os.ifaddrs
  27. // if res := os._getifaddrs(&head); res < 0 {
  28. // return {}, .Unable_To_Enumerate_Network_Interfaces
  29. // }
  30. // /*
  31. // Unlike Windows, *nix regrettably doesn't return all it knows about an interface in one big struct.
  32. // We're going to have to iterate over a list and coalesce information as we go.
  33. // */
  34. // ifaces: map[string]^Network_Interface
  35. // defer delete(ifaces)
  36. // for ifaddr := head; ifaddr != nil; ifaddr = ifaddr.next {
  37. // adapter_name := string(ifaddr.name)
  38. // /*
  39. // Check if we have seen this interface name before so we can reuse the `Network_Interface`.
  40. // Else, create a new one.
  41. // */
  42. // if adapter_name not_in ifaces {
  43. // ifaces[adapter_name] = new(Network_Interface)
  44. // ifaces[adapter_name].adapter_name = strings.clone(adapter_name)
  45. // }
  46. // iface := ifaces[adapter_name]
  47. // address: Address
  48. // netmask: Netmask
  49. // if ifaddr.address != nil {
  50. // switch int(ifaddr.address.sa_family) {
  51. // case os.AF_INET, os.AF_INET6:
  52. // address = _sockaddr_basic_to_endpoint(ifaddr.address).address
  53. // case os.AF_PACKET:
  54. // /*
  55. // For some obscure reason the 64-bit `getifaddrs` call returns a pointer to a
  56. // 32-bit `RTNL_LINK_STATS` structure, which of course means that tx/rx byte count
  57. // is truncated beyond usefulness.
  58. // We're not going to retrieve stats now. Instead this serves as a reminder to use
  59. // the NETLINK protocol for this purpose.
  60. // But in case you were curious:
  61. // stats := transmute(^os.rtnl_link_stats)ifaddr.data
  62. // fmt.println(stats)
  63. // */
  64. // case:
  65. // }
  66. // }
  67. // if ifaddr.netmask != nil {
  68. // switch int(ifaddr.netmask.sa_family) {
  69. // case os.AF_INET, os.AF_INET6:
  70. // netmask = Netmask(_sockaddr_basic_to_endpoint(ifaddr.netmask).address)
  71. // case:
  72. // }
  73. // }
  74. // if ifaddr.broadcast_or_dest != nil && .BROADCAST in ifaddr.flags {
  75. // switch int(ifaddr.broadcast_or_dest.sa_family) {
  76. // case os.AF_INET, os.AF_INET6:
  77. // broadcast := _sockaddr_basic_to_endpoint(ifaddr.broadcast_or_dest).address
  78. // append(&iface.multicast, broadcast)
  79. // case:
  80. // }
  81. // }
  82. // if address != nil {
  83. // lease := Lease{
  84. // address = address,
  85. // netmask = netmask,
  86. // }
  87. // append(&iface.unicast, lease)
  88. // }
  89. // /*
  90. // TODO: Refine this based on the type of adapter.
  91. // */
  92. // state := Link_State{}
  93. // if .UP in ifaddr.flags {
  94. // state |= {.Up}
  95. // }
  96. // if .DORMANT in ifaddr.flags {
  97. // state |= {.Dormant}
  98. // }
  99. // if .LOOPBACK in ifaddr.flags {
  100. // state |= {.Loopback}
  101. // }
  102. // iface.link.state = state
  103. // }
  104. // /*
  105. // Free the OS structures.
  106. // */
  107. // os._freeifaddrs(head)
  108. // /*
  109. // Turn the map into a slice to return.
  110. // */
  111. // _interfaces := make([dynamic]Network_Interface, 0, allocator)
  112. // for _, iface in ifaces {
  113. // append(&_interfaces, iface^)
  114. // free(iface)
  115. // }
  116. // return _interfaces[:], {}
  117. return nil, {}
  118. }