LCOV - code coverage report
Current view: top level - app/fdctl - spy.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 176 0.0 %
Date: 2025-01-08 12:08:44 Functions: 0 8 0.0 %

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

Generated by: LCOV version 1.14