Skip to content

Commit

Permalink
WIP progress on QUIC
Browse files Browse the repository at this point in the history
  • Loading branch information
cyanreg committed Mar 30, 2024
1 parent 33a0385 commit 1d0207a
Show file tree
Hide file tree
Showing 11 changed files with 369 additions and 195 deletions.
17 changes: 17 additions & 0 deletions libavtransport/address.c
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,15 @@ static int parse_settings(void *log_ctx, AVTAddress *addr,
addr->opts.tx_buf = res;
else if (!strcmp(key, "rx_buf"))
addr->opts.rx_buf = res;
} else if (!strcmp(key, "certfile") || !strcmp(key, "keyfile")) {
char *dupd = strdup(val);
if (!dupd)
return AVT_ERROR(ENOMEM);

if (!strcmp(key, "certfile"))
addr->opts.certfile = dupd;
else if (!strcmp(key, "keyfile"))
addr->opts.keyfile = dupd;
} else {
avt_log(log_ctx, AVT_LOG_ERROR, "Option %s not recognized!\n", key);
return AVT_ERROR(EINVAL);
Expand All @@ -335,6 +344,12 @@ static int parse_settings(void *log_ctx, AVTAddress *addr,
snprintf(&opts_buf[strlen(opts_buf)], opts_buf_size - strlen(opts_buf),
"\n");
}
if (addr->opts.certfile)
snprintf(&opts_buf[strlen(opts_buf)], opts_buf_size - strlen(opts_buf),
" Certificate: %s\n", addr->opts.certfile);
if (addr->opts.keyfile)
snprintf(&opts_buf[strlen(opts_buf)], opts_buf_size - strlen(opts_buf),
" Private key: %s\n", addr->opts.keyfile);

return 0;
}
Expand Down Expand Up @@ -636,6 +651,8 @@ int avt_addr_from_info(void *log_ctx, AVTAddress *addr, AVTConnectionInfo *info)

