0
0
mirror of https://gitlab.nic.cz/labs/bird.git synced 2025-01-03 15:41:54 +00:00
bird/socktest/rcv.c
2016-04-06 15:57:26 +02:00

92 lines
1.9 KiB
C

#include "common.h"
/*
* Print out:
* <src>:<port> -> <dst> ifa(<id>) <name>: pkt <value>/<count>, ttl <ttl>
*/
static void
rcv_print(sock *sk, struct socktest_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 socktest_packet *raw;
if (cf_count && ++counter > cf_count)
exit(0);
if (sk->type == SK_IP)
raw = (void *) sk_rx_buffer(sk, &size);
else
raw = (void *) sk->rbuf;
if (size != sizeof(struct socktest_packet))
{
printf("Received a packet with unexpected length of %d bytes \n", size);
return 1;
}
struct socktest_packet pkt = {
.magic = ntohl(raw->magic),
.value = ntohl(raw->value),
.count = ntohl(raw->count),
};
rcv_print(sk, &pkt);
/* Clear receive buffer */
return 1;
}
int
main(int argc, char *argv[])
{
socktest_bird_init();
sock *s = socktest_parse_args(argc, argv, 0);
s->rx_hook = rcv_hook;
s->rbsize = 1500;
s->flags |= SKF_LADDR_RX | SKF_TTL_RX;
socktest_open(s);
while (1)
{
sk_read(s);
usleep(20000);
}
}