LCOV - code coverage report
Current view: top level - flamenco/gossip - fd_gossip_spy.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 149 0.0 %
Date: 2024-11-13 11:58:15 Functions: 0 7 0.0 %

          Line data    Source code
       1             : /**
       2             : 
       3             :    export RUST_LOG=solana_gossip=TRACE
       4             :    cargo run --bin solana-test-validator
       5             : 
       6             :  **/
       7             : 
       8             : #define _GNU_SOURCE         /* See feature_test_macros(7) */
       9             : 
      10             : #include "fd_gossip.h"
      11             : #include "../../util/fd_util.h"
      12             : #include "../../ballet/base58/fd_base58.h"
      13             : #include "../types/fd_types_yaml.h"
      14             : #include "../../util/net/fd_eth.h"
      15             : #include <stdio.h>
      16             : #include <unistd.h>
      17             : #include <signal.h>
      18             : #include <sys/socket.h>
      19             : #include <netinet/in.h>
      20             : #include <arpa/inet.h>
      21             : #include <sys/random.h>
      22             : #include <errno.h>
      23             : #include <netdb.h>
      24             : #include <stdlib.h>
      25             : 
      26           0 : static void print_data(fd_crds_data_t* data, void* arg) {
      27           0 :   fd_flamenco_yaml_t * yamldump = (fd_flamenco_yaml_t *)arg;
      28           0 :   FILE * dumpfile = (FILE *)fd_flamenco_yaml_file(yamldump);
      29           0 :   fd_crds_data_walk(yamldump, data, fd_flamenco_yaml_walk, NULL, 1U);
      30             : 
      31           0 :   if (data->discriminant == fd_crds_data_enum_vote) {
      32           0 :     fd_gossip_vote_t * v = &data->inner.vote;
      33           0 :     fd_txn_t * txn = v->txn.txn;
      34           0 :     for ( ushort i = 0; i < txn->instr_cnt; ++i ) {
      35           0 :       fd_txn_instr_t const * txn_instr = &txn->instr[i];
      36           0 :       uchar * data = v->txn.raw + txn_instr->data_off;
      37           0 :       ushort data_sz = txn_instr->data_sz;
      38           0 :       fd_bincode_decode_ctx_t decode_ctx;
      39           0 :       decode_ctx.data    = data;
      40           0 :       decode_ctx.dataend = data + data_sz;
      41           0 :       decode_ctx.valloc  = fd_libc_alloc_virtual();
      42           0 :       fd_vote_instruction_t vinstruction;
      43           0 :       int rc = fd_vote_instruction_decode( &vinstruction, &decode_ctx );
      44           0 :       if ( rc || decode_ctx.data != decode_ctx.dataend ) {
      45           0 :         FD_LOG_WARNING(("failed to decode vote instruction"));
      46           0 :       } else {
      47           0 :         fd_vote_instruction_walk(yamldump, &vinstruction, fd_flamenco_yaml_walk, NULL, 1U);
      48           0 :       }
      49           0 :     }
      50           0 :   }
      51             : 
      52           0 :   fflush(dumpfile);
      53           0 : }
      54             : 
      55             : // SIGINT signal handler
      56             : volatile int stopflag = 0;
      57           0 : static void stop(int sig) { (void)sig; stopflag = 1; }
      58             : 
      59             : static int sockfd = -1;
      60             : 
      61             : /* Convert my style of address to UNIX style */
      62             : static int
      63           0 : gossip_to_sockaddr( uchar * dst, fd_gossip_peer_addr_t const * src ) {
      64           0 :   fd_memset(dst, 0, sizeof(struct sockaddr_in));
      65           0 :   struct sockaddr_in * t = (struct sockaddr_in *)dst;
      66           0 :   t->sin_family = AF_INET;
      67           0 :   t->sin_addr.s_addr = src->addr;
      68           0 :   t->sin_port = src->port;
      69           0 :   return sizeof(struct sockaddr_in);
      70           0 : }
      71             : 
      72             : /* Convert my style of address from UNIX style */
      73             : static int
      74           0 : gossip_from_sockaddr( fd_gossip_peer_addr_t * dst, uchar const * src ) {
      75           0 :   FD_STATIC_ASSERT(sizeof(fd_gossip_peer_addr_t) == sizeof(ulong),"messed up size");
      76           0 :   dst->l = 0;
      77           0 :   const struct sockaddr_in * sa = (const struct sockaddr_in *)src;
      78           0 :   dst->addr = sa->sin_addr.s_addr;
      79           0 :   dst->port = sa->sin_port;
      80           0 :   return 0;
      81           0 : }
      82             : 
      83             : static void
      84           0 : send_packet( uchar const * data, size_t sz, fd_gossip_peer_addr_t const * addr, void * arg ) {
      85           0 :   (void)arg;
      86           0 :   uchar saddr[sizeof(struct sockaddr_in)];
      87           0 :   int saddrlen = gossip_to_sockaddr(saddr, addr);
      88           0 :   if ( sendto(sockfd, data, sz, MSG_DONTWAIT,
      89           0 :               (const struct sockaddr *)saddr, (socklen_t)saddrlen) < 0 ) {
      90           0 :     FD_LOG_WARNING(("sendto failed: %s", strerror(errno)));
      91           0 :   }
      92           0 : }
      93             : 
      94             : static int
      95           0 : main_loop( fd_gossip_t * glob, fd_gossip_config_t * config, volatile int * stopflag ) {
      96           0 :   int fd;
      97           0 :   if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
      98           0 :     FD_LOG_ERR(("socket failed: %s", strerror(errno)));
      99           0 :     return -1;
     100           0 :   }
     101           0 :   sockfd = fd;
     102           0 :   int optval = 1<<20;
     103           0 :   if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char *)&optval, sizeof(int)) < 0) {
     104           0 :     FD_LOG_ERR(("setsocketopt failed: %s", strerror(errno)));
     105           0 :     return -1;
     106           0 :   }
     107           0 :   if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (char *)&optval, sizeof(int)) < 0) {
     108           0 :     FD_LOG_ERR(("setsocketopt failed: %s", strerror(errno)));
     109           0 :     return -1;
     110           0 :   }
     111           0 :   uchar saddr[sizeof(struct sockaddr_in6)];
     112           0 :   int saddrlen = gossip_to_sockaddr(saddr, &config->my_addr);
     113           0 :   if (saddrlen < 0 || bind(fd, (struct sockaddr*)saddr, (uint)saddrlen) < 0) {
     114           0 :     FD_LOG_ERR(("bind failed: %s", strerror(errno)));
     115           0 :     return -1;
     116           0 :   }
     117           0 :   if( getsockname( fd, (struct sockaddr *)saddr, (uint*)&saddrlen ) < 0 ) {
     118           0 :     FD_LOG_ERR( ( "getsockname failed: %s", strerror( errno ) ) );
     119           0 :     return -1;
     120           0 :   }
     121           0 :   gossip_from_sockaddr( &config->my_addr, saddr );
     122           0 :   fd_gossip_update_addr( glob, &config->my_addr );
     123             : 
     124           0 :   fd_gossip_settime(glob, fd_log_wallclock());
     125           0 :   fd_gossip_start(glob);
     126             : 
     127           0 : #define VLEN 32U
     128           0 :   struct mmsghdr msgs[VLEN];
     129           0 :   struct iovec iovecs[VLEN];
     130           0 :   uchar bufs[VLEN][FD_ETH_PAYLOAD_MAX];
     131           0 :   uchar sockaddrs[VLEN][sizeof(struct sockaddr_in6)]; /* sockaddr is smaller than sockaddr_in6 */
     132             : 
     133           0 :   while ( !*stopflag ) {
     134           0 :     fd_gossip_settime(glob, fd_log_wallclock());
     135           0 :     fd_gossip_continue(glob);
     136             : 
     137           0 :     fd_memset(msgs, 0, sizeof(msgs));
     138           0 :     for (uint i = 0; i < VLEN; i++) {
     139           0 :       iovecs[i].iov_base          = bufs[i];
     140           0 :       iovecs[i].iov_len           = FD_ETH_PAYLOAD_MAX;
     141           0 :       msgs[i].msg_hdr.msg_iov     = &iovecs[i];
     142           0 :       msgs[i].msg_hdr.msg_iovlen  = 1;
     143           0 :       msgs[i].msg_hdr.msg_name    = sockaddrs[i];
     144           0 :       msgs[i].msg_hdr.msg_namelen = sizeof(struct sockaddr_in6);
     145           0 :     }
     146             : 
     147             :     /* Read more packets */
     148           0 :     long retval = recvmmsg(fd, msgs, VLEN, MSG_DONTWAIT, NULL);
     149           0 :     if (retval < 0) {
     150           0 :       if (errno == EINTR || errno == EWOULDBLOCK)
     151           0 :         continue;
     152           0 :       FD_LOG_ERR(("recvmmsg failed: %s", strerror(errno)));
     153           0 :       return -1;
     154           0 :     }
     155           0 :     if (retval == 0)
     156           0 :       continue;
     157             : 
     158           0 :     for (uint i = 0; i < (uint)retval; ++i) {
     159           0 :       fd_gossip_peer_addr_t from;
     160           0 :       gossip_from_sockaddr( &from, msgs[i].msg_hdr.msg_name );
     161           0 :       fd_gossip_recv_packet(glob, bufs[i], (ulong)msgs[i].msg_len, &from);
     162           0 :     }
     163           0 :   }
     164             : 
     165           0 :   close(fd);
     166           0 :   return 0;
     167           0 : }
     168             : 
     169             : /* Convert a host:port string to a gossip network address. If host is
     170             :  * missing, it assumes the local hostname. */
     171             : static fd_gossip_peer_addr_t *
     172           0 : resolve_hostport(const char* str /* host:port */, fd_gossip_peer_addr_t * res) {
     173           0 :   fd_memset(res, 0, sizeof(fd_gossip_peer_addr_t));
     174             : 
     175             :   /* Find the : and copy out the host */
     176           0 :   char buf[128];
     177           0 :   uint i;
     178           0 :   for (i = 0; ; ++i) {
     179           0 :     if (str[i] == '\0' || i > sizeof(buf)-1U) {
     180           0 :       FD_LOG_ERR(("missing colon"));
     181           0 :       return NULL;
     182           0 :     }
     183           0 :     if (str[i] == ':') {
     184           0 :       buf[i] = '\0';
     185           0 :       break;
     186           0 :     }
     187           0 :     buf[i] = str[i];
     188           0 :   }
     189           0 :   if (i == 0)
     190             :     /* :port means $HOST:port */
     191           0 :     gethostname(buf, sizeof(buf));
     192             : 
     193           0 :   struct hostent * host = gethostbyname( buf );
     194           0 :   if (host == NULL) {
     195           0 :     FD_LOG_WARNING(("unable to resolve host %s", buf));
     196           0 :     return NULL;
     197           0 :   }
     198             :   /* Convert result to gossip address */
     199           0 :   res->l = 0;
     200           0 :   res->addr = ((struct in_addr *)host->h_addr)->s_addr;
     201           0 :   int port = atoi(str + i + 1);
     202           0 :   if ((port > 0 && port < 1024) || port > (int)USHORT_MAX) {
     203           0 :     FD_LOG_ERR(("invalid port number"));
     204           0 :     return NULL;
     205           0 :   }
     206           0 :   res->port = htons((ushort)port);
     207             : 
     208           0 :   return res;
     209           0 : }
     210             : 
     211             : int
     212             : main( int     argc,
     213             :       char ** argv ) {
     214             :   fd_boot( &argc, &argv );
     215             : 
     216             :   fd_valloc_t valloc = fd_libc_alloc_virtual();
     217             : 
     218             :   fd_gossip_config_t config;
     219             :   fd_memset(&config, 0, sizeof(config));
     220             : 
     221             :   uchar private_key[32];
     222             :   FD_TEST( 32UL==getrandom( private_key, 32UL, 0 ) );
     223             :   fd_sha512_t sha[1];
     224             :   fd_pubkey_t public_key;
     225             :   FD_TEST( fd_ed25519_public_from_private( public_key.uc, private_key, sha ) );
     226             : 
     227             :   config.private_key = private_key;
     228             :   config.public_key = &public_key;
     229             : 
     230             :   char hostname[64];
     231             :   gethostname(hostname, sizeof(hostname));
     232             : 
     233             :   FD_TEST( resolve_hostport(":0", &config.my_addr) );
     234             : 
     235             :   config.shred_version = 4274;
     236             : 
     237             :   fd_flamenco_yaml_t * yamldump =
     238             :     fd_flamenco_yaml_init( fd_flamenco_yaml_new(
     239             :       fd_valloc_malloc( valloc, fd_flamenco_yaml_align(), fd_flamenco_yaml_footprint() ) ),
     240             :       stdout );
     241             :   config.deliver_fun = print_data;
     242             :   config.deliver_arg = yamldump;
     243             :   config.send_fun    = send_packet;
     244             :   config.send_arg    = NULL;
     245             : 
     246             :   ulong seed = fd_hash(0, hostname, strnlen(hostname, sizeof(hostname)));
     247             : 
     248             :   void * shm = fd_valloc_malloc(valloc, fd_gossip_align(), fd_gossip_footprint());
     249             :   fd_gossip_t * glob = fd_gossip_join(fd_gossip_new(shm, seed));
     250             : 
     251             :   if ( fd_gossip_set_config(glob, &config) )
     252             :     return 1;
     253             : 
     254             :   fd_gossip_peer_addr_t peeraddr;
     255             :   // if ( fd_gossip_add_active_peer(glob, resolve_hostport("entrypoint.mainnet-beta.solana.com:8001", &peeraddr)) )
     256             :   // return 1;
     257             :   // if ( fd_gossip_add_active_peer(glob, resolve_hostport("entrypoint2.mainnet-beta.solana.com:8001", &peeraddr)) )
     258             :   // return 1;
     259             :   // if ( fd_gossip_add_active_peer(glob, resolve_hostport("entrypoint3.mainnet-beta.solana.com:8001", &peeraddr)) )
     260             :   // return 1;
     261             :   // if ( fd_gossip_add_active_peer(glob, resolve_hostport("entrypoint4.mainnet-beta.solana.com:8001", &peeraddr)) )
     262             :   // return 1;
     263             :   // if ( fd_gossip_add_active_peer(glob, resolve_hostport("entrypoint5.mainnet-beta.solana.com:8001", &peeraddr)) )
     264             :   // return 1;
     265             :   // if ( fd_gossip_add_active_peer(glob, resolve_hostport("entrypoint.testnet.solana.com:8001", &peeraddr)) )
     266             :   //   return 1;
     267             :   // if ( fd_gossip_add_active_peer(glob, resolve_hostport("entrypoint2.testnet.solana.com:8001", &peeraddr)) )
     268             :   //   return 1;
     269             :   // if ( fd_gossip_add_active_peer(glob, resolve_hostport("entrypoint3.testnet.solana.com:8001", &peeraddr)) )
     270             :   //   return 1;
     271             :   if ( fd_gossip_add_active_peer(glob, resolve_hostport(":1024", &peeraddr)) )
     272             :   return 1;
     273             : 
     274             :   signal(SIGINT, stop);
     275             :   signal(SIGPIPE, SIG_IGN);
     276             : 
     277             :   if ( main_loop(glob, &config, &stopflag) )
     278             :     return 1;
     279             : 
     280             :   fd_valloc_free(valloc, fd_flamenco_yaml_delete(yamldump));
     281             : 
     282             :   fd_valloc_free(valloc, fd_gossip_delete(fd_gossip_leave(glob)));
     283             : 
     284             :   fd_halt();
     285             :   return 0;
     286             : }

Generated by: LCOV version 1.14