Merge pull request #5354 from Kelimion/init_net

Allow `core:net` to be imported with `-default-to-panic-allocator`.
This commit is contained in:
Jeroen van Rijn
2025-06-17 15:45:22 +02:00
committed by GitHub
3 changed files with 34 additions and 27 deletions
+3
View File
@@ -261,6 +261,9 @@ DNS_Configuration :: struct {
resolv_conf: string, resolv_conf: string,
hosts_file: string, hosts_file: string,
resolv_conf_buf: [128]u8,
hosts_file_buf: [128]u8,
// TODO: Allow loading these up with `reload_configuration()` call or the like, // TODO: Allow loading these up with `reload_configuration()` call or the like,
// so we don't have to do it each call. // so we don't have to do it each call.
name_servers: []Endpoint, name_servers: []Endpoint,
+23 -19
View File
@@ -22,21 +22,22 @@ package net
Haesbaert: Security fixes Haesbaert: Security fixes
*/ */
@(require) import "base:runtime"
import "core:mem" import "core:mem"
import "core:strings" import "core:strings"
import "core:time" import "core:time"
import "core:os" import "core:os"
import "core:math/rand" import "core:math/rand"
/* @(require) import "core:sync"
Default configuration for DNS resolution.
*/ dns_config_initialized: sync.Once
when ODIN_OS == .Windows { when ODIN_OS == .Windows {
DEFAULT_DNS_CONFIGURATION :: DNS_Configuration{ dns_configuration := DNS_Configuration{
resolv_conf = "", resolv_conf = "",
hosts_file = "%WINDIR%\\system32\\drivers\\etc\\hosts", hosts_file = "%WINDIR%\\system32\\drivers\\etc\\hosts",
} }
} else when ODIN_OS == .Linux || ODIN_OS == .Darwin || ODIN_OS == .FreeBSD || ODIN_OS == .OpenBSD || ODIN_OS == .NetBSD { } else when ODIN_OS == .Linux || ODIN_OS == .Darwin || ODIN_OS == .FreeBSD || ODIN_OS == .OpenBSD || ODIN_OS == .NetBSD {
DEFAULT_DNS_CONFIGURATION :: DNS_Configuration{ dns_configuration := DNS_Configuration{
resolv_conf = "/etc/resolv.conf", resolv_conf = "/etc/resolv.conf",
hosts_file = "/etc/hosts", hosts_file = "/etc/hosts",
} }
@@ -44,25 +45,20 @@ when ODIN_OS == .Windows {
#panic("Please add a configuration for this OS.") #panic("Please add a configuration for this OS.")
} }
@(init)
init_dns_configuration :: proc() {
/* /*
Resolve %ENVIRONMENT% placeholders in their paths. Replaces environment placeholders in `dns_configuration`. Only necessary on Windows.
Is automatically called, once, by `get_dns_records_*`.
*/ */
dns_configuration.resolv_conf = os.replace_environment_placeholders(dns_configuration.resolv_conf) @(private)
dns_configuration.hosts_file = os.replace_environment_placeholders(dns_configuration.hosts_file) init_dns_configuration :: proc() {
when ODIN_OS == .Windows {
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
val := os.replace_environment_placeholders(dns_configuration.hosts_file, context.temp_allocator)
copy(dns_configuration.hosts_file_buf[:], val)
dns_configuration.hosts_file = string(dns_configuration.hosts_file_buf[:len(val)])
} }
@(fini, private)
destroy_dns_configuration :: proc() {
delete(dns_configuration.resolv_conf)
dns_configuration.resolv_conf = ""
delete(dns_configuration.hosts_file)
dns_configuration.hosts_file = ""
} }
dns_configuration := DEFAULT_DNS_CONFIGURATION
/* /*
Resolves a hostname to exactly one IP4 and IP6 endpoint. Resolves a hostname to exactly one IP4 and IP6 endpoint.
It's then up to you which one you use. It's then up to you which one you use.
@@ -182,6 +178,9 @@ resolve_ip6 :: proc(hostname_and_maybe_port: string) -> (ep6: Endpoint, err: Net
See `destroy_records`. See `destroy_records`.
*/ */
get_dns_records_from_os :: proc(hostname: string, type: DNS_Record_Type, allocator := context.allocator) -> (records: []DNS_Record, err: DNS_Error) { get_dns_records_from_os :: proc(hostname: string, type: DNS_Record_Type, allocator := context.allocator) -> (records: []DNS_Record, err: DNS_Error) {
when ODIN_OS == .Windows {
sync.once_do(&dns_config_initialized, init_dns_configuration)
}
return _get_dns_records_os(hostname, type, allocator) return _get_dns_records_os(hostname, type, allocator)
} }
@@ -197,6 +196,9 @@ get_dns_records_from_os :: proc(hostname: string, type: DNS_Record_Type, allocat
See `destroy_records`. See `destroy_records`.
*/ */
get_dns_records_from_nameservers :: proc(hostname: string, type: DNS_Record_Type, name_servers: []Endpoint, host_overrides: []DNS_Record, allocator := context.allocator) -> (records: []DNS_Record, err: DNS_Error) { get_dns_records_from_nameservers :: proc(hostname: string, type: DNS_Record_Type, name_servers: []Endpoint, host_overrides: []DNS_Record, allocator := context.allocator) -> (records: []DNS_Record, err: DNS_Error) {
when ODIN_OS == .Windows {
sync.once_do(&dns_config_initialized, init_dns_configuration)
}
context.allocator = allocator context.allocator = allocator
if type != .SRV { if type != .SRV {
@@ -418,6 +420,8 @@ load_hosts :: proc(hosts_file_path: string, allocator := context.allocator) -> (
splits := strings.fields(line) splits := strings.fields(line)
defer delete(splits) defer delete(splits)
(len(splits) >= 2) or_continue
ip_str := splits[0] ip_str := splits[0]
addr := parse_address(ip_str) addr := parse_address(ip_str)
if addr == nil { if addr == nil {