Line data Source code
1 : #ifndef HEADER_fd_src_discof_restore_utils_fd_sspeer_h 2 : #define HEADER_fd_src_discof_restore_utils_fd_sspeer_h 3 : 4 : #include "../../../util/fd_util_base.h" 5 : #include "../../../util/net/fd_net_headers.h" 6 : #include "../../../waltz/fd_fqdn.h" 7 : #include "../../../flamenco/fd_flamenco_base.h" 8 : 9 : struct fd_sspeer_key { 10 : union { 11 : fd_pubkey_t pubkey[ 1 ]; /* gossip peers: key by pubkey. */ 12 : struct { /* HTTP server peers: key by hostname + addr. */ 13 : char hostname[ FD_FQDN_BUF_MAX ]; 14 : fd_ip4_port_t resolved_addr; /* disambiguates multiple IPs for same hostname. */ 15 : } url; 16 : }; 17 : int is_url; 18 : }; 19 : 20 : typedef struct fd_sspeer_key fd_sspeer_key_t; 21 : 22 : FD_FN_UNUSED static inline int 23 : fd_sspeer_key_eq( fd_sspeer_key_t const * k0, 24 732 : fd_sspeer_key_t const * k1 ) { 25 732 : if( k0->is_url!=k1->is_url ) return 0; 26 666 : if( k0->is_url ) { 27 288 : return !strncmp( k0->url.hostname, k1->url.hostname, sizeof(k0->url.hostname) ) 28 288 : && k0->url.resolved_addr.l==k1->url.resolved_addr.l; 29 288 : } 30 378 : return !memcmp( k0->pubkey, k1->pubkey, FD_PUBKEY_FOOTPRINT ); 31 666 : } 32 : 33 : FD_FN_UNUSED static inline ulong 34 : fd_sspeer_key_hash( fd_sspeer_key_t const * key, 35 2871 : ulong seed ) { 36 2871 : if( key->is_url ) { 37 : /* Use strnlen in case the string is not properly \0 terminated. 38 : Ideally, one would prefer sizeof(key->url.hostname) but that 39 : requires guaranteed zero-padding. */ 40 1275 : ulong h = fd_hash( seed, key->url.hostname, strnlen( key->url.hostname, sizeof(key->url.hostname) ) ); 41 : /* fd_ip4_port_t is not a complete 64bit ulong, therefore compose 42 : the word from its parts to avoid random unused bytes. */ 43 1275 : ulong a = (ulong)key->url.resolved_addr.addr | ( ((ulong)key->url.resolved_addr.port) << 32 ); 44 : /* Chaining "a" through fd_hash would give better avalanche 45 : properties, but it is probably overkill for a chain hash map. */ 46 1275 : return h ^ a; 47 1275 : } 48 1596 : return fd_hash( seed, key->pubkey, FD_PUBKEY_FOOTPRINT ); 49 2871 : } 50 : 51 : #endif /* HEADER_fd_src_discof_restore_utils_fd_sspeer_h */