diff options
| author | Colin Davidson <colrdavidson@gmail.com> | 2023-03-01 07:58:30 -0800 |
|---|---|---|
| committer | Colin Davidson <colrdavidson@gmail.com> | 2023-03-01 07:58:30 -0800 |
| commit | 28f7f572473c4e97ccd6133bb4f5fa6f45505530 (patch) | |
| tree | 0bd159c24c617df409f72b0ec75daa086372e94d /core/net/interface.odin | |
| parent | 3567c006e6683d989805c078db48a95a901d9e72 (diff) | |
manually start merging core_net
Diffstat (limited to 'core/net/interface.odin')
| -rw-r--r-- | core/net/interface.odin | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/core/net/interface.odin b/core/net/interface.odin new file mode 100644 index 000000000..354cba53f --- /dev/null +++ b/core/net/interface.odin @@ -0,0 +1,68 @@ +/* + Copyright 2022 Tetralux <tetraluxonpc@gmail.com> + Copyright 2022 Colin Davidson <colrdavidson@gmail.com> + Copyright 2022 Jeroen van Rijn <nom@duclavier.com>. + Made available under Odin's BSD-3 license. + + List of contributors: + Tetralux: Initial implementation + Colin Davidson: Linux platform code, OSX platform code, Odin-native DNS resolver + Jeroen van Rijn: Cross platform unification, code style, documentation +*/ + +/* + Package net implements cross-platform Berkeley Sockets, DNS resolution and associated procedures. + For other protocols and their features, see subdirectories of this package. +*/ +package net + +import "core:strings" + +/* + `destroy_interfaces` cleans up a list of network interfaces retrieved by e.g. `enumerate_interfaces`. +*/ +destroy_interfaces :: proc(interfaces: []Network_Interface, allocator := context.allocator) { + context.allocator = allocator + + for i in interfaces { + delete(i.adapter_name) + delete(i.friendly_name) + delete(i.description) + delete(i.dns_suffix) + + delete(i.physical_address) + + delete(i.unicast) + delete(i.multicast) + delete(i.anycast) + delete(i.gateways) + } + delete(interfaces, allocator) +} + +/* + Turns a slice of bytes (from e.g. `get_adapters_addresses`) into a "XX:XX:XX:..." string. +*/ +physical_address_to_string :: proc(phy_addr: []u8, allocator := context.allocator) -> (phy_string: string) { + context.allocator = allocator + + MAC_HEX := "0123456789ABCDEF" + + if len(phy_addr) == 0 { + return "" + } + + buf: strings.Builder + + for b, i in phy_addr { + if i > 0 { + strings.write_rune_builder(&buf, ':') + } + + hi := rune(MAC_HEX[b >> 4]) + lo := rune(MAC_HEX[b & 15]) + strings.write_rune_builder(&buf, hi) + strings.write_rune_builder(&buf, lo) + } + return strings.to_string(buf) +}
\ No newline at end of file |