1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
|
/* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only */
/* Copyright (c) 2017-2024 Brett Sheffield <bacs@librecast.net> */
#include "testnet.h"
#include <librecast/crypto.h>
#include <librecast/net.h>
#include <assert.h>
#include <pthread.h>
#include <fcntl.h>
#include <stdio.h>
#include <semaphore.h>
#include <unistd.h>
#include <sys/param.h>
#undef TESTDEBUG
#ifdef TESTDEBUG
# define PKTS 10
# define PKTSZ 4
#else
# define PKTS 20
# define PKTSZ 1024
#endif
#define WAITS 20
#ifdef HAVE_LIBSODIUM
#ifdef HAVE_LIBLCRQ
enum {
TID_SEND,
TID_RECV
};
static const size_t SZ = PKTS * PKTSZ + 5;
static char channel_name[] = "0000-0040";
static sem_t receiver_ready, timeout;
static int fec;
static unsigned char key[crypto_secretbox_KEYBYTES];
#ifdef TESTDEBUG
#if 0
static void dumppkt(uint8_t *pkt, int haseq)
{
uint16_t seq;
uint8_t *dat = pkt;
if (haseq) {
dat += sizeof seq;
seq = ntohs(*(uint16_t *)pkt);
fprintf(stderr, "%u: ", seq);
}
else fprintf(stderr, " ");
for (size_t z = 0; z < PKTSZ * 8; z++) {
if (isset(dat, z)) putc('1', stderr);
else putc('0', stderr);
}
putc('\n', stderr);
}
#endif
static void dump_bufs(uint8_t buf[2][SZ])
{
for (size_t p = 0; p < PKTS; p++) {
fprintf(stderr, "%zu: ", p);
for (int i = 0; i < 2; i++) {
for (size_t z = 0; z < PKTSZ * 8; z++) {
uint8_t *b = buf[i];
if (isset(b, p * PKTSZ * 8 + z)) putc('1', stderr);
else putc('0', stderr);
}
putc(' ', stderr);
}
putc('\n', stderr);
}
putc('\n', stderr);
}
#endif
static void generate_source_data(uint8_t *buf, size_t sz)
{
ssize_t rc;
int f;
f = open("/dev/urandom", O_RDONLY);
if (f == -1) return;
rc = read(f, buf, sz);
test_assert(rc == (ssize_t)sz, "%zi random bytes read", rc);
close(f);
}
static void *recv_data_fec(void *arg)
{
uint8_t *buf = (uint8_t *)arg;
ssize_t rc;
lc_ctx_t *lctx = lc_ctx_new();
lc_socket_t * sock = lc_socket_new(lctx);
lc_channel_t * chan = lc_channel_new(lctx, channel_name);
memset(buf, 0, SZ); /* clear receive buffer */
lc_channel_bind(sock, chan);
lc_channel_join(chan);
lc_channel_set_sym_key(chan, key, crypto_secretbox_KEYBYTES);
lc_channel_coding_set(chan, LC_CODE_SYMM | LC_CODE_FEC_RQ);
sem_post(&receiver_ready);
rc = lc_channel_recv(chan, buf, SZ, 0);
test_log("lc_channel_recv returned %zi\n", rc);
test_assert(rc >= (ssize_t)SZ, "data received %zi/%zu bytes", rc, SZ);
lc_channel_part(chan);
lc_ctx_free(lctx);
sem_post(&timeout);
return arg;
}
static void *send_data_fec(void *arg)
{
uint8_t *buf = (uint8_t *)arg;
lc_ctx_t *lctx;
lc_socket_t *sock;
lc_channel_t *chan;
rq_t *rq;
ssize_t rc;
size_t byt = 0;
lctx = lc_ctx_new();
pthread_cleanup_push((void (*)(void *))lc_ctx_free, lctx);
sock = lc_socket_new(lctx);
chan = lc_channel_new(lctx, channel_name);
lc_socket_loop(sock, 1);
lc_channel_bind(sock, chan);
lc_channel_set_sym_key(chan, key, crypto_secretbox_KEYBYTES);
lc_channel_coding_set(chan, LC_CODE_SYMM | LC_CODE_FEC_RQ);
test_log("sending data with FEC + symmetric encryption\n");
sem_wait(&receiver_ready);
rc = lc_channel_send(chan, buf, SZ, 0);
test_assert(rc > 0, "lc_channel_send encoded data, returned %zi", rc);
if (rc == -1) goto err_ctx_free;
byt = rc;
rq = lc_channel_rq(chan);
/* Send packets. Drop packet 3 */
int pkts = rq_KP(rq) + RQ_OVERHEAD * 2;
test_log("sending %u packets\n", pkts);
for (int i = 1; i < pkts; i++) {
int flags = (i == 3) ? MSG_DROP : 0;
rc = lc_channel_send(chan, NULL, 0, flags);
test_assert(rc > 0, "lc_channel_send encoded data, returned %zi", rc);
if (rc > 0) byt += rc;
}
test_assert(byt >= SZ, "data sent %zi bytes (buffer = %zu)", byt, SZ);
err_ctx_free:
pthread_cleanup_pop(1); /* lc_ctx_free(lctx); */
return arg;
}
#endif
#endif
int main(void)
{
#if !defined(HAVE_LIBLCRQ) || !(HAVE_LIBSODIUM)
return test_skip("lc_channel_send() / lc_socket_recv() - FEC + encrypt");
#else
pthread_t tid[2];
uint8_t buf[2][SZ];
struct timespec ts;
test_name("lc_channel_send() / lc_socket_recv() - FEC + encrypt");
test_require_net(TEST_NET_BASIC);
/* run test twice */
for (fec = 0; fec < 2; fec++) {
/* generate symmetric encryption key */
crypto_secretbox_keygen(key);
/* clear buffers */
memset(buf[0], 0, SZ);
memset(buf[1], 0, SZ);
sem_init(&timeout, 0, 0);
sem_init(&receiver_ready, 0, 0);
generate_source_data(buf[TID_SEND], SZ);
test_assert(memcmp(buf[TID_RECV], buf[TID_SEND], SZ), "buffer differ before sync");
pthread_create(&tid[TID_SEND], NULL, &send_data_fec, buf[TID_SEND]);
pthread_create(&tid[TID_RECV], NULL, &recv_data_fec, buf[TID_RECV]);
clock_gettime(CLOCK_REALTIME, &ts);
ts.tv_sec += WAITS;
test_assert(!sem_timedwait(&timeout, &ts), "timeout");
pthread_cancel(tid[TID_RECV]);
pthread_join(tid[TID_RECV], NULL);
pthread_cancel(tid[TID_SEND]);
pthread_join(tid[TID_SEND], NULL);
sem_destroy(&receiver_ready);
sem_destroy(&timeout);
test_assert(!memcmp(buf[TID_RECV], buf[TID_SEND], SZ), "send buffer matches received");
#ifdef TESTDEBUG
dump_bufs(buf);
#endif
}
return test_status;
#endif
}
|