diff --git a/socktest/rcv.c b/socktest/rcv.c index c0c04d63..ce9952fd 100644 --- a/socktest/rcv.c +++ b/socktest/rcv.c @@ -1,11 +1,48 @@ #include "common.h" -int +/* + * Print out: + * : -> ifa() : pkt /, ttl + */ +static void +rcv_print(sock *sk, struct my_packet *pkt) +{ + 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; - char ifa_name[IF_NAMESIZE]; - char buf[1024]; 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;