MINOR: dev/udp: Apply the corruption to both directions

Harden the UDP datagram corruption applying it on both sides. This approaches
the conditions of some tests run by the QUIC interop runner developed by
Marten Seeman.
This commit is contained in:
Frédéric Lécaille 2022-09-08 20:38:59 +02:00
parent 3dd79d378c
commit 192093b581
1 changed files with 15 additions and 7 deletions

View File

@ -275,6 +275,18 @@ int add_connection(struct sockaddr_storage *ss)
return -1; return -1;
} }
/* Corrupt <buf> buffer with <buflen> as length if required */
static void pktbuf_apply_corruption(char *buf, size_t buflen)
{
if (corr_rate > 0 && prng(100) < corr_rate) {
unsigned int rnd = prng(corr_span * 256); // pos and value
unsigned int pos = corr_base + (rnd >> 8);
if (pos < buflen)
buf[pos] ^= rnd;
}
}
/* Handle a read operation on an front FD. Will either reuse the existing /* Handle a read operation on an front FD. Will either reuse the existing
* connection if the source is found, or will allocate a new one, possibly * connection if the source is found, or will allocate a new one, possibly
* replacing the oldest one. Returns <0 on error or the number of bytes * replacing the oldest one. Returns <0 on error or the number of bytes
@ -323,13 +335,7 @@ int handle_frt(int fd, struct pollfd *pfd, struct conn *conns, int nbconn)
if (ret < 0) if (ret < 0)
return errno == EAGAIN ? 0 : -1; return errno == EAGAIN ? 0 : -1;
if (corr_rate > 0 && prng(100) < corr_rate) { pktbuf_apply_corruption(pktbuf, ret);
unsigned int rnd = prng(corr_span * 256); // pos and value
unsigned int pos = corr_base + (rnd >> 8);
if (pos < ret)
pktbuf[pos] ^= rnd;
}
conn = NULL; conn = NULL;
for (i = 0; i < nbconn; i++) { for (i = 0; i < nbconn; i++) {
@ -411,6 +417,8 @@ int handle_bck(int fd, struct pollfd *pfd, struct conn *conns, int nbconn)
if (ret < 0) if (ret < 0)
return errno == EAGAIN ? 0 : -1; return errno == EAGAIN ? 0 : -1;
pktbuf_apply_corruption(pktbuf, ret);
conn = conn_bck_lookup(conns, nbconn, fd); conn = conn_bck_lookup(conns, nbconn, fd);
if (!conn) if (!conn)
return 0; return 0;