LCOV - code coverage report
Current view: top level - discof/restore/utils - fd_sspeer.h (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 16 16 100.0 %
Date: 2026-06-29 05:51:35 Functions: 5 12 41.7 %

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

Generated by: LCOV version 1.14