Skip to content

Commit aeadf44

Browse files
committed
Restructure and add GLib-based implementations.
This commit restructures the repository into two libraries, libcurvecpr and libcurvecpr-glib. An out-of-the-box working implementation of the messager and client are now provided, making use of data structures from the GLib libraries. Understandably, some authors may not want to make use of GLib-based libraries in their projects, so the implementations are provided as a separate library entire. The --without-glib flag to the configure script can be used to prevent it from being built. A basic client implementation using libcurvecpr and libcurvecpr-glib is now about 150 lines of code, which is actually practical.
1 parent 01b1476 commit aeadf44

43 files changed

Lines changed: 807 additions & 51 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Makefile.am

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
ACLOCAL_AMFLAGS = -I m4
22

3-
SUBDIRS = include lib t
3+
SUBDIRS = libcurvecpr
44

5-
pkgconfigdir = $(libdir)/pkgconfig
6-
pkgconfig_DATA = libcurvecpr.pc
5+
if USE_LIBGLIB
6+
SUBDIRS += libcurvecpr-glib
7+
endif

configure.ac

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ AC_PREREQ(2.67)
22
AC_INIT(libcurvecpr, 0.1.0, nfontes+libcurvecpr@cynigram.com, libcurvecpr)
33
AM_INIT_AUTOMAKE([-Wall -Werror foreign subdir-objects])
44

5-
AC_CONFIG_SRCDIR([include/server.h])
6-
AC_CONFIG_HEADER([include/config.h])
5+
AC_CONFIG_SRCDIR([libcurvecpr/include/server.h])
6+
AC_CONFIG_HEADER([config.h])
77
AC_CONFIG_MACRO_DIR([m4])
88

99
# Library version.
@@ -20,11 +20,24 @@ AC_PROG_MAKE_SET
2020

2121
AM_PROG_CC_C_O
2222

23+
PKG_PROG_PKG_CONFIG
24+
2325
# Checks for libraries.
2426
PKG_CHECK_MODULES([CHECK], [check >= 0.9.8])
2527
AC_SEARCH_LIBS([clock_gettime], [rt posix4])
2628
AC_CHECK_LIB([sodium], [crypto_onetimeauth_poly1305_ref], [], [AC_MSG_ERROR([missing libsodium])])
2729

