Skip to content

Commit a65ece4

Browse files
committed
Allow core:net to be imported with -default-to-panic-allocator.
1 parent f3a52a6 commit a65ece4

File tree

3 files changed

+34
-27
lines changed

3 files changed

+34
-27
lines changed

core/net/common.odin

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,9 @@ DNS_Configuration :: struct {
261261
resolv_conf: string,
262262
hosts_file: string,
263263

264+
resolv_conf_buf: [128]u8,
265+
hosts_file_buf: [128]u8,
266+
264267
// TODO: Allow loading these up with `reload_configuration()` call or the like,
265268
// so we don't have to do it each call.
266269
name_servers: []Endpoint,

core/net/dns.odin

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,47 +22,43 @@ package net
2222
Haesbaert: Security fixes
2323
*/
2424

25+
@(require) import "base:runtime"
2526
import "core:mem"
2627
import "core:strings"
2728
import "core:time"
2829
import "core:os"
2930
import "core:math/rand"
30-
/*
31-
Default configuration for DNS resolution.
32-
*/
31+
@(require) import "core:sync"
32+
33+
dns_config_initialized: sync.Once
3334
when ODIN_OS == .Windows {
34-
DEFAULT_DNS_CONFIGURATION :: DNS_Configuration{
35-
resolv_conf = "",
36-
hosts_file = "%WINDIR%\\system32\\drivers\\etc\\hosts",
35+
dns_configuration := DNS_Configuration{
36+
resolv_conf = "",
37+
hosts_file = "%WINDIR%\\system32\\drivers\\etc\\hosts",
3738
}
3839
} else when ODIN_OS == .Linux || ODIN_OS == .Darwin || ODIN_OS == .FreeBSD || ODIN_OS == .OpenBSD || ODIN_OS == .NetBSD {
39-
DEFAULT_DNS_CONFIGURATION :: DNS_Configuration{
40-
resolv_conf = "/etc/resolv.conf",
41-
hosts_file = "/etc/hosts",
40+
dns_configuration := DNS_Configuration{
41+
resolv_conf = "/etc/resolv.conf",
42+
hosts_file = "/etc/hosts",
4243
}
4344
} else {
4445
#panic("Please add a configuration for this OS.")
4546
}
4647

47-
@(init)
48+
/*
49+
Replaces environment placeholders in `dns_configuration`. Only necessary on Windows.
50+
Is automatically called, once, by `get_dns_records_*`.
51+
*/
52+
@(private)
4853
init_dns_configuration :: proc() {
49-
/*
50-
Resolve %ENVIRONMENT% placeholders in their paths.
51-
*/
52-
dns_configuration.resolv_conf = os.replace_environment_placeholders(dns_configuration.resolv_conf)
53-
dns_configuration.hosts_file = os.replace_environment_placeholders(dns_configuration.hosts_file)
54-
}
55-
56-
@(fini, private)
57-
destroy_dns_configuration :: proc() {
58-
delete(dns_configuration.resolv_conf)
59-
dns_configuration.resolv_conf = ""
60-
delete(dns_configuration.hosts_file)
61-
dns_configuration.hosts_file = ""
54+
when ODIN_OS == .Windows {
55+
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
56+
val := os.replace_environment_placeholders(dns_configuration.hosts_file, context.temp_allocator)
57+
copy(dns_configuration.hosts_file_buf[:], val)
58+
dns_configuration.hosts_file = string(dns_configuration.hosts_file_buf[:len(val)])
59+
}
6260
}
6361

64-
dns_configuration := DEFAULT_DNS_CONFIGURATION
65-
6662
/*
6763
Resolves a hostname to exactly one IP4 and IP6 endpoint.
6864
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
182178
See `destroy_records`.
183179
*/
184180
get_dns_records_from_os :: proc(hostname: string, type: DNS_Record_Type, allocator := context.allocator) -> (records: []DNS_Record, err: DNS_Error) {
181+
when ODIN_OS == .Windows {
182+
sync.once_do(&dns_config_initialized, init_dns_configuration)
183+
}
185184
return _get_dns_records_os(hostname, type, allocator)
186185
}
187186

@@ -197,6 +196,9 @@ get_dns_records_from_os :: proc(hostname: string, type: DNS_Record_Type, allocat
197196
See `destroy_records`.
198197
*/
199198
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) {
199+
when ODIN_OS == .Windows {
200+
sync.once_do(&dns_config_initialized, init_dns_configuration)
201+
}
200202
context.allocator = allocator
201203

202204
if type != .SRV {
@@ -418,6 +420,8 @@ load_hosts :: proc(hosts_file_path: string, allocator := context.allocator) -> (
418420
splits := strings.fields(line)
419421
defer delete(splits)
420422

423+
(len(splits) >= 2) or_continue
424+
421425
ip_str := splits[0]
422426
addr := parse_address(ip_str)
423427
if addr == nil {
@@ -864,4 +868,4 @@ parse_response :: proc(response: []u8, filter: DNS_Record_Type = nil, allocator
864868
xid = hdr.id
865869

866870
return _records[:], xid, true
867-
}
871+
}

core/net/dns_unix.odin

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,4 @@ _get_dns_records_os :: proc(hostname: string, type: DNS_Record_Type, allocator :
7979
}
8080

8181
return get_dns_records_from_nameservers(hostname, type, name_servers, host_overrides[:])
82-
}
82+
}

0 commit comments

Comments
 (0)