ripple bill-suggestions

This commit is contained in:
Colin Davidson
2023-03-02 06:56:54 -08:00
parent 090723179b
commit 38d58e818c
3 changed files with 15 additions and 24 deletions
+4 -7
View File
@@ -34,11 +34,8 @@ import "core:fmt"
If `allow_non_decimal` is true, `aton` is told each component must be decimal and max 255. If `allow_non_decimal` is true, `aton` is told each component must be decimal and max 255.
*/ */
parse_ip4_address :: proc(address_and_maybe_port: string, allow_non_decimal := false) -> (addr: IP4_Address, ok: bool) { parse_ip4_address :: proc(address_and_maybe_port: string, allow_non_decimal := false) -> (addr: IP4_Address, ok: bool) {
res, res_ok := aton(address_and_maybe_port, .IP4, !allow_non_decimal) res := aton(address_and_maybe_port, .IP4, !allow_non_decimal) or_return
if ip4, ip4_ok := res.(IP4_Address); ip4_ok { return res.?
return ip4, res_ok
}
return {}, false
} }
/* /*
@@ -432,7 +429,7 @@ parse_hostname_or_endpoint :: proc(endpoint_str: string) -> (target: Host_Or_End
// Returns ok=false if port is not a number. // Returns ok=false if port is not a number.
split_port :: proc(endpoint_str: string) -> (addr_or_host: string, port: int, ok: bool) { split_port :: proc(endpoint_str: string) -> (addr_or_host: string, port: int, ok: bool) {
// IP6 [addr_or_host]:port // IP6 [addr_or_host]:port
if i := strings.last_index(endpoint_str, "]:"); i != -1 { if i := strings.last_index(endpoint_str, "]:"); i >= 0 {
addr_or_host = endpoint_str[1:i] addr_or_host = endpoint_str[1:i]
port, ok = strconv.parse_int(endpoint_str[i+2:], 10) port, ok = strconv.parse_int(endpoint_str[i+2:], 10)
@@ -741,4 +738,4 @@ parse_ip_component :: proc(input: string, max_value := u64(max(u32)), bases := D
// If we consumed at least 1 digit byte, `value` *should* continue a valid number in an appropriate base in the allowable range. // If we consumed at least 1 digit byte, `value` *should* continue a valid number in an appropriate base in the allowable range.
return value, digit_bytes + prefix_bytes, digit_bytes >= 1 return value, digit_bytes + prefix_bytes, digit_bytes >= 1
} }
+10 -16
View File
@@ -58,7 +58,7 @@ dns_configuration := DEFAULT_DNS_CONFIGURATION
replace_environment_path :: proc(path: string, allocator := context.allocator) -> (res: string, ok: bool) { replace_environment_path :: proc(path: string, allocator := context.allocator) -> (res: string, ok: bool) {
// Nothing to replace. Return a clone of the original. // Nothing to replace. Return a clone of the original.
if strings.count(path, "%") != 2 { if strings.count(path, "%") != 2 {
return strings.clone(path), true return strings.clone(path, allocator), true
} }
left := strings.index(path, "%") + 1 left := strings.index(path, "%") + 1
@@ -68,10 +68,10 @@ replace_environment_path :: proc(path: string, allocator := context.allocator) -
assert(right > 0 && right <= len(path)) // should be covered by there being two % assert(right > 0 && right <= len(path)) // should be covered by there being two %
env_key := path[left: right] env_key := path[left: right]
env_val := os.get_env(env_key) env_val := os.get_env(env_key, allocator)
defer delete(env_val) defer delete(env_val)
res, _ = strings.replace(path, path[left - 1: right + 1], env_val, 1) res, _ = strings.replace(path, path[left - 1: right + 1], env_val, 1, allocator)
return res, true return res, true
} }
@@ -356,10 +356,7 @@ unpack_dns_header :: proc(id: u16be, bits: u16be) -> (hdr: DNS_Header) {
load_resolv_conf :: proc(resolv_conf_path: string, allocator := context.allocator) -> (name_servers: []Endpoint, ok: bool) { load_resolv_conf :: proc(resolv_conf_path: string, allocator := context.allocator) -> (name_servers: []Endpoint, ok: bool) {
context.allocator = allocator context.allocator = allocator
res, success := os.read_entire_file_from_filename(resolv_conf_path) res := os.read_entire_file_from_filename(resolv_conf_path) or_return
if !success {
return
}
defer delete(res) defer delete(res)
resolv_str := string(res) resolv_str := string(res)
@@ -393,10 +390,7 @@ load_resolv_conf :: proc(resolv_conf_path: string, allocator := context.allocato
load_hosts :: proc(hosts_file_path: string, allocator := context.allocator) -> (hosts: []DNS_Host_Entry, ok: bool) { load_hosts :: proc(hosts_file_path: string, allocator := context.allocator) -> (hosts: []DNS_Host_Entry, ok: bool) {
context.allocator = allocator context.allocator = allocator
res, success := os.read_entire_file_from_filename(hosts_file_path, allocator) res := os.read_entire_file_from_filename(hosts_file_path, allocator) or_return
if !success {
return
}
defer delete(res) defer delete(res)
_hosts := make([dynamic]DNS_Host_Entry, 0, allocator) _hosts := make([dynamic]DNS_Host_Entry, 0, allocator)
@@ -800,7 +794,7 @@ parse_response :: proc(response: []u8, filter: DNS_Record_Type = nil, allocator
cur_idx := header_size_bytes cur_idx := header_size_bytes
for i := 0; i < question_count; i += 1 { for _ in 0..<question_count {
if cur_idx == len(response) { if cur_idx == len(response) {
continue continue
} }
@@ -812,7 +806,7 @@ parse_response :: proc(response: []u8, filter: DNS_Record_Type = nil, allocator
cur_idx += hn_sz + dq_sz cur_idx += hn_sz + dq_sz
} }
for i := 0; i < answer_count; i += 1 { for _ in 0..<answer_count {
if cur_idx == len(response) { if cur_idx == len(response) {
continue continue
} }
@@ -825,7 +819,7 @@ parse_response :: proc(response: []u8, filter: DNS_Record_Type = nil, allocator
append(&_records, rec) append(&_records, rec)
} }
for i := 0; i < authority_count; i += 1 { for _ in 0..<authority_count {
if cur_idx == len(response) { if cur_idx == len(response) {
continue continue
} }
@@ -838,7 +832,7 @@ parse_response :: proc(response: []u8, filter: DNS_Record_Type = nil, allocator
append(&_records, rec) append(&_records, rec)
} }
for i := 0; i < additional_count; i += 1 { for _ in 0..<additional_count {
if cur_idx == len(response) { if cur_idx == len(response) {
continue continue
} }
@@ -852,4 +846,4 @@ parse_response :: proc(response: []u8, filter: DNS_Record_Type = nil, allocator
} }
return _records[:], true return _records[:], true
} }
+1 -1
View File
@@ -25,7 +25,7 @@ split_url :: proc(url: string, allocator := context.allocator) -> (scheme, host,
s := url s := url
i := strings.last_index(s, "://") i := strings.last_index(s, "://")
if i != -1 { if i >= 0 {
scheme = s[:i] scheme = s[:i]
s = s[i+3:] s = s[i+3:]
} }