From 393d1671eea5a660d735b29c31281e8bad363471 Mon Sep 17 00:00:00 2001 From: Noah Fontes Date: Sun, 6 Oct 2013 15:18:59 -0400 Subject: [PATCH] Update server support; remove libcurvecpr-glib. This changeset backports changes to the libcurvecpr core library from the add-glib-server branch. It further removes libcurvecpr-glib entirely from the source tree; the GLib-based source code will be made available in a separate repository (currently https://github.com/impl/libcurvecpr-glib). --- .gitignore | 1 - .travis.yml | 6 +- Makefile.am | 4 - configure.ac | 16 - libcurvecpr-glib/Makefile.am | 4 - libcurvecpr-glib/include/Makefile.am | 4 - libcurvecpr-glib/include/curvecpr_glib.h | 7 - .../curvecpr_glib/client_messager_glib.h | 63 --- .../include/curvecpr_glib/messager_glib.h | 59 --- libcurvecpr-glib/lib/Makefile.am | 8 - libcurvecpr-glib/lib/client_messager_glib.c | 137 ------ libcurvecpr-glib/lib/messager_glib.c | 438 ------------------ libcurvecpr-glib/libcurvecpr-glib.pc.in | 14 - libcurvecpr-glib/test/Makefile.am | 7 - libcurvecpr/include/curvecpr/messager.h | 2 +- libcurvecpr/include/curvecpr/server.h | 10 +- libcurvecpr/lib/chicago.c | 3 +- libcurvecpr/lib/messager.c | 5 +- libcurvecpr/lib/server_recv.c | 42 +- libcurvecpr/lib/server_send.c | 9 +- ..._with_1_failure_moves_message_from_sendq.c | 7 +- libcurvecpr/test/util/.gitignore | 1 + 22 files changed, 49 insertions(+), 798 deletions(-) delete mode 100644 libcurvecpr-glib/Makefile.am delete mode 100644 libcurvecpr-glib/include/Makefile.am delete mode 100644 libcurvecpr-glib/include/curvecpr_glib.h delete mode 100644 libcurvecpr-glib/include/curvecpr_glib/client_messager_glib.h delete mode 100644 libcurvecpr-glib/include/curvecpr_glib/messager_glib.h delete mode 100644 libcurvecpr-glib/lib/Makefile.am delete mode 100644 libcurvecpr-glib/lib/client_messager_glib.c delete mode 100644 libcurvecpr-glib/lib/messager_glib.c delete mode 100644 libcurvecpr-glib/libcurvecpr-glib.pc.in delete mode 100644 libcurvecpr-glib/test/Makefile.am create mode 100644 libcurvecpr/test/util/.gitignore diff --git a/.gitignore b/.gitignore index 615224d..80a89ce 100644 --- a/.gitignore +++ b/.gitignore @@ -12,7 +12,6 @@ /depcomp /install-sh /libcurvecpr/libcurvecpr.pc -/libcurvecpr-glib/libcurvecpr-glib.pc /libtool /ltmain.sh /m4/libtool.m4 diff --git a/.travis.yml b/.travis.yml index a49da90..16f48ef 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,13 +12,9 @@ before_install: - popd - popd - sudo ldconfig -env: - - USE_LIBGLIB=no - - USE_LIBGLIB=yes before_script: - ./autogen.sh - - sh -c 'if [ "$USE_LIBGLIB" = "yes" ]; then ./configure --with-libglib CFLAGS="-Werror"; fi' - - sh -c 'if [ "$USE_LIBGLIB" = "no" ]; then ./configure --without-libglib CFLAGS="-Werror"; fi' + - ./configure CFLAGS="-Werror" script: make check compiler: - clang diff --git a/Makefile.am b/Makefile.am index 820c972..6efffec 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,7 +1,3 @@ ACLOCAL_AMFLAGS = -I m4 SUBDIRS = libcurvecpr - -if USE_LIBGLIB - SUBDIRS += libcurvecpr-glib -endif diff --git a/configure.ac b/configure.ac index 384939f..18aab63 100644 --- a/configure.ac +++ b/configure.ac @@ -29,17 +29,6 @@ PKG_CHECK_MODULES([CHECK], [check >= 0.9.8]) AC_SEARCH_LIBS([clock_gettime], [rt posix4]) AC_CHECK_LIB([sodium], [sodium_init], [], [AC_MSG_ERROR([missing libsodium])]) -AC_ARG_WITH([libglib], - [AS_HELP_STRING([--with-libglib], [build with glib support @<:@default=check@:>@])], - [], [with_libglib=check] -) -AS_CASE(["$with_glib"], - [yes], [PKG_CHECK_MODULES([GLIB], [glib-2.0 >= 2.14.0], [HAVE_LIBGLIB=1])], - [no], [], - [PKG_CHECK_MODULES([GLIB], [glib-2.0 >= 2.14.0], [HAVE_LIBGLIB=1], [HAVE_LIBGLIB=0])] -) -AM_CONDITIONAL([USE_LIBGLIB], [test "$with_libglib" != no -a "$HAVE_LIBGLIB" -eq 1]) - # Checks for header files. AC_HEADER_STDC AC_CHECK_HEADERS([errno.h stdint.h string.h time.h], [], [AC_MSG_ERROR([missing required header file(s)])]) @@ -85,11 +74,6 @@ AC_CONFIG_FILES([ libcurvecpr/test/Makefile libcurvecpr/Makefile libcurvecpr/libcurvecpr.pc - libcurvecpr-glib/include/Makefile - libcurvecpr-glib/lib/Makefile - libcurvecpr-glib/test/Makefile - libcurvecpr-glib/Makefile - libcurvecpr-glib/libcurvecpr-glib.pc Makefile ]) AC_OUTPUT diff --git a/libcurvecpr-glib/Makefile.am b/libcurvecpr-glib/Makefile.am deleted file mode 100644 index a44693a..0000000 --- a/libcurvecpr-glib/Makefile.am +++ /dev/null @@ -1,4 +0,0 @@ -SUBDIRS = include lib test - -pkgconfigdir = $(libdir)/pkgconfig -pkgconfig_DATA = libcurvecpr-glib.pc diff --git a/libcurvecpr-glib/include/Makefile.am b/libcurvecpr-glib/include/Makefile.am deleted file mode 100644 index 067cf4a..0000000 --- a/libcurvecpr-glib/include/Makefile.am +++ /dev/null @@ -1,4 +0,0 @@ -nobase_include_HEADERS = \ - curvecpr_glib/client_messager_glib.h \ - curvecpr_glib/messager_glib.h \ - curvecpr_glib.h diff --git a/libcurvecpr-glib/include/curvecpr_glib.h b/libcurvecpr-glib/include/curvecpr_glib.h deleted file mode 100644 index f73799d..0000000 --- a/libcurvecpr-glib/include/curvecpr_glib.h +++ /dev/null @@ -1,7 +0,0 @@ -#ifndef __CURVECPR_GLIB_CURVECPR_GLIB_H -#define __CURVECPR_GLIB_CURVECPR_GLIB_H - -#include -#include - -#endif diff --git a/libcurvecpr-glib/include/curvecpr_glib/client_messager_glib.h b/libcurvecpr-glib/include/curvecpr_glib/client_messager_glib.h deleted file mode 100644 index 8d84309..0000000 --- a/libcurvecpr-glib/include/curvecpr_glib/client_messager_glib.h +++ /dev/null @@ -1,63 +0,0 @@ -#ifndef __CURVECPR_GLIB_CLIENT_MESSAGER_GLIB_H -#define __CURVECPR_GLIB_CLIENT_MESSAGER_GLIB_H - -#include "messager_glib.h" - -#include -#include - -#include - -#include - -struct curvecpr_client_messager_glib; - -struct curvecpr_client_messager_glib_ops { - int (*send)(struct curvecpr_client_messager_glib *cmg, const unsigned char *buf, size_t num); - int (*recv)(struct curvecpr_client_messager_glib *cmg, const unsigned char *buf, size_t num); - void (*finished)(struct curvecpr_client_messager_glib *cmg, enum curvecpr_block_eofflag flag); - - int (*next_nonce)(struct curvecpr_client_messager_glib *cmg, unsigned char *destination, size_t num); -}; - -struct curvecpr_client_messager_glib_cf { - /* Any extensions. */ - unsigned char my_extension[16]; - - /* Curve25519 public/private keypairs. */ - unsigned char my_global_pk[32]; - unsigned char my_global_sk[32]; - - /* Server configuration. */ - unsigned char their_extension[16]; - unsigned char their_global_pk[32]; - unsigned char their_domain_name[256]; - - /* Messager configuration. */ - crypto_uint64 pending_maximum; - unsigned int sendmarkq_maximum; - unsigned int recvmarkq_maximum; - - struct curvecpr_client_messager_glib_ops ops; - - void *priv; -}; - -struct curvecpr_client_messager_glib { - struct curvecpr_client_messager_glib_cf cf; - - struct curvecpr_client client; - struct curvecpr_messager_glib mg; -}; - -void curvecpr_client_messager_glib_new (struct curvecpr_client_messager_glib *cmg, struct curvecpr_client_messager_glib_cf *cf); -void curvecpr_client_messager_glib_dealloc (struct curvecpr_client_messager_glib *cmg); -int curvecpr_client_messager_glib_connected (struct curvecpr_client_messager_glib *cmg); -int curvecpr_client_messager_glib_send (struct curvecpr_client_messager_glib *cmg, const unsigned char *buf, size_t num); -int curvecpr_client_messager_glib_recv (struct curvecpr_client_messager_glib *cmg, const unsigned char *buf, size_t num); -unsigned char curvecpr_client_messager_glib_is_finished (struct curvecpr_client_messager_glib *cmg); -int curvecpr_client_messager_glib_finish (struct curvecpr_client_messager_glib *cmg); -int curvecpr_client_messager_glib_process_sendq (struct curvecpr_client_messager_glib *cmg); -long long curvecpr_client_messager_glib_next_timeout (struct curvecpr_client_messager_glib *cmg); - -#endif diff --git a/libcurvecpr-glib/include/curvecpr_glib/messager_glib.h b/libcurvecpr-glib/include/curvecpr_glib/messager_glib.h deleted file mode 100644 index 8b9d2d4..0000000 --- a/libcurvecpr-glib/include/curvecpr_glib/messager_glib.h +++ /dev/null @@ -1,59 +0,0 @@ -#ifndef __CURVECPR_GLIB_MESSAGER_GLIB_H -#define __CURVECPR_GLIB_MESSAGER_GLIB_H - -#include -#include - -#include - -#include - -#include - -struct curvecpr_messager_glib; - -struct curvecpr_messager_glib_ops { - int (*send)(struct curvecpr_messager_glib *mg, const unsigned char *buf, size_t num); - int (*recv)(struct curvecpr_messager_glib *mg, const unsigned char *buf, size_t num); - void (*finished)(struct curvecpr_messager_glib *mg, enum curvecpr_block_eofflag flag); -}; - -struct curvecpr_messager_glib_cf { - crypto_uint64 pending_maximum; - unsigned int sendmarkq_maximum; - unsigned int recvmarkq_maximum; - - struct curvecpr_messager_glib_ops ops; - - void *priv; -}; - -struct curvecpr_messager_glib { - struct curvecpr_messager_glib_cf cf; - - struct curvecpr_messager messager; - - unsigned char sendq_head_exists; - struct curvecpr_block sendq_head; - - unsigned char *pending; - crypto_uint64 pending_used; - unsigned char pending_eof; - crypto_uint64 pending_current; - crypto_uint64 pending_next; - - GSequence *sendmarkq; - GSequence *recvmarkq; - crypto_uint64 recvmarkq_distributed; -}; - -void curvecpr_messager_glib_new (struct curvecpr_messager_glib *mg, struct curvecpr_messager_glib_cf *cf, unsigned char client); -void curvecpr_messager_glib_dealloc (struct curvecpr_messager_glib *mg); -unsigned char curvecpr_messager_glib_is_finished (struct curvecpr_messager_glib *mg); -int curvecpr_messager_glib_finish (struct curvecpr_messager_glib *mg); -int curvecpr_messager_glib_send (struct curvecpr_messager_glib *mg, const unsigned char *buf, size_t num); -int curvecpr_messager_glib_recv (struct curvecpr_messager_glib *mg, const unsigned char *buf, size_t num); -int curvecpr_messager_glib_process_sendq (struct curvecpr_messager_glib *mg); -long long curvecpr_messager_glib_next_timeout (struct curvecpr_messager_glib *mg); - -#endif diff --git a/libcurvecpr-glib/lib/Makefile.am b/libcurvecpr-glib/lib/Makefile.am deleted file mode 100644 index 723b026..0000000 --- a/libcurvecpr-glib/lib/Makefile.am +++ /dev/null @@ -1,8 +0,0 @@ -lib_LTLIBRARIES = libcurvecpr-glib.la - -libcurvecpr_glib_la_CPPFLAGS = -I$(top_srcdir)/libcurvecpr/include -I$(top_srcdir)/libcurvecpr-glib/include -libcurvecpr_glib_la_CFLAGS = @GLIB_CFLAGS@ -libcurvecpr_glib_la_LDFLAGS = -version-info $(CURVECPR_LIBRARY_VERSION) @GLIB_LIBS@ -L$(top_srcdir)/libcurvecpr/lib -lcurvecpr -libcurvecpr_glib_la_SOURCES = \ - client_messager_glib.c \ - messager_glib.c diff --git a/libcurvecpr-glib/lib/client_messager_glib.c b/libcurvecpr-glib/lib/client_messager_glib.c deleted file mode 100644 index a9d710b..0000000 --- a/libcurvecpr-glib/lib/client_messager_glib.c +++ /dev/null @@ -1,137 +0,0 @@ -#include "config.h" - -#include - -#include - -#include -#include - -static int _client_send (struct curvecpr_client *client, const unsigned char *buf, size_t num) -{ - struct curvecpr_client_messager_glib *cmg = client->cf.priv; - - return cmg->cf.ops.send(cmg, buf, num); -} - -static int _client_recv (struct curvecpr_client *client, const unsigned char *buf, size_t num) -{ - struct curvecpr_client_messager_glib *cmg = client->cf.priv; - - return curvecpr_messager_glib_recv(&cmg->mg, buf, num); -} - -static int _client_next_nonce(struct curvecpr_client *client, unsigned char *destination, size_t num) -{ - struct curvecpr_client_messager_glib *cmg = client->cf.priv; - - return cmg->cf.ops.next_nonce(cmg, destination, num); -} - -static int _messager_glib_send (struct curvecpr_messager_glib *mg, const unsigned char *buf, size_t num) -{ - struct curvecpr_client_messager_glib *cmg = mg->cf.priv; - - return curvecpr_client_send(&cmg->client, buf, num); -} - -static int _messager_glib_recv (struct curvecpr_messager_glib *mg, const unsigned char *buf, size_t num) -{ - struct curvecpr_client_messager_glib *cmg = mg->cf.priv; - - return cmg->cf.ops.recv(cmg, buf, num); -} - -static void _messager_glib_finished (struct curvecpr_messager_glib *mg, enum curvecpr_block_eofflag flag) -{ - struct curvecpr_client_messager_glib *cmg = mg->cf.priv; - - if (cmg->cf.ops.finished) - cmg->cf.ops.finished(cmg, flag); -} - -void curvecpr_client_messager_glib_new (struct curvecpr_client_messager_glib *cmg, struct curvecpr_client_messager_glib_cf *cf) -{ - struct curvecpr_client_cf client_cf = { - .ops = { - .send = _client_send, - .recv = _client_recv, - - .next_nonce = _client_next_nonce - }, - .priv = cmg - }; - - struct curvecpr_messager_glib_cf mg_cf = { - .ops = { - .send = _messager_glib_send, - .recv = _messager_glib_recv, - .finished = _messager_glib_finished - }, - .priv = cmg - }; - - curvecpr_bytes_zero(cmg, sizeof(struct curvecpr_client_messager_glib)); - - if (cf) - curvecpr_bytes_copy(&cmg->cf, cf, sizeof(struct curvecpr_client_messager_glib_cf)); - - /* Client configuration. */ - curvecpr_bytes_copy(client_cf.my_extension, cmg->cf.my_extension, 16); - - curvecpr_bytes_copy(client_cf.my_global_pk, cmg->cf.my_global_pk, 32); - curvecpr_bytes_copy(client_cf.my_global_sk, cmg->cf.my_global_sk, 32); - - curvecpr_bytes_copy(client_cf.their_extension, cmg->cf.their_extension, 16); - curvecpr_bytes_copy(client_cf.their_global_pk, cmg->cf.their_global_pk, 32); - curvecpr_bytes_copy(client_cf.their_domain_name, cmg->cf.their_domain_name, 256); - - /* Messager configuration. */ - mg_cf.pending_maximum = cmg->cf.pending_maximum; - mg_cf.sendmarkq_maximum = cmg->cf.sendmarkq_maximum; - mg_cf.recvmarkq_maximum = cmg->cf.recvmarkq_maximum; - - /* Initialize client and messager. */ - curvecpr_client_new(&cmg->client, &client_cf); - curvecpr_messager_glib_new(&cmg->mg, &mg_cf, 1); -} - -void curvecpr_client_messager_glib_dealloc (struct curvecpr_client_messager_glib *cmg) -{ - curvecpr_messager_glib_dealloc(&cmg->mg); -} - -int curvecpr_client_messager_glib_connected (struct curvecpr_client_messager_glib *cmg) -{ - return curvecpr_client_connected(&cmg->client); -} - -int curvecpr_client_messager_glib_send (struct curvecpr_client_messager_glib *cmg, const unsigned char *buf, size_t num) -{ - return curvecpr_messager_glib_send(&cmg->mg, buf, num); -} - -unsigned char curvecpr_client_messager_glib_is_finished (struct curvecpr_client_messager_glib *cmg) -{ - return curvecpr_messager_glib_is_finished(&cmg->mg); -} - -int curvecpr_client_messager_glib_finish (struct curvecpr_client_messager_glib *cmg) -{ - return curvecpr_messager_glib_finish(&cmg->mg); -} - -int curvecpr_client_messager_glib_recv (struct curvecpr_client_messager_glib *cmg, const unsigned char *buf, size_t num) -{ - return curvecpr_client_recv(&cmg->client, buf, num); -} - -int curvecpr_client_messager_glib_process_sendq (struct curvecpr_client_messager_glib *cmg) -{ - return cmg->client.negotiated == CURVECPR_CLIENT_PENDING ? 0 : curvecpr_messager_glib_process_sendq(&cmg->mg); -} - -long long curvecpr_client_messager_glib_next_timeout (struct curvecpr_client_messager_glib *cmg) -{ - return cmg->client.negotiated == CURVECPR_CLIENT_PENDING ? -1 : curvecpr_messager_glib_next_timeout(&cmg->mg); -} diff --git a/libcurvecpr-glib/lib/messager_glib.c b/libcurvecpr-glib/lib/messager_glib.c deleted file mode 100644 index c109679..0000000 --- a/libcurvecpr-glib/lib/messager_glib.c +++ /dev/null @@ -1,438 +0,0 @@ -#include "config.h" - -#include - -#include -#include - -#include -#include - -#include - -#include - -#define RECVMARKQ_ELEMENT_NONE 0 -#define RECVMARKQ_ELEMENT_DISTRIBUTED (1 << 0) -#define RECVMARKQ_ELEMENT_ACKNOWLEDGED (1 << 1) -#define RECVMARKQ_ELEMENT_DONE (RECVMARKQ_ELEMENT_DISTRIBUTED | RECVMARKQ_ELEMENT_ACKNOWLEDGED) - -struct _recvmarkq_element { - struct curvecpr_block block; - unsigned char status; -}; - -static gint _gs_compare_clock (gconstpointer a, gconstpointer b, gpointer unused) -{ - const struct curvecpr_block *block_a = a; - const struct curvecpr_block *block_b = b; - - if (block_a->clock == block_b->clock) - return 0; - else - return block_a->clock < block_b->clock ? -1 : 1; -} - -static unsigned char _sendmarkq_is_full (struct curvecpr_messager *messager) -{ - struct curvecpr_messager_glib *mg = messager->cf.priv; - - gint len = g_sequence_get_length(mg->sendmarkq); - - return len >= 0 && (guint)len >= mg->cf.sendmarkq_maximum; -} - -static int _sendq_head (struct curvecpr_messager *messager, struct curvecpr_block **block_stored) -{ - struct curvecpr_messager_glib *mg = messager->cf.priv; - - if (mg->sendq_head_exists) { - *block_stored = &mg->sendq_head; - return 0; - } - - if (mg->pending_used || mg->pending_eof) { - curvecpr_bytes_zero(&mg->sendq_head, sizeof(struct curvecpr_block)); - - mg->sendq_head.eof = mg->pending_eof ? CURVECPR_BLOCK_EOF_SUCCESS : CURVECPR_BLOCK_STREAM; - - if (mg->pending) { - int requested = mg->pending_used > mg->messager.my_maximum_send_bytes ? mg->messager.my_maximum_send_bytes : mg->pending_used; - - mg->sendq_head.data_len = requested; - if (mg->pending_current + requested > mg->cf.pending_maximum) { - /* Two reads, one from the end and one from the beginning. */ - int avail = mg->cf.pending_maximum - mg->pending_current; - - curvecpr_bytes_copy(mg->sendq_head.data, mg->pending + mg->pending_current, avail); - curvecpr_bytes_copy(mg->sendq_head.data + avail, mg->pending, requested - avail); - - mg->pending_current = requested - avail; - } else { - /* Just one read at the end. */ - curvecpr_bytes_copy(mg->sendq_head.data, mg->pending + mg->pending_current, requested); - - mg->pending_current += requested; - } - - mg->pending_used -= requested; - } - - mg->sendq_head_exists = 1; - } - - return -1; -} - -static unsigned char _sendq_is_empty (struct curvecpr_messager *messager) -{ - struct curvecpr_messager_glib *mg = messager->cf.priv; - - return !mg->sendq_head_exists && /* We don't have a block actually waiting to be - written. */ - mg->pending_used == 0 && /* We don't have any bytes that we could turn into a - block to be written. */ - (!mg->pending_eof || /* The EOF flag isn't set. */ - mg->messager.my_eof); /* Even if our EOF flag is set, the messager must - not have sent the EOF message. */ -} - -static int _sendq_move_to_sendmarkq (struct curvecpr_messager *messager, const struct curvecpr_block *block, struct curvecpr_block **block_stored) -{ - struct curvecpr_messager_glib *mg = messager->cf.priv; - - struct curvecpr_block *new_block; - - if (!mg->sendq_head_exists || block != &mg->sendq_head) { - /* NB: This is slow, but the clock has likely updated for this block. Good news - is we should run into it as the first element of the sendmarkq or so. */ - GSequenceIter *it = g_sequence_get_begin_iter(mg->sendmarkq); - for (; !g_sequence_iter_is_end(it); it = g_sequence_iter_next(it)) { - if (g_sequence_get(it) == block) { - g_sequence_sort_changed(it, _gs_compare_clock, NULL); - break; - } - } - - return -1; - } - - if (_sendmarkq_is_full(messager)) - return -1; - - new_block = g_slice_new(struct curvecpr_block); - curvecpr_bytes_copy(new_block, block, sizeof(struct curvecpr_block)); - - g_sequence_insert_sorted(mg->sendmarkq, new_block, _gs_compare_clock, NULL); - - mg->sendq_head_exists = 0; - - if (block_stored) - *block_stored = new_block; - - return 0; -} - -static int _sendmarkq_head (struct curvecpr_messager *messager, struct curvecpr_block **block_stored) -{ - struct curvecpr_messager_glib *mg = messager->cf.priv; - - GSequenceIter *it = g_sequence_get_begin_iter(mg->sendmarkq); - if (g_sequence_iter_is_end(it)) - return -1; - - *block_stored = g_sequence_get(it); - return 0; -} - -static int _sendmarkq_get (struct curvecpr_messager *messager, crypto_uint32 acknowledging_id, struct curvecpr_block **block_stored) -{ - struct curvecpr_messager_glib *mg = messager->cf.priv; - - GSequenceIter *it = g_sequence_get_begin_iter(mg->sendmarkq); - for (; !g_sequence_iter_is_end(it); it = g_sequence_iter_next(it)) { - struct curvecpr_block *block = g_sequence_get(it); - - if (block->id == acknowledging_id) { - *block_stored = block; - return 0; - } - } - - return -1; -} - -static int _sendmarkq_remove_range (struct curvecpr_messager *messager, unsigned long long start, unsigned long long end) -{ - struct curvecpr_messager_glib *mg = messager->cf.priv; - - GSequenceIter *it = g_sequence_get_begin_iter(mg->sendmarkq); - while (!g_sequence_iter_is_end(it)) { - struct curvecpr_block *block = g_sequence_get(it); - GSequenceIter *candidate = it; - - /* Avoid any issues with g_sequence_remove() by updating the iterator - beforehand. */ - it = g_sequence_iter_next(it); - - if (block->offset >= start && block->offset + block->data_len <= end) - g_sequence_remove(candidate); - } - - return 0; -} - -static gint _gs_compare_range (gconstpointer a, gconstpointer b, gpointer unused) -{ - const struct _recvmarkq_element *element_a = a; - const struct _recvmarkq_element *element_b = b; - - if (element_a->block.offset == element_b->block.offset) { - if (element_a->block.data_len == element_b->block.data_len) - return 0; - else - return element_a->block.data_len < element_b->block.data_len ? -1 : 1; - } else { - return element_a->block.offset < element_b->block.offset ? -1 : 1; - } -} - -static unsigned char _recvmarkq_is_full (struct curvecpr_messager *messager) -{ - struct curvecpr_messager_glib *mg = messager->cf.priv; - - gint len = g_sequence_get_length(mg->recvmarkq); - - return len >= 0 && (guint)len >= mg->cf.recvmarkq_maximum; -} - -static int _recvmarkq_put (struct curvecpr_messager *messager, const struct curvecpr_block *block, struct curvecpr_block **block_stored) -{ - struct curvecpr_messager_glib *mg = messager->cf.priv; - - struct _recvmarkq_element *new_element; - - if (_recvmarkq_is_full(messager)) - return -1; - - new_element = g_slice_new(struct _recvmarkq_element); - new_element->status = RECVMARKQ_ELEMENT_NONE; - curvecpr_bytes_copy(&new_element->block, block, sizeof(struct curvecpr_block)); - - g_sequence_insert_sorted(mg->recvmarkq, new_element, _gs_compare_range, NULL); - - /* Publish any received messages in sequential blocks. */ - { - GSequenceIter *it = g_sequence_get_begin_iter(mg->recvmarkq); - while (!g_sequence_iter_is_end(it)) { - struct _recvmarkq_element *element = g_sequence_get(it); - GSequenceIter *candidate = it; - - /* Avoid any issues with g_sequence_remove() by updating the iterator - beforehand. */ - it = g_sequence_iter_next(it); - - if (element->block.offset <= mg->recvmarkq_distributed) { - if (element->block.data_len > 0 && element->block.offset + element->block.data_len > mg->recvmarkq_distributed) { - crypto_uint64 idx = mg->recvmarkq_distributed - element->block.offset; - size_t len = element->block.data_len - idx; - - mg->cf.ops.recv(mg, element->block.data + idx, len); - mg->recvmarkq_distributed += len; - } - - if (element->block.eof != CURVECPR_BLOCK_STREAM) { - if (mg->cf.ops.finished) - mg->cf.ops.finished(mg, element->block.eof); - } - - element->status |= RECVMARKQ_ELEMENT_DISTRIBUTED; - - /* If this element has been acknowledged and distributed, remove it. */ - if (element->status == RECVMARKQ_ELEMENT_DONE) - g_sequence_remove(candidate); - } else { - /* Since the blocks are sorted, nothing else will match after this. */ - break; - } - } - } - - if (block_stored) - *block_stored = &new_element->block; - - return 0; -} - -static int _recvmarkq_get_nth_unacknowledged (struct curvecpr_messager *messager, unsigned int n, struct curvecpr_block **block_stored) -{ - struct curvecpr_messager_glib *mg = messager->cf.priv; - - unsigned int i = 0; - - GSequenceIter *it = g_sequence_get_begin_iter(mg->recvmarkq); - for (;;) { - struct _recvmarkq_element *element; - - if (g_sequence_iter_is_end(it)) - break; - - element = g_sequence_get(it); - if (!(element->status & RECVMARKQ_ELEMENT_ACKNOWLEDGED)) { - if (i == n) { - *block_stored = &element->block; - return 0; - } else { - ++i; - } - } - - it = g_sequence_iter_next(it); - } - - return -1; -} - -static int _recvmarkq_remove_range (struct curvecpr_messager *messager, unsigned long long start, unsigned long long end) -{ - struct curvecpr_messager_glib *mg = messager->cf.priv; - - GSequenceIter *it = g_sequence_get_begin_iter(mg->recvmarkq); - while (!g_sequence_iter_is_end(it)) { - struct _recvmarkq_element *element = g_sequence_get(it); - GSequenceIter *candidate = it; - - /* Avoid any issues with g_sequence_remove() by updating the iterator - beforehand. */ - it = g_sequence_iter_next(it); - - if (element->block.offset >= start && element->block.offset + element->block.data_len <= end) { - element->status |= RECVMARKQ_ELEMENT_ACKNOWLEDGED; - - if (element->status == RECVMARKQ_ELEMENT_DONE) - g_sequence_remove(candidate); - } - } - - return 0; -} - -static int _send (struct curvecpr_messager *messager, const unsigned char *buf, size_t num) -{ - struct curvecpr_messager_glib *mg = messager->cf.priv; - - return mg->cf.ops.send(mg, buf, num); -} - -void curvecpr_messager_glib_new (struct curvecpr_messager_glib *mg, struct curvecpr_messager_glib_cf *cf, unsigned char client) -{ - struct curvecpr_messager_cf messager_cf = { - .ops = { - .sendq_head = _sendq_head, - .sendq_move_to_sendmarkq = _sendq_move_to_sendmarkq, - .sendq_is_empty = _sendq_is_empty, - - .sendmarkq_head = _sendmarkq_head, - .sendmarkq_get = _sendmarkq_get, - .sendmarkq_remove_range = _sendmarkq_remove_range, - .sendmarkq_is_full = _sendmarkq_is_full, - - .recvmarkq_put = _recvmarkq_put, - .recvmarkq_get_nth_unacknowledged = _recvmarkq_get_nth_unacknowledged, - .recvmarkq_is_full = _recvmarkq_is_full, - .recvmarkq_remove_range = _recvmarkq_remove_range, - - .send = _send - }, - .priv = mg - }; - - curvecpr_bytes_zero(mg, sizeof(struct curvecpr_messager_glib)); - - if (cf) - curvecpr_bytes_copy(&mg->cf, cf, sizeof(struct curvecpr_messager_glib_cf)); - - /* Initialize messager. */ - curvecpr_messager_new(&mg->messager, &messager_cf, client); - - /* Set up queues. */ - mg->pending_eof = CURVECPR_BLOCK_STREAM; - mg->pending = NULL; - mg->pending_current = mg->pending_next = mg->pending_used = 0; - - mg->sendq_head_exists = 0; - - mg->sendmarkq = g_sequence_new(g_free); - - mg->recvmarkq = g_sequence_new(g_free); - mg->recvmarkq_distributed = 0; -} - -void curvecpr_messager_glib_dealloc (struct curvecpr_messager_glib *mg) -{ - g_free(mg->pending); - - g_sequence_free(mg->sendmarkq); - g_sequence_free(mg->recvmarkq); -} - -unsigned char curvecpr_messager_glib_is_finished (struct curvecpr_messager_glib *mg) -{ - return mg->messager.my_final && mg->messager.their_final; -} - -int curvecpr_messager_glib_finish (struct curvecpr_messager_glib *mg) -{ - if (mg->pending_eof) - return -EINVAL; - - mg->pending_eof = 1; - return 0; -} - -int curvecpr_messager_glib_send (struct curvecpr_messager_glib *mg, const unsigned char *buf, size_t num) -{ - if (num == 0) - return -EINVAL; - if (mg->pending_eof) - return -EINVAL; - if (num > mg->cf.pending_maximum - mg->pending_used) - return -ENOBUFS; - - if (!mg->pending) - mg->pending = g_malloc(mg->cf.pending_maximum); - - if (mg->pending_next + num > mg->cf.pending_maximum) { - /* Two writes; one at the end and one at the beginning. */ - int avail = mg->cf.pending_maximum - mg->pending_next; - - curvecpr_bytes_copy(mg->pending + mg->pending_next, buf, avail); - curvecpr_bytes_copy(mg->pending, buf + avail, num - avail); - - mg->pending_next = num - avail; - } else { - /* Just one write at the end. */ - curvecpr_bytes_copy(mg->pending + mg->pending_next, buf, num); - - mg->pending_next += num; - } - - mg->pending_used += num; - - return 0; -} - -int curvecpr_messager_glib_recv (struct curvecpr_messager_glib *mg, const unsigned char *buf, size_t num) -{ - return curvecpr_messager_recv(&mg->messager, buf, num); -} - -int curvecpr_messager_glib_process_sendq (struct curvecpr_messager_glib *mg) -{ - return curvecpr_messager_process_sendq(&mg->messager); -} - -long long curvecpr_messager_glib_next_timeout (struct curvecpr_messager_glib *mg) -{ - return curvecpr_messager_next_timeout(&mg->messager); -} diff --git a/libcurvecpr-glib/libcurvecpr-glib.pc.in b/libcurvecpr-glib/libcurvecpr-glib.pc.in deleted file mode 100644 index 7ac35eb..0000000 --- a/libcurvecpr-glib/libcurvecpr-glib.pc.in +++ /dev/null @@ -1,14 +0,0 @@ -prefix=@prefix@ -exec_prefix=@exec_prefix@ - -includedir=@includedir@ -libdir=@libdir@ - -Name: @PACKAGE_NAME@-glib -Description: libcurvecpr, a portable implementation of CurveCP -Version: @PACKAGE_VERSION@ -URL: https://github.com/impl/libcurvecpr -Requires: libcurvecpr = @PACKAGE_VERSION@ -Requires.private: glib-2.0 >= 2.14.0 -Libs: -L${libdir} -lcurvecpr-glib -Cflags: -I${includedir} diff --git a/libcurvecpr-glib/test/Makefile.am b/libcurvecpr-glib/test/Makefile.am deleted file mode 100644 index 1fa8716..0000000 --- a/libcurvecpr-glib/test/Makefile.am +++ /dev/null @@ -1,7 +0,0 @@ -AM_CPPFLAGS = -I$(top_srcdir)/libcurvecpr/include -I$(top_srcdir)/libcurvecpr/test -AM_CFLAGS = @CHECK_CFLAGS@ -LDADD = $(top_builddir)/libcurvecpr/lib/libcurvecpr.la @CHECK_LIBS@ - -check_PROGRAMS = - -TESTS = $(check_PROGRAMS) diff --git a/libcurvecpr/include/curvecpr/messager.h b/libcurvecpr/include/curvecpr/messager.h index 7e8cd61..f5d0a9b 100644 --- a/libcurvecpr/include/curvecpr/messager.h +++ b/libcurvecpr/include/curvecpr/messager.h @@ -24,7 +24,7 @@ struct curvecpr_messager_ops { int (*recvmarkq_put)(struct curvecpr_messager *messager, const struct curvecpr_block *block, struct curvecpr_block **block_stored); int (*recvmarkq_get_nth_unacknowledged)(struct curvecpr_messager *messager, unsigned int n, struct curvecpr_block **block_stored); - unsigned char (*recvmarkq_is_full)(struct curvecpr_messager *messager); + unsigned char (*recvmarkq_is_empty)(struct curvecpr_messager *messager); int (*recvmarkq_remove_range)(struct curvecpr_messager *messager, unsigned long long start, unsigned long long end); int (*send)(struct curvecpr_messager *messager, const unsigned char *buf, size_t num); diff --git a/libcurvecpr/include/curvecpr/server.h b/libcurvecpr/include/curvecpr/server.h index bd47039..2e83241 100644 --- a/libcurvecpr/include/curvecpr/server.h +++ b/libcurvecpr/include/curvecpr/server.h @@ -8,11 +8,11 @@ struct curvecpr_server; struct curvecpr_server_ops { - int (*put_session)(struct curvecpr_server *server, const struct curvecpr_session *s, struct curvecpr_session **s_stored); + int (*put_session)(struct curvecpr_server *server, const struct curvecpr_session *s, void *priv, struct curvecpr_session **s_stored); int (*get_session)(struct curvecpr_server *server, const unsigned char their_session_pk[32], struct curvecpr_session **s_stored); - int (*send)(struct curvecpr_server *server, struct curvecpr_session *s, const unsigned char *buf, size_t num); - int (*recv)(struct curvecpr_server *server, struct curvecpr_session *s, const unsigned char *buf, size_t num); + int (*send)(struct curvecpr_server *server, struct curvecpr_session *s, void *priv, const unsigned char *buf, size_t num); + int (*recv)(struct curvecpr_server *server, struct curvecpr_session *s, void *priv, const unsigned char *buf, size_t num); int (*next_nonce)(struct curvecpr_server *server, unsigned char *destination, size_t num); }; @@ -39,7 +39,7 @@ struct curvecpr_server { void curvecpr_server_new (struct curvecpr_server *server, const struct curvecpr_server_cf *cf); void curvecpr_server_refresh_temporal_keys (struct curvecpr_server *server); -int curvecpr_server_recv (struct curvecpr_server *server, void *priv, const unsigned char *buf, size_t num); -int curvecpr_server_send (struct curvecpr_server *server, const unsigned char their_session_pk[32], const unsigned char *buf, size_t num); +int curvecpr_server_recv (struct curvecpr_server *server, void *priv, const unsigned char *buf, size_t num, struct curvecpr_session **s_stored); +int curvecpr_server_send (struct curvecpr_server *server, struct curvecpr_session *s, void *priv, const unsigned char *buf, size_t num); #endif diff --git a/libcurvecpr/lib/chicago.c b/libcurvecpr/lib/chicago.c index 3ed2f2a..7a53a25 100644 --- a/libcurvecpr/lib/chicago.c +++ b/libcurvecpr/lib/chicago.c @@ -143,7 +143,8 @@ void curvecpr_chicago_new (struct curvecpr_chicago *chicago) chicago->rtt_phase = 0; - chicago->wr_rate = 1000000000; + /* FIXME: This should be 1 second? */ + chicago->wr_rate = 0; chicago->ns_last_update = 0; chicago->ns_last_edge = 0; diff --git a/libcurvecpr/lib/messager.c b/libcurvecpr/lib/messager.c index 50c9af3..3708437 100644 --- a/libcurvecpr/lib/messager.c +++ b/libcurvecpr/lib/messager.c @@ -432,8 +432,9 @@ int curvecpr_messager_process_sendq (struct curvecpr_messager *messager) curvecpr_chicago_refresh_clock(chicago); /* Should we send a block? */ - if (cf->ops.recvmarkq_is_full(messager)) - /* We must send a block, as we can't recv() any more until we do. */ + if (!cf->ops.recvmarkq_is_empty(messager)) + /* Always acknowledge any received data immediately -- not doing so messes up the + RTT calculations for flow control. */ acknowledge = 1; if (chicago->clock >= messager->my_sent_clock + chicago->wr_rate) diff --git a/libcurvecpr/lib/server_recv.c b/libcurvecpr/lib/server_recv.c index 99bb50d..2bff2d7 100644 --- a/libcurvecpr/lib/server_recv.c +++ b/libcurvecpr/lib/server_recv.c @@ -23,7 +23,6 @@ static int _handle_hello (struct curvecpr_server *server, void *priv, const stru /* Dummy initialization. */ curvecpr_session_new(&s); - curvecpr_session_set_priv(&s, priv); /* Verify initial connection parameters. */ curvecpr_bytes_copy(s.their_session_pk, p->client_session_pk, 32); @@ -72,14 +71,14 @@ static int _handle_hello (struct curvecpr_server *server, void *priv, const stru curvecpr_bytes_copy(po.nonce, nonce + 8, 16); curvecpr_bytes_copy(po.box, (const unsigned char *)&po_box + 16, 144); - if (cf->ops.send(server, &s, (const unsigned char *)&po, sizeof(struct curvecpr_packet_cookie))) + if (cf->ops.send(server, &s, priv, (const unsigned char *)&po, sizeof(struct curvecpr_packet_cookie))) return -EINVAL; } return 0; } -static int _handle_initiate (struct curvecpr_server *server, struct curvecpr_session *s, void *priv, const struct curvecpr_packet_initiate *p, const unsigned char *buf, size_t num) +static int _handle_initiate (struct curvecpr_server *server, struct curvecpr_session *s, void *priv, const struct curvecpr_packet_initiate *p, const unsigned char *buf, size_t num, struct curvecpr_session **s_stored) { const struct curvecpr_server_cf *cf = &server->cf; @@ -102,9 +101,8 @@ static int _handle_initiate (struct curvecpr_server *server, struct curvecpr_ses return -EINVAL; s->their_session_nonce = unpacked_nonce; - curvecpr_session_set_priv(s, priv); - if (cf->ops.recv(server, s, data + sizeof(struct curvecpr_packet_initiate_box), num + 16 - sizeof(struct curvecpr_packet_initiate_box))) + if (cf->ops.recv(server, s, priv, data + sizeof(struct curvecpr_packet_initiate_box), num + 16 - sizeof(struct curvecpr_packet_initiate_box))) return -EINVAL; return 0; @@ -173,17 +171,19 @@ static int _handle_initiate (struct curvecpr_server *server, struct curvecpr_ses /* All good, we can go ahead and submit the client for registration. */ s_new.their_session_nonce = curvecpr_bytes_unpack_uint64(p->nonce); curvecpr_bytes_copy(s_new.my_domain_name, p_box->server_domain_name, 256); - curvecpr_session_set_priv(&s_new, priv); - if (cf->ops.put_session(server, &s_new, &s_new_stored)) + if (cf->ops.put_session(server, &s_new, priv, &s_new_stored)) return -EINVAL; /* This can fail for a variety of reasons that are up to the delegate to determine, but two typical ones will be too many connections or an invalid domain name. */ /* Now the session is registered; we can send the encapsulated message. */ - if (cf->ops.recv(server, s_new_stored, data + sizeof(struct curvecpr_packet_initiate_box), num + 16 - sizeof(struct curvecpr_packet_initiate_box))) + if (cf->ops.recv(server, s_new_stored, priv, data + sizeof(struct curvecpr_packet_initiate_box), num + 16 - sizeof(struct curvecpr_packet_initiate_box))) return -EINVAL; + if (s_stored) + *s_stored = s_new_stored; + return 0; } } @@ -209,15 +209,14 @@ static int _handle_client_message (struct curvecpr_server *server, struct curvec return -EINVAL; s->their_session_nonce = unpacked_nonce; - curvecpr_session_set_priv(s, priv); - if (cf->ops.recv(server, s, data + 32, num - 16)) + if (cf->ops.recv(server, s, priv, data + 32, num - 16)) return -EINVAL; return 0; } -int curvecpr_server_recv (struct curvecpr_server *server, void *priv, const unsigned char *buf, size_t num) +int curvecpr_server_recv (struct curvecpr_server *server, void *priv, const unsigned char *buf, size_t num, struct curvecpr_session **s_stored) { const struct curvecpr_server_cf *cf = &server->cf; @@ -245,12 +244,20 @@ int curvecpr_server_recv (struct curvecpr_server *server, void *priv, const unsi if (num < 560) return -EINVAL; - p_initiate = (const struct curvecpr_packet_initiate *)p; + p_initiate = (const struct curvecpr_packet_initiate *)buf; /* Try to get session. */ cf->ops.get_session(server, p_initiate->client_session_pk, &s); - return _handle_initiate(server, s, priv, p_initiate, buf + sizeof(struct curvecpr_packet_initiate), num - sizeof(struct curvecpr_packet_initiate)); + { + struct curvecpr_session *s_return = NULL; + int result = _handle_initiate(server, s, priv, p_initiate, buf + sizeof(struct curvecpr_packet_initiate), num - sizeof(struct curvecpr_packet_initiate), &s_return); + + if (result == 0 && s_stored) + *s_stored = s_return; + + return result; + } } else if (p->id[7] == 'M') { /* Client message packet. */ struct curvecpr_session *s = NULL; @@ -264,7 +271,14 @@ int curvecpr_server_recv (struct curvecpr_server *server, void *priv, const unsi if (cf->ops.get_session(server, p_message->client_session_pk, &s)) return -EINVAL; - return _handle_client_message(server, s, priv, p_message, buf + sizeof(struct curvecpr_packet_client_message), num - sizeof(struct curvecpr_packet_client_message)); + { + int result = _handle_client_message(server, s, priv, p_message, buf + sizeof(struct curvecpr_packet_client_message), num - sizeof(struct curvecpr_packet_client_message)); + + if (result == 0 && s_stored) + *s_stored = s; + + return result; + } } return -EINVAL; diff --git a/libcurvecpr/lib/server_send.c b/libcurvecpr/lib/server_send.c index d84abdc..11d34f1 100644 --- a/libcurvecpr/lib/server_send.c +++ b/libcurvecpr/lib/server_send.c @@ -11,10 +11,9 @@ #include -int curvecpr_server_send (struct curvecpr_server *server, const unsigned char their_session_pk[32], const unsigned char *buf, size_t num) +int curvecpr_server_send (struct curvecpr_server *server, struct curvecpr_session *s, void *priv, const unsigned char *buf, size_t num) { const struct curvecpr_server_cf *cf = &server->cf; - struct curvecpr_session *s; unsigned char nonce[24]; unsigned char p_raw[sizeof(struct curvecpr_packet_server_message) + 1104]; @@ -25,10 +24,6 @@ int curvecpr_server_send (struct curvecpr_server *server, const unsigned char th if (num < 16 || num > 1088 || num & 15) return -EMSGSIZE; - /* Get the session. */ - if (cf->ops.get_session(server, their_session_pk, &s)) - return -EINVAL; - /* Build the box. */ curvecpr_bytes_copy(nonce, "CurveCP-server-M", 16); curvecpr_session_next_nonce(s, nonce + 16); @@ -49,7 +44,7 @@ int curvecpr_server_send (struct curvecpr_server *server, const unsigned char th curvecpr_bytes_copy(p_raw + sizeof(struct curvecpr_packet_server_message), data + 16, num + 16); /* Fire away! */ - if (cf->ops.send(server, s, p_raw, sizeof(struct curvecpr_packet_server_message) + num + 16)) + if (cf->ops.send(server, s, priv, p_raw, sizeof(struct curvecpr_packet_server_message) + num + 16)) return -EINVAL; return 0; diff --git a/libcurvecpr/test/messager/test_send_with_1_failure_moves_message_from_sendq.c b/libcurvecpr/test/messager/test_send_with_1_failure_moves_message_from_sendq.c index 37f72d0..b1314dc 100644 --- a/libcurvecpr/test/messager/test_send_with_1_failure_moves_message_from_sendq.c +++ b/libcurvecpr/test/messager/test_send_with_1_failure_moves_message_from_sendq.c @@ -16,6 +16,11 @@ static unsigned char t_q_is_full (struct curvecpr_messager *messager) return 0; } +static unsigned char t_q_is_empty (struct curvecpr_messager *messager) +{ + return 1; +} + static int t_recvmarkq_get_nth_unacknowledged (struct curvecpr_messager *messager, unsigned int n, struct curvecpr_block **block_stored) { return 1; @@ -63,7 +68,7 @@ START_TEST (test_send_with_1_failure_moves_message_from_sendq) struct curvecpr_messager_cf cf = { .ops = { .sendmarkq_is_full = t_q_is_full, - .recvmarkq_is_full = t_q_is_full, + .recvmarkq_is_empty = t_q_is_empty, .recvmarkq_get_nth_unacknowledged = t_recvmarkq_get_nth_unacknowledged, .sendmarkq_head = t_sendmarkq_head, .sendq_head = t_sendq_head, diff --git a/libcurvecpr/test/util/.gitignore b/libcurvecpr/test/util/.gitignore new file mode 100644 index 0000000..a602f2e --- /dev/null +++ b/libcurvecpr/test/util/.gitignore @@ -0,0 +1 @@ +/test_nanoseconds