summaryrefslogtreecommitdiff
path: root/ecp
diff options
context:
space:
mode:
authorUros Majstorovic <majstor@majstor.org>2024-05-16 01:44:50 +0200
committerUros Majstorovic <majstor@majstor.org>2024-05-16 01:44:50 +0200
commit7f06f7fb26ee52547d30b620a4137bca0395da6d (patch)
tree43a6bee535afcef99f6ed9c35cc5ce5f927dfd47 /ecp
parente870fe53f71c88c636b7c4aadd5655c0fe15bde3 (diff)
open handler moved to the end of open message; conn authenticator has access to remainder of open req msg; improved logging and error handling; removed extensions
Diffstat (limited to 'ecp')
-rw-r--r--ecp/README4
-rw-r--r--ecp/server/dir.c4
-rw-r--r--ecp/server/server.c74
-rw-r--r--ecp/server/server.h8
-rw-r--r--ecp/src/ecp/common.mk17
-rw-r--r--ecp/src/ecp/core.c278
-rw-r--r--ecp/src/ecp/core.h33
-rw-r--r--ecp/src/ecp/crypto/test/Makefile1
-rw-r--r--ecp/src/ecp/dir/dir.c5
-rw-r--r--ecp/src/ecp/ext.h24
-rw-r--r--ecp/src/ecp/ext/Makefile21
-rw-r--r--ecp/src/ecp/ext/msgq.c150
-rw-r--r--ecp/src/ecp/ext/msgq.h20
-rw-r--r--ecp/src/ecp/ext/rbuf.c125
-rw-r--r--ecp/src/ecp/ext/rbuf.h125
-rw-r--r--ecp/src/ecp/ext/rbuf_ext.c57
-rw-r--r--ecp/src/ecp/ext/rbuf_recv.c489
-rw-r--r--ecp/src/ecp/ext/rbuf_send.c422
-rwxr-xr-xecp/src/ecp/htable/hashtable.c1
-rw-r--r--ecp/src/ecp/timer.c27
-rw-r--r--ecp/src/ecp/vconn/vconn.c22
-rw-r--r--ecp/src/platform/fe310/features_tmpl.mk1
-rw-r--r--ecp/src/platform/fe310/transport.c11
-rw-r--r--ecp/src/platform/posix/features_tmpl.mk3
-rw-r--r--ecp/test/dir.c2
-rw-r--r--ecp/test/init.c2
-rw-r--r--ecp/test/init_vconn.c2
27 files changed, 213 insertions, 1715 deletions
diff --git a/ecp/README b/ecp/README
index cee6d1d..ff4600b 100644
--- a/ecp/README
+++ b/ecp/README
@@ -3,10 +3,10 @@ To build this library you will need GNU Make installed and any modern C compiler
First you need to create features.mk makefile:
cp src/platform/posix/features_tmpl.mk src/platform/posix/features.mk
-Edit it according to your needs. Defaults are fine. Note that with_rbuf and with_msgq are experimental features and should not be used in production.
+Edit it according to your needs. Defaults are fine.
To (re)build library, utilities and tests simply do:
- ./build.sh (MAKE=gmake ./build.sh on *BSD systems)
+ ./build.sh
In tests subdirectory you will find simple test examples. To test vc_server and vc_client (vconn feature) do:
mkdir test/keys
diff --git a/ecp/server/dir.c b/ecp/server/dir.c
index 13bea68..95db4b5 100644
--- a/ecp/server/dir.c
+++ b/ecp/server/dir.c
@@ -898,11 +898,11 @@ void dir_init_switch(ECPSocket *sock, int init_ann) {
sleep(1);
}
dir_announce_block();
- LOG(LOG_DEBUG, "init switch sleeping for %ds...\n", init_ann);
+ LOG(LOG_DEBUG, "dir_init_switch: sleeping for %ds...\n", init_ann);
sleep(init_ann);
dir_online_switch(sock, 0);
dir_announce_allow();
- LOG(LOG_DEBUG, "init switch sleeping for %ds...\n", init_ann);
+ LOG(LOG_DEBUG, "dir_init_switch: sleeping for %ds...\n", init_ann);
sleep(init_ann);
}
diff --git a/ecp/server/server.c b/ecp/server/server.c
index 1ec55f8..3fc0e7c 100644
--- a/ecp/server/server.c
+++ b/ecp/server/server.c
@@ -24,6 +24,8 @@
static SRVConfig srv_config;
static int proc_forked = 0;
static FILE *log_file = NULL;
+
+int log_level;
static const char *log_level_str[] = {
"ERROR",
"INFO",
@@ -91,32 +93,44 @@ static void conn_free(ECPConnection *conn) {
free(conn);
}
-static int conn_auth(ECPSocket *sock, ECPConnection *parent, unsigned char ctype, ecp_ecdh_public_t *public) {
+static ssize_t conn_auth(ECPSocket *sock, ECPConnection *parent, unsigned char ctype, ecp_ecdh_public_t *public, unsigned char *msg, size_t msg_size) {
switch (ctype) {
case CTYPE_DIR: {
- if (public == NULL) return 0;
- return acl_inlist(public);
+ if (public == NULL) return ECP_ERR_AUTH;
+ if (!acl_inlist(public)) return ECP_ERR_AUTH;
+ return 0;
}
case ECP_CTYPE_VLINK: {
- if (public == NULL) return 0;
- if (parent == NULL) return acl_inlist(public);
- return 1;
+ if (public == NULL) return ECP_ERR_AUTH;
+ if ((parent == NULL) && !acl_inlist(public)) return ECP_ERR_AUTH;
+ return 0;
}
case ECP_CTYPE_DIR:
case ECP_CTYPE_VCONN:
- return 1;
+ return 0;
default:
- return 0;
+ return ECP_ERR_AUTH;
}
}
+static int core_logger(const char *format, ...) {
+ va_list ap;
+ FILE *file = log_file ? log_file : stderr;
+
+ va_start(ap, format);
+ log_vfprintf(LOG_ERR, file, format, ap);
+ va_end(ap);
+
+ return 0;
+}
+
int ecp_init(ECPContext *ctx, ECPConnHandler *vconn_handler, ECPConnHandler *vlink_handler) {
int rv;
- rv = ecp_ctx_init(ctx, conn_auth, conn_new, conn_free, handle_err);
+ rv = ecp_ctx_init(ctx, conn_auth, conn_new, conn_free, handle_err, core_logger);
if (rv) return rv;
rv = ecp_vconn_handler_init(ctx, vconn_handler);
@@ -128,11 +142,12 @@ int ecp_init(ECPContext *ctx, ECPConnHandler *vconn_handler, ECPConnHandler *vli
return ECP_OK;
}
-void log_vfprintf(int level, FILE *file, char *format, va_list ap) {
+void log_vfprintf(int level, FILE *file, const char *format, va_list ap) {
time_t t;
char t_buf[26];
char s_buf[256];
+ if (level > log_level) return;
if (level >= (sizeof(log_level_str) / sizeof(char *))) return;
t = time(NULL);
@@ -146,16 +161,18 @@ void log_vfprintf(int level, FILE *file, char *format, va_list ap) {
fflush(file);
}
-void log_printf(int level, char *format, ...) {
+void log_printf(int level, const char *format, ...) {
va_list ap;
FILE *file = log_file ? log_file : stderr;
+ if (level > log_level) return;
+
va_start(ap, format);
log_vfprintf(level, file, format, ap);
va_end(ap);
}
-static void fail(char *format, ...) {
+static void fail(const char *format, ...) {
va_list ap;
FILE *file;
@@ -179,8 +196,11 @@ static void usage(char *arg) {
fprintf(stderr, "\t-d\n");
fprintf(stderr, "\t\tdetach\n");
- fprintf(stderr, "\t-l <log file>\n");
- fprintf(stderr, "\t\twrite optput to <log file>\n");
+ fprintf(stderr, "\t-l <level>\n");
+ fprintf(stderr, "\t\tset log level: 0 - error, 1 - info, 2 - debug\n");
+
+ fprintf(stderr, "\t-f <log file>\n");
+ fprintf(stderr, "\t\twrite log to <log file>\n");
fprintf(stderr, "\t-u <uid>\n");
fprintf(stderr, "\t\trun as user <uid>\n");
@@ -201,7 +221,8 @@ static void daemonize(void) {
proc_forked = 1;
- if (setsid() < 0) fail("setsid failed\n");
+ pid = setsid()
+ if (pid < 0) fail("setsid failed\n");
/* second fork */
pid = fork();
@@ -210,18 +231,18 @@ static void daemonize(void) {
/* redirect stdio to /dev/null */
null_rd = open("/dev/null", O_RDONLY);
- if (null_rd == -1) fail("open (r) /dev/null failed\n");
+ if (null_rd < 0) fail("open (r) /dev/null failed\n");
null_wr = open("/dev/null", O_WRONLY);
- if (null_wr == -1) fail("open (w) /dev/null failed\n");
+ if (null_wr < 0) fail("open (w) /dev/null failed\n");
rv = dup2(null_rd, STDIN_FILENO);
- if (rv == -1) fail("dup2 (stdin) failed\n");
+ if (rv < 0) fail("dup2 (stdin) failed\n");
rv = dup2(null_wr, STDOUT_FILENO);
- if (rv == -1) fail("dup2 (stdout) failed\n");
+ if (rv < 0) fail("dup2 (stdout) failed\n");
rv = dup2(null_wr, STDERR_FILENO);
- if (rv == -1) fail("dup2 (stderr) failed\n");
+ if (rv < 0) fail("dup2 (stderr) failed\n");
close(null_rd);
close(null_wr);
@@ -263,11 +284,24 @@ int main(int argc, char *argv[]) {
detach = 1;
break;
}
+
case 'l': {
_argi++;
_argc--;
if (_argc == 0) usage(argv[0]);
log_fn = strdup(argv[_argi]);
+ log_level = (uint8_t)strtol(argv[_argi], &endptr, 10);
+ if ((endptr[0] != '\0') || log_level > LOG_MAX_LEVEL) fail("Bad log level\n");
+ _argi++;
+ _argc--;
+ break;
+ }
+
+ case 'f': {
+ _argi++;
+ _argc--;
+ if (_argc == 0) usage(argv[0]);
+ log_fn = strdup(argv[_argi]);
_argi++;
_argc--;
break;
diff --git a/ecp/server/server.h b/ecp/server/server.h
index 47c3a8c..3c720ba 100644
--- a/ecp/server/server.h
+++ b/ecp/server/server.h
@@ -2,8 +2,8 @@
#define LOG_INFO 1
#define LOG_DEBUG 2
-#define LOG_LEVEL LOG_DEBUG
-#define LOG(l, ...) { if (l <= LOG_LEVEL) log_printf(l, __VA_ARGS__); }
+#define LOG_MAX_LEVEL 2
+#define LOG(l, ...) { log_printf(l, __VA_ARGS__); }
typedef struct SRVConfig {
ECPDHKey key_perma;
@@ -15,5 +15,5 @@ typedef struct SRVConfig {
} SRVConfig;
SRVConfig *srv_get_config(void);
-void log_vfprintf(int level, FILE *file, char *format, va_list ap);
-void log_printf(int level, char *format, ...);
+void log_vfprintf(int level, FILE *file, const char *format, va_list ap);
+void log_printf(int level, const char *format, ...);
diff --git a/ecp/src/ecp/common.mk b/ecp/src/ecp/common.mk
index 60f7087..d23c68d 100644
--- a/ecp/src/ecp/common.mk
+++ b/ecp/src/ecp/common.mk
@@ -23,24 +23,7 @@ CFLAGS += -DECP_WITH_VCONN=1
subdirs += vconn
endif
-ifeq ($(with_rbuf),yes)
-CFLAGS += -DECP_WITH_RBUF=1
-ext_subdir = yes
-endif
-
-ifeq ($(with_msgq),yes)
-CFLAGS += -DECP_WITH_MSGQ=1
-endif
-
-ifdef ext_subdir
-subdirs += ext
-endif
-
ifeq ($(with_dir),yes)
CFLAGS += -DECP_WITH_DIR=1
subdirs += dir
endif
-
-ifeq ($(with_debug),yes)
-CFLAGS += -DECP_DEBUG=1
-endif
diff --git a/ecp/src/ecp/core.c b/ecp/src/ecp/core.c
index 92b0ef5..6934587 100644
--- a/ecp/src/ecp/core.c
+++ b/ecp/src/ecp/core.c
@@ -1,12 +1,7 @@
#include <stdlib.h>
#include <string.h>
-#ifdef ECP_DEBUG
-#include <stdio.h>
-#endif
-
#include "core.h"
-#include "ext.h"
#ifdef ECP_WITH_VCONN
#include "vconn/vconn.h"
@@ -30,7 +25,7 @@ int ecp_dhkey_gen(ECPDHKey *key) {
return ECP_OK;
}
-int ecp_ctx_init(ECPContext *ctx, ecp_conn_auth_t conn_auth, ecp_conn_new_t conn_new, ecp_conn_free_t conn_free, ecp_err_handler_t handle_err) {
+int ecp_ctx_init(ECPContext *ctx, ecp_conn_auth_t conn_auth, ecp_conn_new_t conn_new, ecp_conn_free_t conn_free, ecp_err_handler_t handle_err, ecp_logger_t logger) {
int rv;
memset(ctx, 0, sizeof(ECPContext));
@@ -38,6 +33,7 @@ int ecp_ctx_init(ECPContext *ctx, ecp_conn_auth_t conn_auth, ecp_conn_new_t conn
ctx->conn_new = conn_new;
ctx->conn_free = conn_free;
ctx->handle_err = handle_err;
+ ctx->logger = logger;
rv = ecp_tr_init(ctx);
if (rv) return rv;
@@ -1214,17 +1210,6 @@ int ecp_conn_create(ECPConnection *conn, ECPConnection *parent) {
}
#endif
- rv = ecp_ext_conn_create(conn);
- if (rv) {
-#ifdef ECP_WITH_VCONN
- if (conn->parent) ecp_conn_refcount_dec(conn->parent);
-#endif
-#ifdef ECP_WITH_PTHREAD
- pthread_mutex_destroy(&conn->mutex);
-#endif
-
- return ECP_ERR;
- }
if (!ecp_conn_is_gc(conn)) conn->refcount++;
return ECP_OK;
@@ -1234,7 +1219,7 @@ int ecp_conn_create_inb(ECPConnection *conn, ECPConnection *parent, unsigned cha
ECPSocket *sock = conn->sock;
int rv;
- if (ecp_conn_has_vbox(conn) && ((rkey_perma == NULL) || !rkey_perma->valid)) return ECP_ERR_VBOX;
+ if (ecp_conn_has_vbox(conn) && ((rkey_perma == NULL) || !rkey_perma->valid)) return ECP_ERR_AUTH;
ecp_conn_set_inb(conn);
@@ -1282,8 +1267,6 @@ void ecp_conn_destroy(ECPConnection *conn) {
if (conn->parent) ecp_conn_refcount_dec(conn->parent);
#endif
- ecp_ext_conn_destroy(conn);
-
#ifdef ECP_WITH_PTHREAD
pthread_mutex_destroy(&conn->mutex);
#endif
@@ -1371,7 +1354,7 @@ int _ecp_conn_remove(ECPConnection *conn) {
#endif
if (!_ecp_conn_is_reg(conn)) rv = ECP_ERR_CLOSED;
- if (!rv && _ecp_conn_is_open(conn)) rv = ECP_ERR_BUSY;
+ if (!rv && _ecp_conn_is_open_p(conn)) rv = ECP_ERR_BUSY;
if (!rv) {
_ecp_conn_clr_reg(conn);
conn_table_remove(conn);
@@ -1505,9 +1488,9 @@ void _ecp_conn_close(ECPConnection *conn) {
ecp_close_handler_t handler;
_ecp_conn_clr_open(conn);
+ _ecp_conn_clr_open_p(conn);
handler = ecp_get_close_handler(conn);
if (handler) handler(conn);
- ecp_ext_conn_close(conn);
}
ecp_conn_destroy(conn);
}
@@ -1799,9 +1782,6 @@ void ecp_err_handle(ECPConnection *conn, unsigned char mtype, int err) {
ecp_err_handler_t err_handler;
int rv;
- rv = ecp_ext_err_handle(conn, mtype, err);
- if (rv != ECP_PASS) return;
-
if (err == ECP_ERR_CLOSED) return;
err_handler = ecp_get_err_handler(conn);
@@ -1863,6 +1843,7 @@ static ssize_t _retry_ireq(ECPConnection *conn, ECPTimerItem *ti) {
pthread_mutex_unlock(&conn->mutex);
pthread_mutex_unlock(&sock->conn_table.mutex);
#endif
+
if (rv) return rv;
if (is_open) {
@@ -2090,16 +2071,17 @@ ssize_t ecp_handle_open_req(ECPSocket *sock, ECPConnection *parent, unsigned cha
ecp_ecdh_shkey(&vbox_shkey, &rkey_perma.public, &key_perma.private);
rv = ecp_aead_dec(vbox_buf, sizeof(vbox_buf), msg, msg_size, &vbox_shkey, &vbox_nonce, ECP_NTYPE_VBOX);
- if (rv < 0) return ECP_ERR_VBOX;
+ if (rv < 0) return ECP_ERR_AUTH;
- if (memcmp(vbox_buf, public_buf, ECP_SIZE_ECDH_PUB) != 0) return ECP_ERR_VBOX;
- if (memcmp(vbox_buf+ECP_SIZE_ECDH_PUB, &key_curr.public, ECP_SIZE_ECDH_PUB) != 0) return ECP_ERR_VBOX;
+ if (memcmp(vbox_buf, public_buf, ECP_SIZE_ECDH_PUB) != 0) return ECP_ERR_AUTH;
+ if (memcmp(vbox_buf+ECP_SIZE_ECDH_PUB, &key_curr.public, ECP_SIZE_ECDH_PUB) != 0) return ECP_ERR_AUTH;
rkey_perma.valid = 1;
}
+ rv = 0;
if (sock->ctx->conn_auth) {
- _rv = sock->ctx->conn_auth(sock, parent, ctype, rkey_perma.valid ? &rkey_perma.public : NULL);
- if (!_rv) return ECP_ERR_VBOX;
+ rv = sock->ctx->conn_auth(sock, parent, ctype, rkey_perma.valid ? &rkey_perma.public : NULL, msg, msg_size);
+ if (rv < 0) return rv;
}
conn = ecp_conn_new_inb(sock, parent, ctype);
@@ -2118,8 +2100,7 @@ ssize_t ecp_handle_open_req(ECPSocket *sock, ECPConnection *parent, unsigned cha
}
*_conn = conn;
- /* handle_open will be called from ecp_unpack */
- return 2+(vbox ? ECP_SIZE_VBOX : 0);
+ return 2+(vbox ? ECP_SIZE_VBOX : 0) + rv;
}
ssize_t ecp_send_open_rep(ECPConnection *conn) {
@@ -2139,100 +2120,65 @@ ssize_t ecp_send_open_rep(ECPConnection *conn) {
return rv;
}
-ssize_t ecp_handle_open(ECPConnection *conn, unsigned char mtype, unsigned char *msg, size_t msg_size, ECP2Buffer *bufs) {
+ssize_t ecp_handle_open_rep(ECPConnection *conn, unsigned char mtype, unsigned char *msg, size_t msg_size, ECP2Buffer *bufs) {
+ if (ecp_conn_is_inb(conn)) return ECP_ERR;
+ return 0;
+}
+
+int ecp_handle_open(ECPConnection *conn, ECP2Buffer *bufs) {
ecp_open_handler_t handler;
- int is_reg, is_open, is_gc;
- int _rv = ECP_OK;
- ssize_t rv;
+ int is_reg, is_gc;
+ int rv = ECP_OK;
#ifdef ECP_WITH_PTHREAD
pthread_mutex_lock(&conn->mutex);
#endif
is_reg = _ecp_conn_is_reg(conn);
- is_open = _ecp_conn_is_open(conn);
- if (!is_reg) _rv = ECP_ERR_CLOSED;
- if (!_rv && is_open) _rv = ECP_ERR;
+ if (!is_reg) rv = ECP_ERR_CLOSED;
- /* set open if registered, avoids race condition with timeout handler */
- if (!_rv) _ecp_conn_set_open(conn);
+ /* set open_p if registered, avoids race condition with timeout handler */
+ if (!rv) _ecp_conn_set_open_p(conn);
#ifdef ECP_WITH_PTHREAD
pthread_mutex_unlock(&conn->mutex);
#endif
- if (_rv) return _rv;
-
- if (mtype == ECP_MTYPE_OPEN_REQ) {
- if (ecp_conn_is_outb(conn)) {
- _rv = ECP_ERR;
- goto handle_open_fin;
- }
-
- rv = 2;
- if (msg_size < rv) {
- _rv = ECP_ERR_SIZE;
- goto handle_open_fin;
- }
-
- if (msg[1]) rv += ECP_SIZE_VBOX;
- if (msg_size < rv) {
- _rv = ECP_ERR_SIZE;
- goto handle_open_fin;
- }
- } else {
- if (ecp_conn_is_inb(conn)) {
- _rv = ECP_ERR;
- goto handle_open_fin;
- }
- rv = 0;
- }
+ if (rv) return rv;
is_gc = ecp_conn_is_gc(conn);
if (is_gc) {
- _rv = ecp_conn_insert_gc(conn);
- if (_rv) goto handle_open_fin;
- }
-
- _rv = ecp_ext_conn_open(conn);
- if (_rv) {
- if (is_gc) ecp_conn_remove_gc(conn);
- goto handle_open_fin;
+ rv = ecp_conn_insert_gc(conn);
+ if (rv) return rv;
}
handler = ecp_get_open_handler(conn);
if (handler) {
- _rv = handler(conn, bufs);
- if (_rv) {
+ rv = handler(conn, bufs);
+ if (rv) {
if (is_gc) ecp_conn_remove_gc(conn);
- ecp_ext_conn_close(conn);
- goto handle_open_fin;
+ if (rv) return rv;
}
}
- if (ecp_conn_is_inb(conn)) {
- ecp_tr_release(bufs->packet, 1);
- ecp_send_open_rep(conn);
- } else if (ecp_conn_is_root(conn)) {
- ecp_conn_remove_addr(conn);
- }
-
-handle_open_fin:
- if (_rv) {
#ifdef ECP_WITH_PTHREAD
- pthread_mutex_lock(&conn->mutex);
+ pthread_mutex_lock(&conn->mutex);
#endif
- _ecp_conn_clr_open(conn);
+ _ecp_conn_set_open(conn);
#ifdef ECP_WITH_PTHREAD
- pthread_mutex_unlock(&conn->mutex);
+ pthread_mutex_unlock(&conn->mutex);
#endif
- return _rv;
+ if (ecp_conn_is_inb(conn)) {
+ ecp_tr_release(bufs->packet, 1);
+ ecp_send_open_rep(conn);
+ } else if (ecp_conn_is_root(conn)) {
+ ecp_conn_remove_addr(conn);
}
- return rv;
+ return ECP_OK;
}
static ssize_t _send_kxreq(ECPConnection *conn, ECPTimerItem *ti) {
@@ -2322,16 +2268,15 @@ ssize_t ecp_handle_keyx(ECPConnection *conn, unsigned char mtype, unsigned char
ssize_t ecp_msg_handle(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, size_t msg_size, ECP2Buffer *bufs) {
if (mtype & ECP_MTYPE_FLAG_SYS) {
switch (mtype) {
- case ECP_MTYPE_OPEN_REQ:
case ECP_MTYPE_OPEN_REP:
- return ecp_handle_open(conn, mtype, msg, msg_size, bufs);
+ return ecp_handle_open_rep(conn, mtype, msg, msg_size, bufs);
case ECP_MTYPE_KEYX_REQ:
case ECP_MTYPE_KEYX_REP:
return ecp_handle_keyx(conn, mtype, msg, msg_size, bufs);
default:
- return ecp_ext_msg_handle(conn, seq, mtype, msg, msg_size, bufs);
+ return ECP_ERR_MTYPE;
}
} else {
ecp_msg_handler_t handler;
@@ -2345,51 +2290,39 @@ ssize_t ecp_msg_handle(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype,
}
}
-ssize_t ecp_pld_handle_one(ECPConnection *conn, ecp_seq_t seq, unsigned char *payload, size_t pld_size, ECP2Buffer *bufs) {
+ssize_t ecp_pld_handle(ECPConnection *conn, ecp_seq_t seq, unsigned char *payload, size_t _pld_size, ECP2Buffer *bufs) {
unsigned char mtype;
unsigned char *msg;
unsigned char frag_cnt, frag_tot;
- size_t hdr_size, msg_size;
+ size_t pld_size, hdr_size, msg_size;
ssize_t rv;
int _rv;
- _rv = ecp_pld_get_type(payload, pld_size, &mtype);
- if (_rv) return _rv;
-
- frag_cnt = 0;
- frag_tot = 0;
- if (mtype & ECP_MTYPE_FLAG_FRAG) {
- _rv = ecp_pld_get_frag(payload, pld_size, &frag_cnt, &frag_tot, NULL);
+ pld_size = _pld_size;
+ while (pld_size) {
+ _rv = ecp_pld_get_type(payload, pld_size, &mtype);
if (_rv) return _rv;
- }
-
- ecp_timer_pop(conn, mtype, seq, frag_cnt, frag_tot);
-
- msg = ecp_pld_get_msg(payload, pld_size);
- if (msg == NULL) return ECP_ERR;
- hdr_size = msg - payload;
- msg_size = pld_size - hdr_size;
-
- rv = ecp_msg_handle(conn, seq, mtype, msg, msg_size, bufs);
- if (rv < 0) {
- ecp_err_handle(conn, mtype, rv);
- return rv;
- }
- rv += hdr_size;
+ frag_cnt = 0;
+ frag_tot = 0;
+ if (mtype & ECP_MTYPE_FLAG_FRAG) {
+ _rv = ecp_pld_get_frag(payload, pld_size, &frag_cnt, &frag_tot, NULL);
+ if (_rv) return _rv;
+ }
- if (rv > pld_size) return ECP_ERR_SIZE;
- return rv;
-}
+ ecp_timer_pop(conn, mtype, seq, frag_cnt, frag_tot);
-ssize_t ecp_pld_handle(ECPConnection *conn, ecp_seq_t seq, unsigned char *payload, size_t _pld_size, ECP2Buffer *bufs) {
- size_t pld_size = _pld_size;
- ssize_t rv;
+ msg = ecp_pld_get_msg(payload, pld_size);
+ if (msg == NULL) return ECP_ERR;
+ hdr_size = msg - payload;
+ msg_size = pld_size - hdr_size;
- while (pld_size) {
- rv = ecp_pld_handle_one(conn, seq, payload, pld_size, bufs);
+ rv = ecp_msg_handle(conn, seq, mtype, msg, msg_size, bufs);
if (rv < 0) return rv;
+ rv += hdr_size;
+ if (rv > pld_size) return ECP_ERR_SIZE;
+
payload += rv;
pld_size -= rv;
}
@@ -2397,7 +2330,7 @@ ssize_t ecp_pld_handle(ECPConnection *conn, ecp_seq_t seq, unsigned char *payloa
return _pld_size;
}
-ssize_t ecp_unpack(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t *addr, ECP2Buffer *bufs, size_t _pkt_size, ECPConnection **_conn, unsigned char **_payload, ecp_seq_t *_seq) {
+ssize_t ecp_unpack(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t *addr, ECP2Buffer *bufs, size_t _pkt_size, ECPConnection **_conn, unsigned char **_payload, ecp_seq_t *_seq, int *_is_open_msg) {
ECPConnection *conn = NULL;
unsigned char idx;
unsigned char s_idx;
@@ -2633,14 +2566,13 @@ ssize_t ecp_unpack(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t *addr,
hdr_size = msg - payload;
msg_size = pld_size - hdr_size;
+ rv = 0;
if (conn == NULL) {
switch (mtype) {
case ECP_MTYPE_INIT_REQ: {
rv = ecp_handle_init_req(sock, parent, addr, c_idx, public_buf, msg, msg_size, &shkey, bufs);
if (rv < 0) goto unpack_fin;
- payload += (hdr_size + rv);
- pld_size -= (hdr_size + rv);
break;
}
@@ -2648,7 +2580,6 @@ ssize_t ecp_unpack(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t *addr,
rv = ecp_handle_open_req(sock, parent, s_idx, c_idx, public_buf, msg, msg_size, &shkey, &conn);
if (rv < 0) goto unpack_fin;
- /* pass to open handler */
nonce_in = nonce_pkt;
nonce_map = ECP_ACK_FULL;
is_open_msg = 1;
@@ -2665,8 +2596,6 @@ ssize_t ecp_unpack(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t *addr,
rv = ecp_handle_init_rep(conn, msg, msg_size, nonce_buf, bufs);
if (rv < 0) goto unpack_fin;
- payload += (hdr_size + rv);
- pld_size -= (hdr_size + rv);
break;
}
@@ -2683,6 +2612,11 @@ ssize_t ecp_unpack(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t *addr,
goto unpack_fin;
}
}
+
+ if (rv) {
+ payload += (hdr_size + rv);
+ pld_size -= (hdr_size + rv);
+ }
}
if (is_open || is_open_msg) {
@@ -2703,21 +2637,10 @@ ssize_t ecp_unpack(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t *addr,
pthread_mutex_unlock(&conn->mutex);
#endif
- if (is_open_msg) {
- rv = ecp_pld_handle_one(conn, seq, payload, pld_size, bufs);
- if (rv < 0) {
- ecp_conn_remove(conn);
- if (!ecp_conn_is_gc(conn)) ecp_conn_refcount_dec(conn);
- goto unpack_fin;
- }
-
- payload += rv;
- pld_size -= rv;
- }
-
*_conn = conn;
*_payload = payload;
*_seq = seq;
+ *_is_open_msg = is_open_msg;
}
rv = _pkt_size - pld_size;
@@ -2731,10 +2654,12 @@ ssize_t ecp_pkt_handle(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t *ad
ECPConnection *conn;
unsigned char *payload;
ecp_seq_t seq;
+ int is_open_msg;
size_t pld_size;
ssize_t rv;
+ int _rv;
- rv = ecp_unpack(sock, parent, addr, bufs, pkt_size, &conn, &payload, &seq);
+ rv = ecp_unpack(sock, parent, addr, bufs, pkt_size, &conn, &payload, &seq, &is_open_msg);
if (rv < 0) return rv;
pld_size = pkt_size - rv;
@@ -2743,27 +2668,26 @@ ssize_t ecp_pkt_handle(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t *ad
return pkt_size;
}
- if (pld_size) {
- rv = ecp_ext_pld_handle(conn, seq, payload, pld_size, bufs);
- if (rv < 0) goto pkt_handle_fin;
+ _rv = ECP_OK;
+ rv = ecp_pld_handle(conn, seq, payload, pld_size, bufs);
+ if (rv < 0) _rv = rv;
- payload += rv;
- pld_size -= rv;
- }
+ if (is_open_msg) {
+ if (!_rv) _rv = ecp_handle_open(conn, bufs);
+ if (_rv) {
+ unsigned char mtype;
- if (pld_size) {
- rv = ecp_pld_handle(conn, seq, payload, pld_size, bufs);
- if (rv < 0) goto pkt_handle_fin;
+ ecp_conn_remove(conn);
- payload += rv;
- pld_size -= rv;
+ mtype = ecp_conn_is_inb(conn) ? ECP_MTYPE_OPEN_REQ : ECP_MTYPE_OPEN_REP;
+ ecp_err_handle(conn, mtype, _rv);
+ rv = 0;
+ }
}
- rv = pkt_size - pld_size;
-
-pkt_handle_fin:
ecp_conn_refcount_dec(conn);
- return rv;
+ if (rv < 0) return rv;
+ return pkt_size;
}
ssize_t ecp_pkt_send(ECPSocket *sock, ECPBuffer *packet, size_t pkt_size, unsigned char flags, ECPTimerItem *ti, ecp_tr_addr_t *addr) {
@@ -3147,9 +3071,6 @@ ssize_t _ecp_pld_send(ECPConnection *conn, ECPBuffer *packet, unsigned char s_id
if (rv < 0) return rv;
pkt_size = rv;
- rv = ecp_ext_pld_send(conn, payload, pld_size, packet, pkt_size, flags, ti, &addr);
- if (rv) return rv;
-
rv = ecp_pkt_send(conn->sock, packet, pkt_size, flags, ti, &addr);
if (rv < 0) return rv;
@@ -3212,7 +3133,7 @@ ssize_t ecp_msg_send(ECPConnection *conn, unsigned char mtype, unsigned char *ms
return msg_size;
}
-static int recv_p(ECPSocket *sock, ecp_tr_addr_t *addr, ECPBuffer *packet, size_t size) {
+static ssize_t recv_p(ECPSocket *sock, ecp_tr_addr_t *addr, ECPBuffer *packet, size_t pkt_size) {
ECP2Buffer bufs;
ECPBuffer payload;
unsigned char pld_buf[ECP_MAX_PLD];
@@ -3224,20 +3145,19 @@ static int recv_p(ECPSocket *sock, ecp_tr_addr_t *addr, ECPBuffer *packet, size_
payload.buffer = pld_buf;
payload.size = ECP_MAX_PLD;
- rv = ecp_pkt_handle(sock, NULL, addr, &bufs, size);
- if (rv < 0) return rv;
-
- return ECP_OK;
+ rv = ecp_pkt_handle(sock, NULL, addr, &bufs, pkt_size);
+ return rv;
}
-int ecp_receiver(ECPSocket *sock) {
- ecp_tr_addr_t addr;
+void ecp_receiver(ECPSocket *sock) {
+ ECPContext *ctx;
ECPBuffer packet;
unsigned char pkt_buf[ECP_MAX_PKT];
+ ecp_tr_addr_t addr;
ecp_sts_t next = 0;
ssize_t rv;
- int _rv;
+ ctx = sock->ctx;
sock->running = 1;
while(sock->running) {
packet.buffer = pkt_buf;
@@ -3245,17 +3165,17 @@ int ecp_receiver(ECPSocket *sock) {
rv = ecp_tr_recv(sock, &packet, &addr, next ? next : ECP_POLL_TIMEOUT);
if (rv > 0) {
- _rv = recv_p(sock, &addr, &packet, rv);
+ size_t pkt_size = rv;
-#ifdef ECP_DEBUG
- if (_rv) printf("RECEIVE ERR:%d\n", _rv);
-#endif
+ rv = recv_p(sock, &addr, &packet, pkt_size);
+ if ((rv < 0) && ctx->logger) {
+ int _rv = rv;
+ ctx->logger("ecp_receiver: err:%d\n", rv);
+ }
}
next = ecp_timer_exe(sock);
}
-
- return ECP_OK;
}
#ifdef ECP_WITH_PTHREAD
diff --git a/ecp/src/ecp/core.h b/ecp/src/ecp/core.h
index f5d21bb..c5b18ce 100644
--- a/ecp/src/ecp/core.h
+++ b/ecp/src/ecp/core.h
@@ -7,8 +7,7 @@
#endif
#define ECP_OK 0
-#define ECP_PASS 1
-#define ECP_ITR_END 2
+#define ECP_ITR_END 1
#define ECP_ERR -1
#define ECP_ERR_TIMEOUT -2
@@ -26,7 +25,7 @@
#define ECP_ERR_PKT -20
#define ECP_ERR_ZPAD -21
#define ECP_ERR_COOKIE -22
-#define ECP_ERR_VBOX -23
+#define ECP_ERR_AUTH -23
#define ECP_ERR_KEYID -24
#define ECP_ERR_SEQ -25
#define ECP_ERR_FRAG -26
@@ -166,10 +165,11 @@
/* mutable flags */
#define ECP_CONN_FLAG_REG 0x01
#define ECP_CONN_FLAG_OPEN 0x02
-#define ECP_CONN_FLAG_CLOSED 0x04
-#define ECP_CONN_FLAG_GCT 0x08
+#define ECP_CONN_FLAG_OPEN_P 0x04
+#define ECP_CONN_FLAG_CLOSED 0x08
+#define ECP_CONN_FLAG_GCT 0x10
-#define ECP_CONN_FLAG_UMASK 0xF0
+#define ECP_CONN_FLAG_UMASK 0xE0
#define ECP_SEND_FLAG_REPLY 0x01
#define ECP_SEND_FLAG_MORE 0x02
@@ -182,6 +182,7 @@
#define ecp_conn_is_sys(conn) ((conn)->type & ECP_CTYPE_FLAG_SYS)
#define _ecp_conn_is_reg(conn) ((conn)->flags & ECP_CONN_FLAG_REG)
#define _ecp_conn_is_open(conn) ((conn)->flags & ECP_CONN_FLAG_OPEN)
+#define _ecp_conn_is_open_p(conn) ((conn)->flags & ECP_CONN_FLAG_OPEN_P)
#define _ecp_conn_is_closed(conn) ((conn)->flags & ECP_CONN_FLAG_CLOSED)
#define _ecp_conn_in_gct(conn) ((conn)->flags & ECP_CONN_FLAG_GCT)
@@ -189,11 +190,13 @@
#define ecp_conn_set_outb(conn) ((conn)->flags_im &= ~ECP_CONN_FLAG_INB)
#define _ecp_conn_set_reg(conn) ((conn)->flags |= ECP_CONN_FLAG_REG)
#define _ecp_conn_set_open(conn) ((conn)->flags |= ECP_CONN_FLAG_OPEN)
+#define _ecp_conn_set_open_p(conn) ((conn)->flags |= ECP_CONN_FLAG_OPEN_P)
#define _ecp_conn_set_closed(conn) ((conn)->flags |= ECP_CONN_FLAG_CLOSED)
#define _ecp_conn_push_gct(conn) ((conn)->flags |= ECP_CONN_FLAG_GCT)
#define _ecp_conn_clr_reg(conn) ((conn)->flags &= ~ECP_CONN_FLAG_REG)
#define _ecp_conn_clr_open(conn) ((conn)->flags &= ~ECP_CONN_FLAG_OPEN)
+#define _ecp_conn_clr_open_p(conn) ((conn)->flags &= ~ECP_CONN_FLAG_OPEN_P)
#define _ecp_conn_clr_closed(conn) ((conn)->flags &= ~ECP_CONN_FLAG_CLOSED)
#define _ecp_conn_pull_gct(conn) ((conn)->flags &= ~ECP_CONN_FLAG_GCT)
@@ -234,14 +237,15 @@ struct ECPConnection;
typedef int (*ecp_conn_expired_t) (struct ECPConnection *conn, ecp_sts_t now);
-typedef void (*ecp_err_handler_t) (struct ECPConnection *conn, unsigned char mtype, int err);
+typedef ssize_t (*ecp_conn_auth_t) (struct ECPSocket *sock, struct ECPConnection *parent, unsigned char ctype, ecp_ecdh_public_t *pub, unsigned char *msg, size_t msg_size);
typedef struct ECPConnection * (*ecp_conn_new_t) (struct ECPSocket *sock, struct ECPConnection *parent, unsigned char type);
typedef void (*ecp_conn_free_t) (struct ECPConnection *conn);
-typedef int (*ecp_conn_auth_t) (struct ECPSocket *sock, struct ECPConnection *parent, unsigned char ctype, ecp_ecdh_public_t *pub);
+typedef void (*ecp_err_handler_t) (struct ECPConnection *conn, unsigned char mtype, int err);
+typedef int (*ecp_logger_t) (const char *fmt, ...);
-typedef ssize_t (*ecp_msg_handler_t) (struct ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, size_t msg_size, struct ECP2Buffer *b);
typedef int (*ecp_open_handler_t) (struct ECPConnection *conn, struct ECP2Buffer *b);
typedef void (*ecp_close_handler_t) (struct ECPConnection *conn);
+typedef ssize_t (*ecp_msg_handler_t) (struct ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, size_t msg_size, struct ECP2Buffer *b);
typedef ssize_t (*ecp_oreq_send_t) (struct ECPConnection *conn, unsigned char *cookie);
typedef struct ECPBuffer {
@@ -299,6 +303,7 @@ typedef struct ECPContext {
ecp_conn_new_t conn_new; /* inbound connections only */
ecp_conn_free_t conn_free;
ecp_err_handler_t handle_err;
+ ecp_logger_t logger;
ECPConnHandler *handler[ECP_MAX_CTYPE];
ECPConnHandler *handler_sys[ECP_MAX_CTYPE_SYS];
} ECPContext;
@@ -386,7 +391,7 @@ typedef struct ECPConnection {
int ecp_dhkey_gen(ECPDHKey *key);
-int ecp_ctx_init(ECPContext *ctx, ecp_conn_auth_t conn_auth, ecp_conn_new_t conn_new, ecp_conn_free_t conn_free, ecp_err_handler_t handle_err);
+int ecp_ctx_init(ECPContext *ctx, ecp_conn_auth_t conn_auth, ecp_conn_new_t conn_new, ecp_conn_free_t conn_free, ecp_err_handler_t handle_err, ecp_logger_t logger);
int ecp_ctx_set_handler(ECPContext *ctx, unsigned char ctype, ECPConnHandler *handler);
ECPConnHandler *ecp_ctx_get_handler(ECPContext *ctx, unsigned char ctype);
@@ -482,17 +487,17 @@ ssize_t ecp_write_open_req(ECPConnection *conn, ECPBuffer *payload);
ssize_t ecp_send_open_req(ECPConnection *conn, unsigned char *cookie);
ssize_t ecp_handle_open_req(ECPSocket *sock, ECPConnection *parent, unsigned char s_idx, unsigned char c_idx, unsigned char *public_buf, unsigned char *msg, size_t msg_size, ecp_aead_key_t *shkey, ECPConnection **_conn);
ssize_t ecp_send_open_rep(ECPConnection *conn);
-ssize_t ecp_handle_open(ECPConnection *conn, unsigned char mtype, unsigned char *msg, size_t msg_size, ECP2Buffer *bufs);
+ssize_t ecp_handle_open_rep(ECPConnection *conn, unsigned char mtype, unsigned char *msg, size_t msg_size, ECP2Buffer *bufs);
+int ecp_handle_open(ECPConnection *conn, ECP2Buffer *bufs);
ssize_t ecp_send_keyx_req(ECPConnection *conn, int retry);
ssize_t ecp_send_keyx_rep(ECPConnection *conn);
ssize_t ecp_handle_keyx(ECPConnection *conn, unsigned char mtype, unsigned char *msg, size_t msg_size, ECP2Buffer *bufs);
ssize_t ecp_msg_handle(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, size_t msg_size, ECP2Buffer *bufs);
-ssize_t ecp_pld_handle_one(ECPConnection *conn, ecp_seq_t seq, unsigned char *payload, size_t pld_size, ECP2Buffer *bufs);
ssize_t ecp_pld_handle(ECPConnection *conn, ecp_seq_t seq, unsigned char *payload, size_t _pld_size, ECP2Buffer *bufs);
-ssize_t ecp_unpack(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t *addr, ECP2Buffer *bufs, size_t _pkt_size, ECPConnection **_conn, unsigned char **_payload, ecp_seq_t *_seq);
+ssize_t ecp_unpack(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t *addr, ECP2Buffer *bufs, size_t _pkt_size, ECPConnection **_conn, unsigned char **_payload, ecp_seq_t *_seq, int *_is_open_msg);
ssize_t ecp_pkt_handle(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t *addr, ECP2Buffer *bufs, size_t pkt_size);
ssize_t ecp_pkt_send(ECPSocket *sock, ECPBuffer *packet, size_t pkt_size, unsigned char flags, ECPTimerItem *ti, ecp_tr_addr_t *addr);
@@ -524,6 +529,6 @@ ssize_t ecp_pld_send_wnonce(ECPConnection *conn, ECPBuffer *packet, ECPBuffer *p
ssize_t ecp_pld_send_irep(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t *addr, ECPBuffer *packet, ECPPktMeta *pkt_meta, ECPBuffer *payload, size_t pld_size, unsigned char flags);
ssize_t ecp_msg_send(ECPConnection *conn, unsigned char mtype, unsigned char *msg, size_t msg_size);
-int ecp_receiver(ECPSocket *sock);
+void ecp_receiver(ECPSocket *sock);
int ecp_start_receiver(ECPSocket *sock);
int ecp_stop_receiver(ECPSocket *sock);
diff --git a/ecp/src/ecp/crypto/test/Makefile b/ecp/src/ecp/crypto/test/Makefile
index f5e955b..497c2ed 100644
--- a/ecp/src/ecp/crypto/test/Makefile
+++ b/ecp/src/ecp/crypto/test/Makefile
@@ -31,4 +31,5 @@ ed25519_open: ed25519_open.o
clean:
rm -f *.o
+ rm -f msg.*
rm -f aead aead_enc aead_dec ed25519 ed25519_sign ed25519_open
diff --git a/ecp/src/ecp/dir/dir.c b/ecp/src/ecp/dir/dir.c
index 6312eb7..59764bb 100644
--- a/ecp/src/ecp/dir/dir.c
+++ b/ecp/src/ecp/dir/dir.c
@@ -40,9 +40,8 @@ size_t ecp_dir_parse_item(ECPDirItem *item, unsigned char *buf) {
buf += sizeof(addr->host);
rsize += sizeof(addr->host);
- addr->port = \
- ((uint16_t)buf[0] << 8) | \
- ((uint16_t)buf[1]);
+ addr->port = (uint16_t)buf[0] << 8;
+ addr->port |= (uint16_t)buf[1];
buf += sizeof(uint16_t);
rsize += sizeof(uint16_t);
diff --git a/ecp/src/ecp/ext.h b/ecp/src/ecp/ext.h
deleted file mode 100644
index 835aa95..0000000
--- a/ecp/src/ecp/ext.h
+++ /dev/null
@@ -1,24 +0,0 @@
-#ifdef ECP_WITH_RBUF
-
-int ecp_ext_err_handle(ECPConnection *conn, unsigned char mtype, int err);
-int ecp_ext_conn_create(ECPConnection *conn);
-void ecp_ext_conn_destroy(ECPConnection *conn);
-int ecp_ext_conn_open(ECPConnection *conn);
-void ecp_ext_conn_close(ECPConnection *conn);
-
-ssize_t ecp_ext_msg_handle(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, size_t msg_size, ECP2Buffer *bufs);
-ssize_t ecp_ext_pld_handle(ECPConnection *conn, ecp_seq_t seq, unsigned char *payload, size_t pld_size, ECP2Buffer *bufs);
-ssize_t ecp_ext_pld_send(ECPConnection *conn, ECPBuffer *payload, size_t pld_size, ECPBuffer *packet, size_t pkt_size, unsigned char flags, ECPTimerItem *ti, ecp_tr_addr_t *addr);
-
-#else
-
-#define ecp_ext_err_handle(c,t,e) (ECP_PASS)
-#define ecp_ext_conn_create(c) (0)
-#define ecp_ext_conn_destroy(c) ;
-#define ecp_ext_conn_open(c) (ECP_OK)
-#define ecp_ext_conn_close(c) ;
-#define ecp_ext_msg_handle(c,s,t,m,sz,b) (0)
-#define ecp_ext_pld_handle(c,s,p,sz,b) (0)
-#define ecp_ext_pld_send(c,p1,sz1,p2,sz2,f,t,a) (0)
-
-#endif
diff --git a/ecp/src/ecp/ext/Makefile b/ecp/src/ecp/ext/Makefile
deleted file mode 100644
index 67c9cbc..0000000
--- a/ecp/src/ecp/ext/Makefile
+++ /dev/null
@@ -1,21 +0,0 @@
-include ../common.mk
-
-obj =
-
-ifeq ($(with_rbuf),yes)
-obj += rbuf.o rbuf_send.o rbuf_recv.o rbuf_ext.o
-endif
-
-ifeq ($(with_msgq),yes)
-obj += msgq.o
-endif
-
-
-%.o: %.c
- $(CC) $(CFLAGS) -c $<
-
-all: $(obj)
- $(AR) rcs libecpext.a $(obj)
-
-clean:
- rm -f *.o *.a
diff --git a/ecp/src/ecp/ext/msgq.c b/ecp/src/ecp/ext/msgq.c
deleted file mode 100644
index 7172e51..0000000
--- a/ecp/src/ecp/ext/msgq.c
+++ /dev/null
@@ -1,150 +0,0 @@
-#include <sys/time.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include <ecp/core.h>
-
-#include "rbuf.h"
-#include "msgq.h"
-
-#define MSGQ_IDX_MASK(idx) ((idx) & ((ECP_MSGQ_MAX_MSG) - 1))
-
-static struct timespec *abstime_ts(struct timespec *ts, ecp_sts_t msec) {
- struct timeval tv;
- uint64_t us_start;
-
- gettimeofday(&tv, NULL);
- us_start = tv.tv_sec * (uint64_t) 1000000 + tv.tv_usec;
- us_start += msec * 1000;
- ts->tv_sec = us_start / 1000000;
- ts->tv_nsec = (us_start % 1000000) * 1000;
- return ts;
-}
-
-void ecp_msgq_init(ECPRBConn *conn, ECPMsgQ *msgq) {
- memset(msgq, 0, sizeof(ECPMsgQ));
-
- conn->recv->msgq = msgq;
-}
-
-int ecp_msgq_create(ECPRBConn *conn) {
- ECPMsgQ *msgq = conn->recv->msgq;
- int i, rv;
-
- for (i=0; i<ECP_MSGQ_MAX_MTYPE; i++) {
- rv = pthread_cond_init(&msgq->cond[i], NULL);
- if (rv) {
- int j;
-
- for (j=0; j<i; j++) {
- pthread_cond_destroy(&msgq->cond[j]);
- }
- return ECP_ERR;
- }
- }
-
- return ECP_OK;
-}
-
-void ecp_msgq_destroy(ECPRBConn *conn) {
- ECPMsgQ *msgq = conn->recv->msgq;
- int i;
-
- for (i=0; i<ECP_MSGQ_MAX_MTYPE; i++) {
- pthread_cond_destroy(&msgq->cond[i]);
- }
-}
-
-void ecp_msgq_start(ECPRBConn *conn, ecp_seq_t seq) {
- ECPMsgQ *msgq = conn->recv->msgq;
-
- msgq->seq_max = seq;
- msgq->seq_start = seq + 1;
-}
-
-int ecp_msgq_push(ECPRBConn *conn, ecp_seq_t seq, unsigned char mtype) {
- ECPRBRecv *buf = conn->recv;
- ECPMsgQ *msgq = buf->msgq;
-
- if (mtype >= ECP_MSGQ_MAX_MTYPE) return ECP_ERR_MTYPE;
-
- if ((unsigned short)(msgq->idx_w[mtype] - msgq->idx_r[mtype]) == ECP_MSGQ_MAX_MSG) return ECP_ERR_FULL;
- if (msgq->idx_w[mtype] == msgq->idx_r[mtype]) pthread_cond_signal(&msgq->cond[mtype]);
-
- msgq->seq_msg[mtype][MSGQ_IDX_MASK(msgq->idx_w[mtype])] = seq;
- msgq->idx_w[mtype]++;
-
- if (ECP_SEQ_LT(msgq->seq_max, seq)) msgq->seq_max = seq;
-
- return ECP_OK;
-}
-
-ssize_t ecp_msgq_pop(ECPRBConn *conn, unsigned char mtype, unsigned char *msg, size_t _msg_size, ecp_sts_t timeout) {
- ECPRBRecv *buf = conn->recv;
- ECPMsgQ *msgq = buf->msgq;
- ECPRBuffer *rbuf = &buf->rbuf;
- unsigned char *pld_buf;
- unsigned char *msg_buf;
- size_t pld_size, hdr_size, msg_size;
- ecp_seq_t seq;
- unsigned short idx;
- int rv;
-
- if (mtype >= ECP_MSGQ_MAX_MTYPE) return ECP_ERR_MTYPE;
-
- if (msgq->idx_r[mtype] == msgq->idx_w[mtype]) {
- if (timeout == -1) {
- pthread_cond_wait(&msgq->cond[mtype], &buf->mutex);
- } else {
- struct timespec ts;
-
- rv = pthread_cond_timedwait(&msgq->cond[mtype], &buf->mutex, abstime_ts(&ts, timeout));
- if (rv) return ECP_ERR_TIMEOUT;
- }
- }
- seq = msgq->seq_msg[mtype][MSGQ_IDX_MASK(msgq->idx_r[mtype])];
-
- rv = _ecp_rbuf_msg_idx(rbuf, seq, &idx);
- if (rv) return ECP_ERR;
-
- pld_buf = rbuf->arr.pld[idx].buf;
- pld_size = rbuf->arr.pld[idx].size;
-
- msg_buf = ecp_pld_get_msg(pld_buf, pld_size);
- if (msg_buf == NULL) return ECP_ERR;
- hdr_size = msg_buf - pld_buf;
- msg_size = pld_size - hdr_size;
-
- rbuf->arr.pld[idx].flags &= ~ECP_RBUF_FLAG_IN_MSGQ;
- // if (rbuf->arr.pld[idx].flags == 0);
-
- msgq->idx_r[mtype]++;
- if (msgq->seq_start == seq) {
- int i;
- unsigned short msg_cnt = msgq->seq_max - msgq->seq_start + 1;
-
- for (i=0; i<msg_cnt; i++) {
- if (rbuf->arr.pld[idx].flags & ECP_RBUF_FLAG_IN_MSGQ) break;
- idx = ECP_RBUF_IDX_MASK(idx + 1, rbuf->arr_size);
- }
- msgq->seq_start += i;
- }
-
- if (_msg_size < msg_size) return ECP_ERR_FULL;
- if (msg_size) memcpy(msg, msg_buf, msg_size);
- return msg_size;
-}
-
-ssize_t ecp_msg_receive(ECPConnection *conn, unsigned char mtype, unsigned char *msg, size_t msg_size, ecp_sts_t timeout) {
- ECPRBConn *_conn;
- ssize_t rv;
-
- _conn = ecp_rbuf_get_rbconn(conn);
- if (_conn == NULL) return ECP_ERR;
-
- pthread_mutex_lock(&_conn->recv->mutex);
- rv = ecp_msgq_pop(_conn, mtype, msg, msg_size, timeout);
- pthread_mutex_unlock(&_conn->recv->mutex);
-
- return rv;
-}
diff --git a/ecp/src/ecp/ext/msgq.h b/ecp/src/ecp/ext/msgq.h
deleted file mode 100644
index 669a5af..0000000
--- a/ecp/src/ecp/ext/msgq.h
+++ /dev/null
@@ -1,20 +0,0 @@
-#define ECP_MSGQ_MAX_MSG 32
-
-#define ECP_MSGQ_MAX_MTYPE (ECP_MAX_MTYPE)
-
-typedef struct ECPMsgQ {
- unsigned short idx_w[ECP_MSGQ_MAX_MTYPE];
- unsigned short idx_r[ECP_MSGQ_MAX_MTYPE];
- ecp_seq_t seq_start;
- ecp_seq_t seq_max;
- ecp_seq_t seq_msg[ECP_MSGQ_MAX_MTYPE][ECP_MSGQ_MAX_MSG];
- pthread_cond_t cond[ECP_MSGQ_MAX_MTYPE];
-} ECPMsgQ;
-
-void ecp_msgq_init(ECPRBConn *conn, ECPMsgQ *msgq);
-int ecp_msgq_create(ECPRBConn *conn);
-void ecp_msgq_destroy(ECPRBConn *conn);
-void ecp_msgq_start(ECPRBConn *conn, ecp_seq_t seq);
-int ecp_msgq_push(ECPRBConn *conn, ecp_seq_t seq, unsigned char mtype);
-ssize_t ecp_msgq_pop(ECPRBConn *conn, unsigned char mtype, unsigned char *msg, size_t _msg_size, ecp_sts_t timeout);
-ssize_t ecp_msg_receive(ECPConnection *conn, unsigned char mtype, unsigned char *msg, size_t msg_size, ecp_sts_t timeout); \ No newline at end of file
diff --git a/ecp/src/ecp/ext/rbuf.c b/ecp/src/ecp/ext/rbuf.c
deleted file mode 100644
index e1fab7b..0000000
--- a/ecp/src/ecp/ext/rbuf.c
+++ /dev/null
@@ -1,125 +0,0 @@
-#include <stdlib.h>
-
-#include <ecp/core.h>
-
-#include "rbuf.h"
-
-ECPRBConn *ecp_rbuf_get_rbconn(ECPConnection *conn) {
- if (ecp_conn_has_rbuf(conn)) return (ECPRBConn *)conn;
- return NULL;
-}
-
-ECPConnection *ecp_rbuf_get_conn(ECPRBConn *conn) {
- return &conn->b;
-}
-
-void _ecp_rbuf_start(ECPRBuffer *rbuf, ecp_seq_t seq) {
- rbuf->seq_max = seq;
- rbuf->seq_start = seq + 1;
-}
-
-int _ecp_rbuf_msg_idx(ECPRBuffer *rbuf, ecp_seq_t seq, unsigned short *idx) {
- ecp_seq_t seq_offset = seq - rbuf->seq_start;
-
- /* This also checks for seq_start <= seq if seq type range >> rbuf->arr_size */
- if (seq_offset >= rbuf->arr_size) return ECP_ERR_FULL;
-
- if (idx) *idx = ECP_RBUF_IDX_MASK(rbuf->idx_start + seq_offset, rbuf->arr_size);
- return ECP_OK;
-}
-
-void ecp_rbuf_init(ECPRBConn *conn) {
- ECPConnection *_conn = ecp_rbuf_get_conn(conn);
-
- ecp_conn_set_flags(_conn, ECP_CONN_FLAG_RBUF);
- conn->send = NULL;
- conn->recv = NULL;
-}
-
-int ecp_rbuf_create(ECPRBConn *conn) {
- int rv;
-
- if (conn->send) {
- rv = ecp_rbsend_create(conn);
- if (rv) return rv;
- }
- if (conn->recv) {
- rv = ecp_rbrecv_create(conn);
- if (rv) {
- if (conn->send) ecp_rbsend_destroy(conn);
- return rv;
- }
- }
-
- return ECP_OK;
-}
-
-void ecp_rbuf_destroy(ECPRBConn *conn) {
- if (conn->send) ecp_rbsend_destroy(conn);
- if (conn->recv) ecp_rbrecv_destroy(conn);
-}
-
-void ecp_rbuf_start(ECPRBConn *conn) {
- ECPConnection *_conn = ecp_rbuf_get_conn(conn);
-
- if (conn->send) {
- ecp_seq_t seq_out;
-
-#ifdef ECP_WITH_PTHREAD
- pthread_mutex_lock(&_conn->mutex);
-#endif
-
- seq_out = (ecp_seq_t)(_conn->nonce_out);
-
-#ifdef ECP_WITH_PTHREAD
- pthread_mutex_unlock(&_conn->mutex);
-#endif
-
- ecp_rbsend_start(conn, seq_out);
- }
-
- if (conn->recv) {
- ecp_seq_t seq_in;
-
-#ifdef ECP_WITH_PTHREAD
- pthread_mutex_lock(&_conn->mutex);
-#endif
-
- seq_in = (ecp_seq_t)(_conn->nonce_in);
-
-#ifdef ECP_WITH_PTHREAD
- pthread_mutex_unlock(&_conn->mutex);
-#endif
-
- ecp_rbrecv_start(conn, seq_in);
- }
-}
-
-ssize_t ecp_rbuf_msg_handle(ECPRBConn *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, size_t msg_size, ECP2Buffer *bufs) {
- switch (mtype) {
- case ECP_MTYPE_RBACK:
- if (conn->send) return ecp_rbuf_handle_ack(conn, msg, msg_size);
- break;
-
- case ECP_MTYPE_RBNOP:
- if (conn->recv) return ecp_rbuf_handle_nop(conn, msg, msg_size);
- break;
-
- case ECP_MTYPE_RBFLUSH:
- if (conn->recv) return ecp_rbuf_handle_flush(conn);
- break;
-
- default:
- break;
- }
-
- return ECP_ERR_MTYPE;
-}
-
-int ecp_rbuf_err_handle(ECPRBConn *conn, unsigned char mtype, int err) {
- if (conn->recv && (mtype == ECP_MTYPE_RBTIMER)) {
- ecp_rbuf_handle_timer(conn);
- return ECP_OK;
- }
- return ECP_PASS;
-}
diff --git a/ecp/src/ecp/ext/rbuf.h b/ecp/src/ecp/ext/rbuf.h
deleted file mode 100644
index bf6e6df..0000000
--- a/ecp/src/ecp/ext/rbuf.h
+++ /dev/null
@@ -1,125 +0,0 @@
-#define ECP_RBUF_FLAG_IN_RBUF 0x01
-#define ECP_RBUF_FLAG_IN_MSGQ 0x02
-#define ECP_RBUF_FLAG_IN_TIMER 0x04
-#define ECP_RBUF_FLAG_SKIP 0x08
-
-#define ECP_RBUF_FLAG_CCONTROL 0x01
-#define ECP_RBUF_FLAG_RELIABLE 0x02
-
-
-#define ECP_MTYPE_RBNOP (0x08 | ECP_MTYPE_FLAG_SYS)
-#define ECP_MTYPE_RBACK (0x09 | ECP_MTYPE_FLAG_SYS)
-#define ECP_MTYPE_RBFLUSH (0x0a | ECP_MTYPE_FLAG_SYS)
-#define ECP_MTYPE_RBTIMER (0x0b | ECP_MTYPE_FLAG_SYS)
-
-#define ECP_ERR_RBUF_DUP -100
-#define ECP_ERR_RBUF_TIMER -101
-
-#define ecp_rbuf_skip(mtype) (mtype & ECP_MTYPE_FLAG_SYS)
-
-/* size must be power of 2 */
-#define ECP_RBUF_IDX_MASK(idx, size) ((idx) & ((size) - 1))
-
-typedef uint32_t ecp_win_t;
-
-struct ECPMsgQ;
-struct ECPFragIter;
-
-typedef struct ECPRBPayload {
- unsigned char buf[ECP_MAX_PLD];
- size_t size;
- unsigned char flags;
-} ECPRBPayload;
-
-typedef struct ECPRBPacket {
- unsigned char buf[ECP_MAX_PKT];
- size_t size;
- unsigned char flags;
-} ECPRBPacket;
-
-typedef struct ECPRBuffer {
- ecp_seq_t seq_start;
- ecp_seq_t seq_max;
- unsigned short arr_size;
- unsigned short idx_start;
- union {
- ECPRBPayload *pld;
- ECPRBPacket *pkt;
- } arr;
-} ECPRBuffer;
-
-typedef struct ECPRBRecv {
- unsigned char start;
- unsigned char flags;
- ecp_sts_t deliver_delay;
- unsigned short hole_max;
- unsigned short ack_rate;
- unsigned short ack_pkt;
- ecp_seq_t seq_ack;
- ecp_ack_t ack_map;
- ECPRBuffer rbuf;
-#ifdef ECP_WITH_PTHREAD
- pthread_mutex_t mutex;
-#endif
-#ifdef ECP_WITH_MSGQ
- struct ECPMsgQ *msgq;
-#endif
-} ECPRBRecv;
-
-typedef struct ECPRBSend {
- unsigned char start;
- unsigned char flags;
- ecp_win_t win_size;
- ecp_win_t in_transit;
- ecp_win_t cnt_cc;
- ecp_seq_t seq_cc;
- ecp_seq_t seq_flush;
- ecp_seq_t seq_nack;
- unsigned char flush;
- unsigned int nack_rate;
- ECPRBuffer rbuf;
-#ifdef ECP_WITH_PTHREAD
- pthread_mutex_t mutex;
-#endif
-} ECPRBSend;
-
-typedef struct ECPRBConn {
- ECPConnection b;
- ECPRBRecv *recv;
- ECPRBSend *send;
-} ECPRBConn;
-
-ECPRBConn *ecp_rbuf_get_rbconn(ECPConnection *conn);
-ECPConnection *ecp_rbuf_get_conn(ECPRBConn *conn);
-void _ecp_rbuf_start(ECPRBuffer *rbuf, ecp_seq_t seq);
-int _ecp_rbuf_msg_idx(ECPRBuffer *rbuf, ecp_seq_t seq, unsigned short *idx);
-void ecp_rbuf_init(ECPRBConn *conn);
-int ecp_rbuf_create(ECPRBConn *conn);
-void ecp_rbuf_destroy(ECPRBConn *conn);
-void ecp_rbuf_start(ECPRBConn *conn);
-ssize_t ecp_rbuf_msg_handle(ECPRBConn *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, size_t msg_size, ECP2Buffer *bufs);
-int ecp_rbuf_err_handle(ECPRBConn *conn, unsigned char mtype, int err);
-
-/* send */
-ssize_t ecp_rbuf_send_flush(ECPRBConn *conn);
-ssize_t ecp_rbuf_handle_ack(ECPRBConn *conn, unsigned char *msg, size_t msg_size);
-void ecp_rbsend_init(ECPRBConn *conn, ECPRBSend *buf, ECPRBPacket *pkt, unsigned short pkt_size);
-int ecp_rbsend_create(ECPRBConn *conn);
-void ecp_rbsend_destroy(ECPRBConn *conn);
-void ecp_rbsend_start(ECPRBConn *conn, ecp_seq_t seq);
-int ecp_rbuf_set_wsize(ECPRBConn *conn, ecp_win_t size);
-int ecp_rbuf_flush(ECPRBConn *conn);
-ssize_t ecp_rbuf_pld_send(ECPRBConn *conn, ECPBuffer *payload, size_t pld_size, ECPBuffer *packet, size_t pkt_size, ECPTimerItem *ti);
-
-/* recv */
-ssize_t ecp_rbuf_handle_nop(ECPRBConn *conn, unsigned char *msg, size_t msg_size);
-ssize_t ecp_rbuf_handle_flush(ECPRBConn *conn);
-void ecp_rbuf_handle_timer(ECPRBConn *conn);
-void ecp_rbrecv_init(ECPRBConn *conn, ECPRBRecv *buf, ECPRBPayload *pld, unsigned short pld_size);
-int ecp_rbrecv_create(ECPRBConn *conn);
-void ecp_rbrecv_destroy(ECPRBConn *conn);
-void ecp_rbrecv_start(ECPRBConn *conn, ecp_seq_t seq);
-
-int ecp_rbuf_set_hole(ECPRBConn *conn, unsigned short hole_max);
-int ecp_rbuf_set_delay(ECPRBConn *conn, ecp_sts_t delay);
-ssize_t ecp_rbuf_store(ECPRBConn *conn, ecp_seq_t seq, unsigned char *pld, size_t pld_size);
diff --git a/ecp/src/ecp/ext/rbuf_ext.c b/ecp/src/ecp/ext/rbuf_ext.c
deleted file mode 100644
index 67b6a8d..0000000
--- a/ecp/src/ecp/ext/rbuf_ext.c
+++ /dev/null
@@ -1,57 +0,0 @@
-#include <stdlib.h>
-
-#include <ecp/core.h>
-#include <ecp/ext.h>
-
-#include "rbuf.h"
-
-int ecp_ext_err_handle(ECPConnection *conn, unsigned char mtype, int err) {
- ECPRBConn *_conn = ecp_rbuf_get_rbconn(conn);
-
- if (_conn) return ecp_rbuf_err_handle(_conn, mtype, err);
- return ECP_PASS;
-}
-
-int ecp_ext_conn_create(ECPConnection *conn) {
- ECPRBConn *_conn = ecp_rbuf_get_rbconn(conn);
- int rv = ECP_OK;
-
- if (_conn) rv = ecp_rbuf_create(_conn);
- return rv;
-}
-
-void ecp_ext_conn_destroy(ECPConnection *conn) {
- ECPRBConn *_conn = ecp_rbuf_get_rbconn(conn);
-
- if (_conn) ecp_rbuf_destroy(_conn);
-}
-
-int ecp_ext_conn_open(ECPConnection *conn) {
- ECPRBConn *_conn = ecp_rbuf_get_rbconn(conn);
-
- if (_conn) ecp_rbuf_start(_conn);
- return ECP_OK;
-}
-
-void ecp_ext_conn_close(ECPConnection *conn) {}
-
-ssize_t ecp_ext_msg_handle(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, size_t msg_size, ECP2Buffer *bufs) {
- ECPRBConn *_conn = ecp_rbuf_get_rbconn(conn);
-
- if (_conn) return ecp_rbuf_msg_handle(_conn, seq, mtype, msg, msg_size, bufs);
- return 0;
-}
-
-ssize_t ecp_ext_pld_handle(ECPConnection *conn, ecp_seq_t seq, unsigned char *payload, size_t pld_size, ECP2Buffer *bufs) {
- ECPRBConn *_conn = ecp_rbuf_get_rbconn(conn);
-
- if (_conn && _conn->recv) return ecp_rbuf_store(_conn, seq, payload, pld_size);
- return 0;
-}
-
-ssize_t ecp_ext_pld_send(ECPConnection *conn, ECPBuffer *payload, size_t pld_size, ECPBuffer *packet, size_t pkt_size, unsigned char flags, ECPTimerItem *ti, ecp_tr_addr_t *addr) {
- ECPRBConn *_conn = ecp_rbuf_get_rbconn(conn);
-
- if (_conn && _conn->send) return ecp_rbuf_pld_send(_conn, payload, pld_size, packet, pkt_size, ti);
- return 0;
-}
diff --git a/ecp/src/ecp/ext/rbuf_recv.c b/ecp/src/ecp/ext/rbuf_recv.c
deleted file mode 100644
index 777af66..0000000
--- a/ecp/src/ecp/ext/rbuf_recv.c
+++ /dev/null
@@ -1,489 +0,0 @@
-#include <stdlib.h>
-#include <string.h>
-
-#include <ecp/core.h>
-#include <ecp/tm.h>
-
-#include "rbuf.h"
-
-#ifdef ECP_WITH_MSGQ
-#include "msgq.h"
-#endif
-
-#define ACK_RATE 8
-#define ACK_MASK_FIRST ((ecp_ack_t)1 << (ECP_SIZE_ACKB - 1))
-
-static ssize_t msg_store(ECPRBConn *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *pld, size_t pld_size) {
- ECPConnection *_conn = ecp_rbuf_get_conn(conn);
- ECPRBRecv *buf = conn->recv;
- ECPRBuffer *rbuf = &buf->rbuf;
- unsigned short idx;
- unsigned char flags;
- int skip;
- int rv;
-
- rv = _ecp_rbuf_msg_idx(rbuf, seq, &idx);
- if (rv) return rv;
-
- if (rbuf->arr.pld[idx].flags) return ECP_ERR_RBUF_DUP;
-
-#ifdef ECP_WITH_MSGQ
- if (buf->msgq) {
- ecp_seq_t seq_offset;
-
-#ifndef ECP_WITH_RTHD
- pthread_mutex_lock(&buf->mutex);
-#endif
-
- seq_offset = seq - buf->msgq->seq_start;
- if (seq_offset >= rbuf->arr_size) rv = ECP_ERR_FULL;
-
-#ifndef ECP_WITH_RTHD
- pthread_mutex_unlock(&buf->mutex);
-#endif
-
- if (rv) return rv;
- }
-#endif
-
- skip = ecp_rbuf_skip(mtype);
- flags = ECP_RBUF_FLAG_IN_RBUF;
- if (skip) flags |= ECP_RBUF_FLAG_SKIP;
- rbuf->arr.pld[idx].flags = flags;
-
- if ((mtype == ECP_MTYPE_RBNOP) && pld) {
- return ecp_pld_handle_one(_conn, seq, pld, pld_size, NULL);
- } else if (skip) {
- return 0;
- }
-
- if (pld && pld_size) memcpy(rbuf->arr.pld[idx].buf, pld, pld_size);
- rbuf->arr.pld[idx].size = pld_size;
- if (ECP_SEQ_LT(rbuf->seq_max, seq)) rbuf->seq_max = seq;
-
- return pld_size;
-}
-
-static void msg_flush(ECPRBConn *conn) {
- ECPConnection *_conn = ecp_rbuf_get_conn(conn);
- ECPRBRecv *buf = conn->recv;
- ECPRBuffer *rbuf = &buf->rbuf;
- ecp_seq_t seq;
- unsigned short idx;
- int i;
-
-#ifdef ECP_WITH_MSGQ
-#ifndef ECP_WITH_RTHD
- if (buf->msgq) pthread_mutex_lock(&buf->mutex);
-#endif
-#endif
-
- seq = rbuf->seq_start;
- idx = rbuf->idx_start;
-
- while (ECP_SEQ_LTE(seq, rbuf->seq_max)) {
- if (rbuf->arr.pld[idx].flags & ECP_RBUF_FLAG_IN_RBUF) {
- if (!(rbuf->arr.pld[idx].flags & ECP_RBUF_FLAG_SKIP)) {
- ecp_pts_t msg_pts;
- int rv;
-
- rv = ecp_pld_get_pts(rbuf->arr.pld[idx].buf, rbuf->arr.pld[idx].size, &msg_pts);
- if (!rv && buf->deliver_delay) {
- ecp_sts_t now = ecp_tm_get_ms();
-
- msg_pts += buf->deliver_delay;
- if (ECP_PTS_LT(now, msg_pts)) {
- if (!(rbuf->arr.pld[idx].flags & ECP_RBUF_FLAG_IN_TIMER)) {
- ECPTimerItem ti;
-
- ecp_timer_item_init(&ti, _conn, ECP_MTYPE_RBTIMER, NULL, 0, msg_pts - now, NULL);
- rv = ecp_timer_push(&ti);
- if (!rv) rbuf->arr.pld[idx].flags |= ECP_RBUF_FLAG_IN_TIMER;
- }
- break;
- } else if (rbuf->arr.pld[idx].flags & ECP_RBUF_FLAG_IN_TIMER) {
- rbuf->arr.pld[idx].flags &= ~ECP_RBUF_FLAG_IN_TIMER;
- }
- }
-
-#ifdef ECP_WITH_MSGQ
- if (buf->msgq) {
- unsigned char mtype;
-
- rv = ecp_pld_get_type(rbuf->arr.pld[idx].buf, rbuf->arr.pld[idx].size, &mtype);
- if (!rv) rv = ecp_msgq_push(conn, seq, mtype & ECP_MTYPE_MASK);
- if (rv) break;
-
- rbuf->arr.pld[idx].flags |= ECP_RBUF_FLAG_IN_MSGQ;
- } else
-#endif
- ecp_pld_handle(_conn, seq, rbuf->arr.pld[idx].buf, rbuf->arr.pld[idx].size, NULL);
- } else {
- rbuf->arr.pld[idx].flags &= ~ECP_RBUF_FLAG_SKIP;
- }
- rbuf->arr.pld[idx].flags &= ~ECP_RBUF_FLAG_IN_RBUF;
- // if (rbuf->arr.pld[idx].flags == 0);
- } else {
- if (buf->flags & ECP_RBUF_FLAG_RELIABLE) break;
- if (!ECP_SEQ_LT(seq, rbuf->seq_max - buf->hole_max)) break;
- }
- seq++;
- idx = ECP_RBUF_IDX_MASK(idx + 1, rbuf->arr_size);
- }
- rbuf->seq_start = seq;
- rbuf->idx_start = idx;
-
-#ifdef ECP_WITH_MSGQ
-#ifndef ECP_WITH_RTHD
- if (buf->msgq) pthread_mutex_unlock(&buf->mutex);
-#endif
-#endif
-}
-
-static int ack_shift(ECPRBConn *conn) {
- ECPRBRecv *buf = conn->recv;
- ECPRBuffer *rbuf = &buf->rbuf;
- unsigned short idx;
- int do_ack = 0;
- int in_rbuf = 0;
- int rv;
-
- if ((buf->flags & ECP_RBUF_FLAG_RELIABLE) && ((buf->ack_map & ACK_MASK_FIRST) == 0)) return 0;
-
- /* walks through messages that are not delivered yet, so no need for msgq mutex lock */
- while (ECP_SEQ_LT(buf->seq_ack, rbuf->seq_max)) {
- buf->seq_ack++;
- rv = _ecp_rbuf_msg_idx(rbuf, buf->seq_ack, &idx);
- if (!rv) {
- in_rbuf = rbuf->arr.pld[idx].flags & ECP_RBUF_FLAG_IN_RBUF;
- } else {
- in_rbuf = 1;
- }
- if (in_rbuf && (buf->ack_map == ECP_ACK_FULL)) continue;
-
- buf->ack_map = buf->ack_map << 1;
- if (in_rbuf) {
- buf->ack_map |= 1;
- } else if (!do_ack && ECP_SEQ_LT(buf->seq_ack, rbuf->seq_max - buf->hole_max)) {
- do_ack = 1;
- }
-
- if ((buf->flags & ECP_RBUF_FLAG_RELIABLE) && ((buf->ack_map & ACK_MASK_FIRST) == 0)) {
- do_ack = 1;
- break;
- }
- }
-
- return do_ack;
-}
-
-static int ack_send(ECPRBConn *conn, ecp_seq_t seq_ack, ecp_seq_t ack_map) {
- ECPConnection *_conn = ecp_rbuf_get_conn(conn);
- ECPRBRecv *buf = conn->recv;
- ECPBuffer packet;
- ECPBuffer payload;
- unsigned char pkt_buf[ECP_SIZE_PKT_BUF(ECP_SIZE_PLD(sizeof(ecp_seq_t) + sizeof(ecp_ack_t), ECP_MTYPE_RBACK), _conn)];
- unsigned char pld_buf[ECP_SIZE_PLD_BUF(ECP_SIZE_PLD(sizeof(ecp_seq_t) + sizeof(ecp_ack_t), ECP_MTYPE_RBACK), _conn)];
- unsigned char *_buf;
- ssize_t rv;
-
- packet.buffer = pkt_buf;
- packet.size = sizeof(pkt_buf);
- payload.buffer = pld_buf;
- payload.size = sizeof(pld_buf);
-
- ecp_pld_set_type(payload.buffer, payload.size, ECP_MTYPE_RBACK);
- _buf = ecp_pld_get_msg(payload.buffer, payload.size);
- _buf[0] = seq_ack >> 24;
- _buf[1] = seq_ack >> 16;
- _buf[2] = seq_ack >> 8;
- _buf[3] = seq_ack;
- _buf[4] = ack_map >> 24;
- _buf[5] = ack_map >> 16;
- _buf[6] = ack_map >> 8;
- _buf[7] = ack_map;
-
- rv = ecp_pld_send(_conn, &packet, &payload, ECP_SIZE_PLD(sizeof(ecp_seq_t) + sizeof(ecp_ack_t), ECP_MTYPE_RBACK), 0);
- if (rv < 0) return rv;
-
- buf->ack_pkt = 0;
-
- return ECP_OK;
-}
-
-ssize_t ecp_rbuf_handle_nop(ECPRBConn *conn, unsigned char *msg, size_t msg_size) {
- ECPRBRecv *buf = conn->recv;
- ECPRBuffer *rbuf = &buf->rbuf;
- ecp_seq_t seq_ack;
- ecp_ack_t ack_map;
- ecp_ack_t ack_mask = (ecp_ack_t)1 << (ECP_SIZE_ACKB - 1);
- size_t rsize = sizeof(ecp_seq_t) + sizeof(ecp_ack_t);
- int i;
-
- if (msg_size < rsize) return ECP_ERR_SIZE;
-
- seq_ack = \
- ((ecp_seq_t)msg[0] << 24) | \
- ((ecp_seq_t)msg[1] << 16) | \
- ((ecp_seq_t)msg[2] << 8) | \
- ((ecp_seq_t)msg[3]);
- ack_map = \
- ((ecp_ack_t)msg[4] << 24) | \
- ((ecp_ack_t)msg[5] << 16) | \
- ((ecp_ack_t)msg[6] << 8) | \
- ((ecp_ack_t)msg[7]);
-
- seq_ack -= (ECP_SIZE_ACKB - 1);
- for (i=0; i<ECP_SIZE_ACKB; i++) {
- if (ack_map & ack_mask) {
- msg_store(conn, seq_ack, ECP_MTYPE_RBNOP, NULL, 0);
- }
- seq_ack++;
- ack_mask = ack_mask >> 1;
- }
-
- return rsize;
-}
-
-ssize_t ecp_rbuf_handle_flush(ECPRBConn *conn) {
- ECPRBRecv *buf = conn->recv;
-
-#ifdef ECP_WITH_RTHD
- pthread_mutex_lock(&buf->mutex);
-#endif
-
- ack_send(conn, buf->seq_ack, buf->ack_map);
-
-#ifdef ECP_WITH_RTHD
- pthread_mutex_unlock(&buf->mutex);
-#endif
-
- return 0;
-}
-
-void ecp_rbuf_handle_timer(ECPRBConn *conn) {
- ECPRBRecv *buf = conn->recv;
-
-#ifdef ECP_WITH_RTHD
- pthread_mutex_lock(&buf->mutex);
-#endif
-
- msg_flush(conn);
-
-#ifdef ECP_WITH_RTHD
- pthread_mutex_unlock(&buf->mutex);
-#endif
-}
-
-void ecp_rbrecv_init(ECPRBConn *conn, ECPRBRecv *buf, ECPRBPayload *pld, unsigned short pld_size) {
- ECPRBuffer *rbuf = &buf->rbuf;
-
- memset(buf, 0, sizeof(ECPRBRecv));
- memset(pld, 0, sizeof(ECPRBPayload) * pld_size);
-
- buf->ack_map = ECP_ACK_FULL;
- buf->ack_rate = ACK_RATE;
- rbuf->arr.pld = pld;
- rbuf->arr_size = pld_size;
-
- conn->recv = buf;
-}
-
-int ecp_rbrecv_create(ECPRBConn *conn) {
- ECPRBRecv *buf = conn->recv;
- int rv;
-
-#ifdef ECP_WITH_PTHREAD
- rv = pthread_mutex_init(&buf->mutex, NULL);
- if (rv) return ECP_ERR;
-#endif
-
-#ifdef ECP_WITH_MSGQ
- if (buf->msgq) {
- rv = ecp_msgq_create(conn);
- if (rv) {
-#ifdef ECP_WITH_PTHREAD
- pthread_mutex_destroy(&buf->mutex);
-#endif
-
- return ECP_ERR;
- }
- }
-#endif
-
- return ECP_OK;
-}
-
-void ecp_rbrecv_destroy(ECPRBConn *conn) {
- ECPRBRecv *buf = conn->recv;
-
-#ifdef ECP_WITH_PTHREAD
- pthread_mutex_destroy(&buf->mutex);
-#endif
-
-#ifdef ECP_WITH_MSGQ
- if (buf->msgq) ecp_msgq_destroy(conn);
-#endif
-}
-
-void ecp_rbrecv_start(ECPRBConn *conn, ecp_seq_t seq) {
- ECPRBRecv *buf = conn->recv;
- ECPRBuffer *rbuf = &buf->rbuf;
-
-#ifdef ECP_WITH_RTHD
- pthread_mutex_lock(&buf->mutex);
-#endif
-
- buf->start = 1;
- buf->seq_ack = seq;
- _ecp_rbuf_start(rbuf, seq);
-
-#ifdef ECP_WITH_MSGQ
- if (buf->msgq) ecp_msgq_start(conn, seq);
-#endif
-
-#ifdef ECP_WITH_RTHD
- pthread_mutex_unlock(&buf->mutex);
-#endif
-}
-
-int ecp_rbuf_set_hole(ECPRBConn *conn, unsigned short hole_max) {
- ECPRBRecv *buf = conn->recv;
-
-#ifdef ECP_WITH_RTHD
- pthread_mutex_lock(&buf->mutex);
-#endif
-
- buf->hole_max = hole_max;
-
-#ifdef ECP_WITH_RTHD
- pthread_mutex_unlock(&buf->mutex);
-#endif
-
- return ECP_OK;
-}
-
-int ecp_rbuf_set_delay(ECPRBConn *conn, ecp_sts_t delay) {
- ECPRBRecv *buf = conn->recv;
-
-#ifdef ECP_WITH_RTHD
- pthread_mutex_lock(&buf->mutex);
-#endif
-
- buf->deliver_delay = delay;
-
-#ifdef ECP_WITH_RTHD
- pthread_mutex_unlock(&buf->mutex);
-#endif
-
- return ECP_OK;
-}
-
-ssize_t ecp_rbuf_store(ECPRBConn *conn, ecp_seq_t seq, unsigned char *pld, size_t pld_size) {
- ECPRBRecv *buf = conn->recv;
- ECPRBuffer *rbuf = &buf->rbuf;
- unsigned char mtype;
- unsigned short ack_pkt = 0;
- int do_ack = 0;
- int _rv;
- ssize_t rv;
-
- _rv = ecp_pld_get_type(pld, pld_size, &mtype);
- if (_rv) return _rv;
-
-#ifdef ECP_WITH_RTHD
- pthread_mutex_lock(&buf->mutex);
-#endif
-
- if (!buf->start) {
- rv = 0;
- goto rbuf_store_fin;
- }
-
- if (ECP_SEQ_LT(rbuf->seq_max, seq)) {
- ack_pkt = seq - rbuf->seq_max;
- }
- if (ECP_SEQ_LTE(seq, buf->seq_ack)) {
- ecp_seq_t seq_offset = buf->seq_ack - seq;
- if (seq_offset < ECP_SIZE_ACKB) {
- ecp_ack_t ack_bit = ((ecp_ack_t)1 << seq_offset);
-
- if (ack_bit & buf->ack_map) {
- rv = ECP_ERR_RBUF_DUP;
- goto rbuf_store_fin;
- }
-
- rv = msg_store(conn, seq, mtype, pld, pld_size);
- if (rv < 0) goto rbuf_store_fin;
-
- buf->ack_map |= ack_bit;
- /* reliable transport can prevent seq_ack from reaching seq_max */
- if (ECP_SEQ_LT(buf->seq_ack, rbuf->seq_max)) {
- do_ack = ack_shift(conn);
- }
- } else {
- rv = ECP_ERR_RBUF_DUP;
- goto rbuf_store_fin;
- }
- } else {
- unsigned short msg_cnt = rbuf->seq_max - rbuf->seq_start + 1;
-
- if ((msg_cnt == 0) && (seq == (ecp_seq_t)(buf->seq_ack + 1))) {
- int deliver = 1;
-#ifdef ECP_WITH_MSGQ
- if (buf->msgq) deliver = 0;
-#endif
- if (buf->deliver_delay) deliver = 0;
- if (deliver) {
- /* receive buffer is empty, so no need for msgq mutex lock */
- rv = 0;
- rbuf->seq_max++;
- rbuf->seq_start++;
- rbuf->idx_start = ECP_RBUF_IDX_MASK(rbuf->idx_start + 1, rbuf->arr_size);
- } else {
- rv = msg_store(conn, seq, mtype, pld, pld_size);
- if (rv < 0) goto rbuf_store_fin;
- }
- buf->seq_ack++;
- } else {
- rv = msg_store(conn, seq, mtype, pld, pld_size);
- if (rv < 0) goto rbuf_store_fin;
-
- do_ack = ack_shift(conn);
- }
- }
- msg_flush(conn);
- if (ack_pkt) {
- buf->ack_pkt += ack_pkt;
- if (!do_ack && (buf->ack_pkt > buf->ack_rate)) do_ack = 1;
- }
- if (do_ack) {
- ecp_seq_t seq_ack = buf->seq_ack;
- ecp_seq_t ack_map = buf->ack_map;
-
- /* account for missing mackets within hole_max range */
- if (buf->hole_max && (buf->seq_ack == rbuf->seq_max)) {
- unsigned short h_bits = buf->hole_max + 1;
- ecp_seq_t h_mask = ~(~((ecp_seq_t)0) << h_bits);
-
- if ((ack_map & h_mask) != h_mask) {
- h_mask = ~(~((ecp_seq_t)0) >> h_bits);
- seq_ack -= h_bits;
- ack_map = (ack_map >> h_bits) | h_mask;
- }
- }
- _rv = ack_send(conn, seq_ack, ack_map);
- if (_rv) {
- rv = _rv;
- goto rbuf_store_fin;
- }
- }
-
-rbuf_store_fin:
-
-#ifdef ECP_WITH_RTHD
- pthread_mutex_unlock(&buf->mutex);
-#endif
-
- return rv;
-}
diff --git a/ecp/src/ecp/ext/rbuf_send.c b/ecp/src/ecp/ext/rbuf_send.c
deleted file mode 100644
index c235ad9..0000000
--- a/ecp/src/ecp/ext/rbuf_send.c
+++ /dev/null
@@ -1,422 +0,0 @@
-#include <stdlib.h>
-#include <string.h>
-
-#include <ecp/core.h>
-
-#include "rbuf.h"
-
-#define NACK_RATE_UNIT 10000
-
-#define MIN(X, Y) (((X) < (Y)) ? (X) : (Y))
-#define MAX(X, Y) (((X) > (Y)) ? (X) : (Y))
-
-static void cc_flush(ECPRBConn *conn, unsigned char flags) {
- ECPConnection *_conn = ecp_rbuf_get_conn(conn);
- ECPRBSend *buf = conn->send;
- ECPRBuffer *rbuf = &buf->rbuf;
- unsigned short idx;
- int rv;
-
- rv = _ecp_rbuf_msg_idx(rbuf, buf->seq_cc, &idx);
- if (rv) return;
-
- while (ECP_SEQ_LTE(buf->seq_cc, rbuf->seq_max)) {
- ECPBuffer packet;
-
- if ((buf->cnt_cc == 0) || (buf->in_transit >= buf->win_size)) break;
-
- if ((rbuf->arr.pkt[idx].flags & ECP_RBUF_FLAG_IN_RBUF) && !(rbuf->arr.pkt[idx].flags & ECP_RBUF_FLAG_SKIP)) {
-#ifdef ECP_WITH_PTHREAD
- pthread_mutex_unlock(&buf->mutex);
-#endif
-
- packet.buffer = rbuf->arr.pkt[idx].buf;
- packet.size = ECP_MAX_PKT;
- ecp_pkt_send(_conn->sock, &packet, rbuf->arr.pkt[idx].size, flags, NULL, &_conn->remote.addr);
-
-#ifdef ECP_WITH_PTHREAD
- pthread_mutex_lock(&buf->mutex);
-#endif
-
- buf->cnt_cc--;
- buf->in_transit++;
- }
- if (!(buf->flags & ECP_RBUF_FLAG_RELIABLE)) {
- rbuf->arr.pkt[idx].flags = 0;
- // if (rbuf->arr.pkt[idx].flags == 0);
- }
- buf->seq_cc++;
- idx = ECP_RBUF_IDX_MASK(idx + 1, rbuf->arr_size);
- }
- if (!(buf->flags & ECP_RBUF_FLAG_RELIABLE)) {
- rbuf->seq_start = buf->seq_cc;
- rbuf->idx_start = idx;
- }
-}
-
-static ssize_t _rbuf_send_flush(ECPConnection *_conn, ECPTimerItem *ti) {
- ECPBuffer packet;
- ECPBuffer payload;
- unsigned char pkt_buf[ECP_SIZE_PKT_BUF(ECP_SIZE_PLD(0, ECP_MTYPE_RBFLUSH), _conn)];
- unsigned char pld_buf[ECP_SIZE_PLD_BUF(ECP_SIZE_PLD(0, ECP_MTYPE_RBFLUSH), _conn)];
-
- packet.buffer = pkt_buf;
- packet.size = sizeof(pkt_buf);
- payload.buffer = pld_buf;
- payload.size = sizeof(pld_buf);
-
- ecp_pld_set_type(payload.buffer, payload.size, ECP_MTYPE_RBFLUSH);
-
- return ecp_pld_send_wtimer(_conn, &packet, &payload, ECP_SIZE_PLD(0, ECP_MTYPE_RBFLUSH), 0, ti);
-}
-
-ssize_t ecp_rbuf_send_flush(ECPRBConn *conn) {
- ECPConnection *_conn = ecp_rbuf_get_conn(conn);
-
- return ecp_timer_send(_conn, _rbuf_send_flush, ECP_MTYPE_RBACK, 3, 500, NULL);
-}
-
-ssize_t ecp_rbuf_handle_ack(ECPRBConn *conn, unsigned char *msg, size_t msg_size) {
- ECPConnection *_conn = ecp_rbuf_get_conn(conn);
- ECPRBSend *buf = conn->send;
- ECPRBuffer *rbuf = &buf->rbuf;
- ssize_t rsize = sizeof(ecp_seq_t) + sizeof(ecp_ack_t);
- ecp_seq_t seq_start;
- ecp_seq_t seq_max;
- ecp_seq_t seq_ack;
- ecp_ack_t ack_map;
- unsigned short idx;
- unsigned short msg_cnt;
- int do_flush = 0;
- int i;
- int rv;
-
- if (msg_size < rsize) return ECP_ERR_SIZE;
-
- seq_ack = \
- ((ecp_seq_t)msg[0] << 24) | \
- ((ecp_seq_t)msg[1] << 16) | \
- ((ecp_seq_t)msg[2] << 8) | \
- ((ecp_seq_t)msg[3]);
- ack_map = \
- ((ecp_ack_t)msg[4] << 24) | \
- ((ecp_ack_t)msg[5] << 16) | \
- ((ecp_ack_t)msg[6] << 8) | \
- ((ecp_ack_t)msg[7]);
-
-#ifdef ECP_WITH_PTHREAD
- pthread_mutex_lock(&buf->mutex);
-#endif
-
- seq_max = (buf->cnt_cc ? buf->seq_cc - 1 : rbuf->seq_max);
- if (ECP_SEQ_LT(seq_max, seq_ack)) return ECP_ERR;
-
- if (buf->flags & ECP_RBUF_FLAG_RELIABLE) {
- rv = _ecp_rbuf_msg_idx(rbuf, seq_ack, NULL);
- if (rv) goto handle_ack_fin;
- }
-
- if (buf->flags & ECP_RBUF_FLAG_CCONTROL) {
- buf->in_transit = seq_max - seq_ack;
- }
-
- if (ack_map != ECP_ACK_FULL) {
- ecp_ack_t ack_mask = (ecp_ack_t)1 << (ECP_SIZE_ACKB - 1);
- ecp_ack_t ack_map_nop = 0;
- unsigned short nack_cnt = 0;
- int nack_first = 0;
-
- seq_ack -= (ECP_SIZE_ACKB - 1);
- if (buf->flags & ECP_RBUF_FLAG_RELIABLE) {
- rv = _ecp_rbuf_msg_idx(rbuf, seq_ack, &idx);
- if (rv) goto handle_ack_fin;
- }
-
-#ifdef ECP_WITH_PTHREAD
- pthread_mutex_unlock(&buf->mutex);
-#endif
-
- for (i=0; i<ECP_SIZE_ACKB; i++) {
- if ((ack_map & ack_mask) == 0) {
- if (ECP_SEQ_LT(buf->seq_nack, seq_ack)) {
- nack_cnt++;
- buf->seq_nack = seq_ack;
- }
-
- if (buf->flags & ECP_RBUF_FLAG_RELIABLE) {
- if (!(rbuf->arr.pkt[idx].flags & ECP_RBUF_FLAG_IN_RBUF) || (rbuf->arr.pkt[idx].flags & ECP_RBUF_FLAG_SKIP)) {
- ack_map_nop |= ack_mask;
- } else {
- ECPBuffer packet;
-
- packet.buffer = rbuf->arr.pkt[idx].buf;
- packet.size = ECP_MAX_PKT;
- ecp_pkt_send(_conn->sock, &packet, rbuf->arr.pkt[idx].size, ECP_SEND_FLAG_MORE, NULL, &_conn->remote.addr);
- if (buf->flags & ECP_RBUF_FLAG_CCONTROL) {
- buf->in_transit++;
- }
- }
- if (!nack_first) {
- nack_first = 1;
- seq_start = seq_ack;
- }
- }
- }
- seq_ack++;
- ack_mask = ack_mask >> 1;
- if (buf->flags & ECP_RBUF_FLAG_RELIABLE) idx = ECP_RBUF_IDX_MASK(idx + 1, rbuf->arr_size);
- }
-
- if ((buf->flags & ECP_RBUF_FLAG_RELIABLE) && ack_map_nop) {
- ECPBuffer packet;
- ECPBuffer payload;
- unsigned char pkt_buf[ECP_SIZE_PKT_BUF(ECP_SIZE_PLD(sizeof(ecp_seq_t) + sizeof(ecp_ack_t), ECP_MTYPE_RBNOP), _conn)];
- unsigned char pld_buf[ECP_SIZE_PLD_BUF(ECP_SIZE_PLD(sizeof(ecp_seq_t) + sizeof(ecp_ack_t), ECP_MTYPE_RBNOP), _conn)];
- unsigned char *_buf;
-
- packet.buffer = pkt_buf;
- packet.size = sizeof(pkt_buf);
- payload.buffer = pld_buf;
- payload.size = sizeof(pld_buf);
-
- ecp_pld_set_type(payload.buffer, payload.size, ECP_MTYPE_RBNOP);
- _buf = ecp_pld_get_msg(payload.buffer, payload.size);
-
- seq_ack--;
- _buf[0] = seq_ack >> 24;
- _buf[1] = seq_ack >> 16;
- _buf[2] = seq_ack >> 8;
- _buf[3] = seq_ack;
- _buf[4] = ack_map_nop >> 24;
- _buf[5] = ack_map_nop >> 16;
- _buf[6] = ack_map_nop >> 8;
- _buf[7] = ack_map_nop;
-
- ecp_pld_send(_conn, &packet, &payload, ECP_SIZE_PLD(sizeof(ecp_seq_t) + sizeof(ecp_ack_t), ECP_MTYPE_RBNOP), ECP_SEND_FLAG_MORE);
- }
-
-#ifdef ECP_WITH_PTHREAD
- pthread_mutex_lock(&buf->mutex);
-#endif
-
- buf->nack_rate = (buf->nack_rate * 7 + ((ECP_SIZE_ACKB - nack_cnt) * NACK_RATE_UNIT) / ECP_SIZE_ACKB) / 8;
- } else {
- buf->nack_rate = (buf->nack_rate * 7) / 8;
- seq_start = seq_ack + 1;
- }
-
- if (buf->flags & ECP_RBUF_FLAG_RELIABLE) {
- msg_cnt = seq_start - rbuf->seq_start;
- idx = rbuf->idx_start;
- for (i=0; i<msg_cnt; i++) {
- rbuf->arr.pkt[idx].flags = 0;
- // if (rbuf->arr.pkt[idx].flags == 0);
- idx = ECP_RBUF_IDX_MASK(idx + 1, rbuf->arr_size);
- }
- rbuf->seq_start = seq_start;
- rbuf->idx_start = idx;
- }
- if (buf->flush) {
- if (ECP_SEQ_LT(buf->seq_flush, rbuf->seq_start)) buf->flush = 0;
- if (buf->flush) do_flush = 1;
- }
- if (buf->cnt_cc) cc_flush(conn, ECP_SEND_FLAG_MORE);
-
-handle_ack_fin:
-
-#ifdef ECP_WITH_PTHREAD
- pthread_mutex_unlock(&buf->mutex);
-#endif
-
- if (!rv && do_flush) {
- ssize_t _rv;
-
- _rv = ecp_rbuf_send_flush(conn);
- if (_rv < 0) rv = _rv;
- }
-
- if (rv) return rv;
- return rsize;
-}
-
-void ecp_rbsend_init(ECPRBConn *conn, ECPRBSend *buf, ECPRBPacket *pkt, unsigned short pkt_size) {
- ECPRBuffer *rbuf = &buf->rbuf;
-
- memset(buf, 0, sizeof(ECPRBRecv));
- memset(pkt, 0, sizeof(ECPRBPacket) * pkt_size);
-
- rbuf->arr.pkt = pkt;
- rbuf->arr_size = pkt_size;
-
- conn->send = buf;
-}
-
-int ecp_rbsend_create(ECPRBConn *conn) {
- ECPRBSend *buf = conn->send;
- int rv;
-
-#ifdef ECP_WITH_PTHREAD
- rv = pthread_mutex_init(&buf->mutex, NULL);
- if (rv) return ECP_ERR;
-#endif
-
- return ECP_OK;
-}
-
-void ecp_rbsend_destroy(ECPRBConn *conn) {
- ECPRBSend *buf = conn->send;
-
-#ifdef ECP_WITH_PTHREAD
- pthread_mutex_destroy(&buf->mutex);
-#endif
-}
-
-void ecp_rbsend_start(ECPRBConn *conn, ecp_seq_t seq) {
- ECPRBSend *buf = conn->send;
- ECPRBuffer *rbuf = &buf->rbuf;
-
-#ifdef ECP_WITH_PTHREAD
- pthread_mutex_lock(&buf->mutex);
-#endif
-
- buf->start = 1;
- buf->seq_nack = seq;
- _ecp_rbuf_start(rbuf, seq);
-
-#ifdef ECP_WITH_PTHREAD
- pthread_mutex_unlock(&buf->mutex);
-#endif
-}
-
-int ecp_rbuf_set_wsize(ECPRBConn *conn, ecp_win_t size) {
- ECPRBSend *buf = conn->send;
-
-#ifdef ECP_WITH_PTHREAD
- pthread_mutex_lock(&buf->mutex);
-#endif
-
- buf->win_size = size;
- if (buf->cnt_cc) cc_flush(conn, 0);
-
-#ifdef ECP_WITH_PTHREAD
- pthread_mutex_unlock(&buf->mutex);
-#endif
-
- return ECP_OK;
-}
-
-int ecp_rbuf_flush(ECPRBConn *conn) {
- ECPConnection *_conn = ecp_rbuf_get_conn(conn);
- ECPRBSend *buf = conn->send;
- ecp_seq_t seq;
- ssize_t rv;
-
-#ifdef ECP_WITH_PTHREAD
- pthread_mutex_lock(&_conn->mutex);
-#endif
- seq = (ecp_seq_t)(_conn->nonce_out) - 1;
-#ifdef ECP_WITH_PTHREAD
- pthread_mutex_unlock(&_conn->mutex);
-#endif
-
-#ifdef ECP_WITH_PTHREAD
- pthread_mutex_lock(&buf->mutex);
-#endif
-
- if (buf->flush) {
- if (ECP_SEQ_LT(buf->seq_flush, seq)) buf->seq_flush = seq;
- } else {
- buf->flush = 1;
- buf->seq_flush = seq;
- }
-
-#ifdef ECP_WITH_PTHREAD
- pthread_mutex_unlock(&buf->mutex);
-#endif
-
- rv = ecp_rbuf_send_flush(conn);
- if (rv < 0) return rv;
-
- return ECP_OK;
-}
-
-ssize_t ecp_rbuf_pld_send(ECPRBConn *conn, ECPBuffer *payload, size_t pld_size, ECPBuffer *packet, size_t pkt_size, ECPTimerItem *ti) {
- ECPRBSend *buf = conn->send;
- ECPRBuffer *rbuf = &buf->rbuf;
- unsigned char mtype;
- int rb_rel;
- int rb_cc;
- int do_send;
- int do_skip;
- int _rv = ECP_OK;
- ssize_t rv = 0;
-
- _rv = ecp_pld_get_type(payload->buffer, pld_size, &mtype);
- if (_rv) return _rv;
-
- do_send = 1;
- do_skip = ecp_rbuf_skip(mtype);
- if (ti && !do_skip) return ECP_ERR_RBUF_TIMER;
-
-#ifdef ECP_WITH_PTHREAD
- pthread_mutex_lock(&buf->mutex);
-#endif
-
- if (!buf->start) {
- rv = 0;
- goto pld_send_fin;
- }
-
- rb_rel = (buf->flags & ECP_RBUF_FLAG_RELIABLE);
- rb_cc = ((buf->flags & ECP_RBUF_FLAG_CCONTROL) && (buf->cnt_cc || (buf->in_transit >= buf->win_size)));
-
- if (rb_rel || rb_cc) {
- ecp_seq_t seq;
- unsigned short idx;
- unsigned char _flags;
-
- _rv = ecp_pkt_get_seq(packet->buffer, pkt_size, &seq);
- if (_rv) {
- rv = _rv;
- goto pld_send_fin;
- }
-
- _rv = _ecp_rbuf_msg_idx(rbuf, seq, &idx);
- if (_rv) rv = ECP_ERR_RBUF_DUP;
- if (!rv && rbuf->arr.pkt[idx].flags) rv = ECP_ERR_RBUF_DUP;
- if (rv) goto pld_send_fin;
-
- _flags = ECP_RBUF_FLAG_IN_RBUF;
- if (do_skip) {
- _flags |= ECP_RBUF_FLAG_SKIP;
- } else {
- do_send = 0;
- }
-
- rbuf->arr.pkt[idx].flags = _flags;
- if (!do_send) {
- memcpy(rbuf->arr.pkt[idx].buf, packet->buffer, pkt_size);
- rbuf->arr.pkt[idx].size = pkt_size;
- rv = pld_size;
- }
-
- if (ECP_SEQ_LT(rbuf->seq_max, seq)) rbuf->seq_max = seq;
-
- if (rb_cc && !do_send) {
- if (buf->cnt_cc == 0) buf->seq_cc = seq;
- buf->cnt_cc++;
- }
- }
-
- if ((buf->flags & ECP_RBUF_FLAG_CCONTROL) && !do_skip && do_send) {
- buf->in_transit++;
- }
-
-pld_send_fin:
-
-#ifdef ECP_WITH_PTHREAD
- pthread_mutex_unlock(&buf->mutex);
-#endif
-
- return rv;
-}
diff --git a/ecp/src/ecp/htable/hashtable.c b/ecp/src/ecp/htable/hashtable.c
index f6d3a0b..eff79cd 100755
--- a/ecp/src/ecp/htable/hashtable.c
+++ b/ecp/src/ecp/htable/hashtable.c
@@ -3,7 +3,6 @@
#include "hashtable.h"
#include "hashtable_private.h"
#include <stdlib.h>
-#include <stdio.h>
#include <string.h>
#include <math.h>
diff --git a/ecp/src/ecp/timer.c b/ecp/src/ecp/timer.c
index 3cacca0..1c4cae6 100644
--- a/ecp/src/ecp/timer.c
+++ b/ecp/src/ecp/timer.c
@@ -1,6 +1,5 @@
#include <stdlib.h>
#include <string.h>
-#include <stdio.h>
#include "core.h"
#include "tm.h"
@@ -176,13 +175,16 @@ void ecp_timer_remove(ECPConnection *conn) {
}
ecp_sts_t ecp_timer_exe(ECPSocket *sock) {
- ecp_sts_t ret = 0;
+ ECPContext *ctx;
ECPTimer *timer = &sock->timer;
ECPTimerItem to_exec[ECP_MAX_TIMER];
+ ecp_sts_t ret = 0;
ecp_sts_t now = ecp_tm_get_ms();
int to_exec_size = 0;
int i;
+ ctx = sock->ctx;
+
#ifdef ECP_WITH_PTHREAD
pthread_mutex_lock(&timer->mutex);
#endif
@@ -210,22 +212,23 @@ ecp_sts_t ecp_timer_exe(ECPSocket *sock) {
ECPConnection *conn = to_exec[i].conn;
unsigned char mtype = to_exec[i].mtype;
ecp_timer_retry_t retry = to_exec[i].retry;
- int rv;
- rv = ECP_OK;
if (to_exec[i].cnt > 0) {
to_exec[i].cnt--;
to_exec[i].frag_cnt = 0;
if (retry) {
- ssize_t _rv;
+ ssize_t rv;
- _rv = retry(conn, to_exec+i);
- if (_rv < 0) rv = _rv;
- }
- if (rv) {
- ecp_err_handle(conn, mtype, rv);
+ rv = retry(conn, to_exec+i);
+ if ((rv < 0) && ctx->logger) {
+ int _rv = rv;
+
+ ctx->logger("ecp_timer_exe: retry err:%d\n", _rv);
+ }
}
} else {
+ int rv;
+
rv = ECP_ERR_TIMEOUT;
if (mtype == ECP_MTYPE_OPEN_REP) {
int _rv;
@@ -239,10 +242,6 @@ ecp_sts_t ecp_timer_exe(ECPSocket *sock) {
}
}
-#ifdef ECP_DEBUG
- if (rv) printf("TIMER ERR:%d\n", rv);
-#endif
-
ecp_conn_refcount_dec(conn);
}
diff --git a/ecp/src/ecp/vconn/vconn.c b/ecp/src/ecp/vconn/vconn.c
index 39e31ce..c0e269f 100644
--- a/ecp/src/ecp/vconn/vconn.c
+++ b/ecp/src/ecp/vconn/vconn.c
@@ -64,18 +64,11 @@ static ssize_t handle_next(ECPConnection *conn, unsigned char *msg, size_t msg_s
if (msg_size < ECP_SIZE_ECDH_PUB) return ECP_ERR_SIZE;
if (ecp_conn_is_outb(conn)) return ECP_ERR;
-
-#ifdef ECP_WITH_PTHREAD
- pthread_mutex_lock(&conn->mutex);
-#endif
+ if (ecp_conn_is_open(conn)) return ECP_ERR;
memcpy(&vconn->vlink_next.public, msg, sizeof(vconn->vlink_next.public));
vconn->vlink_next.valid = 1;
-#ifdef ECP_WITH_PTHREAD
- pthread_mutex_unlock(&conn->mutex);
-#endif
-
return ECP_SIZE_ECDH_PUB;
}
@@ -113,15 +106,16 @@ static ssize_t handle_relay(ECPConnection *conn, unsigned char *msg, size_t msg_
if (_idx != vconn->vconn_next_curr) insert_next = 1;
- if (vconn->vlink_next.valid) conn_next = ecp_ht_search(sock->vconn_table.vlink_keys, &vconn->vlink_next.public);
-
#ifdef ECP_WITH_PTHREAD
pthread_mutex_unlock(&conn->mutex);
#endif
- if (conn_next) {
- _rv = ecp_conn_refcount_inc(conn_next);
- if (_rv) conn_next = NULL;
+ if (vconn->vlink_next.valid) {
+ conn_next = ecp_ht_search(sock->vconn_table.vlink_keys, &vconn->vlink_next.public);
+ if (conn_next) {
+ _rv = ecp_conn_refcount_inc(conn_next);
+ if (_rv) conn_next = NULL;
+ }
}
#ifdef ECP_WITH_PTHREAD
@@ -273,7 +267,7 @@ int ecp_vconn_handle_open(ECPConnection *conn, ECP2Buffer *bufs) {
int rv;
if (ecp_conn_is_inb(conn)) return ECP_OK;
- if (vconn->next == NULL) return ECP_ERR;
+ if (vconn->next == NULL) return ECP_ERR_NEXT;
/* we should release incoming packet before sending next open packet */
ecp_tr_release(bufs->packet, 1);
diff --git a/ecp/src/platform/fe310/features_tmpl.mk b/ecp/src/platform/fe310/features_tmpl.mk
index 39f95cf..54f777c 100644
--- a/ecp/src/platform/fe310/features_tmpl.mk
+++ b/ecp/src/platform/fe310/features_tmpl.mk
@@ -1,3 +1,2 @@
with_vconn = yes
with_dir = yes
-with_debug = yes
diff --git a/ecp/src/platform/fe310/transport.c b/ecp/src/platform/fe310/transport.c
index 35addfa..b53534d 100644
--- a/ecp/src/platform/fe310/transport.c
+++ b/ecp/src/platform/fe310/transport.c
@@ -1,6 +1,5 @@
#include <stdlib.h>
#include <string.h>
-#include <stdio.h>
#include <ecp/core.h>
#include <ecp/tr.h>
@@ -12,6 +11,7 @@ ECPSocket *_ecp_tr_sock = NULL;
unsigned char pld_buf[ECP_MAX_PLD];
static void packet_handler(unsigned char type, unsigned char *buffer, uint16_t len) {
+ ECPContext *ctx;
ECP2Buffer bufs;
ECPBuffer packet;
ECPBuffer payload;
@@ -33,12 +33,13 @@ static void packet_handler(unsigned char type, unsigned char *buffer, uint16_t l
eos_sock_recvfrom(buffer, len, NULL, 0, &addr);
+ ctx = _ecp_tr_sock->ctx;
rv = ecp_pkt_handle(_ecp_tr_sock, NULL, &addr, &bufs, len-EOS_SOCK_SIZE_UDP_HDR);
-#ifdef ECP_DEBUG
- if (rv < 0) {
- printf("RCV ERR:%d\n", rv);
+ if ((rv < 0) && ctx->logger) {
+ int _rv = rv;
+
+ ctx->logger("packet_handler: err:%d\n", _rv);
}
-#endif
if (bufs.packet->buffer) {
eos_net_free(buffer, 0);
} else {
diff --git a/ecp/src/platform/posix/features_tmpl.mk b/ecp/src/platform/posix/features_tmpl.mk
index a840328..2887e08 100644
--- a/ecp/src/platform/posix/features_tmpl.mk
+++ b/ecp/src/platform/posix/features_tmpl.mk
@@ -2,6 +2,3 @@ with_pthread = yes
with_htable = yes
with_vconn = yes
with_dir = yes
-with_debug = yes
-#with_rbuf = yes
-#with_msgq = yes
diff --git a/ecp/test/dir.c b/ecp/test/dir.c
index 099ecbb..9c04432 100644
--- a/ecp/test/dir.c
+++ b/ecp/test/dir.c
@@ -36,7 +36,7 @@ int main(int argc, char *argv[]) {
if (argc != 3) usage(argv[0]);
- rv = ecp_ctx_init(&ctx, NULL, NULL, NULL, handle_err);
+ rv = ecp_ctx_init(&ctx, NULL, NULL, NULL, handle_err, printf);
LOG("ecp_ctx_init", rv);
rv = ecp_dir_set_handler(&ctx, &dir_handler, print_list);
diff --git a/ecp/test/init.c b/ecp/test/init.c
index fbf17c6..7004794 100644
--- a/ecp/test/init.c
+++ b/ecp/test/init.c
@@ -26,6 +26,6 @@ static void conn_free(ECPConnection *conn) {
int ecp_init(ECPContext *ctx) {
int rv;
- rv = ecp_ctx_init(ctx, NULL, conn_new, conn_free, handle_err);
+ rv = ecp_ctx_init(ctx, NULL, conn_new, conn_free, handle_err, printf);
return rv;
}
diff --git a/ecp/test/init_vconn.c b/ecp/test/init_vconn.c
index ba72c01..273ae5d 100644
--- a/ecp/test/init_vconn.c
+++ b/ecp/test/init_vconn.c
@@ -51,7 +51,7 @@ static void conn_free(ECPConnection *conn) {
int ecp_init(ECPContext *ctx, ECPConnHandler *vconn_handler, ECPConnHandler *vlink_handler) {
int rv;
- rv = ecp_ctx_init(ctx, NULL, conn_new, conn_free, handle_err);
+ rv = ecp_ctx_init(ctx, NULL, conn_new, conn_free, handle_err, printf);
if (rv) return rv;
rv = ecp_vconn_handler_init(ctx, vconn_handler);