interface.odin 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // +build windows, linux, darwin
  2. package net
  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. */
  17. import "core:strings"
  18. MAX_INTERFACE_ENUMERATION_TRIES :: 3
  19. /*
  20. `enumerate_interfaces` retrieves a list of network interfaces with their associated properties.
  21. */
  22. enumerate_interfaces :: proc(allocator := context.allocator) -> (interfaces: []Network_Interface, err: Network_Error) {
  23. return _enumerate_interfaces(allocator)
  24. }
  25. /*
  26. `destroy_interfaces` cleans up a list of network interfaces retrieved by e.g. `enumerate_interfaces`.
  27. */
  28. destroy_interfaces :: proc(interfaces: []Network_Interface, allocator := context.allocator) {
  29. context.allocator = allocator
  30. for i in interfaces {
  31. delete(i.adapter_name)
  32. delete(i.friendly_name)
  33. delete(i.description)
  34. delete(i.dns_suffix)
  35. delete(i.physical_address)
  36. delete(i.unicast)
  37. delete(i.multicast)
  38. delete(i.anycast)
  39. delete(i.gateways)
  40. }
  41. delete(interfaces, allocator)
  42. }
  43. /*
  44. Turns a slice of bytes (from e.g. `get_adapters_addresses`) into a "XX:XX:XX:..." string.
  45. */
  46. physical_address_to_string :: proc(phy_addr: []u8, allocator := context.allocator) -> (phy_string: string) {
  47. context.allocator = allocator
  48. MAC_HEX := "0123456789ABCDEF"
  49. if len(phy_addr) == 0 {
  50. return ""
  51. }
  52. buf: strings.Builder
  53. for b, i in phy_addr {
  54. if i > 0 {
  55. strings.write_rune(&buf, ':')
  56. }
  57. hi := rune(MAC_HEX[b >> 4])
  58. lo := rune(MAC_HEX[b & 15])
  59. strings.write_rune(&buf, hi)
  60. strings.write_rune(&buf, lo)
  61. }
  62. return strings.to_string(buf)
  63. }