Commit a6e111ae authored by Pavel Tvrdik's avatar Pavel Tvrdik
Browse files

Socktest: Minor change in code style

Move printing out received packet into own function
parent 2bd35b06
Loading
Loading
Loading
Loading
+41 −17
Original line number Diff line number Diff line
#include "common.h"

int
rcv_hook(sock *sk, int size)
/*
 * Print out:
 *  <src>:<port> -> <dst> ifa(<id>) <name>: pkt <value>/<count>, ttl <ttl>
 */
static void
rcv_print(sock *sk, struct my_packet *pkt)
{
  struct my_packet *raw;
  char ifa_name[IF_NAMESIZE];
  char buf[1024];
  char *pos;
  int unused_size;

  if (!if_indextoname(sk->lifindex, ifa_name))
  {
    perror("if_indextoname");
    snprintf(ifa_name, sizeof(ifa_name), "???");
  }

  bsnprintf(buf, sizeof(buf), "%I", sk->faddr);
  pos = buf + strlen(buf);
  unused_size = sizeof(buf) - (pos - buf);

  if (sk->type != SK_IP)
    bsnprintf(pos, unused_size, ":%u", sk->fport);
  pos = buf + strlen(buf);
  unused_size = sizeof(buf) - (pos - buf);

  bsnprintf(pos, unused_size, " -> %I ifa(%u) %s: ", sk->laddr, sk->lifindex, ifa_name);
  pos = buf + strlen(buf);
  unused_size = sizeof(buf) - (pos - buf);

  if (pkt->magic == (u32)PKT_MAGIC)
    bsnprintf(pos, unused_size, "pkt %d/%d, ttl %d", pkt->value, pkt->count, sk->rcv_ttl);
  else
    bsnprintf(pos, unused_size, "magic value does not pass: recv %u, expected %u", pkt->magic, (u32)PKT_MAGIC);

  printf("%s\n", buf);
}

static int
rcv_hook(sock *sk, int size)
{
  struct my_packet *raw;

  if (cf_count && ++counter > cf_count)
    exit(0);
@@ -27,20 +64,7 @@ rcv_hook(sock *sk, int size)
      .count = ntohl(raw->count),
  };

  if (!if_indextoname(sk->lifindex, ifa_name))
  {
    perror("if_indextoname");
    snprintf(ifa_name, sizeof(ifa_name), "???");
  }

  bsnprintf(buf, sizeof(buf), "%I:%u -> %I ifa(%u) %s: ", sk->faddr, sk->fport, sk->laddr, sk->lifindex, ifa_name);
  char *pos = buf + strlen(buf);
  if (pkt.magic == (u32)PKT_MAGIC)
    bsnprintf(pos, pos-buf, "pkt %d/%d, ttl %d", pkt.value, pkt.count, sk->rcv_ttl);
  else
    bsnprintf(pos, pos-buf, "magic value does not pass: recv %u, expected %u", pkt.magic, (u32)PKT_MAGIC);

  printf("%s\n", buf);
  rcv_print(sk, &pkt);

  /* Clear receive buffer */
  return 1;