Skip to content

Commit 7afc683

Browse files
committed
Add a helper struct to allow a user to safely pass in an addr list
1 parent bec0a26 commit 7afc683

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
@@ -306,6 +306,58 @@ 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 addresses
323+
/// (if any).
324+
pub fn set_v4(&mut self, addr: [u8; 4], port: u16) {
325+
self.v4 = Some(NetAddress::IPv4 { addr, port });
326+
}
327+
/// Sets the IPv6 socket address in this set, overwriting any previous IPv6 socket addresses
328+
/// (if any).
329+
pub fn set_v6(&mut self, addr: [u8; 16], port: u16) {
330+
self.v6 = Some(NetAddress::IPv6 { addr, port });
331+
}
332+
/// Sets the Tor Onion v2 socket address in this set, overwriting any previous Tor Onion v2
333+
/// socket address (if any).
334+
pub fn set_onion_v2(&mut self, addr: [u8; 10], port: u16) {
335+
self.onion2 = Some(NetAddress::OnionV2 { addr, port });
336+
}
337+
/// Sets the Tor Onion v3 socket address in this set, overwriting any previous Tor Onion v3
338+
/// socket address (if any).
339+
pub fn set_onion_v3(&mut self, ed25519_pubkey: [u8; 32], checksum: u16, version: u8, port: u16) {
340+
self.onion3 = Some(NetAddress::OnionV3 { ed25519_pubkey, checksum, version, port });
341+
}
342+
343+
pub(crate) fn to_vec(mut self) -> Vec<NetAddress> {
344+
let mut res = Vec::new();
345+
if let Some(addr) = self.v4.take() {
346+
res.push(addr);
347+
}
348+
if let Some(addr) = self.v6.take() {
349+
res.push(addr);
350+
}
351+
if let Some(addr) = self.onion2.take() {
352+
res.push(addr);
353+
}
354+
if let Some(addr) = self.onion3.take() {
355+
res.push(addr);
356+
}
357+
res
358+
}
359+
}
360+
309361
impl Writeable for NetAddress {
310362
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
311363
match self {

0 commit comments

Comments
 (0)