@@ -22,47 +22,43 @@ package net
2222 Haesbaert: Security fixes
2323*/
2424
25+ @(require) import " base:runtime"
2526import " core:mem"
2627import " core:strings"
2728import " core:time"
2829import " core:os"
2930import " core:math/rand"
30- /*
31- Default configuration for DNS resolution.
32- */
31+ @(require) import " core:sync "
32+
33+ dns_config_initialized: sync.Once
3334when 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)
4853init_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*/
184180get_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*/
199198get_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+ }
0 commit comments