@@ -304,6 +304,58 @@ impl NetAddress {
304304 }
305305}
306306
307+ /// A "set" of addresses which enforces that there can be only up to one of each net address type.
308+ pub struct NetAddressSet {
309+ v4 : Option < NetAddress > ,
310+ v6 : Option < NetAddress > ,
311+ onion2 : Option < NetAddress > ,
312+ onion3 : Option < NetAddress > ,
313+ }
314+ impl NetAddressSet {
315+ /// Creates a new, empty, NetAddressSet
316+ pub fn new ( ) -> Self {
317+ NetAddressSet { v4 : None , v6 : None , onion2 : None , onion3 : None }
318+ }
319+
320+ /// Sets the IPv4 socket address in this set, overwriting any previous IPv4 socket addresses
321+ /// (if any).
322+ pub fn set_v4 ( & mut self , addr : [ u8 ; 4 ] , port : u16 ) {
323+ self . v4 = Some ( NetAddress :: IPv4 { addr, port } ) ;
324+ }
325+ /// Sets the IPv6 socket address in this set, overwriting any previous IPv4 socket addresses
326+ /// (if any).
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 IPv4 socket
331+ /// address (if any).
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 IPv4 socket
336+ /// address (if any).
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 to_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+
307359impl Writeable for NetAddress {
308360 fn write < W : Writer > ( & self , writer : & mut W ) -> Result < ( ) , :: std:: io:: Error > {
309361 match self {
0 commit comments