Skip to content

Commit 8456540

Browse files
committed
rework
1 parent 46cb95c commit 8456540

File tree

3 files changed

+32
-30
lines changed

3 files changed

+32
-30
lines changed

dash-spv-ffi/include/dash_spv_ffi.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -609,9 +609,14 @@ int32_t dash_spv_ffi_config_set_max_peers(struct FFIClientConfig *config,
609609
/**
610610
* Adds a peer address to the configuration
611611
*
612-
* Accepts either a full socket address (e.g., `192.168.1.1:9999` or `[::1]:19999`)
613-
* or an IP-only string (e.g., "127.0.0.1" or "2001:db8::1"). When an IP-only
614-
* string is given, the default P2P port for the configured network is used.
612+
* Accepts socket addresses with or without port. When no port is specified,
613+
* the default P2P port for the configured network is used.
614+
*
615+
* Supported formats:
616+
* - IP with port: `192.168.1.1:9999`, `[::1]:19999`
617+
* - IP without port: `127.0.0.1`, `2001:db8::1`
618+
* - Hostname with port: `node.example.com:9999`
619+
* - Hostname without port: `node.example.com`
615620
*
616621
* # Safety
617622
* - `config` must be a valid pointer to an FFIClientConfig created by dash_spv_ffi_config_new/mainnet/testnet

dash-spv-ffi/src/config.rs

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,14 @@ pub unsafe extern "C" fn dash_spv_ffi_config_set_max_peers(
125125

126126
/// Adds a peer address to the configuration
127127
///
128-
/// Accepts either a full socket address (e.g., `192.168.1.1:9999` or `[::1]:19999`)
129-
/// or an IP-only string (e.g., "127.0.0.1" or "2001:db8::1"). When an IP-only
130-
/// string is given, the default P2P port for the configured network is used.
128+
/// Accepts socket addresses with or without port. When no port is specified,
129+
/// the default P2P port for the configured network is used.
130+
///
131+
/// Supported formats:
132+
/// - IP with port: `192.168.1.1:9999`, `[::1]:19999`
133+
/// - IP without port: `127.0.0.1`, `2001:db8::1`
134+
/// - Hostname with port: `node.example.com:9999`
135+
/// - Hostname without port: `node.example.com`
131136
///
132137
/// # Safety
133138
/// - `config` must be a valid pointer to an FFIClientConfig created by dash_spv_ffi_config_new/mainnet/testnet
@@ -171,24 +176,14 @@ pub unsafe extern "C" fn dash_spv_ffi_config_add_peer(
171176
return FFIErrorCode::Success as i32;
172177
}
173178

174-
// 3) Attempt DNS resolution for hostnames with explicit port
175-
match addr_str.split_once(':') {
176-
None => {
177-
set_last_error("Invalid hostname. Use 'host:port' or IP address");
178-
return FFIErrorCode::InvalidArgument as i32;
179-
}
180-
Some(("", _)) => {
181-
set_last_error("Missing hostname. Use 'host:port' or IP address");
182-
return FFIErrorCode::InvalidArgument as i32;
183-
}
184-
Some((_, "")) => {
185-
set_last_error("Missing port. Use 'host:port' or IP address");
186-
return FFIErrorCode::InvalidArgument as i32;
187-
}
188-
Some(_) => {}
189-
}
179+
// 3) Must be a hostname - reject empty, append default port if not specified
180+
let addr_with_port = if addr_str.contains(':') {
181+
addr_str.to_string()
182+
} else {
183+
format!("{}:{}", addr_str, default_port)
184+
};
190185

191-
match addr_str.to_socket_addrs() {
186+
match addr_with_port.to_socket_addrs() {
192187
Ok(mut iter) => match iter.next() {
193188
Some(sock) => {
194189
cfg.peers.push(sock);

dash-spv-ffi/tests/unit/test_configuration.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,14 @@ mod tests {
5454

5555
// Test various invalid addresses
5656
let invalid_addrs = [
57-
"256.256.256.256:9999", // invalid IP octets
58-
"127.0.0.1:99999", // port too high
59-
"127.0.0.1:-1", // negative port
60-
"hostname-without-port", // hostname without port should be rejected
61-
":9999", // missing IP
62-
":::", // invalid IPv6
63-
"localhost:abc", // non-numeric port
57+
"", // empty string
58+
"256.256.256.256:9999", // invalid IP octets
59+
"127.0.0.1:99999", // port too high
60+
"127.0.0.1:-1", // negative port
61+
":9999", // missing hostname
62+
":::", // invalid IPv6
63+
"localhost:abc", // non-numeric port
64+
"localhost:", // empty port
6465
];
6566

6667
for addr in &invalid_addrs {
@@ -87,6 +88,7 @@ mod tests {
8788
"127.0.0.1", // IP-only v4
8889
"2001:db8::1", // IP-only v6
8990
"localhost:9999", // Hostname with port
91+
"localhost", // Hostname without port (uses default)
9092
];
9193

9294
for addr in &valid_addrs {

0 commit comments

Comments
 (0)