void avt_addr_free(AVTAddress *addr)
{
free(addr->opts.certfile);
free(addr->opts.keyfile);
free(addr->opts.default_sid);
free(addr->path);
free(addr->interface);
Expand Down
4 changes: 4 additions & 0 deletions libavtransport/address.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ typedef struct AVTAddress {
int nb_default_sid;

int64_t start_time; // 1ns timebase

/* For QUIC */
char *certfile;
char *keyfile;
} opts;

/** CALLBACKS **/
Expand Down
87 changes: 52 additions & 35 deletions libavtransport/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,16 @@

#include "connection_internal.h"
#include "protocol_common.h"
#include "io_common.h"
#include "utils_internal.h"
#include "scheduler.h"

struct AVTConnection {
AVTAddress addr;
AVTContext *ctx;

/* I/O */
const AVTIO *io;
AVTIOCtx *io_ctx;

/* Protocol */
const AVTProtocol *p;
Expand All @@ -43,50 +47,81 @@ struct AVTConnection {
const AVTProtocol *mirror;
AVTProtocolCtx *mirror_ctx;

/* Input reorder buffer */
AVTReorderBuffer in_buffer;
/* Input buffer */
AVTPacketFifo in_fifo;

/* Output FIFO, pre-scheduler */
AVTPacketFifo out_fifo_pre;
AVTPacketFifo out_fifo_post;
AVTScheduler out_scheduler;
};

int avt_connection_destroy(AVTConnection **_conn)
{
AVTConnection *conn = *_conn;
if (conn)
return 0;

int err = conn->p->close(&conn->p_ctx);

avt_pkt_fifo_free(&conn->out_fifo_post);
avt_scheduler_free(&conn->out_scheduler);
avt_pkt_fifo_free(&conn->out_fifo_pre);
avt_addr_free(&conn->addr);

if (conn->p_ctx)
conn->p->close(&conn->p_ctx);

if (conn->io_ctx)
conn->io->close(&conn->io_ctx);

free(conn);
*_conn = NULL;
return err;
}

int avt_connection_create(AVTContext *ctx, AVTConnection **_conn,
AVTConnectionInfo *info)
{
AVTAddress addr;
int ret = avt_addr_from_info(ctx, &addr, info);
if (ret < 0)
return ret;

int ret;
AVTConnection *conn = calloc(1, sizeof(*conn));
if (!conn)
return AVT_ERROR(ENOMEM);

conn->addr = addr;
conn->ctx = ctx;
/* Address parsing */
ret = avt_addr_from_info(ctx, &conn->addr, info);
if (ret < 0)
goto fail;

/* I/O init */
ret = avt_io_init(ctx, &conn->io, &conn->io_ctx, &conn->addr);
if (ret < 0)
goto fail;

/* Protocol init */
ret = avt_protocol_init(ctx, &conn->p, &conn->p_ctx, &addr);
if (ret < 0) {
free(conn);
return ret;
}
ret = avt_protocol_init(ctx, &conn->p, &conn->p_ctx, &conn->addr,
conn->io, conn->io_ctx);
if (ret < 0)
goto fail;

/* Get max packet size */
ret = conn->p->get_max_pkt_len(conn->p_ctx);
if (ret < 0)
return ret;
goto fail;

/* Output scheduler */
ret = avt_scheduler_init(&conn->out_scheduler, ret,
info->output_opts.buffer,
info->output_opts.bandwidth);
if (ret < 0)
return ret;
goto fail;

*_conn = conn;

return 0;
fail:
avt_connection_destroy(&conn);
return ret;
}

int avt_connection_send(AVTConnection *conn,
Expand Down Expand Up @@ -144,24 +179,6 @@ int avt_connection_flush(AVTConnection *conn, int64_t timeout)
return conn->p->flush(conn->p_ctx, timeout);
}

int avt_connection_destroy(AVTConnection **_conn)
{
AVTConnection *conn = *_conn;
if (conn)
return 0;

int err = conn->p->close(&conn->p_ctx);

avt_pkt_fifo_free(&conn->out_fifo_post);
avt_scheduler_free(&conn->out_scheduler);
avt_pkt_fifo_free(&conn->out_fifo_pre);
avt_addr_free(&conn->addr);

free(conn);
*_conn = NULL;
return err;
}

int avt_connection_mirror_open(AVTContext *ctx, AVTConnection *conn,
AVTConnectionInfo *info)
{
Expand Down
5 changes: 3 additions & 2 deletions libavtransport/protocol_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ static const AVTProtocol *avt_protocol_list[AVT_PROTOCOL_MAX] = {

/* For connections to call */
int avt_protocol_init(AVTContext *ctx, const AVTProtocol **_p,
AVTProtocolCtx **p_ctx, AVTAddress *addr)
AVTProtocolCtx **p_ctx, AVTAddress *addr,
const AVTIO *io, AVTIOCtx *io_ctx)
{
const AVTProtocol *p = avt_protocol_list[addr->proto];
if (!p) {
Expand All @@ -57,7 +58,7 @@ int avt_protocol_init(AVTContext *ctx, const AVTProtocol **_p,
return AVT_ERROR(ENOTSUP);
}

int err = p->init(ctx, p_ctx, addr);
int err = p->init(ctx, p_ctx, addr, io, io_ctx);
if (err >= 0)
*_p = p;

Expand Down
7 changes: 5 additions & 2 deletions libavtransport/protocol_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

#include "connection_internal.h"
#include "utils_internal.h"
#include "io_common.h"

/* High level interface */
typedef struct AVTProtocolCtx AVTProtocolCtx;
Expand All @@ -37,7 +38,8 @@ typedef struct AVTProtocol {
enum AVTProtocolType type;

/* Initialize a context */
int (*init)(AVTContext *ctx, AVTProtocolCtx **p, AVTAddress *addr);
int (*init)(AVTContext *ctx, AVTProtocolCtx **p, AVTAddress *addr,
const AVTIO *io, AVTIOCtx *io_ctx);

/* Attempt to add a secondary destination, NULL if unsupported */
int (*add_dst)(AVTProtocolCtx *p, AVTAddress *addr);
Expand Down Expand Up @@ -77,6 +79,7 @@ typedef struct AVTProtocol {
} AVTProtocol;

COLD int avt_protocol_init(AVTContext *ctx, const AVTProtocol **_p,
AVTProtocolCtx **p_ctx, AVTAddress *addr);
AVTProtocolCtx **p_ctx, AVTAddress *addr,
const AVTIO *io, AVTIOCtx *io_ctx);

#endif /* AVTRANSPORT_PROTOCOL_COMMON */
34 changes: 16 additions & 18 deletions libavtransport/protocol_datagram.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,26 @@ struct AVTProtocolCtx {
AVTIOCtx *io_ctx;
};

static COLD int datagram_proto_init(AVTContext *ctx, AVTProtocolCtx **p, AVTAddress *addr)
static COLD int datagram_proto_close(AVTProtocolCtx **p)
{
AVTProtocolCtx *priv = malloc(sizeof(*priv));
if (!priv)
AVTProtocolCtx *priv = *p;
free(priv);
*p = NULL;
return 0;
}

static COLD int datagram_proto_init(AVTContext *ctx, AVTProtocolCtx **_p, AVTAddress *addr,
const AVTIO *io, AVTIOCtx *io_ctx)
{
AVTProtocolCtx *p = malloc(sizeof(*p));
if (!p)
return AVT_ERROR(ENOMEM);

int err = avt_io_init(ctx, &priv->io, &priv->io_ctx, addr);
if (err < 0)
free(priv);
else
*p = priv;
p->io = io;
p->io_ctx = io_ctx;
*_p = p;

return err;
return 0;
}

static int datagram_proto_add_dst(AVTProtocolCtx *p, AVTAddress *addr)
Expand Down Expand Up @@ -112,15 +119,6 @@ static int datagram_proto_flush(AVTProtocolCtx *p, int64_t timeout)
return AVT_ERROR(ENOTSUP);
}

static COLD int datagram_proto_close(AVTProtocolCtx **p)
{
AVTProtocolCtx *priv = *p;
int err = priv->io->close(&priv->io_ctx);
free(priv);
*p = NULL;
return err;
}

const AVTProtocol avt_protocol_datagram = {
.name = "datagram",
.type = AVT_PROTOCOL_DATAGRAM,
Expand Down
Loading

0 comments on commit 1d0207a

Please sign in to comment.