Skip to content

Commit d470e4a

Browse files
committed
Add a helper struct to allow a user to safely pass in an addr list
1 parent 8829d1b commit d470e4a

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

lightning/src/ln/msgs.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,56 @@ impl NetAddress {
306306
}
307307
}
308308

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

0 commit comments

Comments
 (0)