Skip to content

Commit e29ffbb

Browse files
committed
Add a helper struct to allow a user to safely pass in an addr list
1 parent 1619d08 commit e29ffbb

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

lightning/src/ln/msgs.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,58 @@ impl NetAddress {
305305
}
306306
}
307307

308+
/// A "set" of addresses which enforces that there can be only up to one of each net address type.
309+
pub struct NetAddressSet {
310+
v4: Option<NetAddress>,
311+
v6: Option<NetAddress>,
312+
onion2: Option<NetAddress>,
313+
onion3: Option<NetAddress>,
314+
}
315+
impl NetAddressSet {
316+
/// Creates a new, empty, NetAddressSet
317+
pub fn new() -> Self {
318+
NetAddressSet { v4: None, v6: None, onion2: None, onion3: None }
319+
}
320+
321+
/// Sets the IPv4 socket address in this set, overwriting any previous IPv4 socket addresses
322+
/// (if any).
323+
pub fn set_v4(&mut self, addr: [u8; 4], port: u16) {
324+
self.v4 = Some(NetAddress::IPv4 { addr, port });
325+
}
326+
/// Sets the IPv6 socket address in this set, overwriting any previous IPv6 socket addresses
327+
/// (if any).
328+
pub fn set_v6(&mut self, addr: [u8; 16], port: u16) {
329+
self.v6 = Some(NetAddress::IPv6 { addr, port });
330+
}
331+
/// Sets the Tor Onion v2 socket address in this set, overwriting any previous Tor Onion v2
332+
/// socket address (if any).
333+
pub fn set_onion_v2(&mut self, addr: [u8; 10], port: u16) {
334+
self.onion2 = Some(NetAddress::OnionV2 { addr, port });
335+
}
336+
/// Sets the Tor Onion v3 socket address in this set, overwriting any previous Tor Onion v3
337+
/// socket address (if any).
338+
pub fn set_onion_v3(&mut self, ed25519_pubkey: [u8; 32], checksum: u16, version: u8, port: u16) {
339+
self.onion3 = Some(NetAddress::OnionV3 { ed25519_pubkey, checksum, version, port });
340+
}
341+
342+
pub(crate) fn to_vec(mut self) -> Vec<NetAddress> {
343+
let mut res = Vec::new();
344+
if let Some(addr) = self.v4.take() {
345+
res.push(addr);
346+
}
347+
if let Some(addr) = self.v6.take() {
348+
res.push(addr);
349+
}
350+
if let Some(addr) = self.onion2.take() {
351+
res.push(addr);
352+
}
353+
if let Some(addr) = self.onion3.take() {
354+
res.push(addr);
355+
}
356+
res
357+
}
358+
}
359+
308360
impl Writeable for NetAddress {
309361
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
310362
match self {

0 commit comments

Comments
 (0)