From 7a0e44998f426752c58975ae686d8e88aa131195 Mon Sep 17 00:00:00 2001 From: Uros Majstorovic Date: Sat, 11 May 2024 15:58:48 +0200 Subject: added error handler per connection type; improved handlers API and error reporting --- ecp/server/dir.c | 4 +- ecp/server/server.c | 4 +- ecp/src/ecp/core.c | 171 +++++++++++++++++++++---------------- ecp/src/ecp/core.h | 71 ++++++++------- ecp/src/ecp/vconn/vconn.c | 19 +++-- ecp/src/platform/fe310/transport.c | 13 +-- ecp/src/platform/posix/transport.c | 38 +++++---- ecp/test/basic.c | 4 +- ecp/test/client.c | 2 +- ecp/test/init.c | 2 +- ecp/test/init_vconn.c | 2 +- ecp/test/server.c | 2 +- ecp/test/vc_client.c | 2 +- ecp/test/vc_inb.c | 4 +- ecp/test/vc_outb.c | 4 +- ecp/test/vc_server.c | 2 +- 16 files changed, 191 insertions(+), 153 deletions(-) (limited to 'ecp') diff --git a/ecp/server/dir.c b/ecp/server/dir.c index 03474df..149d14d 100644 --- a/ecp/server/dir.c +++ b/ecp/server/dir.c @@ -990,7 +990,7 @@ int dir_init(ECPSocket *sock) { pthread_rwlock_destroy(&dir_shadow_rwlock); return ECP_ERR_ALLOC; } - ecp_conn_handler_init(handler, dir_handle_msg, NULL, NULL, NULL); + ecp_conn_handler_init(handler, NULL, NULL, dir_handle_msg, NULL); if (is_dir) { c_handler = malloc(sizeof(ECPConnHandler)); @@ -1005,7 +1005,7 @@ int dir_init(ECPSocket *sock) { if (c_handler) free(c_handler); return ECP_ERR_ALLOC; } - ecp_conn_handler_init(c_handler, dir_handle_client_msg, NULL, NULL, NULL); + ecp_conn_handler_init(c_handler, NULL, NULL, dir_handle_client_msg, NULL); memset(dir_online, 0, sizeof(DIROnline)); } diff --git a/ecp/server/server.c b/ecp/server/server.c index 5493762..223c548 100644 --- a/ecp/server/server.c +++ b/ecp/server/server.c @@ -103,7 +103,7 @@ static void conn_free(ECPConnection *conn) { free(conn); } -static int key_check(ECPSocket *sock, ECPConnection *parent, unsigned char ctype, ecp_ecdh_public_t *public) { +static int conn_auth(ECPSocket *sock, ECPConnection *parent, unsigned char ctype, ecp_ecdh_public_t *public) { switch (ctype) { case CTYPE_DIR: { if (public == NULL) return 0; @@ -145,7 +145,7 @@ void log_print(int level, char *format, ...) { int ecp_init(ECPContext *ctx, ECPConnHandler *vconn_handler, ECPConnHandler *vlink_handler) { int rv; - rv = ecp_ctx_init(ctx, handle_err, conn_new, conn_free, key_check); + rv = ecp_ctx_init(ctx, conn_auth, conn_new, conn_free, handle_err); if (rv) return rv; rv = ecp_vconn_handler_init(ctx, vconn_handler); diff --git a/ecp/src/ecp/core.c b/ecp/src/ecp/core.c index 357514c..3063fd2 100644 --- a/ecp/src/ecp/core.c +++ b/ecp/src/ecp/core.c @@ -30,14 +30,14 @@ int ecp_dhkey_gen(ECPDHKey *key) { return ECP_OK; } -int ecp_ctx_init(ECPContext *ctx, ecp_err_handler_t handle_err, ecp_conn_new_t conn_new, ecp_conn_free_t conn_free, ecp_key_checker_t key_checker) { +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 rv; memset(ctx, 0, sizeof(ECPContext)); - ctx->handle_err = handle_err; + ctx->conn_auth = conn_auth; ctx->conn_new = conn_new; ctx->conn_free = conn_free; - ctx->key_checker = key_checker; + ctx->handle_err = handle_err; rv = ecp_tr_init(ctx); if (rv) return rv; @@ -111,7 +111,7 @@ int ecp_node_set_addr(ECPNode *node, void *addr) { int rv; rv = ecp_tr_addr_set(&node->addr, addr); - if (rv) return ECP_ERR_NET_ADDR; + if (rv) return ECP_ERR_ADDR; return ECP_OK; } @@ -301,7 +301,7 @@ static ECPConnection *conn_table_search_pub(ECPSocket *sock, unsigned char c_idx } else { unsigned char _c_idx; - if (c_idx & ~ECP_ECDH_IDX_MASK) continue; + if (c_idx & ~ECP_KEYID_MASK) continue; _c_idx = c_idx % ECP_MAX_NODE_KEY; if (conn->rkey[_c_idx].valid && (memcmp(c_public, &conn->rkey[_c_idx].public, sizeof(conn->key[c_idx].public)) == 0)) { @@ -587,7 +587,7 @@ int ecp_sock_dhkey_new(ECPSocket *sock) { int ecp_sock_dhkey_get(ECPSocket *sock, unsigned char idx, ECPDHKey *key) { int rv = ECP_OK; - if (idx == ECP_ECDH_IDX_PERMA) { + if (idx == ECP_KEYID_PERMA) { *key = sock->key_perma; } else { @@ -598,7 +598,7 @@ int ecp_sock_dhkey_get(ECPSocket *sock, unsigned char idx, ECPDHKey *key) { if (idx < ECP_MAX_SOCK_KEY) { *key = sock->key[idx]; } else { - rv = ECP_ERR_ECDH_IDX; + rv = ECP_ERR_KEYID; } #ifdef ECP_WITH_PTHREAD @@ -607,7 +607,7 @@ int ecp_sock_dhkey_get(ECPSocket *sock, unsigned char idx, ECPDHKey *key) { } - if (!rv && !key->valid) rv = ECP_ERR_ECDH_IDX; + if (!rv && !key->valid) rv = ECP_ERR_KEYID; return rv; } @@ -621,11 +621,11 @@ int ecp_sock_dhkey_get_pub(ECPSocket *sock, unsigned char *idx, ecp_ecdh_public_ #endif _idx = sock->key_curr; - if (_idx == ECP_ECDH_IDX_INV) rv = ECP_ERR_ECDH_IDX; + if (_idx == ECP_KEYID_INV) rv = ECP_ERR_KEYID; if (!rv) { key = &sock->key[_idx]; - if (!key->valid) rv = ECP_ERR_ECDH_IDX; + if (!key->valid) rv = ECP_ERR_KEYID; } if (!rv) memcpy(public, &key->public, sizeof(key->public)); @@ -801,7 +801,7 @@ static ECPDHKey *conn_dhkey_get(ECPConnection *conn, unsigned char idx) { static int conn_dhkey_set(ECPConnection *conn, unsigned char idx, ECPDHKey *key) { ECPSocket *sock = conn->sock; - if (idx >= ECP_MAX_CONN_KEY) return ECP_ERR_ECDH_IDX; + if (idx >= ECP_MAX_CONN_KEY) return ECP_ERR_KEYID; #ifdef ECP_WITH_HTABLE if (ecp_conn_is_outb(conn) && _ecp_conn_is_reg(conn) && conn->key[idx].valid) { @@ -837,7 +837,7 @@ static int conn_dhkey_new(ECPConnection *conn, ECPDHKey *key) { int rv; idx = conn->key_curr; - if (idx == ECP_ECDH_IDX_INV) return ECP_ERR_ECDH_IDX; + if (idx == ECP_KEYID_INV) return ECP_ERR_KEYID; idx = (idx + 1) % ECP_MAX_CONN_KEY; rv = conn_dhkey_set(conn, idx, key); @@ -848,9 +848,9 @@ static int conn_dhkey_new(ECPConnection *conn, ECPDHKey *key) { } static void conn_dhkey_set_curr(ECPConnection *conn) { - if (conn->key_next != ECP_ECDH_IDX_INV) { + if (conn->key_next != ECP_KEYID_INV) { conn->key_curr = conn->key_next; - conn->key_next = ECP_ECDH_IDX_INV; + conn->key_next = ECP_KEYID_INV; } } @@ -871,12 +871,12 @@ static int conn_dhkey_get_pub(ECPConnection *conn, unsigned char *idx, ecp_ecdh_ } } } else { - if (conn->key_next != ECP_ECDH_IDX_INV) { + if (conn->key_next != ECP_KEYID_INV) { *idx = conn->key_next; } else { *idx = conn->key_curr; } - if ((*idx == ECP_ECDH_IDX_INV) || !conn->key[*idx].valid) return ECP_ERR_ECDH_IDX; + if ((*idx == ECP_KEYID_INV) || !conn->key[*idx].valid) return ECP_ERR_KEYID; memcpy(public, &conn->key[*idx].public, sizeof(conn->key[*idx].public)); } @@ -891,7 +891,7 @@ static int conn_dhkey_set_pub(ECPConnection *conn, unsigned char idx, ecp_ecdh_p int i; ecp_sts_t now = ecp_tm_get_s(); - if (idx & ~ECP_ECDH_IDX_MASK) return ECP_ERR_ECDH_IDX; + if (idx & ~ECP_KEYID_MASK) return ECP_ERR_KEYID; _idx = idx % ECP_MAX_NODE_KEY; key = &conn->rkey[_idx]; @@ -935,9 +935,9 @@ static int conn_dhkey_set_pub(ECPConnection *conn, unsigned char idx, ecp_ecdh_p static ECPDHPub *conn_dhkey_get_remote(ECPConnection *conn, unsigned char idx) { ECPDHPub *key = NULL; - if (idx == ECP_ECDH_IDX_PERMA) { + if (idx == ECP_KEYID_PERMA) { key = &conn->remote.key_perma; - } else if ((idx & ECP_ECDH_IDX_MASK) == idx) { + } else if ((idx & ECP_KEYID_MASK) == idx) { key = &conn->rkey[idx % ECP_MAX_NODE_KEY]; } @@ -950,17 +950,17 @@ static int conn_shkey_get(ECPConnection *conn, unsigned char s_idx, unsigned cha ECPDHKey *priv; int rv; - if (ecp_conn_is_outb(conn) && (s_idx == ECP_ECDH_IDX_PERMA)) { + if (ecp_conn_is_outb(conn) && (s_idx == ECP_KEYID_PERMA)) { pub = conn_dhkey_get_remote(conn, s_idx); priv = conn_dhkey_get(conn, c_idx); - if ((pub == NULL) || (priv == NULL)) return ECP_ERR_ECDH_IDX; + if ((pub == NULL) || (priv == NULL)) return ECP_ERR_KEYID; ecp_ecdh_shkey(shkey, &pub->public, &priv->private); } else { ECPDHShkey *_shkey; - if (s_idx & ~ECP_ECDH_IDX_MASK) return ECP_ERR_ECDH_IDX; - if (c_idx & ~ECP_ECDH_IDX_MASK) return ECP_ERR_ECDH_IDX; + if (s_idx & ~ECP_KEYID_MASK) return ECP_ERR_KEYID; + if (c_idx & ~ECP_KEYID_MASK) return ECP_ERR_KEYID; _shkey = &conn->shkey[s_idx % ECP_MAX_NODE_KEY][c_idx % ECP_MAX_NODE_KEY]; if (!_shkey->valid) { @@ -969,7 +969,7 @@ static int conn_shkey_get(ECPConnection *conn, unsigned char s_idx, unsigned cha ECPDHKey priv; pub = conn_dhkey_get_remote(conn, c_idx); - if (pub == NULL) return ECP_ERR_ECDH_IDX; + if (pub == NULL) return ECP_ERR_KEYID; rv = ecp_sock_dhkey_get(sock, s_idx, &priv); if (rv) return rv; @@ -981,7 +981,7 @@ static int conn_shkey_get(ECPConnection *conn, unsigned char s_idx, unsigned cha pub = conn_dhkey_get_remote(conn, s_idx); priv = conn_dhkey_get(conn, c_idx); - if ((pub == NULL) || (priv == NULL)) return ECP_ERR_ECDH_IDX; + if ((pub == NULL) || (priv == NULL)) return ECP_ERR_KEYID; ecp_ecdh_shkey(&_shkey->key, &pub->public, &priv->private); } _shkey->valid = 1; @@ -995,8 +995,8 @@ static int conn_shkey_get(ECPConnection *conn, unsigned char s_idx, unsigned cha static int conn_shkey_set(ECPConnection *conn, unsigned char s_idx, unsigned char c_idx, ecp_aead_key_t *shkey) { ECPDHShkey *_shkey; - if (s_idx & ~ECP_ECDH_IDX_MASK) return ECP_ERR_ECDH_IDX; - if (c_idx & ~ECP_ECDH_IDX_MASK) return ECP_ERR_ECDH_IDX; + if (s_idx & ~ECP_KEYID_MASK) return ECP_ERR_KEYID; + if (c_idx & ~ECP_KEYID_MASK) return ECP_ERR_KEYID; _shkey = &conn->shkey[s_idx % ECP_MAX_NODE_KEY][c_idx % ECP_MAX_NODE_KEY]; memcpy(_shkey->key, shkey, sizeof(_shkey->key)); @@ -1019,9 +1019,9 @@ void ecp_conn_init(ECPConnection *conn, ECPSocket *sock, unsigned char ctype) { conn->sock = sock; conn->type = ctype; - conn->key_curr = ECP_ECDH_IDX_INV; - conn->key_next = ECP_ECDH_IDX_INV; - conn->rkey_curr = ECP_ECDH_IDX_INV; + conn->key_curr = ECP_KEYID_INV; + conn->key_next = ECP_KEYID_INV; + conn->rkey_curr = ECP_KEYID_INV; arc4random_buf(&conn->nonce_out, sizeof(conn->nonce_out)); conn->access_ts = 0; conn->keyx_ts = 0; @@ -1547,7 +1547,7 @@ int ecp_conn_dhkey_get(ECPConnection *conn, unsigned char idx, ECPDHKey *key) { pthread_mutex_lock(&conn->mutex); #endif - if (idx == ECP_ECDH_IDX_INV) idx = conn->key_curr; + if (idx == ECP_KEYID_INV) idx = conn->key_curr; _key = conn_dhkey_get(conn, idx); if (_key) *key = *_key; @@ -1555,7 +1555,7 @@ int ecp_conn_dhkey_get(ECPConnection *conn, unsigned char idx, ECPDHKey *key) { pthread_mutex_unlock(&conn->mutex); #endif - if (_key == NULL) return ECP_ERR_ECDH_IDX; + if (_key == NULL) return ECP_ERR_KEYID; return ECP_OK; } @@ -1619,7 +1619,7 @@ int ecp_conn_dhkey_get_remote(ECPConnection *conn, unsigned char idx, ECPDHPub * pthread_mutex_lock(&conn->mutex); #endif - if (idx == ECP_ECDH_IDX_INV) idx = conn->rkey_curr; + if (idx == ECP_KEYID_INV) idx = conn->rkey_curr; _key = conn_dhkey_get_remote(conn, idx); if (_key) *key = *_key; @@ -1627,83 +1627,106 @@ int ecp_conn_dhkey_get_remote(ECPConnection *conn, unsigned char idx, ECPDHPub * pthread_mutex_unlock(&conn->mutex); #endif - if (_key == NULL) return ECP_ERR_ECDH_IDX; + if (_key == NULL) return ECP_ERR_KEYID; return ECP_OK; } -void ecp_conn_handler_init(ECPConnHandler *handler, ecp_msg_handler_t handle_msg, ecp_open_handler_t handle_open, ecp_close_handler_t handle_close, ecp_send_open_t send_open) { +void ecp_conn_handler_init(ECPConnHandler *handler, ecp_open_handler_t handle_open, ecp_close_handler_t handle_close, ecp_msg_handler_t handle_msg, ecp_err_handler_t handle_err) { memset(handler, 0, sizeof(ECPConnHandler)); - handler->handle_msg = handle_msg; handler->handle_open = handle_open; handler->handle_close = handle_close; - handler->send_open = send_open; + handler->handle_msg = handle_msg; + handler->handle_err = handle_err; + handler->send_oreq = NULL; } -ecp_msg_handler_t ecp_get_msg_handler(ECPConnection *conn) { +void ecp_conn_handler_set_oreq_f(ECPConnHandler *handler, ecp_oreq_send_t send_oreq) { + handler->send_oreq = send_oreq; +} + +ecp_open_handler_t ecp_get_open_handler(ECPConnection *conn) { ECPContext *ctx = conn->sock->ctx; unsigned char ctype; ctype = conn->type & ECP_CTYPE_MASK; if (ecp_conn_is_sys(conn)) { if (ctype >= ECP_MAX_CTYPE_SYS) return NULL; - return ctx->handler_sys[ctype] ? ctx->handler_sys[ctype]->handle_msg : NULL; + return ctx->handler_sys[ctype] ? ctx->handler_sys[ctype]->handle_open : NULL; } else { if (ctype >= ECP_MAX_CTYPE) return NULL; - return ctx->handler[ctype] ? ctx->handler[ctype]->handle_msg : NULL; + return ctx->handler[ctype] ? ctx->handler[ctype]->handle_open : NULL; } } -ecp_open_handler_t ecp_get_open_handler(ECPConnection *conn) { +ecp_close_handler_t ecp_get_close_handler(ECPConnection *conn) { ECPContext *ctx = conn->sock->ctx; unsigned char ctype; ctype = conn->type & ECP_CTYPE_MASK; if (ecp_conn_is_sys(conn)) { if (ctype >= ECP_MAX_CTYPE_SYS) return NULL; - return ctx->handler_sys[ctype] ? ctx->handler_sys[ctype]->handle_open : NULL; + return ctx->handler_sys[ctype] ? ctx->handler_sys[ctype]->handle_close : NULL; } else { if (ctype >= ECP_MAX_CTYPE) return NULL; - return ctx->handler[ctype] ? ctx->handler[ctype]->handle_open : NULL; + return ctx->handler[ctype] ? ctx->handler[ctype]->handle_close : NULL; } } -ecp_close_handler_t ecp_get_close_handler(ECPConnection *conn) { +ecp_msg_handler_t ecp_get_msg_handler(ECPConnection *conn) { ECPContext *ctx = conn->sock->ctx; unsigned char ctype; ctype = conn->type & ECP_CTYPE_MASK; if (ecp_conn_is_sys(conn)) { if (ctype >= ECP_MAX_CTYPE_SYS) return NULL; - return ctx->handler_sys[ctype] ? ctx->handler_sys[ctype]->handle_close : NULL; + return ctx->handler_sys[ctype] ? ctx->handler_sys[ctype]->handle_msg : NULL; } else { if (ctype >= ECP_MAX_CTYPE) return NULL; - return ctx->handler[ctype] ? ctx->handler[ctype]->handle_close : NULL; + return ctx->handler[ctype] ? ctx->handler[ctype]->handle_msg : NULL; + } +} + +ecp_err_handler_t ecp_get_err_handler(ECPConnection *conn) { + ECPContext *ctx = conn->sock->ctx; + unsigned char ctype; + + ctype = conn->type & ECP_CTYPE_MASK; + if (ecp_conn_is_sys(conn)) { + if (ctype >= ECP_MAX_CTYPE_SYS) return NULL; + return ctx->handler_sys[ctype] ? ctx->handler_sys[ctype]->handle_err : NULL; + } else { + if (ctype >= ECP_MAX_CTYPE) return NULL; + return ctx->handler[ctype] ? ctx->handler[ctype]->handle_err : NULL; } } -ecp_send_open_t ecp_get_send_open_f(ECPConnection *conn) { +ecp_oreq_send_t ecp_get_oreq_send_f(ECPConnection *conn) { ECPContext *ctx = conn->sock->ctx; unsigned char ctype; ctype = conn->type & ECP_CTYPE_MASK; if (ecp_conn_is_sys(conn)) { if (ctype >= ECP_MAX_CTYPE_SYS) return NULL; - return ctx->handler_sys[ctype] ? ctx->handler_sys[ctype]->send_open : NULL; + return ctx->handler_sys[ctype] ? ctx->handler_sys[ctype]->send_oreq : NULL; } else { if (ctype >= ECP_MAX_CTYPE) return NULL; - return ctx->handler[ctype] ? ctx->handler[ctype]->send_open : NULL; + return ctx->handler[ctype] ? ctx->handler[ctype]->send_oreq : NULL; } } void ecp_err_handle(ECPConnection *conn, unsigned char mtype, int err) { ECPContext *ctx = conn->sock->ctx; + 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; - if (ctx->handle_err) ctx->handle_err(conn, mtype, err); + + err_handler = ecp_get_err_handler(conn); + if (err_handler == NULL) err_handler = ctx->handle_err; + if (err_handler) err_handler(conn, mtype, err); } static ssize_t _send_ireq(ECPConnection *conn, ECPTimerItem *ti) { @@ -1723,7 +1746,7 @@ static ssize_t _send_ireq(ECPConnection *conn, ECPTimerItem *ti) { msg = ecp_pld_get_msg(payload.buffer, payload.size); memset(msg, 0, ECP_SIZE_ZPAD_PLD); - rv = _ecp_pld_send(conn, &packet, ECP_ECDH_IDX_PERMA, ECP_ECDH_IDX_INV, ECP_SIZE_ZPAD_HDR, NULL, NULL, &payload, ECP_SIZE_PLD(ECP_SIZE_ZPAD_PLD, ECP_MTYPE_INIT_REQ), 0, ti); + rv = _ecp_pld_send(conn, &packet, ECP_KEYID_PERMA, ECP_KEYID_INV, ECP_SIZE_ZPAD_HDR, NULL, NULL, &payload, ECP_SIZE_PLD(ECP_SIZE_ZPAD_PLD, ECP_MTYPE_INIT_REQ), 0, ti); return rv; } @@ -1842,7 +1865,7 @@ ssize_t ecp_send_init_rep(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t pkt_meta.nonce = &nonce; pkt_meta.ntype = ECP_NTYPE_INB; pkt_meta.public = (ecp_ecdh_public_t *)public_buf; - pkt_meta.s_idx = ECP_ECDH_IDX_PERMA; + pkt_meta.s_idx = ECP_KEYID_PERMA; pkt_meta.c_idx = c_idx; rv = ecp_pld_send_irep(sock, parent, addr, &packet, &pkt_meta, &payload, ECP_SIZE_PLD(1+ECP_SIZE_ECDH_PUB+ECP_SIZE_ATAG, ECP_MTYPE_INIT_REP), 0); @@ -1850,7 +1873,7 @@ ssize_t ecp_send_init_rep(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t } ssize_t ecp_handle_init_rep(ECPConnection *conn, unsigned char *msg, size_t msg_size, unsigned char *nonce_buf, ECP2Buffer *bufs) { - ecp_send_open_t send_open_f; + ecp_oreq_send_t send_oreq_f; unsigned char cookie[ECP_SIZE_COOKIE]; unsigned char *atag; ssize_t rv; @@ -1862,13 +1885,13 @@ ssize_t ecp_handle_init_rep(ECPConnection *conn, unsigned char *msg, size_t msg_ if (_rv) return _rv; atag = msg+1+ECP_SIZE_ECDH_PUB; - send_open_f = ecp_get_send_open_f(conn); - if (send_open_f == NULL) send_open_f = ecp_send_open_req; + send_oreq_f = ecp_get_oreq_send_f(conn); + if (send_oreq_f == NULL) send_oreq_f = ecp_send_open_req; memcpy(cookie, nonce_buf, ECP_SIZE_NONCE); memcpy(cookie+ECP_SIZE_NONCE, atag, ECP_SIZE_ATAG); ecp_tr_release(bufs->packet, 1); - rv = send_open_f(conn, cookie); + rv = send_oreq_f(conn, cookie); if (rv < 0) return rv; return 1+ECP_SIZE_ECDH_PUB+ECP_SIZE_ATAG; @@ -1904,9 +1927,9 @@ ssize_t ecp_write_open_req(ECPConnection *conn, ECPBuffer *payload) { if (payload->size < ECP_SIZE_PLD(2+ECP_SIZE_VBOX, ECP_MTYPE_OPEN_REQ)) return ECP_ERR_SIZE; - _rv = ecp_sock_dhkey_get(sock, ECP_ECDH_IDX_PERMA, &key_perma); - if (!_rv) _rv = ecp_conn_dhkey_get_remote(conn, ECP_ECDH_IDX_PERMA, &rkey_perma); - if (!_rv) _rv = ecp_conn_dhkey_get_remote(conn, ECP_ECDH_IDX_INV, &rkey_curr); + _rv = ecp_sock_dhkey_get(sock, ECP_KEYID_PERMA, &key_perma); + if (!_rv) _rv = ecp_conn_dhkey_get_remote(conn, ECP_KEYID_PERMA, &rkey_perma); + if (!_rv) _rv = ecp_conn_dhkey_get_remote(conn, ECP_KEYID_INV, &rkey_curr); if (!_rv) _rv = ecp_conn_dhkey_get_pub(conn, NULL, &public); if (_rv) return _rv; @@ -1973,7 +1996,7 @@ ssize_t ecp_handle_open_req(ECPSocket *sock, ECPConnection *parent, unsigned cha if (msg_size < ECP_SIZE_VBOX) return ECP_ERR_SIZE; - _rv = ecp_sock_dhkey_get(sock, ECP_ECDH_IDX_PERMA, &key_perma); + _rv = ecp_sock_dhkey_get(sock, ECP_KEYID_PERMA, &key_perma); if (!_rv) _rv = ecp_sock_dhkey_get(sock, s_idx, &key_curr); if (_rv) return _rv; @@ -1994,8 +2017,8 @@ ssize_t ecp_handle_open_req(ECPSocket *sock, ECPConnection *parent, unsigned cha rkey_perma.valid = 1; } - if (sock->ctx->key_checker) { - _rv = sock->ctx->key_checker(sock, parent, ctype, rkey_perma.valid ? &rkey_perma.public : NULL); + 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; } @@ -2329,7 +2352,7 @@ ssize_t ecp_unpack(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t *addr, idx = packet[ECP_SIZE_PROTO]; s_idx = (idx & 0xF0) >> 4; c_idx = (idx & 0x0F); - if ((s_idx == ECP_ECDH_IDX_PERMA) && (c_idx == ECP_ECDH_IDX_NOKEY)) { + if ((s_idx == ECP_KEYID_PERMA) && (c_idx == ECP_KEYID_NOKEY)) { public_buf = NULL; c_idx = 0; } else { @@ -2381,7 +2404,7 @@ ssize_t ecp_unpack(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t *addr, is_inb = 1; - if (s_idx == ECP_ECDH_IDX_PERMA) { + if (s_idx == ECP_KEYID_PERMA) { /* init request message */ unsigned char *zpad_buf; int i; @@ -2741,7 +2764,7 @@ static ssize_t _pack(ECPBuffer *packet, ECPPktMeta *pkt_meta, ECPBuffer *payload pkt_buf[0] = 0; pkt_buf[1] = 0; pkt_buf[ECP_SIZE_PROTO] = (pkt_meta->s_idx << 4) | pkt_meta->c_idx; - pkt_buf += 3; + pkt_buf += ECP_SIZE_PROTO + 1; if (pkt_meta->public) { memcpy(pkt_buf, pkt_meta->public, ECP_SIZE_ECDH_PUB); @@ -2776,14 +2799,14 @@ static ssize_t _pack_conn(ECPConnection *conn, ECPBuffer *packet, unsigned char pthread_mutex_lock(&conn->mutex); #endif - if (s_idx == ECP_ECDH_IDX_INV) { + if (s_idx == ECP_KEYID_INV) { if (ecp_conn_is_inb(conn)) { s_idx = conn->key_curr; } else { s_idx = conn->rkey_curr; } } - if (c_idx == ECP_ECDH_IDX_INV) { + if (c_idx == ECP_KEYID_INV) { if (ecp_conn_is_outb(conn)) { c_idx = conn->key_curr; } else { @@ -2799,7 +2822,7 @@ static ssize_t _pack_conn(ECPConnection *conn, ECPBuffer *packet, unsigned char if (key) { memcpy(&public, &key->public, sizeof(public)); } else { - rv = ECP_ERR_ECDH_IDX; + rv = ECP_ERR_KEYID; } } else { ECPDHPub *key = conn_dhkey_get_remote(conn, c_idx); @@ -2807,7 +2830,7 @@ static ssize_t _pack_conn(ECPConnection *conn, ECPBuffer *packet, unsigned char if (key) { memcpy(&public, &key->public, sizeof(public)); } else { - rv = ECP_ERR_ECDH_IDX; + rv = ECP_ERR_KEYID; } memcpy(&public, &key->public, sizeof(public)); } @@ -2845,7 +2868,7 @@ ssize_t ecp_pack_irep(ECPConnection *parent, ECPBuffer *packet, ECPPktMeta *pkt_ if (parent == NULL) { pkt_meta->public = NULL; - pkt_meta->c_idx = ECP_ECDH_IDX_NOKEY; + pkt_meta->c_idx = ECP_KEYID_NOKEY; } rv = _pack(packet, pkt_meta, payload, pld_size); @@ -2982,19 +3005,19 @@ ssize_t _ecp_pld_send(ECPConnection *conn, ECPBuffer *packet, unsigned char s_id } ssize_t ecp_pld_send(ECPConnection *conn, ECPBuffer *packet, ECPBuffer *payload, size_t pld_size, unsigned char flags) { - return _ecp_pld_send(conn, packet, ECP_ECDH_IDX_INV, ECP_ECDH_IDX_INV, 0, NULL, NULL, payload, pld_size, flags, NULL); + return _ecp_pld_send(conn, packet, ECP_KEYID_INV, ECP_KEYID_INV, 0, NULL, NULL, payload, pld_size, flags, NULL); } ssize_t ecp_pld_send_wtimer(ECPConnection *conn, ECPBuffer *packet, ECPBuffer *payload, size_t pld_size, unsigned char flags, ECPTimerItem *ti) { - return _ecp_pld_send(conn, packet, ECP_ECDH_IDX_INV, ECP_ECDH_IDX_INV, 0, NULL, NULL, payload, pld_size, flags, ti); + return _ecp_pld_send(conn, packet, ECP_KEYID_INV, ECP_KEYID_INV, 0, NULL, NULL, payload, pld_size, flags, ti); } ssize_t ecp_pld_send_wcookie(ECPConnection *conn, ECPBuffer *packet, ECPBuffer *payload, size_t pld_size, unsigned char flags, unsigned char *cookie) { - return _ecp_pld_send(conn, packet, ECP_ECDH_IDX_INV, ECP_ECDH_IDX_INV, 0, cookie, NULL, payload, pld_size, flags, NULL); + return _ecp_pld_send(conn, packet, ECP_KEYID_INV, ECP_KEYID_INV, 0, cookie, NULL, payload, pld_size, flags, NULL); } ssize_t ecp_pld_send_wnonce(ECPConnection *conn, ECPBuffer *packet, ECPBuffer *payload, size_t pld_size, unsigned char flags, ecp_nonce_t *nonce) { - return _ecp_pld_send(conn, packet, ECP_ECDH_IDX_INV, ECP_ECDH_IDX_INV, 0, NULL, nonce, payload, pld_size, flags, NULL); + return _ecp_pld_send(conn, packet, ECP_KEYID_INV, ECP_KEYID_INV, 0, NULL, nonce, payload, pld_size, flags, NULL); } 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) { diff --git a/ecp/src/ecp/core.h b/ecp/src/ecp/core.h index f0d6534..3631942 100644 --- a/ecp/src/ecp/core.h +++ b/ecp/src/ecp/core.h @@ -21,24 +21,28 @@ #define ECP_ERR_MTYPE -9 #define ECP_ERR_CTYPE -10 #define ECP_ERR_HANDLER -11 -#define ECP_ERR_PKT -12 -#define ECP_ERR_ZPAD -13 -#define ECP_ERR_COOKIE -14 - -#define ECP_ERR_NET_ADDR -20 -#define ECP_ERR_MAX_PARENT -21 -#define ECP_ERR_NEXT -22 - -#define ECP_ERR_ECDH_IDX -25 -#define ECP_ERR_ENCRYPT -26 -#define ECP_ERR_DECRYPT -27 -#define ECP_ERR_SIGN -28 -#define ECP_ERR_VERIFY -29 -#define ECP_ERR_SEND -30 -#define ECP_ERR_RECV -31 -#define ECP_ERR_SEQ -32 -#define ECP_ERR_VBOX -33 -#define ECP_ERR_CLOSED -34 +#define ECP_ERR_CLOSED -12 + +#define ECP_ERR_PKT -20 +#define ECP_ERR_ZPAD -21 +#define ECP_ERR_COOKIE -22 +#define ECP_ERR_VBOX -23 +#define ECP_ERR_KEYID -24 +#define ECP_ERR_SEQ -25 + +#define ECP_ERR_ENCRYPT -30 +#define ECP_ERR_DECRYPT -31 +#define ECP_ERR_SIGN -32 +#define ECP_ERR_VERIFY -33 + +#define ECP_ERR_ADDR -40 +#define ECP_ERR_OPEN -41 +#define ECP_ERR_BIND -42 +#define ECP_ERR_SEND -43 +#define ECP_ERR_RECV -44 + +#define ECP_ERR_MAX_PARENT -50 +#define ECP_ERR_NEXT -51 #define ECP_MAX_SOCK_CONN 4 #define ECP_MAX_SOCK_KEY 2 @@ -126,10 +130,10 @@ #define ECP_SEND_TIMEOUT 500 #define ECP_POLL_TIMEOUT 500 -#define ECP_ECDH_IDX_INV 0xFF -#define ECP_ECDH_IDX_PERMA 0x0F -#define ECP_ECDH_IDX_NOKEY 0x08 -#define ECP_ECDH_IDX_MASK 0x07 +#define ECP_KEYID_INV 0xFF +#define ECP_KEYID_PERMA 0x0F +#define ECP_KEYID_NOKEY 0x08 +#define ECP_KEYID_MASK 0x07 #define ECP_NTYPE_INB 1 #define ECP_NTYPE_OUTB 2 @@ -231,12 +235,12 @@ 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 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_key_checker_t) (struct ECPSocket *sock, struct ECPConnection *parent, unsigned char ctype, ecp_ecdh_public_t *pub); +typedef int (*ecp_conn_auth_t) (struct ECPSocket *sock, struct ECPConnection *parent, unsigned char ctype, ecp_ecdh_public_t *pub); 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_send_open_t) (struct ECPConnection *conn, unsigned char *cookie); +typedef ssize_t (*ecp_oreq_send_t) (struct ECPConnection *conn, unsigned char *cookie); typedef struct ECPBuffer { unsigned char *buffer; @@ -281,17 +285,18 @@ typedef struct ECPPktMeta { } ECPPktMeta; typedef struct ECPConnHandler { - ecp_msg_handler_t handle_msg; ecp_open_handler_t handle_open; ecp_close_handler_t handle_close; - ecp_send_open_t send_open; + ecp_msg_handler_t handle_msg; + ecp_err_handler_t handle_err; + ecp_oreq_send_t send_oreq; } ECPConnHandler; typedef struct ECPContext { - ecp_err_handler_t handle_err; + ecp_conn_auth_t conn_auth; /* inbound connections only */ ecp_conn_new_t conn_new; /* inbound connections only */ ecp_conn_free_t conn_free; - ecp_key_checker_t key_checker; + ecp_err_handler_t handle_err; ECPConnHandler *handler[ECP_MAX_CTYPE]; ECPConnHandler *handler_sys[ECP_MAX_CTYPE_SYS]; } ECPContext; @@ -379,7 +384,7 @@ typedef struct ECPConnection { int ecp_dhkey_gen(ECPDHKey *key); -int ecp_ctx_init(ECPContext *ctx, ecp_err_handler_t handle_err, ecp_conn_new_t conn_new, ecp_conn_free_t conn_free, ecp_key_checker_t key_checker); +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_set_handler(ECPContext *ctx, unsigned char ctype, ECPConnHandler *handler); ECPConnHandler *ecp_ctx_get_handler(ECPContext *ctx, unsigned char ctype); @@ -454,11 +459,13 @@ int ecp_conn_dhkey_get_pub(ECPConnection *conn, unsigned char *idx, ecp_ecdh_pub int ecp_conn_dhkey_set_pub(ECPConnection *conn, unsigned char idx, ecp_ecdh_public_t *public); int ecp_conn_dhkey_get_remote(ECPConnection *conn, unsigned char idx, ECPDHPub *key); -void ecp_conn_handler_init(ECPConnHandler *handler, ecp_msg_handler_t handle_msg, ecp_open_handler_t handle_open, ecp_close_handler_t handle_close, ecp_send_open_t send_open); -ecp_msg_handler_t ecp_get_msg_handler(ECPConnection *conn); +void ecp_conn_handler_init(ECPConnHandler *handler, ecp_open_handler_t handle_open, ecp_close_handler_t handle_close, ecp_msg_handler_t handle_msg, ecp_err_handler_t handle_err); +void ecp_conn_handler_set_oreq_f(ECPConnHandler *handler, ecp_oreq_send_t send_oreq); ecp_open_handler_t ecp_get_open_handler(ECPConnection *conn); ecp_close_handler_t ecp_get_close_handler(ECPConnection *conn); -ecp_send_open_t ecp_get_send_open_f(ECPConnection *conn); +ecp_msg_handler_t ecp_get_msg_handler(ECPConnection *conn); +ecp_err_handler_t ecp_get_err_handler(ECPConnection *conn); +ecp_oreq_send_t ecp_get_oreq_send_f(ECPConnection *conn); void ecp_err_handle(ECPConnection *conn, unsigned char mtype, int err); ssize_t ecp_send_init_req(ECPConnection *conn, int retry); diff --git a/ecp/src/ecp/vconn/vconn.c b/ecp/src/ecp/vconn/vconn.c index b1be561..39e31ce 100644 --- a/ecp/src/ecp/vconn/vconn.c +++ b/ecp/src/ecp/vconn/vconn.c @@ -91,7 +91,7 @@ static ssize_t handle_relay(ECPConnection *conn, unsigned char *msg, size_t msg_ if (msg_size < ECP_MIN_PKT) return ECP_ERR_SIZE; idx = msg[ECP_SIZE_PROTO]; - if (idx == ECP_ECDH_IDX_INV) return ECP_ERR_ECDH_IDX; + if (idx == ECP_KEYID_INV) return ECP_ERR_KEYID; switch (conn->type) { /* forward message */ @@ -104,7 +104,7 @@ static ssize_t handle_relay(ECPConnection *conn, unsigned char *msg, size_t msg_ _idx = (idx & 0x0F); conn_next = NULL; - if (_idx & ~ECP_ECDH_IDX_MASK) return ECP_ERR_ECDH_IDX; + if (_idx & ~ECP_KEYID_MASK) return ECP_ERR_KEYID; #ifdef ECP_WITH_PTHREAD pthread_mutex_lock(&sock->vconn_table.vlink_keys_mutex); @@ -157,9 +157,9 @@ static ssize_t handle_relay(ECPConnection *conn, unsigned char *msg, size_t msg_ if (conn_next == NULL) return ECP_ERR_NEXT; _idx = (idx & 0xF0) >> 4; - if (_idx == ECP_ECDH_IDX_PERMA) { + if (_idx == ECP_KEYID_PERMA) { /* this is init reply */ - msg[ECP_SIZE_PROTO] = (ECP_ECDH_IDX_PERMA << 4) | ECP_ECDH_IDX_NOKEY; + msg[ECP_SIZE_PROTO] = (ECP_KEYID_PERMA << 4) | ECP_KEYID_NOKEY; memmove(msg+ECP_SIZE_PROTO+1, msg+ECP_SIZE_PROTO+1+ECP_SIZE_ECDH_PUB, msg_size-(ECP_SIZE_PROTO+1+ECP_SIZE_ECDH_PUB)); _msg_size -= ECP_SIZE_ECDH_PUB; } @@ -215,7 +215,7 @@ ssize_t ecp_vconn_pack_parent(ECPConnection *conn, ECPBuffer *payload, ECPBuffer if (payload->size < pkt_size+hdr_size) return ECP_ERR_SIZE; memcpy(msg, packet->buffer, pkt_size); - rv = ecp_pack_conn(conn, packet, ECP_ECDH_IDX_INV, ECP_ECDH_IDX_INV, 0, NULL, NULL, payload, pkt_size+hdr_size, addr); + rv = ecp_pack_conn(conn, packet, ECP_KEYID_INV, ECP_KEYID_INV, 0, NULL, NULL, payload, pkt_size+hdr_size, addr); return rv; } @@ -244,7 +244,7 @@ void ecp_vconn_init_inb(ECPVConnInb *vconn, ECPSocket *sock) { ecp_conn_init(conn, sock, ECP_CTYPE_VCONN); memset(&vconn->vlink_next, 0, sizeof(vconn->vlink_next)); memset(&vconn->vconn_next, 0, sizeof(vconn->vconn_next)); - vconn->vconn_next_curr = ECP_ECDH_IDX_INV; + vconn->vconn_next_curr = ECP_KEYID_INV; } #endif /* ECP_WITH_HTABLE */ @@ -353,7 +353,7 @@ ssize_t ecp_vconn_send_open_req(ECPConnection *conn, unsigned char *cookie) { if (vconn->next == NULL) return ECP_ERR_NEXT; - _rv = ecp_conn_dhkey_get_remote(vconn->next, ECP_ECDH_IDX_PERMA, &key_next); + _rv = ecp_conn_dhkey_get_remote(vconn->next, ECP_KEYID_PERMA, &key_next); if (_rv) return _rv; packet.buffer = pkt_buf; @@ -510,7 +510,8 @@ void ecp_vconn_sock_destroy(ECPSocket *sock) { int ecp_vconn_handler_init(ECPContext *ctx, ECPConnHandler *vconn_handler) { int rv; - ecp_conn_handler_init(vconn_handler, ecp_vconn_handle_msg, ecp_vconn_handle_open, ecp_vconn_handle_close, ecp_vconn_send_open_req); + ecp_conn_handler_init(vconn_handler, ecp_vconn_handle_open, ecp_vconn_handle_close, ecp_vconn_handle_msg, NULL); + ecp_conn_handler_set_oreq_f(vconn_handler, ecp_vconn_send_open_req); rv = ecp_ctx_set_handler(ctx, ECP_CTYPE_VCONN, vconn_handler); return rv; } @@ -518,7 +519,7 @@ int ecp_vconn_handler_init(ECPContext *ctx, ECPConnHandler *vconn_handler) { int ecp_vlink_handler_init(ECPContext *ctx, ECPConnHandler *vlink_handler) { int rv; - ecp_conn_handler_init(vlink_handler, ecp_vlink_handle_msg, ecp_vlink_handle_open, ecp_vlink_handle_close, NULL); + ecp_conn_handler_init(vlink_handler, ecp_vlink_handle_open, ecp_vlink_handle_close, ecp_vlink_handle_msg, NULL); rv = ecp_ctx_set_handler(ctx, ECP_CTYPE_VLINK, vlink_handler); return rv; } diff --git a/ecp/src/platform/fe310/transport.c b/ecp/src/platform/fe310/transport.c index bc758c9..35addfa 100644 --- a/ecp/src/platform/fe310/transport.c +++ b/ecp/src/platform/fe310/transport.c @@ -66,11 +66,12 @@ int ecp_tr_addr_set(ecp_tr_addr_t *addr, void *addr_s) { } int ecp_tr_open(ECPSocket *sock, ecp_tr_addr_t *addr) { - sock->sock = eos_sock_open_udp(packet_handler, NULL); - if (sock->sock < 0) { - sock->sock = 0; - return ECP_ERR; - } + int rv; + + rv = eos_sock_open_udp(packet_handler, NULL); + if (rv < 0) return ECP_ERR_OPEN; + + sock->sock = rv; _ecp_tr_sock = sock; return ECP_OK; @@ -97,7 +98,7 @@ ssize_t ecp_tr_send(ECPSocket *sock, ECPBuffer *packet, size_t pkt_size, ecp_tr_ } else { buf = eos_net_alloc(); } - if (buf == NULL) return ECP_ERR; + if (buf == NULL) return ECP_ERR_ALLOC; rv = eos_sock_sendto_async(sock->sock, reply ? NULL : packet->buffer, pkt_size, addr, buf, !!(flags & ECP_SEND_FLAG_MORE)); if (rv) return ECP_ERR_SEND; diff --git a/ecp/src/platform/posix/transport.c b/ecp/src/platform/posix/transport.c index 51f311a..71267a1 100644 --- a/ecp/src/platform/posix/transport.c +++ b/ecp/src/platform/posix/transport.c @@ -36,17 +36,17 @@ int ecp_tr_addr_set(ecp_tr_addr_t *addr, void *addr_s) { memset(addr_c, 0, sizeof(addr_c)); strncpy(addr_c, addr_s, sizeof(addr_c)-1); colon = strchr(addr_c, ':'); - if (colon == NULL) return -1; + if (colon == NULL) return ECP_ERR_ADDR; *colon = '\0'; colon++; - if (*colon == '\0') return -1; + if (*colon == '\0') return ECP_ERR_ADDR; rv = inet_pton(AF_INET, addr_c, addr->host); - if (rv != 1) return -1; + if (rv != 1) return ECP_ERR_ADDR; hport = strtol(colon, &endptr, 10); - if (*endptr != '\0') return -1; + if (*endptr != '\0') return ECP_ERR_ADDR; addr->port = htons(hport); - return 0; + return ECP_OK; } int ecp_tr_open(ECPSocket *sock, ecp_tr_addr_t *addr) { @@ -63,13 +63,14 @@ int ecp_tr_open(ECPSocket *sock, ecp_tr_addr_t *addr) { _myaddr.sin_port = htons(0); } - sock->sock = socket(PF_INET, SOCK_DGRAM, 0); - if (sock->sock < 0) return sock->sock; + rv = socket(PF_INET, SOCK_DGRAM, 0); + if (rv < 0) return ECP_ERR_OPEN; + sock->sock = rv; rv = bind(sock->sock, (struct sockaddr *)&_myaddr, sizeof(_myaddr)); if (rv < 0) { close(sock->sock); - return rv; + return ECP_ERR_BIND; } return ECP_OK; @@ -81,12 +82,16 @@ void ecp_tr_close(ECPSocket *sock) { ssize_t ecp_tr_send(ECPSocket *sock, ECPBuffer *packet, size_t pkt_size, ecp_tr_addr_t *addr, unsigned char flags) { struct sockaddr_in servaddr; + ssize_t rv; memset((void *)&servaddr, 0, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = addr->port; memcpy((void *)&servaddr.sin_addr, addr->host, sizeof(addr->host)); - return sendto(sock->sock, packet->buffer, pkt_size, 0, (struct sockaddr *)&servaddr, sizeof(servaddr)); + rv = sendto(sock->sock, packet->buffer, pkt_size, 0, (struct sockaddr *)&servaddr, sizeof(servaddr)); + if (rv < 0) return ECP_ERR_SEND; + + return rv; } ssize_t ecp_tr_recv(ECPSocket *sock, ECPBuffer *packet, ecp_tr_addr_t *addr, int timeout) { @@ -95,20 +100,21 @@ ssize_t ecp_tr_recv(ECPSocket *sock, ECPBuffer *packet, ecp_tr_addr_t *addr, int struct pollfd fds[] = { {sock->sock, POLLIN, 0} }; - int rv; + ssize_t rv; + int _rv; - rv = poll(fds, 1, timeout); + _rv = poll(fds, 1, timeout); memset((void *)&servaddr, 0, sizeof(servaddr)); - if (rv == 1) { - ssize_t recvlen = recvfrom(sock->sock, packet->buffer, packet->size, 0, (struct sockaddr *)&servaddr, &addrlen); - if (recvlen < 0) return recvlen; - if (recvlen < ECP_MIN_PKT) return ECP_ERR_RECV; + if (_rv == 1) { + rv = recvfrom(sock->sock, packet->buffer, packet->size, 0, (struct sockaddr *)&servaddr, &addrlen); + if (rv < 0) return ECP_ERR_RECV; + if (rv < ECP_MIN_PKT) return ECP_ERR_RECV; if (addr) { addr->port = servaddr.sin_port; memcpy(addr->host, (void *)&servaddr.sin_addr, sizeof(addr->host)); } - return recvlen; + return rv; } return ECP_ERR_TIMEOUT; diff --git a/ecp/test/basic.c b/ecp/test/basic.c index 637a829..2e6bfd0 100644 --- a/ecp/test/basic.c +++ b/ecp/test/basic.c @@ -55,7 +55,7 @@ int main(int argc, char *argv[]) { rv = ecp_init(&ctx_s); printf("ecp_init RV:%d\n", rv); - ecp_conn_handler_init(&handler_s, handle_msg_s, NULL, NULL, NULL); + ecp_conn_handler_init(&handler_s, NULL, NULL, handle_msg_s, NULL); ecp_ctx_set_handler(&ctx_s, CTYPE_TEST, &handler_s); rv = ecp_dhkey_gen(&key_perma_s); @@ -77,7 +77,7 @@ int main(int argc, char *argv[]) { rv = ecp_init(&ctx_c); printf("ecp_init RV:%d\n", rv); - ecp_conn_handler_init(&handler_c, handle_msg_c, handle_open_c, NULL, NULL); + ecp_conn_handler_init(&handler_c, handle_open_c, NULL, handle_msg_c, NULL); ecp_ctx_set_handler(&ctx_c, CTYPE_TEST, &handler_c); rv = ecp_dhkey_gen(&key_perma_c); diff --git a/ecp/test/client.c b/ecp/test/client.c index af905d5..0236f97 100644 --- a/ecp/test/client.c +++ b/ecp/test/client.c @@ -49,7 +49,7 @@ int main(int argc, char *argv[]) { rv = ecp_init(&ctx); printf("ecp_init RV:%d\n", rv); - ecp_conn_handler_init(&handler, handle_msg, handle_open, NULL, NULL); + ecp_conn_handler_init(&handler, handle_open, NULL, handle_msg, NULL); ecp_ctx_set_handler(&ctx, CTYPE_TEST, &handler); rv = ecp_dhkey_gen(&key_perma); diff --git a/ecp/test/init.c b/ecp/test/init.c index 79c1098..fbf17c6 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, handle_err, conn_new, conn_free, NULL); + rv = ecp_ctx_init(ctx, NULL, conn_new, conn_free, handle_err); return rv; } diff --git a/ecp/test/init_vconn.c b/ecp/test/init_vconn.c index 3393a16..ba72c01 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, handle_err, conn_new, conn_free, NULL); + rv = ecp_ctx_init(ctx, NULL, conn_new, conn_free, handle_err); if (rv) return rv; rv = ecp_vconn_handler_init(ctx, vconn_handler); diff --git a/ecp/test/server.c b/ecp/test/server.c index 003e3b3..0af41c6 100644 --- a/ecp/test/server.c +++ b/ecp/test/server.c @@ -43,7 +43,7 @@ int main(int argc, char *argv[]) { rv = ecp_init(&ctx); printf("ecp_init RV:%d\n", rv); - ecp_conn_handler_init(&handler, handle_msg, NULL, NULL, NULL); + ecp_conn_handler_init(&handler, NULL, NULL, handle_msg, NULL); ecp_ctx_set_handler(&ctx, CTYPE_TEST, &handler); rv = ecp_util_load_key(argv[2], &key_perma.public, &key_perma.private); diff --git a/ecp/test/vc_client.c b/ecp/test/vc_client.c index af4a3cd..d157081 100644 --- a/ecp/test/vc_client.c +++ b/ecp/test/vc_client.c @@ -54,7 +54,7 @@ int main(int argc, char *argv[]) { rv = ecp_init(&ctx, &vconn_handler, &vlink_handler); printf("ecp_init RV:%d\n", rv); - ecp_conn_handler_init(&handler, handle_msg, handle_open, NULL, NULL); + ecp_conn_handler_init(&handler, handle_open, NULL, handle_msg, NULL); ecp_ctx_set_handler(&ctx, CTYPE_TEST, &handler); rv = ecp_dhkey_gen(&key_perma); diff --git a/ecp/test/vc_inb.c b/ecp/test/vc_inb.c index 825d948..0c75c5d 100644 --- a/ecp/test/vc_inb.c +++ b/ecp/test/vc_inb.c @@ -87,10 +87,10 @@ int main(int argc, char *argv[]) { rv = ecp_init(&ctx, &vconn_handler, &vlink_handler); printf("ecp_init RV:%d\n", rv); - ecp_conn_handler_init(&dir_handler, handle_dir_msg, ecp_dir_handle_open, NULL, NULL); + ecp_conn_handler_init(&dir_handler, ecp_dir_handle_open, NULL, handle_dir_msg, NULL); ecp_ctx_set_handler(&ctx, ECP_CTYPE_DIR, &dir_handler); - ecp_conn_handler_init(&handler, handle_msg, handle_open, NULL, NULL); + ecp_conn_handler_init(&handler, handle_open, NULL, handle_msg, NULL); ecp_ctx_set_handler(&ctx, CTYPE_TEST, &handler); rv = ecp_util_load_key(argv[1], &key_perma.public, &key_perma.private); diff --git a/ecp/test/vc_outb.c b/ecp/test/vc_outb.c index cc772d5..4b14244 100644 --- a/ecp/test/vc_outb.c +++ b/ecp/test/vc_outb.c @@ -89,10 +89,10 @@ int main(int argc, char *argv[]) { rv = ecp_init(&ctx, &vconn_handler, &vlink_handler); printf("ecp_init RV:%d\n", rv); - ecp_conn_handler_init(&dir_handler, handle_dir_msg, ecp_dir_handle_open, NULL, NULL); + ecp_conn_handler_init(&dir_handler, ecp_dir_handle_open, NULL, handle_dir_msg, NULL); ecp_ctx_set_handler(&ctx, ECP_CTYPE_DIR, &dir_handler); - ecp_conn_handler_init(&handler, handle_msg, handle_open, NULL, NULL); + ecp_conn_handler_init(&handler, handle_open, NULL, handle_msg, NULL); ecp_ctx_set_handler(&ctx, CTYPE_TEST, &handler); rv = ecp_dhkey_gen(&key_perma); diff --git a/ecp/test/vc_server.c b/ecp/test/vc_server.c index daf35f8..b664a1c 100644 --- a/ecp/test/vc_server.c +++ b/ecp/test/vc_server.c @@ -53,7 +53,7 @@ int main(int argc, char *argv[]) { rv = ecp_init(&ctx, &vconn_handler, &vlink_handler); printf("ecp_init RV:%d\n", rv); - ecp_conn_handler_init(&handler, handle_msg, handle_open, NULL, NULL); + ecp_conn_handler_init(&handler, handle_open, NULL, handle_msg, NULL); ecp_ctx_set_handler(&ctx, CTYPE_TEST, &handler); rv = ecp_util_load_key(argv[1], &key_perma.public, &key_perma.private); -- cgit v1.2.3