30+
AC_ARG_WITH([libglib],
31+
[AS_HELP_STRING([--with-libglib], [build with glib support @<:@default=check@:>@])],
32+
[], [with_libglib=check]
33+
)
34+
AS_CASE(["$with_glib"],
35+
[yes], [PKG_CHECK_MODULES([GLIB], [glib-2.0 >= 2.14.0], [HAVE_LIBGLIB=1])],
36+
[no], [],
37+
[PKG_CHECK_MODULES([GLIB], [glib-2.0 >= 2.14.0], [HAVE_LIBGLIB=1], [HAVE_LIBGLIB=0])]
38+
)
39+
AM_CONDITIONAL([USE_LIBGLIB], [test "$with_libglib" != no -a "$HAVE_LIBGLIB" -eq 1])
40+
2841
# Checks for header files.
2942
AC_HEADER_STDC
3043
AC_CHECK_HEADERS([errno.h stdint.h string.h time.h], [], [AC_MSG_ERROR([missing required header file(s)])])
@@ -62,10 +75,16 @@ AX_APPEND_LINK_FLAGS([-Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack], [], [$CCHECKF
6275

6376
# Done!
6477
AC_CONFIG_FILES([
78+
libcurvecpr/include/Makefile
79+
libcurvecpr/lib/Makefile
80+
libcurvecpr/test/Makefile
81+
libcurvecpr/Makefile
82+
libcurvecpr/libcurvecpr.pc
83+
libcurvecpr-glib/include/Makefile
84+
libcurvecpr-glib/lib/Makefile
85+
libcurvecpr-glib/test/Makefile
86+
libcurvecpr-glib/Makefile
87+
libcurvecpr-glib/libcurvecpr-glib.pc
6588
Makefile
66-
include/Makefile
67-
lib/Makefile
68-
t/Makefile
69-
libcurvecpr.pc
7089
])
7190
AC_OUTPUT

libcurvecpr-glib/Makefile.am

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
SUBDIRS = include lib test
2+
3+
pkgconfigdir = $(libdir)/pkgconfig
4+
pkgconfig_DATA = libcurvecpr-glib.pc
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
curvecprglibincludedir = $(includedir)/curvecpr/glib
2+
curvecprglibinclude_HEADERS = \
3+
client_messager_glib.h \
4+
curvecpr_glib.h \
5+
messager_glib.h
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#ifndef __CURVECPR_GLIB_CLIENT_MESSAGER_GLIB_H
2+
#define __CURVECPR_GLIB_CLIENT_MESSAGER_GLIB_H
3+
4+
#include "block.h"
5+
#include "client.h"
6+
#include "messager_glib.h"
7+
8+
#include <string.h>
9+
10+
#include <sodium/crypto_uint64.h>
11+
12+
struct curvecpr_client_messager_glib;
13+
14+
struct curvecpr_client_messager_glib_ops {
15+
int (*send)(struct curvecpr_client_messager_glib *cmg, const unsigned char *buf, size_t num);
16+
int (*recv)(struct curvecpr_client_messager_glib *cmg, const unsigned char *buf, size_t num);
17+
void (*finished)(struct curvecpr_client_messager_glib *cmg, enum curvecpr_block_eofflag flag);
18+
19+
int (*next_nonce)(struct curvecpr_client_messager_glib *cmg, unsigned char *destination, size_t num);
20+
};
21+
22+
struct curvecpr_client_messager_glib_cf {
23+
/* Any extensions. */
24+
unsigned char my_extension[16];
25+
26+
/* Curve25519 public/private keypairs. */
27+
unsigned char my_global_pk[32];
28+
unsigned char my_global_sk[32];
29+
30+
/* Server configuration. */
31+
unsigned char their_extension[16];
32+
unsigned char their_global_pk[32];
33+
unsigned char their_domain_name[256];
34+
35+
/* Messager configuration. */
36+
crypto_uint64 pending_maximum;
37+
unsigned int sendmarkq_maximum;
38+
unsigned int recvmarkq_maximum;
39+
40+
struct curvecpr_client_messager_glib_ops ops;
41+
42+
void *priv;
43+
};
44+
45+
struct curvecpr_client_messager_glib {
46+
struct curvecpr_client_messager_glib_cf cf;
47+
48+
struct curvecpr_client client;
49+
struct curvecpr_messager_glib mg;
50+
};
51+
52+
void curvecpr_client_messager_glib_new (struct curvecpr_client_messager_glib *cmg, struct curvecpr_client_messager_glib_cf *cf);
53+
void curvecpr_client_messager_glib_dealloc (struct curvecpr_client_messager_glib *cmg);
54+
int curvecpr_client_messager_glib_connected (struct curvecpr_client_messager_glib *cmg);
55+
int curvecpr_client_messager_glib_send (struct curvecpr_client_messager_glib *cmg, const unsigned char *buf, size_t num);
56+
int curvecpr_client_messager_glib_close (struct curvecpr_client_messager_glib *cmg);
57+
int curvecpr_client_messager_glib_recv (struct curvecpr_client_messager_glib *cmg, const unsigned char *buf, size_t num);
58+
int curvecpr_client_messager_glib_process_sendq (struct curvecpr_client_messager_glib *cmg);
59+
long long curvecpr_client_messager_glib_next_timeout (struct curvecpr_client_messager_glib *cmg);
60+
61+
#endif
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#ifndef __CURVECPR_GLIB_CURVECPR_GLIB_H
2+
#define __CURVECPR_GLIB_CURVECPR_GLIB_H
3+
4+
#include "client_messager_glib.h"
5+
#include "messager_glib.h"
6+
7+
#endif
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#ifndef __CURVECPR_GLIB_MESSAGER_GLIB_H
2+
#define __CURVECPR_GLIB_MESSAGER_GLIB_H
3+
4+
#include "block.h"
5+
#include "messager.h"
6+
7+
#include <string.h>
8+
9+
#include <sodium/crypto_uint64.h>
10+
11+
#include <glib.h>
12+
13+
struct curvecpr_messager_glib;
14+
15+
struct curvecpr_messager_glib_ops {
16+
int (*send)(struct curvecpr_messager_glib *mg, const unsigned char *buf, size_t num);
17+
int (*recv)(struct curvecpr_messager_glib *mg, const unsigned char *buf, size_t num);
18+
void (*finished)(struct curvecpr_messager_glib *mg, enum curvecpr_block_eofflag flag);
19+
};
20+
21+
struct curvecpr_messager_glib_cf {
22+
crypto_uint64 pending_maximum;
23+
unsigned int sendmarkq_maximum;
24+
unsigned int recvmarkq_maximum;
25+
26+
struct curvecpr_messager_glib_ops ops;
27+
28+
void *priv;
29+
};
30+
31+
struct curvecpr_messager_glib {
32+
struct curvecpr_messager_glib_cf cf;
33+
34+
struct curvecpr_messager messager;
35+
36+
unsigned char sendq_head_exists;
37+
struct curvecpr_block sendq_head;
38+
39+
unsigned char *pending;
40+
crypto_uint64 pending_used;
41+
unsigned char pending_eof;
42+
crypto_uint64 pending_current;
43+
crypto_uint64 pending_next;
44+
45+
GSequence *sendmarkq;
46+
GSequence *recvmarkq;
47+
crypto_uint64 recvmarkq_distributed;
48+
};
49+
50+
void curvecpr_messager_glib_new (struct curvecpr_messager_glib *mg, struct curvecpr_messager_glib_cf *cf, unsigned char client);
51+
void curvecpr_messager_glib_dealloc (struct curvecpr_messager_glib *mg);
52+
int curvecpr_messager_glib_close (struct curvecpr_messager_glib *mg);
53+
int curvecpr_messager_glib_send (struct curvecpr_messager_glib *mg, const unsigned char *buf, size_t num);
54+
int curvecpr_messager_glib_recv (struct curvecpr_messager_glib *mg, const unsigned char *buf, size_t num);
55+
int curvecpr_messager_glib_process_sendq (struct curvecpr_messager_glib *mg);
56+
long long curvecpr_messager_glib_next_timeout (struct curvecpr_messager_glib *mg);
57+
58+
#endif

libcurvecpr-glib/lib/Makefile.am

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
lib_LTLIBRARIES = libcurvecpr-glib.la
2+
3+
libcurvecpr_glib_la_CPPFLAGS = -I$(top_srcdir)/libcurvecpr/include -I$(top_srcdir)/libcurvecpr-glib/include
4+
libcurvecpr_glib_la_CFLAGS = @GLIB_CFLAGS@
5+
libcurvecpr_glib_la_LDFLAGS = -version-info $(CURVECPR_LIBRARY_VERSION) @GLIB_LIBS@ -L$(top_srcdir)/libcurvecpr/lib -lcurvecpr
6+
libcurvecpr_glib_la_SOURCES = \
7+
client_messager_glib.c \
8+
messager_glib.c
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
#include "config.h"
2+
3+
#include "bytes.h"
4+
#include "client.h"
5+
#include "client_messager_glib.h"
6+
#include "messager_glib.h"
7+
8+
static int _client_send (struct curvecpr_client *client, const unsigned char *buf, size_t num)
9+
{
10+
struct curvecpr_client_messager_glib *cmg = client->cf.priv;
11+
12+
return cmg->cf.ops.send(cmg, buf, num);
13+
}
14+
15+
static int _client_recv (struct curvecpr_client *client, const unsigned char *buf, size_t num)
16+
{
17+
struct curvecpr_client_messager_glib *cmg = client->cf.priv;
18+
19+
return curvecpr_messager_glib_recv(&cmg->mg, buf, num);
20+
}
21+
22+
static int _client_next_nonce(struct curvecpr_client *client, unsigned char *destination, size_t num)
23+
{
24+
struct curvecpr_client_messager_glib *cmg = client->cf.priv;
25+
26+
return cmg->cf.ops.next_nonce(cmg, destination, num);
27+
}
28+
29+
static int _messager_glib_send (struct curvecpr_messager_glib *mg, const unsigned char *buf, size_t num)
30+
{
31+
struct curvecpr_client_messager_glib *cmg = mg->cf.priv;
32+
33+
return curvecpr_client_send(&cmg->client, buf, num);
34+
}
35+
36+
static int _messager_glib_recv (struct curvecpr_messager_glib *mg, const unsigned char *buf, size_t num)
37+
{
38+
struct curvecpr_client_messager_glib *cmg = mg->cf.priv;
39+
40+
return cmg->cf.ops.recv(cmg, buf, num);
41+
}
42+
43+
static void _messager_glib_finished (struct curvecpr_messager_glib *mg, enum curvecpr_block_eofflag flag)
44+
{
45+
struct curvecpr_client_messager_glib *cmg = mg->cf.priv;
46+
47+
if (cmg->cf.ops.finished)
48+
cmg->cf.ops.finished(cmg, flag);
49+
}
50+
51+
void curvecpr_client_messager_glib_new (struct curvecpr_client_messager_glib *cmg, struct curvecpr_client_messager_glib_cf *cf)
52+
{
53+
struct curvecpr_client_cf client_cf = {
54+
.ops = {
55+
.send = _client_send,
56+
.recv = _client_recv,
57+
58+
.next_nonce = _client_next_nonce
59+
},
60+
.priv = cmg
61+
};
62+
63+
struct curvecpr_messager_glib_cf mg_cf = {
64+
.ops = {
65+
.send = _messager_glib_send,
66+
.recv = _messager_glib_recv,
67+
.finished = _messager_glib_finished
68+
},
69+
.priv = cmg
70+
};
71+
72+
curvecpr_bytes_zero(cmg, sizeof(struct curvecpr_client_messager_glib));
73+
74+
if (cf)
75+
curvecpr_bytes_copy(&cmg->cf, cf, sizeof(struct curvecpr_client_messager_glib_cf));
76+
77+
/* Client configuration. */
78+
curvecpr_bytes_copy(client_cf.my_extension, cmg->cf.my_extension, 16);
79+
80+
curvecpr_bytes_copy(client_cf.my_global_pk, cmg->cf.my_global_pk, 32);
81+
curvecpr_bytes_copy(client_cf.my_global_sk, cmg->cf.my_global_sk, 32);
82+
83+
curvecpr_bytes_copy(client_cf.their_extension, cmg->cf.their_extension, 16);
84+
curvecpr_bytes_copy(client_cf.their_global_pk, cmg->cf.their_global_pk, 32);
85+
curvecpr_bytes_copy(client_cf.their_domain_name, cmg->cf.their_domain_name, 256);
86+
87+
/* Messager configuration. */
88+
mg_cf.pending_maximum = cmg->cf.pending_maximum;
89+
mg_cf.sendmarkq_maximum = cmg->cf.sendmarkq_maximum;
90+
mg_cf.recvmarkq_maximum = cmg->cf.recvmarkq_maximum;
91+
92+
/* Initialize client and messager. */
93+
curvecpr_client_new(&cmg->client, &client_cf);
94+
curvecpr_messager_glib_new(&cmg->mg, &mg_cf, 1);
95+
}
96+
97+
void curvecpr_client_messager_glib_dealloc (struct curvecpr_client_messager_glib *cmg)
98+
{
99+
curvecpr_messager_glib_dealloc(&cmg->mg);
100+
}
101+
102+
int curvecpr_client_messager_glib_connected (struct curvecpr_client_messager_glib *cmg)
103+
{
104+
return curvecpr_client_connected(&cmg->client);
105+
}
106+
107+
int curvecpr_client_messager_glib_send (struct curvecpr_client_messager_glib *cmg, const unsigned char *buf, size_t num)
108+
{
109+
return curvecpr_messager_glib_send(&cmg->mg, buf, num);
110+
}
111+
112+
int curvecpr_client_messager_glib_close (struct curvecpr_client_messager_glib *cmg)
113+
{
114+
return curvecpr_messager_glib_close(&cmg->mg);
115+
}
116+
117+
int curvecpr_client_messager_glib_recv (struct curvecpr_client_messager_glib *cmg, const unsigned char *buf, size_t num)
118+
{
119+
return curvecpr_client_recv(&cmg->client, buf, num);
120+
}
121+
122+
int curvecpr_client_messager_glib_process_sendq (struct curvecpr_client_messager_glib *cmg)
123+
{
124+
return curvecpr_messager_glib_process_sendq(&cmg->mg);
125+
}
126+
127+
long long curvecpr_client_messager_glib_next_timeout (struct curvecpr_client_messager_glib *cmg)
128+
{
129+
return curvecpr_messager_glib_next_timeout(&cmg->mg);
130+
}

0 commit comments

Comments
 (0)