diff options
43 files changed, 966 insertions, 2408 deletions
diff --git a/ecp/src/ecp/Makefile b/ecp/src/ecp/Makefile index c403829..af65ee0 100644 --- a/ecp/src/ecp/Makefile +++ b/ecp/src/ecp/Makefile @@ -1,11 +1,9 @@  include common.mk -obj = core.o timer.o -subdirs = crypto $(platform_dir) +obj += core.o timer.o +subdirs += crypto $(platform_dir)  build_dir = ../../build-$(platform) -include $(platform_dir)/platform_obj.mk -  %.o: %.c  	$(CC) $(CFLAGS) -c $< @@ -21,6 +19,7 @@ all: $(obj)  install: all  	mkdir -p $(build_dir) +	rm -f $(build_dir)/*.a  	install libecp.a $(build_dir)  	install crypto/libecpcr.a $(build_dir)  	install $(platform_dir)/libecptm.a $(build_dir) diff --git a/ecp/src/ecp/common.mk b/ecp/src/ecp/common.mk index 4c511f8..fbc935a 100644 --- a/ecp/src/ecp/common.mk +++ b/ecp/src/ecp/common.mk @@ -5,4 +5,45 @@ src_dir := $(abspath $(dir $(lastword $(MAKEFILE_LIST)))/..)  platform_dir = $(abspath $(src_dir)/platform/$(platform))  include $(platform_dir)/platform.mk +include $(platform_dir)/features.mk  CFLAGS += -I$(src_dir)/ecp -I$(platform_dir) + +ifeq ($(with_dirsrv),yes) +with_dir = yes +endif + +ifeq ($(with_pthread),yes) +CFLAGS += -DECP_WITH_PTHREAD=1 +endif + +ifeq ($(with_htable),yes) +CFLAGS += -DECP_WITH_HTABLE=1 +subdirs	+= htable +endif + +ifeq ($(with_vconn),yes) +CFLAGS += -DECP_WITH_VCONN=1 +subdirs	+= vconn +endif + +ifeq ($(with_rbuf),yes) +CFLAGS += -DECP_WITH_RBUF=1 +subdirs	+= ext +endif + +ifeq ($(with_msgq),yes) +CFLAGS += -DECP_WITH_MSGQ=1 +endif + +ifeq ($(with_dir),yes) +CFLAGS += -DECP_WITH_DIR=1 +subdirs	+= dir +endif + +ifeq ($(with_dirsrv),yes) +CFLAGS += -DECP_WITH_DIRSRV=1 +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 3cb236f..536efff 100644 --- a/ecp/src/ecp/core.c +++ b/ecp/src/ecp/core.c @@ -24,6 +24,15 @@  #include "ht.h"  #endif +int ecp_dhkey_gen(ECPDHKey *key) { +    int rv; + +    rv = ecp_ecdh_mkpair(&key->public, &key->private); +    if (rv) return rv; + +    key->valid = 1; +    return ECP_OK; +}  int ecp_ctx_init(ECPContext *ctx, ecp_err_handler_t handle_err, ecp_dir_handler_t handle_dir, ecp_conn_alloc_t conn_alloc, ecp_conn_free_t conn_free) {      int rv; @@ -71,13 +80,19 @@ int ecp_node_init(ECPNode *node, ecp_ecdh_public_t *public, void *addr) {      return ECP_OK;  } -int ecp_dhkey_gen(ECPDHKey *key) { +void ecp_node_set_pub(ECPNode *node, ecp_ecdh_public_t *public) { +    ECPDHPub *key = &node->key_perma; + +    memcpy(&key->public, public, sizeof(key->public)); +    key->valid = 1; +} + +int ecp_node_set_addr(ECPNode *node, void *addr) {      int rv; -    rv = ecp_ecdh_mkpair(&key->public, &key->private); -    if (rv) return rv; +    rv = ecp_tr_addr_set(&node->addr, addr); +    if (rv) return ECP_ERR_NET_ADDR; -    key->valid = 1;      return ECP_OK;  } @@ -127,31 +142,43 @@ static int conn_table_insert(ECPConnection *conn) {      int i, rv = ECP_OK;      if (ecp_conn_is_outb(conn)) { -        if (!ecp_conn_is_open(conn)) rv = ecp_ht_insert(sock->conn_table.addrs, &conn->remote.addr, conn); -        if (rv) return rv; +        if (ecp_conn_is_root(conn) && !ecp_conn_is_open(conn)) { +            rv = ecp_ht_insert(sock->conn_table.addrs, &conn->remote.addr, conn); +            if (rv) return rv; +        }          for (i=0; i<ECP_MAX_CONN_KEY; i++) { -            if (conn->key[i].valid) rv = ecp_ht_insert(sock->conn_table.keys, &conn->key[i].public, conn); -            if (rv) { -                int j; - -                for (j=0; j<i; j++) { -                    if (conn->key[j].valid) ecp_ht_remove(sock->conn_table.keys, &conn->key[j].public); +            if (conn->key[i].valid) { +                rv = ecp_ht_insert(sock->conn_table.keys, &conn->key[i].public, conn); +                if (rv) { +                    int j; + +                    for (j=0; j<i; j++) { +                        if (conn->key[j].valid) { +                            ecp_ht_remove(sock->conn_table.keys, &conn->key[j].public); +                        } +                    } +                    if (ecp_conn_is_root(conn) && !ecp_conn_is_open(conn)) { +                        ecp_ht_remove(sock->conn_table.addrs, &conn->remote.addr); +                    } +                    return rv;                  } -                if (!ecp_conn_is_open(conn)) ecp_ht_remove(sock->conn_table.addrs, &conn->remote.addr); -                return rv;              }          }      } else {          for (i=0; i<ECP_MAX_NODE_KEY; i++) { -            if (conn->rkey[i].valid) rv = ecp_ht_insert(sock->conn_table.keys, &conn->rkey[i].public, conn); -            if (rv) { -                int j; - -                for (j=0; j<i; j++) { -                    if (conn->rkey[j].valid) ecp_ht_remove(sock->conn_table.keys, &conn->rkey[j].public); +            if (conn->rkey[i].valid) { +                rv = ecp_ht_insert(sock->conn_table.keys, &conn->rkey[i].public, conn); +                if (rv) { +                    int j; + +                    for (j=0; j<i; j++) { +                        if (conn->rkey[j].valid) { +                            ecp_ht_remove(sock->conn_table.keys, &conn->rkey[j].public); +                        } +                    } +                    return rv;                  } -                return rv;              }          }      } @@ -171,12 +198,18 @@ static void conn_table_remove(ECPConnection *conn) {  #ifdef ECP_WITH_HTABLE      if (ecp_conn_is_outb(conn)) {          for (i=0; i<ECP_MAX_CONN_KEY; i++) { -            if (conn->key[i].valid) ecp_ht_remove(sock->conn_table.keys, &conn->key[i].public); +            if (conn->key[i].valid) { +                ecp_ht_remove(sock->conn_table.keys, &conn->key[i].public); +            } +        } +        if (ecp_conn_is_root(conn) && !ecp_conn_is_open(conn)) { +            ecp_ht_remove(sock->conn_table.addrs, &conn->remote.addr);          } -        if (!ecp_conn_is_open(conn)) ecp_ht_remove(sock->conn_table.addrs, &conn->remote.addr);      } else {          for (i=0; i<ECP_MAX_NODE_KEY; i++) { -            if (conn->rkey[i].valid) ecp_ht_remove(sock->conn_table.keys, &conn->rkey[i].public); +            if (conn->rkey[i].valid) { +                ecp_ht_remove(sock->conn_table.keys, &conn->rkey[i].public); +            }          }      }  #else @@ -202,7 +235,13 @@ static void conn_table_remove_addr(ECPConnection *conn) {  #endif  } -static ECPConnection *conn_table_search(ECPSocket *sock, unsigned char c_idx, ecp_ecdh_public_t *c_public, ecp_tr_addr_t *addr) { +static ECPConnection *conn_table_search(ECPSocket *sock, unsigned char c_idx, ecp_ecdh_public_t *c_public, ecp_tr_addr_t *addr, ECPConnection *parent) { +#ifdef ECP_WITH_VCONN +    if ((c_public == NULL) && parent) { +        return parent->next; +    } +#endif +  #ifdef ECP_WITH_HTABLE      if (c_public) {          return ecp_ht_search(sock->conn_table.keys, c_public); @@ -217,27 +256,33 @@ static ECPConnection *conn_table_search(ECPSocket *sock, unsigned char c_idx, ec      if (c_public) {          if (ecp_conn_is_outb(conn)) { -            if (c_idx >= ECP_MAX_CONN_KEY) return ECP_ERR_ECDH_IDX; +            if (c_idx >= ECP_MAX_CONN_KEY) return NULL;              for (i=0; i<sock->conn_table.size; i++) {                  conn = sock->conn_table.arr[i]; -                if (conn->key[c_idx].valid && ecp_ecdh_pub_eq(c_public, &conn->key[c_idx].public)) return conn; +                if (conn->key[c_idx].valid && ecp_ecdh_pub_eq(c_public, &conn->key[c_idx].public)) { +                    return conn; +                }              }          } else { -            unsigned char *_c_idx; +            unsigned char _c_idx; -            if (c_idx & ~ECP_ECDH_IDX_MASK) return ECP_ERR_ECDH_IDX; +            if (c_idx & ~ECP_ECDH_IDX_MASK) return NULL;              _c_idx = c_idx % ECP_MAX_NODE_KEY;              for (i=0; i<sock->conn_table.size; i++) {                  conn = sock->conn_table.arr[i]; -                if (conn->rkey[_c_idx].valid && ecp_ecdh_pub_eq(c_public, &conn->rkey[_c_idx].public)) return conn; +                if (conn->rkey[_c_idx].valid && ecp_ecdh_pub_eq(c_public, &conn->rkey[_c_idx].public)) { +                    return conn; +                }              }          }      } else if (addr) {          for (i=0; i<sock->conn_table.size; i++) {              conn = sock->conn_table.arr[i]; -            if (ecp_conn_is_outb(conn) && ecp_tr_addr_eq(&conn->remote.addr, addr)) return conn; +            if (ecp_conn_is_root(conn) && ecp_conn_is_outb(conn) && ecp_tr_addr_eq(&conn->remote.addr, addr)) { +                return conn; +            }          }      } @@ -399,51 +444,6 @@ int ecp_cookie_verify(ECPSocket *sock, unsigned char *cookie, unsigned char *pub      return ECP_ERR_COOKIE;  } -ECPConnection *ecp_sock_keys_search(ECPSocket *sock, ecp_ecdh_public_t *public) { -    ECPConnection *conn; - -#ifdef ECP_WITH_PTHREAD -    pthread_mutex_lock(&sock->conn_table.mutex); -#endif - -    conn = ecp_ht_search(sock->conn_table.keys, public); -    if (conn) ecp_conn_refcount_inc(conn); - -#ifdef ECP_WITH_PTHREAD -    pthread_mutex_unlock(&sock->conn_table.mutex); -#endif - -    return conn; -} - -int ecp_sock_keys_insert(ECPSocket *sock, ecp_ecdh_public_t *public, ECPConnection *conn) { -    int rv; - -#ifdef ECP_WITH_PTHREAD -    pthread_mutex_lock(&sock->conn_table.mutex); -#endif - -    rv = ecp_ht_insert(sock->conn_table.keys, public, conn); - -#ifdef ECP_WITH_PTHREAD -    pthread_mutex_unlock(&sock->conn_table.mutex); -#endif - -    return rv; -} - -void ecp_sock_keys_remove(ECPSocket *sock, ecp_ecdh_public_t *public) { -#ifdef ECP_WITH_PTHREAD -    pthread_mutex_lock(&sock->conn_table.mutex); -#endif - -    ecp_ht_remove(sock->conn_table.keys, public); - -#ifdef ECP_WITH_PTHREAD -    pthread_mutex_unlock(&sock->conn_table.mutex); -#endif -} -  static int conn_dhkey_new(ECPConnection *conn, unsigned char idx, ECPDHKey *key) {      ECPSocket *sock = conn->sock; @@ -498,7 +498,7 @@ static ECPDHKey *conn_dhkey_get(ECPConnection *conn, unsigned char idx) {  static ECPDHPub *conn_dhkey_get_remote(ECPConnection *conn, unsigned char idx) {      ECPDHPub *key = NULL; -    if (ecp_conn_is_outb(conn) && (idx == ECP_ECDH_IDX_PERMA)) { +    if (idx == ECP_ECDH_IDX_PERMA) {          key = &conn->remote.key_perma;      } else if ((idx & ECP_ECDH_IDX_MASK) == idx) {          key = &conn->rkey[idx % ECP_MAX_NODE_KEY]; @@ -668,7 +668,6 @@ void ecp_conn_init(ECPConnection *conn, ECPSocket *sock, unsigned char ctype) {  void ecp_conn_reinit(ECPConnection *conn) {      conn->flags = 0; -    conn->key_curr = ECP_ECDH_IDX_INV;      conn->key_next = ECP_ECDH_IDX_INV;      conn->rkey_curr = ECP_ECDH_IDX_INV;      memset(&conn->key, 0, sizeof(conn->key)); @@ -772,16 +771,22 @@ int ecp_conn_init_inb(ECPConnection *conn, ECPConnection *parent, unsigned char  int ecp_conn_init_outb(ECPConnection *conn, ECPNode *node) {      ECPDHKey key; +    unsigned char key_curr;      int rv; +    if (conn->key_curr == ECP_ECDH_IDX_INV) { +        key_curr = 0; +    } else { +        key_curr = (conn->key_curr + 1) % ECP_MAX_CONN_KEY; +    }      rv = ecp_dhkey_gen(&key);      if (rv) return rv; -    rv = conn_dhkey_new(conn, 0, &key); +    rv = conn_dhkey_new(conn, key_curr, &key);      if (rv) return rv;      if (node) conn->remote = *node; -    conn->key_curr = 0; +    conn->key_curr = key_curr;      return ECP_OK;  } @@ -913,7 +918,9 @@ void ecp_conn_refcount_inc(ECPConnection *conn) {  #ifdef ECP_WITH_PTHREAD      pthread_mutex_lock(&conn->mutex);  #endif +      conn->refcount++; +  #ifdef ECP_WITH_PTHREAD      pthread_mutex_unlock(&conn->mutex);  #endif @@ -926,9 +933,11 @@ void ecp_conn_refcount_dec(ECPConnection *conn) {  #ifdef ECP_WITH_PTHREAD      pthread_mutex_lock(&conn->mutex);  #endif +      conn->refcount--;      refcount = conn->refcount;      is_reg = ecp_conn_is_reg(conn); +  #ifdef ECP_WITH_PTHREAD      pthread_mutex_unlock(&conn->mutex);  #endif @@ -971,21 +980,44 @@ int ecp_conn_dhkey_new(ECPConnection *conn) {      return ECP_OK;  } -int ecp_conn_dhkey_get(ECPSocket *sock, unsigned char idx, ECPDHKey *key) { +int ecp_conn_dhkey_get(ECPConnection *conn, unsigned char idx, ECPDHKey *key) {      int rv = ECP_OK;  #ifdef ECP_WITH_PTHREAD -    pthread_mutex_lock(&sock->mutex); +    pthread_mutex_lock(&conn->mutex);  #endif      if (idx < ECP_MAX_CONN_KEY) { -        *key = sock->key[idx]; +        *key = conn->key[idx];      } else {          rv = ECP_ERR_ECDH_IDX;      }  #ifdef ECP_WITH_PTHREAD -    pthread_mutex_unlock(&sock->mutex); +    pthread_mutex_unlock(&conn->mutex); +#endif + +    if (!rv && !key->valid) rv = ECP_ERR_ECDH_IDX; +    return rv; +} + +int ecp_conn_dhkey_get_remote(ECPConnection *conn, unsigned char idx, ECPDHPub *key) { +    int rv = ECP_OK; + +#ifdef ECP_WITH_PTHREAD +    pthread_mutex_lock(&conn->mutex); +#endif + +    if (idx == ECP_ECDH_IDX_PERMA) { +        *key = conn->remote.key_perma; +    } else if ((idx & ECP_ECDH_IDX_MASK) == idx) { +        *key = conn->rkey[idx % ECP_MAX_NODE_KEY]; +    } else { +        rv = ECP_ERR_ECDH_IDX; +    } + +#ifdef ECP_WITH_PTHREAD +    pthread_mutex_unlock(&conn->mutex);  #endif      if (!rv && !key->valid) rv = ECP_ERR_ECDH_IDX; @@ -1213,16 +1245,16 @@ ssize_t ecp_send_init_req(ECPConnection *conn) {      return _send_ireq(conn, &ti);  } -ssize_t ecp_handle_init_req(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t *addr, unsigned char *public_buf, ecp_aead_key_t *shkey) { +ssize_t ecp_handle_init_req(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t *addr, unsigned char c_idx, unsigned char *public_buf, ecp_aead_key_t *shkey) {      ssize_t rv; -    rv = ecp_send_init_rep(sock, parent, addr, public_buf, shkey); +    rv = ecp_send_init_rep(sock, parent, addr, c_idx, public_buf, shkey);      if (rv < 0) return rv;      return 0;  } -ssize_t ecp_send_init_rep(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t *addr, unsigned char *public_buf, ecp_aead_key_t *shkey) { +ssize_t ecp_send_init_rep(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t *addr, unsigned char c_idx, unsigned char *public_buf, ecp_aead_key_t *shkey) {      ECPBuffer packet;      ECPBuffer payload;      ECPPktMeta pkt_meta; @@ -1251,12 +1283,12 @@ ssize_t ecp_send_init_rep(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t      ecp_sock_get_nonce(sock, &nonce);      pkt_meta.cookie = NULL; -    pkt_meta.public = NULL;      pkt_meta.shkey = shkey;      pkt_meta.nonce = &nonce;      pkt_meta.ntype = ECP_NTYPE_INB; -    pkt_meta.s_idx = 0xf; -    pkt_meta.c_idx = 0xf; +    pkt_meta.public = (ecp_ecdh_public_t *)public_buf; +    pkt_meta.s_idx = ECP_ECDH_IDX_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_COOKIE, ECP_MTYPE_INIT_REP), 0);      return rv; @@ -1324,15 +1356,15 @@ ssize_t ecp_write_open_req(ECPConnection *conn, ECPBuffer *payload) {      if (vbox) {          ECPSocket *sock = conn->sock;          ECPDHKey key; -        ECPDHPub *remote_key; +        ECPDHPub remote_key;          ecp_ecdh_public_t public;          ecp_aead_key_t vbox_shkey;          ecp_nonce_t vbox_nonce; -        remote_key = &conn->remote.key_perma; +        _rv = ECP_OK;          if (payload->size < ECP_SIZE_PLD(2+ECP_SIZE_VBOX, ECP_MTYPE_OPEN_REQ)) _rv = ECP_ERR_SIZE; -        if (!_rv && !remote_key->valid) _rv = ECP_ERR;          if (!_rv) _rv = ecp_sock_dhkey_get(sock, ECP_ECDH_IDX_PERMA, &key); +        if (!_rv) _rv = ecp_conn_dhkey_get_remote(conn, ECP_ECDH_IDX_PERMA, &remote_key);          if (!_rv) _rv = ecp_conn_dhkey_get_pub(conn, NULL, &public, 0);          if (_rv) return _rv; @@ -1343,9 +1375,10 @@ ssize_t ecp_write_open_req(ECPConnection *conn, ECPBuffer *payload) {          ecp_nonce2buf(msg, &vbox_nonce);          msg += ECP_SIZE_NONCE; -        ecp_ecdh_shkey(&vbox_shkey, &remote_key->public, &key.private); +        ecp_ecdh_shkey(&vbox_shkey, &remote_key.public, &key.private);          rv = ecp_aead_enc(msg, payload->size - (msg - payload->buffer), (unsigned char *)&public, ECP_SIZE_ECDH_PUB, &vbox_shkey, &vbox_nonce, ECP_NTYPE_VBOX);          if (rv < 0) return rv; +        rv += ECP_SIZE_ECDH_PUB + ECP_SIZE_NONCE;      }      return ECP_SIZE_PLD(2+rv, ECP_MTYPE_OPEN_REQ); @@ -1354,8 +1387,8 @@ ssize_t ecp_write_open_req(ECPConnection *conn, ECPBuffer *payload) {  ssize_t ecp_send_open_req(ECPConnection *conn, unsigned char *cookie) {      ECPBuffer packet;      ECPBuffer payload; -    unsigned char pkt_buf[ECP_SIZE_PKT_BUF_WCOOKIE(2+ECP_SIZE_VBOX, ECP_MTYPE_OPEN_REQ, conn)]; -    unsigned char pld_buf[ECP_SIZE_PLD_BUF(2+ECP_SIZE_VBOX, ECP_MTYPE_OPEN_REQ, conn)]; +    unsigned char pkt_buf[ECP_SIZE_PKT_BUF(2+ECP_SIZE_VBOX, ECP_MTYPE_OPEN_REQ, conn)+ECP_SIZE_COOKIE]; +    unsigned char pld_buf[ECP_SIZE_PLD_BUF(2+ECP_SIZE_VBOX, ECP_MTYPE_OPEN_REQ, conn)+ECP_SIZE_COOKIE];      ssize_t rv;      packet.buffer = pkt_buf; @@ -1500,12 +1533,14 @@ ssize_t ecp_handle_open(ECPConnection *conn, unsigned char mtype, unsigned char      if (ecp_conn_is_inb(conn)) {          ssize_t _rv; +        ecp_tr_release(bufs->packet, 1); +          _rv = ecp_send_open_rep(conn);          if (_rv < 0) {              ecp_conn_close(conn);              return _rv;          } -    } else { +    } else if (ecp_conn_is_root(conn)) {          ecp_conn_remove_addr(conn);      }      return rsize; @@ -1563,7 +1598,7 @@ ssize_t ecp_send_keyx_rep(ECPConnection *conn) {      return rv;  } -ssize_t ecp_handle_keyx(ECPConnection *conn, unsigned char mtype, unsigned char *msg, size_t msg_size) { +ssize_t ecp_handle_keyx(ECPConnection *conn, unsigned char mtype, unsigned char *msg, size_t msg_size, ECP2Buffer *bufs) {      ssize_t rv;      int _rv; @@ -1579,6 +1614,8 @@ ssize_t ecp_handle_keyx(ECPConnection *conn, unsigned char mtype, unsigned char      } else {          if (ecp_conn_is_outb(conn)) return ECP_ERR; +        ecp_tr_release(bufs->packet, 1); +          rv = ecp_send_keyx_rep(conn);          if (rv < 0) return rv;      } @@ -1595,7 +1632,7 @@ ssize_t ecp_msg_handle(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype,              case ECP_MTYPE_KEYX_REQ:              case ECP_MTYPE_KEYX_REP: -                return ecp_handle_keyx(conn, mtype, msg, msg_size); +                return ecp_handle_keyx(conn, mtype, msg, msg_size, bufs);              default:                  return ecp_ext_msg_handle(conn, seq, mtype, msg, msg_size, bufs); @@ -1696,7 +1733,7 @@ ssize_t ecp_unpack(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t *addr,      pthread_mutex_lock(&sock->conn_table.mutex);  #endif -    conn = conn_table_search(sock, c_idx, (ecp_ecdh_public_t *)public_buf, addr); +    conn = conn_table_search(sock, c_idx, (ecp_ecdh_public_t *)public_buf, addr, parent);  #ifdef ECP_WITH_PTHREAD      if (conn) pthread_mutex_lock(&conn->mutex); @@ -1827,7 +1864,13 @@ ssize_t ecp_unpack(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t *addr,          switch (mtype) {              case ECP_MTYPE_INIT_REQ: { -                rv = ecp_handle_init_req(sock, parent, addr, public_buf, &shkey); +                unsigned char _public_buf[ECP_SIZE_ECDH_PUB]; + +                /* we should release incoming packet before sending reply packet */ +                memcpy(_public_buf, public_buf, sizeof(_public_buf)); +                ecp_tr_release(bufs->packet, 1); + +                rv = ecp_handle_init_req(sock, parent, addr, c_idx, _public_buf, &shkey);                  if (rv < 0) return rv;                  rv += hdr_size; @@ -1873,6 +1916,9 @@ ssize_t ecp_unpack(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t *addr,          switch (mtype) {              case ECP_MTYPE_INIT_REP: { +                /* we should release incoming packet before sending reply packet */ +                ecp_tr_release(bufs->packet, 1); +                  rv = ecp_handle_init_rep(conn, msg, msg_size);                  if (rv < 0) goto unpack_err; @@ -2112,17 +2158,14 @@ static ssize_t _pack_conn(ECPConnection *conn, ECPBuffer *packet, unsigned char          nonce = conn->nonce_out;          conn->nonce_out++;      } -#ifdef ECP_WITH_VCONN -    if ((conn->parent == NULL) && addr) *addr = conn->remote.addr; -#else -    if (addr) *addr = conn->remote.addr; -#endif +    if (ecp_conn_is_root(conn) && addr) *addr = conn->remote.addr;  pack_conn_fin:  #ifdef ECP_WITH_PTHREAD      pthread_mutex_unlock(&conn->mutex);  #endif +      if (rv) return rv;      pkt_meta.cookie = cookie; @@ -2135,15 +2178,21 @@ pack_conn_fin:      return _pack(packet, &pkt_meta, payload, pld_size);  } -ssize_t ecp_pack(ECPConnection *parent, ECPBuffer *packet, ECPPktMeta *pkt_meta, ECPBuffer *payload, size_t pld_size, ecp_tr_addr_t *addr) { +ssize_t ecp_pack_irep(ECPConnection *parent, ECPBuffer *packet, ECPPktMeta *pkt_meta, ECPBuffer *payload, size_t pld_size, ecp_tr_addr_t *addr) {      ssize_t rv; +    if (parent == NULL) { +        pkt_meta->public = NULL; +        pkt_meta->c_idx = 0xf; +        pkt_meta->s_idx = 0xf; +    } +      rv = _pack(packet, pkt_meta, payload, pld_size);      if (rv < 0) return rv;  #ifdef ECP_WITH_VCONN      if (parent) { -        rv = ecp_vconn_pack_parent(parent, packet, payload, rv, addr); +        rv = ecp_vconn_pack_parent(parent, payload, packet, rv, addr);      }  #endif @@ -2158,7 +2207,7 @@ ssize_t ecp_pack_conn(ECPConnection *conn, ECPBuffer *packet, unsigned char s_id  #ifdef ECP_WITH_VCONN      if (conn->parent) { -        rv = ecp_vconn_pack_parent(conn->parent, packet, payload, rv, addr); +        rv = ecp_vconn_pack_parent(conn->parent, payload, packet, rv, addr);      }  #endif @@ -2291,7 +2340,7 @@ ssize_t ecp_pld_send_irep(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t      ecp_tr_addr_t _addr;      ssize_t rv; -    rv = ecp_pack(parent, packet, pkt_meta, payload, pld_size, addr ? NULL : &_addr); +    rv = ecp_pack_irep(parent, packet, pkt_meta, payload, pld_size, addr ? NULL : &_addr);      if (rv < 0) return rv;      rv = ecp_pkt_send(sock, packet, rv, flags, NULL, addr ? addr : &_addr); diff --git a/ecp/src/ecp/core.h b/ecp/src/ecp/core.h index 6fb165f..85fc453 100644 --- a/ecp/src/ecp/core.h +++ b/ecp/src/ecp/core.h @@ -73,17 +73,20 @@  #else  #define ECP_SIZE_PLD_BUF(X,T,C)         (ECP_SIZE_PLD(X,T))  #endif - -#define ECP_SIZE_PKT_BUF(X,T,C)         (ECP_SIZE_PLD_BUF(X,T,C)+ECP_SIZE_PKT_HDR+ECP_SIZE_AEAD_TAG) -#define ECP_SIZE_PKT_BUF_WCOOKIE(X,T,C) (ECP_SIZE_PKT_BUF(X,T,C)+ECP_SIZE_COOKIE) +#define ECP_SIZE_PKT_BUF(X,T,C)         (ECP_SIZE_PKT_HDR+ECP_SIZE_PLD_BUF(X,T,C)+ECP_SIZE_AEAD_TAG)  #ifdef ECP_WITH_VCONN  #define ECP_SIZE_PLD_BUF_IREP(X,T,P)    (ECP_SIZE_PLD(X,T)+((P) ? ((P)->pcount+1)*(ECP_SIZE_PKT_HDR+ECP_SIZE_MTYPE+ECP_SIZE_AEAD_TAG) : 0))  #else  #define ECP_SIZE_PLD_BUF_IREP(X,T,P)    (ECP_SIZE_PLD(X,T))  #endif +#define ECP_SIZE_PKT_BUF_IREP(X,T,P)    (ECP_SIZE_PKT_HDR+ECP_SIZE_PLD_BUF_IREP(X,T,P)+ECP_SIZE_AEAD_TAG) -#define ECP_SIZE_PKT_BUF_IREP(X,T,P)    (ECP_SIZE_PLD_BUF_IREP(X,T,P)+ECP_SIZE_PKT_HDR-ECP_SIZE_ECDH_PUB+ECP_SIZE_AEAD_TAG) +#ifdef ECP_WITH_VCONN +#define ecp_conn_is_root(C)         ((C)->parent == NULL) +#else +#define ecp_conn_is_root(C)         1 +#endif  #define ECP_SEND_TRIES              3  #define ECP_SEND_TIMEOUT            500 @@ -302,11 +305,15 @@ typedef struct ECPConnection {  #endif  } ECPConnection; +int ecp_dhkey_gen(ECPDHKey *key); +  int ecp_init(ECPContext *ctx);  int ecp_ctx_init(ECPContext *ctx, ecp_err_handler_t handle_err, ecp_dir_handler_t handle_dir, ecp_conn_alloc_t conn_alloc, ecp_conn_free_t conn_free);  int ecp_ctx_set_handler(ECPContext *ctx, ECPConnHandler *handler, unsigned char ctype); +  int ecp_node_init(ECPNode *node, ecp_ecdh_public_t *public, void *addr); -int ecp_dhkey_gen(ECPDHKey *key); +void ecp_node_set_pub(ECPNode *node, ecp_ecdh_public_t *public); +int ecp_node_set_addr(ECPNode *node, void *addr);  int ecp_sock_init(ECPSocket *sock, ECPContext *ctx, ECPDHKey *key);  int ecp_sock_create(ECPSocket *sock, ECPContext *ctx, ECPDHKey *key); @@ -321,10 +328,6 @@ void ecp_sock_get_nonce(ECPSocket *sock, ecp_nonce_t *nonce);  int ecp_cookie_gen(ECPSocket *sock, unsigned char *cookie, unsigned char *public_buf);  int ecp_cookie_verify(ECPSocket *sock, unsigned char *cookie, unsigned char *public_buf); -ECPConnection *ecp_sock_keys_search(ECPSocket *sock, ecp_ecdh_public_t *public); -int ecp_sock_keys_insert(ECPSocket *sock, ecp_ecdh_public_t *public, ECPConnection *conn); -void ecp_sock_keys_remove(ECPSocket *sock, ecp_ecdh_public_t *public); -  int ecp_conn_alloc(ECPSocket *sock, unsigned char ctype, ECPConnection **_conn);  void ecp_conn_free(ECPConnection *conn); @@ -354,7 +357,8 @@ void ecp_conn_refcount_inc(ECPConnection *conn);  void ecp_conn_refcount_dec(ECPConnection *conn);  int ecp_conn_dhkey_new(ECPConnection *conn); -int ecp_conn_dhkey_get(ECPSocket *sock, unsigned char idx, ECPDHKey *key); +int ecp_conn_dhkey_get(ECPConnection *conn, unsigned char idx, ECPDHKey *key); +int ecp_conn_dhkey_get_remote(ECPConnection *conn, unsigned char idx, ECPDHPub *key);  int ecp_conn_dhkey_get_pub(ECPConnection *conn, unsigned char *idx, ecp_ecdh_public_t *public, int will_send);  int ecp_conn_dhkey_set_pub(ECPConnection *conn, unsigned char idx, ecp_ecdh_public_t *public);  void ecp_conn_dhkey_set_curr(ECPConnection *conn); @@ -367,8 +371,8 @@ ecp_dir_handler_t ecp_get_dir_handler(ECPConnection *conn);  void ecp_err_handle(ECPConnection *conn, unsigned char mtype, int err);  ssize_t ecp_send_init_req(ECPConnection *conn); -ssize_t ecp_handle_init_req(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t *addr, unsigned char *public_buf, ecp_aead_key_t *shkey); -ssize_t ecp_send_init_rep(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t *addr, unsigned char *public_buf, ecp_aead_key_t *shkey); +ssize_t ecp_handle_init_req(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t *addr, unsigned char c_idx, unsigned char *public_buf, ecp_aead_key_t *shkey); +ssize_t ecp_send_init_rep(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t *addr, unsigned char c_idx, unsigned char *public_buf, ecp_aead_key_t *shkey);  ssize_t ecp_handle_init_rep(ECPConnection *conn, unsigned char *msg, size_t msg_size);  ssize_t ecp_write_open_req(ECPConnection *conn, ECPBuffer *payload);  ssize_t ecp_send_open_req(ECPConnection *conn, unsigned char *cookie); @@ -378,7 +382,7 @@ ssize_t ecp_handle_open(ECPConnection *conn, unsigned char mtype, unsigned char  ssize_t ecp_send_keyx_req(ECPConnection *conn);  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); +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); @@ -392,7 +396,7 @@ ssize_t ecp_pkt_send(ECPSocket *sock, ECPBuffer *packet, size_t pkt_size, unsign  void ecp_nonce2buf(unsigned char *b, ecp_nonce_t *n);  void ecp_buf2nonce(ecp_nonce_t *n, unsigned char *b);  int ecp_pkt_get_seq(unsigned char *pkt, size_t pkt_size, ecp_seq_t *s); -ssize_t ecp_pack(ECPConnection *parent, ECPBuffer *packet, ECPPktMeta *pkt_meta, ECPBuffer *payload, size_t pld_size, ecp_tr_addr_t *addr); +ssize_t ecp_pack_irep(ECPConnection *parent, ECPBuffer *packet, ECPPktMeta *pkt_meta, ECPBuffer *payload, size_t pld_size, ecp_tr_addr_t *addr);  ssize_t ecp_pack_conn(ECPConnection *conn, ECPBuffer *packet, unsigned char s_idx, unsigned char c_idx, unsigned char *cookie, ecp_nonce_t *nonce, ECPBuffer *payload, size_t pld_size, ecp_tr_addr_t *addr);  int ecp_pld_get_type(unsigned char *pld, size_t pld_size, unsigned char *mtype); diff --git a/ecp/src/ecp/dir/Makefile b/ecp/src/ecp/dir/Makefile index 9da375b..9283144 100644 --- a/ecp/src/ecp/dir/Makefile +++ b/ecp/src/ecp/dir/Makefile @@ -1,6 +1,10 @@  include ../common.mk -obj = dir.o dir_srv.o +obj = dir.o + +ifeq ($(with_dirsrv),yes) +obj += dir_srv.o +endif  %.o: %.c diff --git a/ecp/src/ecp/ext.h b/ecp/src/ecp/ext.h index 811b6d1..995cf15 100644 --- a/ecp/src/ecp/ext.h +++ b/ecp/src/ecp/ext.h @@ -1,3 +1,5 @@ +#ifdef ECP_WITH_RBUF +  int ecp_ext_err_handle(ECPConnection *conn, unsigned char mtype, int err);  int ecp_ext_conn_open(ECPConnection *conn);  void ecp_ext_conn_destroy(ECPConnection *conn); @@ -6,3 +8,16 @@ ssize_t ecp_ext_pld_store(ECPConnection *conn, ecp_seq_t seq, unsigned char *pay  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);  ssize_t ecp_ext_msg_send(ECPConnection *conn, unsigned char mtype, unsigned char *msg, size_t msg_size, ECPBuffer *packet, ECPBuffer *payload); + +#else + +#define ecp_ext_err_handle(c,t,e)                   (ECP_PASS) +#define ecp_ext_conn_open(c)                        (ECP_OK) +#define ecp_ext_conn_destroy(c)                     ; +#define ecp_ext_msg_handle(c,s,t,m,sz,b)            (0) +#define ecp_ext_pld_store(c,s,p,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) +#define ecp_ext_msg_send(c,t,m,sz,p1,p2)            (0) + +#endif
\ No newline at end of file diff --git a/ecp/src/ecp/tr.h b/ecp/src/ecp/tr.h index 5037b07..263577c 100644 --- a/ecp/src/ecp/tr.h +++ b/ecp/src/ecp/tr.h @@ -4,8 +4,6 @@ int ecp_tr_addr_eq(ecp_tr_addr_t *addr1, ecp_tr_addr_t *addr2);  int ecp_tr_addr_set(ecp_tr_addr_t *addr, void *addr_s);  int ecp_tr_open(ECPSocket *sock, void *addr_s);  void ecp_tr_close(ECPSocket *sock); -ssize_t ecp_tr_send(ECPSocket *sock, ECPBuffer *packet, size_t msg_size, ecp_tr_addr_t *addr, unsigned char flags); +ssize_t ecp_tr_send(ECPSocket *sock, ECPBuffer *packet, size_t pkt_size, ecp_tr_addr_t *addr, unsigned char flags);  ssize_t ecp_tr_recv(ECPSocket *sock, ECPBuffer *packet, ecp_tr_addr_t *addr, int timeout);  void ecp_tr_release(ECPBuffer *packet, unsigned char more); -void ecp_tr_flag_set(unsigned char flags); -void ecp_tr_flag_clear(unsigned char flags); diff --git a/ecp/src/ecp/vconn/vconn.c b/ecp/src/ecp/vconn/vconn.c index bbed3c3..23e0466 100644 --- a/ecp/src/ecp/vconn/vconn.c +++ b/ecp/src/ecp/vconn/vconn.c @@ -3,79 +3,35 @@  #include <core.h> -#include "vconn.h" - -ssize_t ecp_vconn_send_open_req(ECPConnection *conn, unsigned char *cookie) { -    if (conn->type == ECP_CTYPE_VCONN) { -        ECPDHPub *key; -        ECPBuffer packet; -        ECPBuffer payload; -        unsigned char pkt_buf[ECP_SIZE_PKT_BUF_WCOOKIE(2+ECP_SIZE_VBOX, ECP_MTYPE_OPEN_REQ, conn)+ECP_SIZE_PLD(ECP_SIZE_ECDH_PUB, ECP_MTYPE_NEXT)]; -        unsigned char pld_buf[ECP_SIZE_PLD_BUF(2+ECP_SIZE_VBOX, ECP_MTYPE_OPEN_REQ, conn)+ECP_SIZE_PLD(ECP_SIZE_ECDH_PUB, ECP_MTYPE_NEXT)]; -        unsigned char *msg; -        unsigned char *_pld_buf; -        size_t _pld_size; -        ssize_t rv; - -        if (conn->next == NULL) return ECP_ERR; - -        key = &conn->next->remote.key_perma; -        if (!key->valid) return ECP_ERR; - -        packet.buffer = pkt_buf; -        packet.size = sizeof(pkt_buf); -        payload.buffer = pld_buf; -        payload.size = sizeof(pld_buf); - -        rv = ecp_write_open_req(conn, &payload); -        if (rv < 0) return rv; +#ifdef ECP_WITH_HTABLE +#include <ht.h> +#endif -        _pld_buf = payload.buffer + rv; -        _pld_size = payload.size - rv; +#include "vconn.h" -        ecp_pld_set_type(_pld_buf, _pld_size, ECP_MTYPE_NEXT); -        msg = ecp_pld_get_msg(_pld_buf, _pld_size); -        memcpy(msg, &key->public, ECP_SIZE_ECDH_PUB); -        rv = ecp_pld_send_wcookie(conn, &packet, &payload, rv+ECP_SIZE_PLD(ECP_SIZE_ECDH_PUB, ECP_MTYPE_NEXT), 0, cookie); -        return rv; -    } else { -        return ecp_send_open_req(conn, cookie); -    } -} +#ifdef ECP_WITH_HTABLE -static void insert_key_next(ECPConnection *conn, unsigned char idx, ecp_ecdh_public_t *public) { +static int insert_key_next(ECPConnection *conn, unsigned char c_idx, ecp_ecdh_public_t *public) { +    ECPSocket *sock = conn->sock;      ECPVConn *_conn = (ECPVConn *)conn; -    ecp_ecdh_public_t to_remove; -    unsigned char c_idx; -    int do_insert = 0, do_remove = 0; - -    if (idx == ECP_ECDH_IDX_INV) return; +    ECPDHPub *key = NULL; +    int rv = ECP_OK; -    c_idx = (idx & 0x0F); -    if (c_idx & ~ECP_ECDH_IDX_MASK) return; +    if (c_idx & ~ECP_ECDH_IDX_MASK) return ECP_ERR_ECDH_IDX;  #ifdef ECP_WITH_PTHREAD      pthread_mutex_lock(&conn->mutex);  #endif      if (c_idx != _conn->key_next_curr) { -        ECPDHPub *key;          unsigned char _c_idx;          _c_idx = c_idx % ECP_MAX_NODE_KEY;          key = &_conn->key_next[_c_idx]; -        if (!key->valid || (memcmp(public, &key->public, sizeof(key->public)) != 0)) { -            if (key->valid) { -                memcpy(&to_remove, &key->public, sizeof(key->public)); -                do_remove = 1; -            } -            memcpy(&key->public, public, sizeof(key->public)); -            key->valid = 1; - -            _conn->key_next_curr = c_idx; -            do_insert = 1; +        if (key->valid && (memcmp(public, &key->public, sizeof(key->public)) == 0)) { +            key = NULL;          }      } @@ -83,164 +39,162 @@ static void insert_key_next(ECPConnection *conn, unsigned char idx, ecp_ecdh_pub      pthread_mutex_unlock(&conn->mutex);  #endif -    if (do_remove) ecp_sock_keys_remove(conn->sock, &to_remove); -    if (do_insert) ecp_sock_keys_insert(conn->sock, public, conn); +    if (key) { +#ifdef ECP_WITH_PTHREAD +        pthread_mutex_lock(&sock->conn_table.mutex); +        pthread_mutex_lock(&conn->mutex); +#endif + +        if (key->valid) { +            ecp_ht_remove(sock->conn_table.keys, &key->public); +            key->valid = 0; +        } +        memcpy(&key->public, public, sizeof(key->public)); +        rv = ecp_ht_insert(sock->conn_table.keys, &key->public, conn); +        if (!rv) { +            key->valid = 1; +            _conn->key_next_curr = c_idx; +        } + +#ifdef ECP_WITH_PTHREAD +        pthread_mutex_unlock(&conn->mutex); +        pthread_mutex_unlock(&sock->conn_table.mutex); +#endif +    } + +    return rv;  }  static ssize_t handle_next(ECPConnection *conn, unsigned char *msg, size_t msg_size, ECP2Buffer *b) { -    ECPConnection *conn_next; +    ECPSocket *sock = conn->sock;      if (msg_size < ECP_SIZE_ECDH_PUB) return ECP_ERR_SIZE; +    if (ecp_conn_is_outb(conn)) return ECP_ERR; -    conn_next = ecp_sock_keys_search(conn->sock, (ecp_ecdh_public_t *)msg); -    if (conn_next == NULL) return ECP_ERR; - -    conn->next = conn_next; - -    return ECP_SIZE_ECDH_PUB; -} +#ifdef ECP_WITH_PTHREAD +    pthread_mutex_lock(&sock->conn_table.mutex); +#endif -static ssize_t handle_exec(ECPConnection *conn, unsigned char *msg, size_t msg_size, ECP2Buffer *b) { -    ECPBuffer *packet; +    conn->next = ecp_ht_search(sock->conn_table.keys, (ecp_ecdh_public_t *)msg); +    if (conn->next) ecp_conn_refcount_inc(conn->next); -    if (b == NULL) return ECP_ERR; +#ifdef ECP_WITH_PTHREAD +    pthread_mutex_unlock(&sock->conn_table.mutex); +#endif -    packet = b->packet; -    if (packet->size < msg_size) return ECP_ERR_SIZE; +    if (conn->next == NULL) return ECP_ERR; -    memcpy(packet->buffer, msg, msg_size); -    return ecp_pkt_handle(conn->sock, conn, NULL, b, msg_size); +    return ECP_SIZE_ECDH_PUB;  }  static ssize_t handle_relay(ECPConnection *conn, unsigned char *msg, size_t msg_size, ECP2Buffer *b) { -    ECPBuffer payload; +    ECPSocket *sock = conn->sock;      ECPConnection *conn_next; +    ECPBuffer payload; +    unsigned char idx, _idx; +    unsigned char s_idx, c_idx; +    size_t _msg_size = msg_size;      ssize_t rv; +    int _rv;      if (msg_size < ECP_MIN_PKT) return ECP_ERR_SIZE; -    if (conn->type == ECP_CTYPE_VCONN) { -        if (ecp_conn_is_outb(conn)) return ECP_ERR; -        conn_next = conn->next; -        insert_key_next(conn, msg[ECP_SIZE_PROTO], (ecp_ecdh_public_t *)(msg+ECP_SIZE_PROTO+1)); -    } else if (conn->type == ECP_CTYPE_VLINK) { -        conn_next = ecp_sock_keys_search(conn->sock, (ecp_ecdh_public_t *)(msg+ECP_SIZE_PROTO+1)); -    } +    idx = msg[ECP_SIZE_PROTO]; +    if (idx == ECP_ECDH_IDX_INV) return ECP_ERR_ECDH_IDX; -    if (conn_next == NULL) return ECP_ERR; +    switch (conn->type) { +        /* forward message */ +        case ECP_CTYPE_VCONN: { +            if (ecp_conn_is_outb(conn)) return ECP_ERR; -    payload.buffer = msg - ECP_SIZE_MTYPE; -    payload.size = b->payload->size - (payload.buffer - b->payload->buffer); - -    ecp_pld_set_type(payload.buffer, payload.size, ECP_MTYPE_EXEC); -    rv = ecp_pld_send(conn_next, b->packet, &payload, ECP_SIZE_MTYPE + msg_size, ECP_SEND_FLAG_REPLY); - -    if (conn->type == ECP_CTYPE_VLINK) { -        ecp_conn_refcount_dec(conn_next); -    } - -    if (rv < 0) return rv; -    return msg_size; -} - -ssize_t ecp_vconn_pack_parent(ECPConnection *conn, ECPBuffer *packet, ECPBuffer *payload, size_t pkt_size, ecp_tr_addr_t *addr) { -    unsigned char *msg; -    size_t hdr_size; -    int rv; +            conn_next = conn->next; +            if (conn_next) { +                _idx = (idx & 0x0F); -    rv = ecp_pld_set_type(payload->buffer, payload->size, ECP_MTYPE_RELAY); -    if (rv) return rv; +                _rv = insert_key_next(conn, _idx, (ecp_ecdh_public_t *)(msg+ECP_SIZE_PROTO+1)); +                if (_rv) return _rv; +            } -    msg = ecp_pld_get_msg(payload->buffer, payload->size); -    if (msg == NULL) return ECP_ERR; +            break; +        } -    hdr_size = msg - payload->buffer; -    if (payload->size < pkt_size+hdr_size) return ECP_ERR_SIZE; +        /* back message */ +        case ECP_CTYPE_VLINK: { +#ifdef ECP_WITH_PTHREAD +            pthread_mutex_lock(&sock->conn_table.mutex); +#endif -    memcpy(msg, packet->buffer, pkt_size); -    return ecp_pack_conn(conn, packet, ECP_ECDH_IDX_INV, ECP_ECDH_IDX_INV, NULL, NULL, payload, pkt_size+hdr_size, addr); -} +            conn_next = ecp_ht_search(sock->conn_table.keys, (ecp_ecdh_public_t *)(msg+ECP_SIZE_PROTO+1)); +            if (conn_next) ecp_conn_refcount_inc(conn_next); -int ecp_vconn_handle_open(ECPConnection *conn, ECP2Buffer *b) { -    if (conn->type == ECP_CTYPE_VCONN) { -        if (ecp_conn_is_outb(conn)) { -            if (conn->next) { -                int rv; +#ifdef ECP_WITH_PTHREAD +            pthread_mutex_unlock(&sock->conn_table.mutex); +#endif -                rv = ecp_conn_open(conn->next, NULL); -                if (rv) ecp_err_handle(conn->next, ECP_MTYPE_INIT_REQ, rv); +            _idx = (idx & 0xF0) >> 4; +            if (conn_next && (_idx == ECP_ECDH_IDX_PERMA)) { +                /* this is init reply */ +                msg[ECP_SIZE_PROTO] = ECP_ECDH_IDX_INV; +                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;              } -        } -    } else if (conn->type == ECP_CTYPE_VLINK) { -        ECPDHPub *key = &conn->remote.key_perma; -        if (key->valid) { -            int rv; - -            rv = ecp_sock_keys_insert(conn->sock, &key->public, conn); -            if (rv) return rv; +            break;          } -    } -    return ECP_OK; -} - -void ecp_vconn_handle_close(ECPConnection *conn) { -    if (conn->type == ECP_CTYPE_VCONN) { -        if (ecp_conn_is_inb(conn)) { -            ecp_vconn_destroy_inb((ECPVConn *)conn); -        } -    } else if (conn->type == ECP_CTYPE_VLINK) { -        ECPDHPub *key = &conn->remote.key_perma; +        default: +            return ECP_ERR_CTYPE; -        if (key->valid) { -            ecp_sock_keys_remove(conn->sock, &key->public); -        }      } -} -ssize_t ecp_vconn_handle_msg(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, size_t msg_size, ECP2Buffer *b) { -    switch (mtype) { -        case ECP_MTYPE_NEXT: -            return handle_next(conn, msg, msg_size, b); +    if (conn_next == NULL) return ECP_ERR; -        case ECP_MTYPE_EXEC: -            return handle_exec(conn, msg, msg_size, b); +    payload.buffer = msg - ECP_SIZE_MTYPE; +    payload.size = b->payload->size - (payload.buffer - b->payload->buffer); -        case ECP_MTYPE_RELAY: -            return handle_relay(conn, msg, msg_size, b); +    ecp_pld_set_type(payload.buffer, payload.size, ECP_MTYPE_EXEC); +    rv = ecp_pld_send(conn_next, b->packet, &payload, ECP_SIZE_MTYPE + _msg_size, ECP_SEND_FLAG_REPLY); -        default: -            return ECP_ERR_MTYPE; +    if (conn->type == ECP_CTYPE_VLINK) { +        ecp_conn_refcount_dec(conn_next);      } + +    if (rv < 0) return rv; +    return msg_size;  } -int ecp_vconn_create_inb(ECPVConn *conn, ECPSocket *sock) { -    ECPConnection *_conn = &conn->b; -    int rv; +#endif  /* ECP_WITH_HTABLE */ -    rv = ecp_conn_create_inb(_conn, sock, ECP_CTYPE_VCONN); -    if (rv) return rv; +static ssize_t handle_exec(ECPConnection *conn, unsigned char *msg, size_t msg_size, ECP2Buffer *b) { +    ECPBuffer *packet; -    memset(conn->key_next, 0, sizeof(conn->key_next)); -    conn->key_next_curr = ECP_ECDH_IDX_INV; +    if (b == NULL) return ECP_ERR; -    return ECP_OK; +    packet = b->packet; +    if (packet->size < msg_size) return ECP_ERR_SIZE; + +    memcpy(packet->buffer, msg, msg_size); +    return ecp_pkt_handle(conn->sock, conn, NULL, b, msg_size);  } -void ecp_vconn_destroy_inb(ECPVConn *conn) { -    ECPConnection *_conn = &conn->b; -    int i; +ssize_t ecp_vconn_pack_parent(ECPConnection *conn, ECPBuffer *payload, ECPBuffer *packet, size_t pkt_size, ecp_tr_addr_t *addr) { +    unsigned char *msg; +    size_t hdr_size; +    ssize_t rv; +    int _rv; -    for (i=0; i<ECP_MAX_NODE_KEY; i++) { -        ECPDHPub *key; +    _rv = ecp_pld_set_type(payload->buffer, payload->size, ECP_MTYPE_RELAY); +    if (_rv) return _rv; -        key = &conn->key_next[i]; -        if (key->valid) { -            ecp_sock_keys_remove(_conn->sock, &key->public); -        } -    } -    if (_conn->next) ecp_conn_refcount_dec(_conn->next); +    msg = ecp_pld_get_msg(payload->buffer, payload->size); +    if (msg == NULL) return ECP_ERR; + +    hdr_size = msg - payload->buffer; +    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, NULL, NULL, payload, pkt_size+hdr_size, addr); +    return rv;  }  static int vconn_create(ECPConnection vconn[], ecp_ecdh_public_t keys[], size_t vconn_size, ECPSocket *sock) { @@ -249,7 +203,7 @@ static int vconn_create(ECPConnection vconn[], ecp_ecdh_public_t keys[], size_t      int rv;      key.valid = 1; -    for (i=0; i<vconn_size; j++) { +    for (i=0; i<vconn_size; i++) {          rv = ecp_conn_create(&vconn[i], sock, ECP_CTYPE_VCONN);          if (rv) {              for (j=0; j<i; j++) { @@ -272,7 +226,7 @@ int ecp_vconn_create(ECPConnection vconn[], ecp_ecdh_public_t keys[], size_t vco      _conn = conn;      pcount = vconn_size; -    if (pcount >= ECP_MAX_PARENT) return ECP_ERR_MAX_PARENT; +    if (pcount > ECP_MAX_PARENT) return ECP_ERR_MAX_PARENT;      rv = vconn_create(vconn, keys, vconn_size, conn->sock);      if (rv) return rv; @@ -289,6 +243,50 @@ int ecp_vconn_create(ECPConnection vconn[], ecp_ecdh_public_t keys[], size_t vco      return ECP_OK;  } +#ifdef ECP_WITH_HTABLE + +int ecp_vconn_create_inb(ECPVConn *conn, ECPSocket *sock) { +    ECPConnection *_conn = &conn->b; +    int rv; + +    rv = ecp_conn_create_inb(_conn, sock, ECP_CTYPE_VCONN); +    if (rv) return rv; + +    memset(conn->key_next, 0, sizeof(conn->key_next)); +    conn->key_next_curr = ECP_ECDH_IDX_INV; + +    return ECP_OK; +} + +void ecp_vconn_destroy(ECPConnection *conn) { +    ECPSocket *sock = conn->sock; +    ECPVConn *_conn = (ECPVConn *)conn; +    int i; + +    if (ecp_conn_is_outb(conn)) return; + +#ifdef ECP_WITH_PTHREAD +    pthread_mutex_lock(&sock->conn_table.mutex); +#endif + +    for (i=0; i<ECP_MAX_NODE_KEY; i++) { +        ECPDHPub *key; + +        key = &_conn->key_next[i]; +        if (key->valid) { +            ecp_ht_remove(sock->conn_table.keys, &key->public); +        } +    } + +#ifdef ECP_WITH_PTHREAD +    pthread_mutex_unlock(&sock->conn_table.mutex); +#endif + +    if (conn->next) ecp_conn_refcount_dec(conn->next); +} + +#endif  /* ECP_WITH_HTABLE */ +  int ecp_vconn_open(ECPConnection *conn, ECPNode *node) {      ECPConnection *vconn0;      int rv; @@ -307,64 +305,195 @@ int ecp_vconn_open(ECPConnection *conn, ECPNode *node) {      return rv;  } -/* -int ecp_vconn_create_parent(ECPConnection *conn, ECPNode *conn_node, ECPVConnOut vconn[], ECPNode vconn_node[], int size) { +static int vconn_handle_open(ECPConnection *conn) { +    int rv = ECP_OK; + +    if (ecp_conn_is_outb(conn) && conn->next) { +        rv = ecp_conn_open(conn->next, NULL); +        if (rv) ecp_err_handle(conn->next, ECP_MTYPE_INIT_REQ, rv); +    } + +    return rv;; +} + +int ecp_vlink_create(ECPConnection *conn, ECPSocket *sock) { +    int rv; + +    rv = ecp_conn_create(conn, sock, ECP_CTYPE_VLINK); +    if (!rv) ecp_conn_set_flags(conn, ECP_CONN_FLAG_VBOX); +    return rv; +} + +#ifdef ECP_WITH_HTABLE + +int ecp_vlink_create_inb(ECPConnection *conn, ECPSocket *sock) { +    int rv; + +    rv = ecp_conn_create_inb(conn, sock, ECP_CTYPE_VLINK); +    if (!rv) ecp_conn_set_flags(conn, ECP_CONN_FLAG_VBOX); +    return rv; +} + +void ecp_vlink_destroy(ECPConnection *conn) {      ECPSocket *sock = conn->sock; -    int i, j, rv; +    ECPDHPub *key = &conn->remote.key_perma; -    conn->parent = (ECPConnection *)&vconn[size-1]; -    conn->pcount = size; -    for (i=0; i<size; i++) { -        rv = ecp_conn_init((ECPConnection *)&vconn[i], sock, ECP_CTYPE_VCONN); -        if (!rv) rv = ecp_conn_create_outb((ECPConnection *)&vconn[i], &vconn_node[i]); -        if (!rv) { -            rv = ecp_conn_insert((ECPConnection *)&vconn[i]); -            if (rv) ecp_conn_destroy((ECPConnection *)&vconn[i]); -        } -        if (rv) { -            for (j=0; j<i; j++) ecp_conn_close((ECPConnection *)&vconn[j]); -            return rv; -        } +    if (key->valid) { +#ifdef ECP_WITH_PTHREAD +        pthread_mutex_lock(&sock->conn_table.mutex); +#endif -        if (i == 0) { -            vconn[i].b.parent = NULL; -        } else { -            vconn[i].b.parent = (ECPConnection *)&vconn[i-1]; -        } -        vconn[i].b.pcount = i; +        ecp_ht_remove(sock->conn_table.keys, &key->public); + +#ifdef ECP_WITH_PTHREAD +        pthread_mutex_unlock(&sock->conn_table.mutex); +#endif +    } +} + +static int vlink_handle_open(ECPConnection *conn) { +    ECPSocket *sock = conn->sock; +    ECPDHPub *key; +    int rv = ECP_OK; + +    key = &conn->remote.key_perma; -        if (i == size - 1) { -            vconn[i].next = conn; -        } else { -            vconn[i].next = (ECPConnection *)&vconn[i+1]; +#ifdef ECP_WITH_PTHREAD +    pthread_mutex_lock(&sock->conn_table.mutex); +    pthread_mutex_lock(&conn->mutex); +#endif + +    if (key->valid) { +        ECPConnection *_conn; + +        /* check for duplicates */ +        _conn = ecp_ht_search(sock->conn_table.keys, &key->public); +        if (_conn) { +#ifdef ECP_WITH_PTHREAD +            pthread_mutex_lock(&_conn->mutex); +#endif + +            _conn->remote.key_perma.valid = 0; + +#ifdef ECP_WITH_PTHREAD +            pthread_mutex_unlock(&_conn->mutex); +#endif + +            ecp_ht_remove(sock->conn_table.keys, &key->public);          } +        rv = ecp_ht_insert(sock->conn_table.keys, &key->public, conn);      } -    return ECP_OK; +#ifdef ECP_WITH_PTHREAD +    pthread_mutex_unlock(&conn->mutex); +    pthread_mutex_unlock(&sock->conn_table.mutex); +#endif + +    return rv;  } -int ecp_vconn_open(ECPConnection *conn, ECPNode *conn_node, ECPVConnOut vconn[], ECPNode vconn_node[], int size) { +#endif  /* ECP_WITH_HTABLE */ + +int ecp_vconn_handle_open(ECPConnection *conn, ECP2Buffer *b) {      int rv; -    ssize_t _rv; -    rv = ecp_conn_create_outb(conn, conn_node); -    if (rv) return rv; +    switch (conn->type) { +        case ECP_CTYPE_VCONN: +            rv = vconn_handle_open(conn); +            break; -    rv = ecp_conn_insert(conn); -    if (rv) { -        ecp_conn_destroy(conn); -        return rv; +        case ECP_CTYPE_VLINK: +#ifdef ECP_WITH_HTABLE +            rv = vlink_handle_open(conn); +#else +            rv = ECP_OK; +#endif +            break; + +        default: +            rv = ECP_ERR_CTYPE; +            break;      } -    rv = ecp_vconn_create_parent(conn, conn_node, vconn, vconn_node, size); -    if (rv) { -        ecp_conn_close(conn); -        return rv; +    return rv; +} + +void ecp_vconn_handle_close(ECPConnection *conn) { +    switch (conn->type) { +#ifdef ECP_WITH_HTABLE +        case ECP_CTYPE_VCONN: +            ecp_vconn_destroy(conn); +            break; + +        case ECP_CTYPE_VLINK: +            ecp_vlink_destroy(conn); +            break; +#endif      } +} -    _rv = ecp_timer_send((ECPConnection *)&vconn[0], _vconn_send_kget, ECP_MTYPE_KGET_REP, 3, 500); -    if (_rv < 0) return _rv; +ssize_t ecp_vconn_handle_msg(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, size_t msg_size, ECP2Buffer *b) { +    ssize_t rv; -    return ECP_OK; +    switch (mtype) { +#ifdef ECP_WITH_HTABLE +        case ECP_MTYPE_NEXT: +            rv = handle_next(conn, msg, msg_size, b); +            break; + +        case ECP_MTYPE_RELAY: +            rv = handle_relay(conn, msg, msg_size, b); +            break; +#endif + +        case ECP_MTYPE_EXEC: +            rv = handle_exec(conn, msg, msg_size, b); +            break; + +        default: +            rv = ECP_ERR_MTYPE; +            break; +    } + +    return rv; +} + +ssize_t ecp_vconn_send_open_req(ECPConnection *conn, unsigned char *cookie) { +    if (conn->type == ECP_CTYPE_VCONN) { +        ECPDHPub key_next; +        ECPBuffer packet; +        ECPBuffer payload; +        unsigned char pkt_buf[ECP_SIZE_PKT_BUF(2+ECP_SIZE_VBOX, ECP_MTYPE_OPEN_REQ, conn)+ECP_SIZE_COOKIE+ECP_SIZE_PLD(ECP_SIZE_ECDH_PUB, ECP_MTYPE_NEXT)]; +        unsigned char pld_buf[ECP_SIZE_PLD_BUF(2+ECP_SIZE_VBOX, ECP_MTYPE_OPEN_REQ, conn)+ECP_SIZE_COOKIE+ECP_SIZE_PLD(ECP_SIZE_ECDH_PUB, ECP_MTYPE_NEXT)]; +        unsigned char *msg; +        unsigned char *_pld_buf; +        size_t _pld_size; +        ssize_t rv; +        int _rv; + +        if (conn->next == NULL) return ECP_ERR; + +        _rv = ecp_conn_dhkey_get_remote(conn->next, ECP_ECDH_IDX_PERMA, &key_next); +        if (_rv) return _rv; + +        packet.buffer = pkt_buf; +        packet.size = sizeof(pkt_buf); +        payload.buffer = pld_buf; +        payload.size = sizeof(pld_buf); + +        rv = ecp_write_open_req(conn, &payload); +        if (rv < 0) return rv; + +        _pld_buf = payload.buffer + rv; +        _pld_size = payload.size - rv; + +        ecp_pld_set_type(_pld_buf, _pld_size, ECP_MTYPE_NEXT); +        msg = ecp_pld_get_msg(_pld_buf, _pld_size); +        memcpy(msg, &key_next.public, ECP_SIZE_ECDH_PUB); + +        rv = ecp_pld_send_wcookie(conn, &packet, &payload, rv+ECP_SIZE_PLD(ECP_SIZE_ECDH_PUB, ECP_MTYPE_NEXT), 0, cookie); +        return rv; +    } else { +        return ecp_send_open_req(conn, cookie); +    }  } -*/
\ No newline at end of file diff --git a/ecp/src/ecp/vconn/vconn.h b/ecp/src/ecp/vconn/vconn.h index eec13d4..9e2749f 100644 --- a/ecp/src/ecp/vconn/vconn.h +++ b/ecp/src/ecp/vconn/vconn.h @@ -12,14 +12,22 @@ typedef struct ECPVConn {      unsigned char key_next_curr;  } ECPVConn; -ssize_t ecp_vconn_send_open_req(ECPConnection *conn, unsigned char *cookie); -ssize_t ecp_vconn_pack_parent(ECPConnection *conn, ECPBuffer *packet, ECPBuffer *payload, size_t pkt_size, ecp_tr_addr_t *addr); -int ecp_vconn_handle_open(ECPConnection *conn, ECP2Buffer *b); -void ecp_vconn_handle_close(ECPConnection *conn); -ssize_t ecp_vconn_handle_msg(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, size_t msg_size, ECP2Buffer *b); - -int ecp_vconn_create_inb(ECPVConn *conn, ECPSocket *sock); -void ecp_vconn_destroy_inb(ECPVConn *conn); +ssize_t ecp_vconn_pack_parent(ECPConnection *conn, ECPBuffer *payload, ECPBuffer *packet, size_t pkt_size, ecp_tr_addr_t *addr);  int ecp_vconn_create(ECPConnection vconn[], ecp_ecdh_public_t keys[], size_t vconn_size, ECPConnection *conn); +#ifdef ECP_WITH_HTABLE +int ecp_vconn_create_inb(ECPVConn *conn, ECPSocket *sock); +void ecp_vconn_destroy(ECPConnection *conn); +#endif  int ecp_vconn_open(ECPConnection *conn, ECPNode *node); + +int ecp_vlink_create(ECPConnection *conn, ECPSocket *sock); +#ifdef ECP_WITH_HTABLE +int ecp_vlink_create_inb(ECPConnection *conn, ECPSocket *sock); +void ecp_vlink_destroy(ECPConnection *conn); +#endif + +int ecp_vconn_handle_open(ECPConnection *conn, ECP2Buffer *b); +void ecp_vconn_handle_close(ECPConnection *conn); +ssize_t ecp_vconn_handle_msg(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, size_t msg_size, ECP2Buffer *b); +ssize_t ecp_vconn_send_open_req(ECPConnection *conn, unsigned char *cookie); diff --git a/ecp/src/platform/fe310/platform.mk b/ecp/src/platform/fe310/platform.mk index 0230f29..0ab0065 100644 --- a/ecp/src/platform/fe310/platform.mk +++ b/ecp/src/platform/fe310/platform.mk @@ -1,3 +1,3 @@  fe310_dir = $(abspath $(src_dir)/../../fw/fe310)  include $(fe310_dir)/platform.mk -CFLAGS += -I$(fe310_dir) -DECP_WITH_VCONN=1 -DECP_DEBUG=1 -D__FE310__ +CFLAGS += -I$(fe310_dir) -D__FE310__ diff --git a/ecp/src/platform/fe310/platform_obj.mk b/ecp/src/platform/fe310/platform_obj.mk deleted file mode 100644 index 52e4bb1..0000000 --- a/ecp/src/platform/fe310/platform_obj.mk +++ /dev/null @@ -1 +0,0 @@ -subdirs	+= vconn diff --git a/ecp/src/platform/fe310/transport.c b/ecp/src/platform/fe310/transport.c index b551c51..2640ecf 100644 --- a/ecp/src/platform/fe310/transport.c +++ b/ecp/src/platform/fe310/transport.c @@ -1,5 +1,6 @@  #include <stdlib.h>  #include <string.h> +#include <stdio.h>  #include <core.h>  #include <tr.h> @@ -7,43 +8,42 @@  #include <eos/eos.h>  #include <eos/net.h> -static unsigned char _flags = 0; -  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) { -    ecp_tr_addr_t addr; -      ECP2Buffer bufs;      ECPBuffer packet;      ECPBuffer payload; -    unsigned char pld_buf[ECP_MAX_PLD]; +    ecp_tr_addr_t addr; +    ssize_t rv;      bufs.packet = &packet;      bufs.payload = &payload;      packet.buffer = buffer+EOS_SOCK_SIZE_UDP_HDR; -    packet.size = ECP_MAX_PKT; +    packet.size = EOS_NET_MTU-EOS_SOCK_SIZE_UDP_HDR;      payload.buffer = pld_buf;      payload.size = ECP_MAX_PLD; -    if ((buffer == NULL) || (len < EOS_SOCK_SIZE_UDP_HDR)) { +    if (len < EOS_SOCK_SIZE_UDP_HDR) {          eos_net_free(buffer, 0);          return;      } -    eos_sock_getfrom(buffer, &addr); -    ssize_t rv = ecp_pkt_handle(_ecp_tr_sock, &addr, NULL, &bufs, len-EOS_SOCK_SIZE_UDP_HDR); +    eos_sock_recvfrom(buffer, len, NULL, 0, &addr); + +    rv = ecp_pkt_handle(_ecp_tr_sock, NULL, &addr, &bufs, len-EOS_SOCK_SIZE_UDP_HDR);  #ifdef ECP_DEBUG      if (rv < 0) { -        char b[16]; -        puts("ERR:"); -        puts(itoa(rv, b, 10)); -        puts("\n"); +        printf("RCV ERR:%d\n", rv);      }  #endif -    if (bufs.packet->buffer) eos_net_free(buffer, 0); -    eos_net_release(); +    if (bufs.packet->buffer) { +        eos_net_free(buffer, 0); +    } else { +        eos_net_release(); +    }  }  int ecp_tr_init(ECPContext *ctx) { @@ -66,38 +66,43 @@ int ecp_tr_addr_set(ecp_tr_addr_t *addr, void *addr_s) {  }  int ecp_tr_open(ECPSocket *sock, void *addr_s) { -    sock->sock = eos_sock_open_udp(packet_handler); +    sock->sock = eos_sock_open_udp(packet_handler, NULL);      if (sock->sock < 0) {          sock->sock = 0; -        return ECP_ERR_SEND; +        return ECP_ERR;      }      _ecp_tr_sock = sock; +      return ECP_OK;  }  void ecp_tr_close(ECPSocket *sock) { -    eos_sock_close(sock->sock); +    eos_sock_close(sock->sock, NULL);      _ecp_tr_sock = NULL;  } -ssize_t ecp_tr_send(ECPSocket *sock, ECPBuffer *packet, size_t msg_size, ecp_tr_addr_t *addr, unsigned char flags) { +ssize_t ecp_tr_send(ECPSocket *sock, ECPBuffer *packet, size_t pkt_size, ecp_tr_addr_t *addr, unsigned char flags) {      unsigned char *buf = NULL; +    int reply;      int rv; -    flags |= _flags; -    if (packet && packet->buffer) { -        if (flags & ECP_SEND_FLAG_REPLY) { +    reply = 0; +    if (flags & ECP_SEND_FLAG_REPLY) { +        if (flags & ECP_SEND_FLAG_MORE) return ECP_ERR; +        if (packet->buffer) {              buf = packet->buffer-EOS_SOCK_SIZE_UDP_HDR;              packet->buffer = NULL; -        } else { -            buf = eos_net_alloc(); -            memcpy(buf+EOS_SOCK_SIZE_UDP_HDR, packet->buffer, msg_size); +            reply = 1;          } +    } else { +        buf = eos_net_alloc();      }      if (buf == NULL) return ECP_ERR; -    rv = eos_sock_sendto(sock->sock, buf, msg_size+EOS_SOCK_SIZE_UDP_HDR, flags & ECP_SEND_FLAG_MORE, addr); + +    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; -    return msg_size; + +    return pkt_size;  }  ssize_t ecp_tr_recv(ECPSocket *sock, ECPBuffer *packet, ecp_tr_addr_t *addr, int timeout) { @@ -105,18 +110,10 @@ ssize_t ecp_tr_recv(ECPSocket *sock, ECPBuffer *packet, ecp_tr_addr_t *addr, int  }  void ecp_tr_release(ECPBuffer *packet, unsigned char more) { -    if (packet && packet->buffer) { +    if (packet->buffer) {          eos_net_free(packet->buffer-EOS_SOCK_SIZE_UDP_HDR, more);          packet->buffer = NULL;      } else if (!more) {          eos_net_release();      }  } - -void ecp_tr_flag_set(unsigned char flags) { -    _flags |= flags; -} - -void ecp_tr_flag_clear(unsigned char flags) { -    _flags &= ~flags; -} diff --git a/ecp/src/platform/fe310/transport.h b/ecp/src/platform/fe310/transport.h index 213ca11..1e0c526 100644 --- a/ecp/src/platform/fe310/transport.h +++ b/ecp/src/platform/fe310/transport.h @@ -1,4 +1,4 @@  #include <eos/sock.h> -typedef EOSNetAddr ecp_tr_addr_t; +typedef struct EOSNetAddr ecp_tr_addr_t;  typedef int ecp_tr_sock_t; diff --git a/ecp/src/platform/posix/platform.mk b/ecp/src/platform/posix/platform.mk index 9dcfe37..8932ac4 100644 --- a/ecp/src/platform/posix/platform.mk +++ b/ecp/src/platform/posix/platform.mk @@ -1,2 +1,2 @@ -CFLAGS	+= -O3 -DECP_WITH_PTHREAD=1 -DECP_WITH_HTABLE=1 -DECP_WITH_VCONN=1 -DECP_WITH_DIR -DECP_WITH_DIRSRV -DECP_WITH_MSGQ=1 -DECP_DEBUG=1 +CFLAGS	+= -O3  LDFLAGS	+= -lm -pthread diff --git a/ecp/src/platform/posix/platform_obj.mk b/ecp/src/platform/posix/platform_obj.mk deleted file mode 100644 index 56a76c3..0000000 --- a/ecp/src/platform/posix/platform_obj.mk +++ /dev/null @@ -1 +0,0 @@ -subdirs	+= htable vconn ext dir diff --git a/ecp/src/platform/posix/transport.c b/ecp/src/platform/posix/transport.c index 30822b6..ebfbc7b 100644 --- a/ecp/src/platform/posix/transport.c +++ b/ecp/src/platform/posix/transport.c @@ -9,7 +9,7 @@  #include <core.h>  #include <tr.h> -#define ADDR_S_MAX  32 +#define MAX_ADDR_STR    32  int ecp_tr_init(ECPContext *ctx) {      return ECP_OK; @@ -28,13 +28,13 @@ int ecp_tr_addr_eq(ecp_tr_addr_t *addr1, ecp_tr_addr_t *addr2) {  int ecp_tr_addr_set(ecp_tr_addr_t *addr, void *addr_s) {      int rv; -    char addr_c[ADDR_S_MAX]; +    char addr_c[MAX_ADDR_STR];      char *colon = NULL;      char *endptr = NULL;  	uint16_t hport;      memset(addr_c, 0, sizeof(addr_c)); -    strncpy(addr_c, addr_s, ADDR_S_MAX-1); +    strncpy(addr_c, addr_s, sizeof(addr_c)-1);      colon = strchr(addr_c, ':');      if (colon == NULL) return -1;      *colon = '\0'; @@ -51,12 +51,14 @@ int ecp_tr_addr_set(ecp_tr_addr_t *addr, void *addr_s) {  int ecp_tr_open(ECPSocket *sock, void *addr_s) {      struct sockaddr_in _myaddr; +    int rv;      memset((char *)&_myaddr, 0, sizeof(_myaddr));      _myaddr.sin_family = AF_INET;      if (addr_s) {          ecp_tr_addr_t addr; -        int rv = ecp_tr_addr_set(&addr, addr_s); + +        rv = ecp_tr_addr_set(&addr, addr_s);          if (rv) return rv;          memcpy((void *)&_myaddr.sin_addr, addr.host, sizeof(addr.host)); @@ -69,11 +71,12 @@ int ecp_tr_open(ECPSocket *sock, void *addr_s) {      sock->sock = socket(PF_INET, SOCK_DGRAM, 0);      if (sock->sock < 0) return sock->sock; -    int rv = bind(sock->sock, (struct sockaddr *)&_myaddr, sizeof(_myaddr)); +    rv = bind(sock->sock, (struct sockaddr *)&_myaddr, sizeof(_myaddr));      if (rv < 0) {          close(sock->sock);          return rv;      } +      return ECP_OK;  } @@ -81,14 +84,14 @@ void ecp_tr_close(ECPSocket *sock) {      close(sock->sock);  } -ssize_t ecp_tr_send(ECPSocket *sock, ECPBuffer *packet, size_t msg_size, ecp_tr_addr_t *addr, unsigned char flags) { +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;      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, msg_size, 0, (struct sockaddr *)&servaddr, sizeof(servaddr)); +    return sendto(sock->sock, packet->buffer, pkt_size, 0, (struct sockaddr *)&servaddr, sizeof(servaddr));  }  ssize_t ecp_tr_recv(ECPSocket *sock, ECPBuffer *packet, ecp_tr_addr_t *addr, int timeout) { @@ -97,8 +100,9 @@ ssize_t ecp_tr_recv(ECPSocket *sock, ECPBuffer *packet, ecp_tr_addr_t *addr, int      struct pollfd fds[] = {          {sock->sock, POLLIN, 0}      }; +    int rv; -    int 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); @@ -111,9 +115,8 @@ ssize_t ecp_tr_recv(ECPSocket *sock, ECPBuffer *packet, ecp_tr_addr_t *addr, int          }          return recvlen;      } +      return ECP_ERR_TIMEOUT;  }  void ecp_tr_release(ECPBuffer *packet, unsigned char more) {} -void ecp_tr_flag_set(unsigned char flags) {} -void ecp_tr_flag_clear(unsigned char flags) {} diff --git a/ecp/test/Makefile b/ecp/test/Makefile index ce12a87..abda4e2 100644 --- a/ecp/test/Makefile +++ b/ecp/test/Makefile @@ -7,11 +7,23 @@ dep=../build-posix/*.a ../util/libecputil.a  %.o: %.c  	$(CC) $(CFLAGS) -c $< -all: basic # dir server echo frag stress vcs vc_server vc_client vc_client_t +all: basic client server vcs vc_server vc_client # dir echo frag stress vc_client_t  basic: basic.o init.o $(dep)  	$(CC) -o $@ $< init.o $(dep) $(LDFLAGS) +vcs: vcs.o init_vconn.o $(dep) +	$(CC) -o $@ $< init_vconn.o $(dep) $(LDFLAGS) + +vc_server: vc_server.o init_vconn.o $(dep) +	$(CC) -o $@ $< init_vconn.o $(dep) $(LDFLAGS) + +vc_server2: vc_server2.o init_vconn.o $(dep) +	$(CC) -o $@ $< init_vconn.o $(dep) $(LDFLAGS) + +vc_client: vc_client.o init_vconn.o $(dep) +	$(CC) -o $@ $< init_vconn.o $(dep) $(LDFLAGS) +  dir: dir.o init.o $(dep)  	$(CC) -DWITH_DIR_SERVER -o $@ $< init.o $(dep) $(LDFLAGS) @@ -30,15 +42,6 @@ frag: frag.o init.o $(dep)  stress: stress.o init.o $(dep)  	$(CC) -o $@ $< init.o $(dep) $(LDFLAGS) -vcs: vcs.o init_vconn.o $(dep) -	$(CC) -o $@ $< init_vconn.o $(dep) $(LDFLAGS) - -vc_server: vc_server.o init_vconn.o $(dep) -	$(CC) -o $@ $< init_vconn.o $(dep) $(LDFLAGS) - -vc_client: vc_client.o init_vconn.o $(dep) -	$(CC) -o $@ $< init_vconn.o $(dep) $(LDFLAGS) -  vc_client_t: vc_client_t.o init_vconn.o $(dep)  	$(CC) -o $@ $< init_vconn.o $(dep) $(LDFLAGS) diff --git a/ecp/test/basic.c b/ecp/test/basic.c index 2edb174..b266a07 100644 --- a/ecp/test/basic.c +++ b/ecp/test/basic.c @@ -1,26 +1,23 @@ -#include <stdio.h>  #include <string.h>  #include <unistd.h> +#include <stdio.h>  #include <core.h>  ECPContext ctx_s;  ECPSocket sock_s; -ECPDHKey key_perma_s;  ECPConnHandler handler_s;  ECPContext ctx_c;  ECPSocket sock_c; -ECPDHKey key_perma_c;  ECPConnHandler handler_c; -ECPNode node;  ECPConnection conn;  #define CTYPE_TEST  0  #define MTYPE_MSG   0 -int handle_open_c(ECPConnection *conn, ECP2Buffer *b) { +static int handle_open_c(ECPConnection *conn, ECP2Buffer *b) {      char *_msg = "PERA JE CAR!";      ssize_t rv; @@ -30,12 +27,12 @@ int handle_open_c(ECPConnection *conn, ECP2Buffer *b) {      return ECP_OK;  } -ssize_t handle_msg_c(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, size_t msg_size, ECP2Buffer *b) { +static ssize_t handle_msg_c(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, size_t msg_size, ECP2Buffer *b) {      printf("MSG C:%s size:%ld\n", msg, msg_size);      return msg_size;  } -ssize_t handle_msg_s(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, size_t msg_size, ECP2Buffer *b) { +static ssize_t handle_msg_s(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, size_t msg_size, ECP2Buffer *b) {      char *_msg = "VAISTINU JE CAR!";      ssize_t rv; @@ -46,6 +43,9 @@ ssize_t handle_msg_s(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, un  }  int main(int argc, char *argv[]) { +    ECPDHKey key_perma_c; +    ECPDHKey key_perma_s; +    ECPNode node;      int rv;      /* server */ @@ -96,4 +96,4 @@ int main(int argc, char *argv[]) {      printf("ecp_conn_open RV:%d\n", rv);      while (1) sleep(1); -}
\ No newline at end of file +} diff --git a/ecp/test/client.c b/ecp/test/client.c index 577a888..d526f76 100644 --- a/ecp/test/client.c +++ b/ecp/test/client.c @@ -1,80 +1,78 @@ -#include <stdio.h> +#include <stdlib.h>  #include <string.h>  #include <unistd.h> -#include <stdlib.h> +#include <stdio.h> + +#include <core.h> -#include "core.h" -#include "util.h" +#include <util.h>  ECPContext ctx;  ECPSocket sock;  ECPConnHandler handler; - -ECPNode node;  ECPConnection conn;  #define CTYPE_TEST  0 -#define MTYPE_MSG   8 - -ssize_t handle_open(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) { -    uint32_t seq = 0; -     -    ecp_conn_handle_open(conn, sq, t, p, s, b); -    if (s < 0) { -        printf("OPEN ERR:%ld\n", s); -        return s; -    } -     -    char *msg = "PERA JE CAR!"; -    unsigned char buf[1000]; - -    strcpy((char *)buf, msg); -    ssize_t _rv = ecp_send(conn, MTYPE_MSG, buf, 1000); - -    return s; -} +#define MTYPE_MSG   0 + +static int handle_open(ECPConnection *conn, ECP2Buffer *b) { +    char *_msg = "PERA JE CAR!"; +    ssize_t rv; + +    printf("OPEN\n"); +    rv = ecp_msg_send(conn, MTYPE_MSG, (unsigned char *)_msg, strlen(_msg)+1); -ssize_t handle_msg(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) { -    printf("MSG C:%s size:%ld\n", p, s); -    return s; +    return ECP_OK;  } +static ssize_t handle_msg(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, size_t msg_size, ECP2Buffer *b) { +    printf("MSG C:%s size:%ld\n", msg, msg_size); +    return msg_size; +}  static void usage(char *arg) { -    fprintf(stderr, "Usage: %s <node.pub>\n", arg); +    fprintf(stderr, "Usage: %s <address> <node.pub>\n", arg);      exit(1);  }  int main(int argc, char *argv[]) { +    ECPDHKey key_perma; +    ECPNode node; +    ecp_ecdh_public_t node_pub;      int rv; -     -    if (argc != 2) usage(argv[0]); -     + +    /* client */ +    if (argc != 3) usage(argv[0]); +      rv = ecp_init(&ctx);      printf("ecp_init RV:%d\n", rv); -    rv = ecp_conn_handler_init(&handler); -    handler.msg[ECP_MTYPE_OPEN] = handle_open; -    handler.msg[MTYPE_MSG] = handle_msg; -    ctx.handler[CTYPE_TEST] = &handler; -     -    rv = ecp_sock_init(&sock, &ctx, NULL); -    printf("ecp_sock_init RV:%d\n", rv); +    ecp_conn_handler_init(&handler, handle_msg, handle_open, NULL, NULL); +    ecp_ctx_set_handler(&ctx, &handler, CTYPE_TEST); + +    rv = ecp_dhkey_gen(&key_perma); +    printf("ecp_dhkey_gen RV:%d\n", rv); + +    rv = ecp_sock_create(&sock, &ctx, &key_perma); +    printf("ecp_sock_create RV:%d\n", rv);      rv = ecp_sock_open(&sock, NULL);      printf("ecp_sock_open RV:%d\n", rv); -     +      rv = ecp_start_receiver(&sock);      printf("ecp_start_receiver RV:%d\n", rv); -    rv = ecp_util_node_load(&ctx, &node, argv[1]); -    printf("ecp_util_node_load RV:%d\n", rv); +    rv = ecp_conn_create(&conn, &sock, CTYPE_TEST); +    printf("ecp_conn_create RV:%d\n", rv); + +    rv = ecp_util_load_pub(&node_pub, argv[2]); +    printf("ecp_util_load_pub RV:%d\n", rv); -    rv = ecp_conn_init(&conn, &sock, CTYPE_TEST); -    printf("ecp_conn_init RV:%d\n", rv); +    rv = ecp_node_init(&node, &node_pub, argv[1]); +    printf("ecp_node_init RV:%d\n", rv);      rv = ecp_conn_open(&conn, &node);      printf("ecp_conn_open RV:%d\n", rv); -     +      while (1) sleep(1); -}
\ No newline at end of file +} diff --git a/ecp/test/dir.c b/ecp/test/dir.c deleted file mode 100644 index 0c2ac72..0000000 --- a/ecp/test/dir.c +++ /dev/null @@ -1,93 +0,0 @@ -#include <stdio.h> -#include <string.h> -#include <unistd.h> - -#include "core.h" -#include "cr.h" -#include "dir.h" -#include "dir_srv.h" - -ECPContext ctx_s; -ECPSocket sock_s; -ECPDHKey key_perma_s; - -ECPContext ctx_c; -ECPSocket sock_c; -ECPDHKey key_perma_c; -ECPConnHandler handler_c; - -ECPNode node; -ECPConnection conn; - -static ECPDirList dir_online; -static ECPDirList dir_shadow; - -#define CTYPE_TEST  0 - -ssize_t handle_dir(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, ssize_t size, ECP2Buffer *b) { -    ECPDirList dir; -    ssize_t rv; - -    dir.count = 0; -    rv = ecp_dir_parse(&dir, msg, size); -    if (rv < 0) return rv; - -    printf("DIR: %s %ld\n", (char *)ecp_cr_dh_pub_get_buf(&dir.item[0].node.public), size); - -    return rv; -} - -int main(int argc, char *argv[]) { -    int rv; - -    dir_online.count = 1; -    strcpy((char *)ecp_cr_dh_pub_get_buf(&dir_online.item[0].node.public), "PERA"); - -    rv = ecp_init(&ctx_s); -    printf("ecp_init RV:%d\n", rv); - -    rv = ecp_dir_init(&ctx_s, &dir_online, &dir_shadow); -    printf("ecp_dir_init RV:%d\n", rv); - -    rv = ecp_dhkey_gen(&ctx_s, &key_perma_s); -    printf("ecp_dhkey_gen RV:%d\n", rv); - -    rv = ecp_sock_init(&sock_s, &ctx_s, &key_perma_s); -    printf("ecp_sock_init RV:%d\n", rv); - -    rv = ecp_sock_open(&sock_s, "0.0.0.0:3000"); -    printf("ecp_sock_open RV:%d\n", rv); - -    rv = ecp_start_receiver(&sock_s); -    printf("ecp_start_receiver RV:%d\n", rv); - -    rv = ecp_init(&ctx_c); -    printf("ecp_init RV:%d\n", rv); - -    rv = ecp_conn_handler_init(&handler_c); -    handler_c.msg[ECP_MTYPE_DIR] = handle_dir; -    ctx_c.handler[CTYPE_TEST] = &handler_c; - -    rv = ecp_dhkey_gen(&ctx_c, &key_perma_c); -    printf("ecp_dhkey_gen RV:%d\n", rv); - -    rv = ecp_sock_init(&sock_c, &ctx_c, &key_perma_c); -    printf("ecp_sock_init RV:%d\n", rv); - -    rv = ecp_sock_open(&sock_c, NULL); -    printf("ecp_sock_open RV:%d\n", rv); - -    rv = ecp_start_receiver(&sock_c); -    printf("ecp_start_receiver RV:%d\n", rv); - -    rv = ecp_node_init(&node, &key_perma_s.public, "127.0.0.1:3000"); -    printf("ecp_node_init RV:%d\n", rv); - -    rv = ecp_conn_init(&conn, &sock_c, CTYPE_TEST); -    printf("ecp_conn_init RV:%d\n", rv); - -    rv = ecp_conn_get_dirlist(&conn, &node); -    printf("ecp_conn_get_dirlist RV:%d\n", rv); - -    while (1) sleep(1); -}
\ No newline at end of file diff --git a/ecp/test/echo.c b/ecp/test/echo.c deleted file mode 100644 index f3057df..0000000 --- a/ecp/test/echo.c +++ /dev/null @@ -1,56 +0,0 @@ -#include <stdio.h> -#include <string.h> -#include <unistd.h> -#include <stdlib.h> - -#include "core.h" -#include "util.h" - -ECPContext ctx_s; -ECPSocket sock_s; -ECPDHKey key_perma_s; -ECPConnHandler handler_s; - -#define CTYPE_TEST  0 -#define MTYPE_MSG   8 - -ssize_t handle_msg_s(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) { -    unsigned char buf[ECP_MAX_PLD]; - -    memcpy(buf, p, s); -    ssize_t _rv = ecp_send(conn, t, buf, s); - -    return s; -} - -static void usage(char *arg) { -    fprintf(stderr, "Usage: %s <address> <node.priv>\n", arg); -    exit(1); -} - -int main(int argc, char *argv[]) { -    int rv; -     -    if (argc != 3) usage(argv[0]); -     -    rv = ecp_init(&ctx_s); -    printf("ecp_init RV:%d\n", rv); -     -    rv = ecp_conn_handler_init(&handler_s); -    handler_s.msg[MTYPE_MSG] = handle_msg_s; -    ctx_s.handler[CTYPE_TEST] = &handler_s; -     -    rv = ecp_util_key_load(&ctx_s, &key_perma_s, argv[2]); -    printf("ecp_util_key_load RV:%d\n", rv); -     -    rv = ecp_sock_init(&sock_s, &ctx_s, &key_perma_s); -    printf("ecp_sock_init RV:%d\n", rv); - -    rv = ecp_sock_open(&sock_s, argv[1]); -    printf("ecp_sock_open RV:%d\n", rv); -     -    rv = ecp_start_receiver(&sock_s); -    printf("ecp_start_receiver RV:%d\n", rv); - -    while (1) sleep(1); -}
\ No newline at end of file diff --git a/ecp/test/frag.c b/ecp/test/frag.c deleted file mode 100644 index 70b20de..0000000 --- a/ecp/test/frag.c +++ /dev/null @@ -1,122 +0,0 @@ -#include <stdio.h> -#include <string.h> -#include <unistd.h> - -#include "core.h" - -ECPContext ctx_s; -ECPSocket sock_s; -ECPDHKey key_perma_s; -ECPConnHandler handler_s; - -ECPContext ctx_c; -ECPSocket sock_c; -ECPDHKey key_perma_c; -ECPConnHandler handler_c; - -ECPNode node; -ECPConnection conn; - -ECPRBRecv rbuf_recv; -ECPRBMessage rbuf_r_msg[128]; -ECPFragIter frag_iter; -unsigned char frag_buffer[8192]; - -#define CTYPE_TEST  0 -#define MTYPE_MSG   8 - -ssize_t handle_open_c(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) { -    uint32_t seq = 0; - -    ecp_conn_handle_open(conn, sq, t, p, s, b); -    if (s < 0) { -        printf("OPEN ERR:%ld\n", s); -        return s; -    } - -    unsigned char content[1000]; -    char *msg = "PERA JE CAR!"; - -    strcpy((char *)content, msg); -    ssize_t _rv = ecp_send(conn, MTYPE_MSG, content, sizeof(content)); -    return s; -} - -ssize_t handle_msg_c(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) { -    printf("MSG C:%s size:%ld\n", p, s); - -    ECPRBuffer *rbuf = &conn->rbuf.recv->rbuf; -    printf("RBUF: %d %d %d %d\n", rbuf->seq_start, rbuf->seq_max, rbuf->idx_start, rbuf->arr_size); -    return s; -} - -ssize_t handle_msg_s(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) { -    printf("MSG S:%s size:%ld\n", p, s); - -    unsigned char content[5000]; -    char *msg = "VAISTINU JE CAR!"; - -    strcpy((char *)content, msg); -    ssize_t _rv = ecp_send(conn, MTYPE_MSG, content, sizeof(content)); -    return s; -} - -int main(int argc, char *argv[]) { -    int rv; - -    rv = ecp_init(&ctx_s); -    printf("ecp_init RV:%d\n", rv); - -    rv = ecp_conn_handler_init(&handler_s); -    handler_s.msg[MTYPE_MSG] = handle_msg_s; -    ctx_s.handler[CTYPE_TEST] = &handler_s; - -    rv = ecp_dhkey_gen(&ctx_s, &key_perma_s); -    printf("ecp_dhkey_gen RV:%d\n", rv); - -    rv = ecp_sock_init(&sock_s, &ctx_s, &key_perma_s); -    printf("ecp_sock_init RV:%d\n", rv); - -    rv = ecp_sock_open(&sock_s, "0.0.0.0:3000"); -    printf("ecp_sock_open RV:%d\n", rv); - -    rv = ecp_start_receiver(&sock_s); -    printf("ecp_start_receiver RV:%d\n", rv); - -    rv = ecp_init(&ctx_c); -    printf("ecp_init RV:%d\n", rv); - -    rv = ecp_conn_handler_init(&handler_c); -    handler_c.msg[ECP_MTYPE_OPEN] = handle_open_c; -    handler_c.msg[MTYPE_MSG] = handle_msg_c; -    ctx_c.handler[CTYPE_TEST] = &handler_c; - -    rv = ecp_dhkey_gen(&ctx_c, &key_perma_c); -    printf("ecp_dhkey_gen RV:%d\n", rv); - -    rv = ecp_sock_init(&sock_c, &ctx_c, &key_perma_c); -    printf("ecp_sock_init RV:%d\n", rv); - -    rv = ecp_sock_open(&sock_c, NULL); -    printf("ecp_sock_open RV:%d\n", rv); - -    rv = ecp_start_receiver(&sock_c); -    printf("ecp_start_receiver RV:%d\n", rv); - -    rv = ecp_node_init(&node, &key_perma_s.public, "127.0.0.1:3000"); -    printf("ecp_node_init RV:%d\n", rv); - -    rv = ecp_conn_init(&conn, &sock_c, CTYPE_TEST); -    printf("ecp_conn_init RV:%d\n", rv); - -    rv = ecp_rbuf_create(&conn, NULL, NULL, 0, &rbuf_recv, rbuf_r_msg, 128); -    printf("ecp_rbuf_create RV:%d\n", rv); - -    ecp_frag_iter_init(&frag_iter, frag_buffer, 8192); -    rbuf_recv.frag_iter = &frag_iter; - -    rv = ecp_conn_open(&conn, &node); -    printf("ecp_conn_open RV:%d\n", rv); - -    while (1) sleep(1); -}
\ No newline at end of file diff --git a/ecp/test/init_vconn.c b/ecp/test/init_vconn.c index 59e9517..88b6fa0 100644 --- a/ecp/test/init_vconn.c +++ b/ecp/test/init_vconn.c @@ -1,18 +1,12 @@  #include <stdlib.h> -#include <fcntl.h>  #include <unistd.h> +#include <stdio.h> -#include "core.h" -#include "vconn/vconn.h" +#include <core.h> +#include <vconn/vconn.h> -static int v_rng(void *buf, size_t bufsize) { -    int fd; - -    if((fd = open("/dev/urandom", O_RDONLY)) < 0) return -1; -    size_t nb = read(fd, buf, bufsize); -    close(fd); -    if (nb != bufsize) return -1; -    return 0; +void handle_err(ECPConnection *conn, unsigned char mtype, int err) { +    printf("ERR: CTYPE:0x%x MTYPE:0x%x ERR:%d\n", conn->type, mtype, err);  }  static ECPConnection *conn_alloc(ECPSocket *sock, unsigned char type) { @@ -21,40 +15,37 @@ static ECPConnection *conn_alloc(ECPSocket *sock, unsigned char type) {      switch (type) {          case ECP_CTYPE_VCONN: -            conn = malloc(sizeof(ECPVConnIn)); +            conn = malloc(sizeof(ECPVConn)); +            if (conn) rv = ecp_vconn_create_inb((ECPVConn *)conn, sock); +            break; + +        case ECP_CTYPE_VLINK: +            conn = malloc(sizeof(ECPConnection)); +            if (conn) rv = ecp_vlink_create_inb(conn, sock);              break; +          default:              conn = malloc(sizeof(ECPConnection)); +            if (conn) rv = ecp_conn_create_inb(conn, sock, type);              break;      } -    if (conn == NULL) return NULL; -    rv = ecp_conn_init(conn, sock, type); +    if (conn == NULL) return NULL;      if (rv) { -        printf("free1\n");          free(conn);          return NULL;      } +      return conn;  }  static void conn_free(ECPConnection *conn) { -    printf("free2\n");      free(conn);  }  int ecp_init(ECPContext *ctx) {      int rv; -    rv = ecp_ctx_init(ctx); -    if (rv) return rv; - -    rv = ecp_vconn_ctx_init(ctx); -    if (rv) return rv; - -    ctx->rng = v_rng; -    ctx->conn_alloc = conn_alloc; -    ctx->conn_free = conn_free; - -    return ECP_OK; +    rv = ecp_ctx_init(ctx, handle_err, NULL, conn_alloc, conn_free); +    return rv;  }
\ No newline at end of file diff --git a/ecp/test/server.c b/ecp/test/server.c index fabaf4e..228dbea 100644 --- a/ecp/test/server.c +++ b/ecp/test/server.c @@ -1,10 +1,11 @@ -#include <stdio.h> +#include <stdlib.h>  #include <string.h>  #include <unistd.h> -#include <stdlib.h> +#include <stdio.h> -#include "core.h" -#include "util.h" +#include <core.h> + +#include <util.h>  ECPContext ctx;  ECPSocket sock; @@ -12,18 +13,16 @@ ECPDHKey key_perma;  ECPConnHandler handler;  #define CTYPE_TEST  0 -#define MTYPE_MSG   8 - -ssize_t handle_msg(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) { -    printf("MSG S:%s size:%ld\n", p, s); +#define MTYPE_MSG   0 -    char *msg = "VAISTINU JE CAR!"; -    unsigned char buf[1000]; +static ssize_t handle_msg(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, size_t msg_size, ECP2Buffer *b) { +    char *_msg = "VAISTINU JE CAR!"; +    ssize_t rv; -    strcpy((char *)buf, msg); -    ssize_t _rv = ecp_send(conn, MTYPE_MSG, buf, 1000); +    printf("MSG S:%s size:%ld\n", msg, msg_size); +    rv = ecp_msg_send(conn, MTYPE_MSG, (unsigned char *)_msg, strlen(_msg)+1); -    return s; +    return msg_size;  }  static void usage(char *arg) { @@ -32,28 +31,30 @@ static void usage(char *arg) {  }  int main(int argc, char *argv[]) { +    ECPDHKey key_perma;      int rv; -     + +    /* server */      if (argc != 3) usage(argv[0]); -     +      rv = ecp_init(&ctx);      printf("ecp_init RV:%d\n", rv); -     -    rv = ecp_conn_handler_init(&handler); -    handler.msg[MTYPE_MSG] = handle_msg; -    ctx.handler[CTYPE_TEST] = &handler; -     -    rv = ecp_util_key_load(&ctx, &key_perma, argv[2]); -    printf("ecp_util_key_load RV:%d\n", rv); -     -    rv = ecp_sock_init(&sock, &ctx, &key_perma); -    printf("ecp_sock_init RV:%d\n", rv); + +    ecp_conn_handler_init(&handler, handle_msg, NULL, NULL, NULL); +    ecp_ctx_set_handler(&ctx, &handler, CTYPE_TEST); + +    rv = ecp_util_load_key(&key_perma.public, &key_perma.private, argv[2]); +    printf("ecp_util_load_key RV:%d\n", rv); +    key_perma.valid = 1; + +    rv = ecp_sock_create(&sock, &ctx, &key_perma); +    printf("ecp_sock_create RV:%d\n", rv);      rv = ecp_sock_open(&sock, argv[1]);      printf("ecp_sock_open RV:%d\n", rv); -     +      rv = ecp_start_receiver(&sock);      printf("ecp_start_receiver RV:%d\n", rv);      while (1) sleep(1); -}
\ No newline at end of file +} diff --git a/ecp/test/vc_client.c b/ecp/test/vc_client.c index cd0ea44..89602ca 100644 --- a/ecp/test/vc_client.c +++ b/ecp/test/vc_client.c @@ -1,70 +1,63 @@ -#include <stdio.h> +#include <stdlib.h>  #include <string.h>  #include <unistd.h> -#include <stdlib.h> +#include <stdio.h> + +#include <core.h> +#include <vconn/vconn.h> -#include "core.h" -#include "vconn/vconn.h" -#include "util.h" +#include <util.h>  ECPContext ctx;  ECPSocket sock;  ECPConnHandler handler; -  ECPConnection conn; -ECPNode node; - -ECPVConnOut vconn[20]; -ECPNode vconn_node[20]; +ECPConnection vconn[3];  #define CTYPE_TEST  0 -#define MTYPE_MSG   8 - -ssize_t handle_open(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) { -    uint32_t seq = 0; - -    ecp_conn_handle_open(conn, sq, t, p, s, b); -    if (s < 0) { -        printf("OPEN ERR:%ld\n", s); -        return s; -    } - -    printf("OPEN!\n"); +#define MTYPE_MSG   0 -    char *msg = "PERA JE CAR!"; -    unsigned char buf[1156]; +static int handle_open(ECPConnection *conn, ECP2Buffer *b) { +    char *_msg = "PERA JE CAR!"; +    ssize_t rv; -    strcpy((char *)buf, msg); -    ssize_t _rv = ecp_send(conn, MTYPE_MSG, buf, 1156); +    printf("OPEN\n"); +    rv = ecp_msg_send(conn, MTYPE_MSG, (unsigned char *)_msg, strlen(_msg)+1); -    return s; +    return ECP_OK;  } -ssize_t handle_msg(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) { -    printf("MSG S:%s size:%ld\n", p, s); -    return s; +static ssize_t handle_msg(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, size_t msg_size, ECP2Buffer *b) { +    printf("MSG:%s size:%ld\n", msg, msg_size); + +    return msg_size;  }  static void usage(char *arg) { -    fprintf(stderr, "Usage: %s <server.pub> <vcs1.pub> ... <vcsn.pub>\n", arg); +    fprintf(stderr, "Usage: %s <server.pub> <address> <vcs0.pub> [ ... <vcsN.pub> ]\n", arg);      exit(1);  }  int main(int argc, char *argv[]) { +    ECPDHKey key_perma; +    ECPNode node; +    ecp_ecdh_public_t node_pub; +    ecp_ecdh_public_t vconn_pub[3];      int rv, i; -    if ((argc < 3) || (argc > 22)) usage(argv[0]); +    if ((argc < 4) || (argc > 6)) usage(argv[0]);      rv = ecp_init(&ctx);      printf("ecp_init RV:%d\n", rv); -    rv = ecp_conn_handler_init(&handler); -    handler.msg[ECP_MTYPE_OPEN] = handle_open; -    handler.msg[MTYPE_MSG] = handle_msg; -    ctx.handler[CTYPE_TEST] = &handler; +    ecp_conn_handler_init(&handler, handle_msg, handle_open, NULL, NULL); +    ecp_ctx_set_handler(&ctx, &handler, CTYPE_TEST); -    rv = ecp_sock_init(&sock, &ctx, NULL); -    printf("ecp_sock_init RV:%d\n", rv); +    rv = ecp_dhkey_gen(&key_perma); +    printf("ecp_dhkey_gen RV:%d\n", rv); + +    rv = ecp_sock_create(&sock, &ctx, &key_perma); +    printf("ecp_sock_create RV:%d\n", rv);      rv = ecp_sock_open(&sock, NULL);      printf("ecp_sock_open RV:%d\n", rv); @@ -72,19 +65,25 @@ int main(int argc, char *argv[]) {      rv = ecp_start_receiver(&sock);      printf("ecp_start_receiver RV:%d\n", rv); -    rv = ecp_util_node_load(&ctx, &node, argv[1]); -    printf("ecp_util_node_load RV:%d\n", rv); - -    for (i=0; i<argc-2; i++) { -        rv = ecp_util_node_load(&ctx, &vconn_node[i], argv[i+2]); -        printf("ecp_util_node_load RV:%d\n", rv); +    for (i=3; i<argc; i++) { +        rv = ecp_util_load_pub(&vconn_pub[i-3], argv[i]); +        printf("ecp_util_load_pub RV:%d\n", rv);      } -    rv = ecp_conn_init(&conn, &sock, CTYPE_TEST); -    printf("ecp_conn_init RV:%d\n", rv); +    rv = ecp_conn_create(&conn, &sock, CTYPE_TEST); +    printf("ecp_conn_create RV:%d\n", rv); -    rv = ecp_vconn_open(&conn, &node, vconn, vconn_node, argc-2); +    rv = ecp_vconn_create(vconn, vconn_pub, argc-3, &conn); +    printf("ecp_vconn_create RV:%d\n", rv); + +    rv = ecp_util_load_pub(&node_pub, argv[1]); +    printf("ecp_util_load_pub RV:%d\n", rv); + +    rv = ecp_node_init(&node, &node_pub, argv[2]); +    printf("ecp_node_init RV:%d\n", rv); + +    rv = ecp_vconn_open(&conn, &node);      printf("ecp_vconn_open RV:%d\n", rv);      while (1) sleep(1); -}
\ No newline at end of file +} diff --git a/ecp/test/vc_server.c b/ecp/test/vc_server.c index b86d7b0..de7f275 100644 --- a/ecp/test/vc_server.c +++ b/ecp/test/vc_server.c @@ -1,63 +1,63 @@ -#include <stdio.h> +#include <stdlib.h>  #include <string.h>  #include <unistd.h> -#include <stdlib.h> +#include <stdio.h> -#include "core.h" -#include "vconn/vconn.h" -#include "util.h" +#include <core.h> +#include <vconn/vconn.h> + +#include <util.h>  ECPContext ctx;  ECPSocket sock; -ECPDHKey key_perma;  ECPConnHandler handler; - -ECPNode node;  ECPConnection conn; +ECPConnection vconn[3];  #define CTYPE_TEST  0 -#define MTYPE_MSG   8 +#define MTYPE_MSG   0 -ssize_t handle_open(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) { -    printf("OPEN RECEIVED\n"); -    return ecp_conn_handle_open(conn, sq, t, p, s, b); -} +static int handle_open(ECPConnection *conn, ECP2Buffer *b) { +    printf("OPEN\n"); -ssize_t handle_msg(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) { -    printf("MSG S:%s size:%ld\n", p, s); +    return ECP_OK; +} -    char *msg = "VAISTINU JE CAR!"; -    unsigned char buf[1000]; +static ssize_t handle_msg(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, size_t msg_size, ECP2Buffer *b) { +    char *_msg = "VAISTINU JE CAR!"; +    ssize_t rv; -    strcpy((char *)buf, msg); -    ssize_t _rv = ecp_send(conn, MTYPE_MSG, buf, strlen(msg)+1); +    printf("MSG:%s size:%ld\n", msg, msg_size); +    rv = ecp_msg_send(conn, MTYPE_MSG, (unsigned char *)_msg, strlen(_msg)+1); -    return s; +    return msg_size;  }  static void usage(char *arg) { -    fprintf(stderr, "Usage: %s <node.priv> <vcs.pub>\n", arg); +    fprintf(stderr, "Usage: %s <server.priv> <address> <vcs0.pub> [ ... <vcsN.pub> ]\n", arg);      exit(1);  }  int main(int argc, char *argv[]) { -    int rv; +    ECPDHKey key_perma; +    ECPNode node; +    ecp_ecdh_public_t node_pub; +    int rv, i; -    if (argc != 3) usage(argv[0]); +    if ((argc < 4) || (argc > 7)) usage(argv[0]);      rv = ecp_init(&ctx);      printf("ecp_init RV:%d\n", rv); -    rv = ecp_conn_handler_init(&handler); -    handler.msg[ECP_MTYPE_OPEN] = handle_open; -    handler.msg[MTYPE_MSG] = handle_msg; -    ctx.handler[CTYPE_TEST] = &handler; +    ecp_conn_handler_init(&handler, handle_msg, handle_open, NULL, NULL); +    ecp_ctx_set_handler(&ctx, &handler, CTYPE_TEST); -    rv = ecp_util_key_load(&ctx, &key_perma, argv[1]); -    printf("ecp_util_key_load RV:%d\n", rv); +    rv = ecp_util_load_key(&key_perma.public, &key_perma.private, argv[1]); +    printf("ecp_util_load_key RV:%d\n", rv); +    key_perma.valid = 1; -    rv = ecp_sock_init(&sock, &ctx, &key_perma); -    printf("ecp_sock_init RV:%d\n", rv); +    rv = ecp_sock_create(&sock, &ctx, &key_perma); +    printf("ecp_sock_create RV:%d\n", rv);      rv = ecp_sock_open(&sock, NULL);      printf("ecp_sock_open RV:%d\n", rv); @@ -65,14 +65,29 @@ int main(int argc, char *argv[]) {      rv = ecp_start_receiver(&sock);      printf("ecp_start_receiver RV:%d\n", rv); -    rv = ecp_util_node_load(&ctx, &node, argv[2]); -    printf("ecp_util_node_load RV:%d\n", rv); +    rv = ecp_vlink_create(&conn, &sock); +    printf("ecp_vlink_create RV:%d\n", rv); + +    if (argc > 4) { +        ecp_ecdh_public_t vconn_key[3]; + +        for (i=3; i<argc-1; i++) { +            rv = ecp_util_load_pub(&vconn_key[i-3], argv[i]); +            printf("ecp_util_load_pub RV:%d\n", rv); +        } -    rv = ecp_conn_init(&conn, &sock, ECP_CTYPE_VLINK); -    printf("ecp_conn_init RV:%d\n", rv); +        rv = ecp_vconn_create(vconn, vconn_key, argc-4, &conn); +        printf("ecp_vconn_create RV:%d\n", rv); +    } -    rv = ecp_conn_open(&conn, &node); -    printf("ecp_conn_open RV:%d\n", rv); +    rv = ecp_util_load_pub(&node_pub, argv[argc-1]); +    printf("ecp_util_load_pub RV:%d\n", rv); + +    rv = ecp_node_init(&node, &node_pub, argv[2]); +    printf("ecp_node_init RV:%d\n", rv); + +    rv = ecp_vconn_open(&conn, &node); +    printf("ecp_vconn_open RV:%d\n", rv);      while (1) sleep(1); -}
\ No newline at end of file +} diff --git a/ecp/test/vcs.c b/ecp/test/vcs.c index f3a0156..c4beed7 100644 --- a/ecp/test/vcs.c +++ b/ecp/test/vcs.c @@ -1,54 +1,60 @@ -#include <stdio.h> +#include <stdlib.h>  #include <string.h>  #include <unistd.h> -#include <stdlib.h> +#include <stdio.h> -#include "core.h" -#include "util.h" -#include "vconn/vconn.h" +#include <core.h> +#include <vconn/vconn.h> + +#include <util.h>  ECPContext ctx;  ECPSocket sock; -ECPDHKey key_perma; - -ECPNode node;  ECPConnection conn;  static void usage(char *arg) { -    fprintf(stderr, "Usage: %s <address> <node.priv> [node.pub]\n", arg); +    fprintf(stderr, "Usage: %s <my addr> <my.priv> [ <node addr> <node.pub> ]\n", arg);      exit(1);  }  int main(int argc, char *argv[]) { +    ECPDHKey key_perma;      int rv; -     -    if ((argc < 3) || (argc > 4)) usage(argv[0]); -     + +    if ((argc < 3) || (argc > 5)) usage(argv[0]); +      rv = ecp_init(&ctx);      printf("ecp_init RV:%d\n", rv); -     -    rv = ecp_util_key_load(&ctx, &key_perma, argv[2]); -    printf("ecp_util_key_load RV:%d\n", rv); -     -    rv = ecp_sock_init(&sock, &ctx, &key_perma); -    printf("ecp_sock_init RV:%d\n", rv); + +    rv = ecp_util_load_key(&key_perma.public, &key_perma.private, argv[2]); +    printf("ecp_util_load_key RV:%d\n", rv); +    key_perma.valid = 1; + +    rv = ecp_sock_create(&sock, &ctx, &key_perma); +    printf("ecp_sock_create RV:%d\n", rv);      rv = ecp_sock_open(&sock, argv[1]);      printf("ecp_sock_open RV:%d\n", rv); -     +      rv = ecp_start_receiver(&sock);      printf("ecp_start_receiver RV:%d\n", rv); -     -    if (argc == 4) { -        rv = ecp_util_node_load(&ctx, &node, argv[3]); -        printf("ecp_util_node_load RV:%d\n", rv); -        rv = ecp_conn_init(&conn, &sock, ECP_CTYPE_VLINK); -        printf("ecp_conn_init RV:%d\n", rv); +    if (argc == 5) { +        ECPNode node; +        ecp_ecdh_public_t node_pub; + +        rv = ecp_vlink_create(&conn, &sock); +        printf("ecp_vlink_create RV:%d\n", rv); + +        rv = ecp_util_load_pub(&node_pub, argv[4]); +        printf("ecp_util_load_pub RV:%d\n", rv); + +        rv = ecp_node_init(&node, &node_pub, argv[3]); +        printf("ecp_node_init RV:%d\n", rv);          rv = ecp_conn_open(&conn, &node);          printf("ecp_conn_open RV:%d\n", rv);      }      while (1) sleep(1); -}
\ No newline at end of file +} diff --git a/ecp/test/vid/Makefile b/ecp/test/vid/Makefile deleted file mode 100644 index a9b92eb..0000000 --- a/ecp/test/vid/Makefile +++ /dev/null @@ -1,23 +0,0 @@ -include ../../ecp/Makefile.platform - -LIBVPX_HOME = /opt/my/libvpx -CFLAGS += -D_V4L2_KERNEL_ -I/usr/src/linux-headers-$(uname -r) -I$(LIBVPX_HOME) -I/opt/local/include/SDL2 -I../../ecp -I../../util -Wno-int-to-void-pointer-cast -LDFLAGS += -L$(LIBVPX_HOME) -L/opt/local/lib - -dep=../init_vconn.o ../../ecp/build-posix/*.a ../../util/libecputil.a - -all: cap - -%.o: %.c -	$(CC) $(CFLAGS) -c $< - -cap: cap.o enc.o tools.o server.o -	$(CC) -o $@ $< enc.o tools.o server.o $(dep) -lvpx $(LDFLAGS) - -client: client.o display.o tools.o -	$(CC) -o $@ $< display.o tools.o $(dep) -lvpx -lSDL2 $(LDFLAGS) - -clean: -	rm -f *.o -	rm -f cap client - diff --git a/ecp/test/vid/cap.c b/ecp/test/vid/cap.c deleted file mode 100644 index e37c28f..0000000 --- a/ecp/test/vid/cap.c +++ /dev/null @@ -1,576 +0,0 @@ -/* - *  OpenCV BSD3 License - *  videodev2.h BSD License (dual license) - *  If you raise some license issue here simply don't use it and let us know - *  - *  Copyright (C) 2016 the contributors - * - *  Luiz Vitor Martinez Cardoso - *  Jules Thuillier - *  @lex (avafinger) - * - *  gcc cap.c -o cap $(pkg-config --libs --cflags opencv) -lm - * - *  gcc -I/usr/src/linux-headers-VERSION/ cap.c -o cap $(pkg-config --libs --cflags opencv) -lm -O3 - * -*/ - -#include <stdlib.h> -#include <stdio.h> -#include <errno.h> -#include <fcntl.h> -#include <stdint.h> -#include <unistd.h> -#include <malloc.h> -#include <string.h> -#include <sys/mman.h> -#include <sys/time.h> -#include <sys/ioctl.h> - -/* ----------------------------------------------------------------------------- - * BananaPi M64 / Pine64+ (A64) or if you want to control Exposure,Hflip,Vflip - * ----------------------------------------------------------------------------- - * _V4L2_KERNEL_ should be defined and point to: /usr/src/linux-headers-version - *  - * build with: gcc -I/usr/src/linux-headers-3.10.102/ cap.c -o cap $(pkg-config --libs --cflags opencv) -lm -O3 - * - *  - * ----------------------------------------------------------------------------- - * OrangePi / BananaPi / NanoPi (H3) / BPI-M3 (A83T - ov5640 & ov8865) - * ----------------------------------------------------------------------------- - * _V4L2_KERNEL_ should not be defined unless you want Exposure, Hflip and Vflip - * - * build with: gcc cap.c -o cap $(pkg-config --libs --cflags opencv) -lm - * - *  -*/ -//#define _V4L2_KERNEL_	// BananaPi M64 / Pine64+ only or for setting Exposure,Hflip,Vflip - -#ifdef _V4L2_KERNEL_ -/* --- A64 --- */ -#include <linux/videodev2.h> -#else -/* --- H3 / A83T --- */ -#include "videodev2.h" -#endif - -#ifdef _V4L2_KERNEL_ -#define V4L2_MODE_VIDEO				0x0002  /* video capture */ -#define V4L2_MODE_IMAGE				0x0003  /* image capture */ -#define V4L2_MODE_PREVIEW			0x0004  /* preview capture */ -#endif - -#define N_BUFFERS 4 -#define CAP_OK 0 -#define CAP_ERROR -1 -#define CAP_ERROR_RET(s) { \ -							fprintf(stderr, "v4l2: %s\n", s); \ -							return CAP_ERROR; \ -						 } -#define CAP_CLIP(val, min, max) (((val) > (max)) ? (max) : (((val) < (min)) ? (min) : (val))) - -#define CLEAR(x) memset (&(x), 0, sizeof (x)) -#define ALIGN_4K(x) (((x) + (4095)) & ~(4095)) -#define ALIGN_16B(x) (((x) + (15)) & ~(15)) - -#include "server.h" -#include "enc.h" - -typedef struct { -    void *start; -    size_t length; -} v4l2_buffer_t; - -int width; -int height; -v4l2_buffer_t *buffers = NULL; -int n_buffers = N_BUFFERS; -int sensor_video_mode; -int sensor_exposure; -int sensor_hflip; -int sensor_vflip; - -double get_wall_time() -{ -    struct timeval time; -    if (gettimeofday(&time, NULL)) -        return 0.; -    return (double) time.tv_sec + (double) time.tv_usec * .000001; -} - -int yuv420p_to_bgr(void *in, int length, unsigned char *out) -{ -    uint8_t *yptr, *uptr, *vptr; -    uint32_t x, y, p; - -    if (length < (width * height * 3) / 2) -        return CAP_ERROR; - -    yptr = (uint8_t *) in; -    uptr = yptr + (width * height); -    vptr = uptr + (width * height / 4); -    p = 0; - -    for (y = 0; y < height; y++) { -        for (x = 0; x < width; x++) { -            int r, g, b; -            int y, u, v; - -            y = *(yptr++) << 8; -            u = uptr[p] - 128; -            v = vptr[p] - 128; - -            r = (y + (359 * v)) >> 8; -            g = (y - (88 * u) - (183 * v)) >> 8; -            b = (y + (454 * u)) >> 8; - -            *(out++) += CAP_CLIP(b, 0x00, 0xFF); -            *(out++) += CAP_CLIP(g, 0x00, 0xFF); -            *(out++) += CAP_CLIP(r, 0x00, 0xFF); - -            if (x & 1) -                p++; -        } - -        if (!(y & 1)) -            p -= width / 2; -    } - -    return CAP_ERROR; -} - -static int xioctl(int fd, int request, void *arg) -{ -    int r; -    int tries = 3; - -    do { -        r = ioctl(fd, request, arg); -    } while (--tries > 0 && -1 == r && EINTR == errno); - -    return r; -} - -int v4l2_display_sizes_pix_format(int fd) -{ -    int ret = 0; -    int fsizeind = 0; /*index for supported sizes*/ -    struct v4l2_frmsizeenum fsize; - -    fprintf(stderr, "V4L2 pixel sizes:\n"); - -    CLEAR(fsize); -    fsize.index = 0; -    fsize.pixel_format = V4L2_PIX_FMT_YUV420; - -    while ((ret = xioctl(fd, VIDIOC_ENUM_FRAMESIZES, &fsize)) == 0) { -        fsize.index++; -        if (fsize.type == V4L2_FRMSIZE_TYPE_DISCRETE) { -            fprintf(stderr, "( %u x %u ) Pixels\n", fsize.discrete.width, fsize.discrete.height); -	    fsizeind++; -        } -    } -    return fsizeind; -} - -int v4l2_display_pix_format(int fd) -{ -    struct v4l2_fmtdesc fmt; -    int index; - -    fprintf(stderr, "V4L2 pixel formats:\n"); - -    index = 0; -    CLEAR(fmt); -    fmt.index = index; -    fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; - -    while (ioctl(fd, VIDIOC_ENUM_FMT, &fmt) != -1) { -        fprintf(stderr, "%i: [0x%08X] '%c%c%c%c' (%s)\n", index, fmt.pixelformat, fmt.pixelformat >> 0, fmt.pixelformat >> 8, fmt.pixelformat >> 16, fmt.pixelformat >> 24, fmt.description); - -        memset(&fmt, 0, sizeof(fmt)); -        fmt.index = ++index; -        fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; -    } -    // fprintf(stderr, "\n"); -} - -#ifdef _V4L2_KERNEL_ -int v4l2_set_exposure(int fd, int exposure) -{ -    struct v4l2_queryctrl queryctrl; -    struct v4l2_control control; -    int rc; - -    fprintf(stderr, "set Exposure: %d\n", exposure); -    rc = 0; -    memset(&control, 0, sizeof(control)); -    control.id = V4L2_CID_EXPOSURE; -    rc = xioctl(fd, VIDIOC_G_CTRL, &control); -    fprintf(stderr, "rc: %d - get exposure: %d\n", rc, control.value); -    control.value = exposure; -    rc = xioctl(fd, VIDIOC_S_CTRL, &control); -    fprintf(stderr, "rc: %d - new exposure: %d\n", rc, exposure); -    return rc; -} - -int v4l2_set_hflip(int fd, int hflip) -{ -    struct v4l2_queryctrl queryctrl; -    struct v4l2_control control; -    int rc; - -    fprintf(stderr, "set Hflip: %d\n", hflip); -    rc = 0; -    memset(&control, 0, sizeof(control)); -    control.id = V4L2_CID_HFLIP; -    rc = xioctl(fd, VIDIOC_G_CTRL, &control); -    fprintf(stderr, "rc: %d - get value: %d\n", rc, control.value); -    control.value = hflip; -    rc = xioctl(fd, VIDIOC_S_CTRL, &control); -    fprintf(stderr, "rc: %d - new value: %d\n", rc, control.value); -    return rc; -} - -int v4l2_set_vflip(int fd, int vflip) -{ -    struct v4l2_queryctrl queryctrl; -    struct v4l2_control control; -    int rc; - -    fprintf(stderr, "set Vflip: %d\n", vflip); -    rc = 0; -    memset(&control, 0, sizeof(control)); -    control.id = V4L2_CID_VFLIP; -    rc = xioctl(fd, VIDIOC_G_CTRL, &control); -    fprintf(stderr, "rc: %d - get value: %d\n", rc, control.value); -    control.value = vflip; -    rc = xioctl(fd, VIDIOC_S_CTRL, &control); -    fprintf(stderr, "rc: %d - new value: %d\n", rc, control.value); -    return rc; -} -#endif - -int v4l2_init_camera(int fd) -{ -    uint32_t i; -    uint32_t index; -    struct v4l2_streamparm parms; -    struct v4l2_format fmt; -    struct v4l2_input input; -    struct v4l2_capability caps; - -    CLEAR(fmt); -    CLEAR(input); -    CLEAR(caps); - - -    if (xioctl(fd, VIDIOC_QUERYCAP, &caps) == -1) { -        CAP_ERROR_RET("unable to query capabilities."); -    } - -    if (!(caps.capabilities & V4L2_CAP_VIDEO_CAPTURE)) { -        CAP_ERROR_RET("doesn't support video capturing."); -    } - -    fprintf(stderr, "Driver: \"%s\"\n", caps.driver); -    fprintf(stderr, "Card: \"%s\"\n", caps.card); -    fprintf(stderr, "Bus: \"%s\"\n", caps.bus_info); -    fprintf(stderr, "Version: %d.%d\n", (caps.version >> 16) && 0xff, (caps.version >> 24) && 0xff); -    fprintf(stderr, "Capabilities: %08x\n", caps.capabilities); - -    input.index = 0; -    if (xioctl(fd, VIDIOC_ENUMINPUT, &input) == -1) { -        CAP_ERROR_RET("unable to enumerate input."); -    } - -    fprintf(stderr, "Input: %d\n", input.index); -    if (xioctl(fd, VIDIOC_S_INPUT, &input.index) == -1) { -        CAP_ERROR_RET("unable to set input."); -    } - -    parms.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; -    parms.parm.capture.capturemode = sensor_video_mode ? V4L2_MODE_VIDEO : V4L2_MODE_IMAGE; -    parms.parm.capture.timeperframe.numerator = 1; -    parms.parm.capture.timeperframe.denominator = sensor_video_mode ? 30 : 7; -    if (-1 == xioctl(fd, VIDIOC_S_PARM, &parms)) { -        CAP_ERROR_RET("unable to set stream parm."); -    } - -    v4l2_display_pix_format(fd); -    v4l2_display_sizes_pix_format(fd); -    fprintf(stderr, "\n"); - -    fmt.fmt.pix.width = width; -    fmt.fmt.pix.height = height; -    fmt.fmt.pix.field = V4L2_FIELD_NONE;        // V4L2_FIELD_ANY; -    fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; -    fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV420; -    //fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_RGB24; - -    if (xioctl(fd, VIDIOC_TRY_FMT, &fmt) == -1) { -        CAP_ERROR_RET("failed trying to set pixel format."); -    } - -    if (fmt.fmt.pix.width != width || fmt.fmt.pix.height != height) { -        width = fmt.fmt.pix.width; -        height = fmt.fmt.pix.height; -        fprintf(stderr, "Sensor size adjusted to: %dx%d pixels\n", width, height); -    } else { -        fprintf(stderr, "Sensor size: %dx%d pixels\n", width, height); -    } - -    if (xioctl(fd, VIDIOC_S_FMT, &fmt) == -1) { -        CAP_ERROR_RET("failed to set pixel format."); -    } - -    switch (fmt.fmt.pix.pixelformat) { -    case V4L2_PIX_FMT_RGB24: -            fprintf(stderr, "Pixel Format: V4L2_PIX_FMT_RGB24 [0x%08X]\n",fmt.fmt.pix.pixelformat); -            break; -         -    case V4L2_PIX_FMT_YUV420: -            fprintf(stderr, "Pixel Format: V4L2_PIX_FMT_YUV420 [0x%08X]\n",fmt.fmt.pix.pixelformat); -            break; -         -    } - -    return CAP_OK; -} - -int v4l2_set_mmap(int fd, int *buffers_count) -{ -    int i; -    int nbf; -    enum v4l2_buf_type type; -    struct v4l2_requestbuffers req; -    struct v4l2_buffer buf; - -    CLEAR(req); -    req.count = sensor_video_mode ? n_buffers : 1; -    req.memory = V4L2_MEMORY_MMAP; -    req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; -    if (xioctl(fd, VIDIOC_REQBUFS, &req) == -1) { -        CAP_ERROR_RET("failed requesting buffers."); -    } -    nbf = req.count; -    if (n_buffers != nbf) { -        CAP_ERROR_RET("insufficient buffer memory."); -    } - -    buffers = (v4l2_buffer_t *) calloc(nbf, sizeof(v4l2_buffer_t)); -    if (!buffers) { -        CAP_ERROR_RET("failed to allocated buffers memory."); -    } - -    for (i = 0; i < nbf; i++) { -        CLEAR(buf); -        buf.index = i; -        buf.memory = V4L2_MEMORY_MMAP; -        buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; -        if (xioctl(fd, VIDIOC_QUERYBUF, &buf) == -1) { -            CAP_ERROR_RET("failed to query buffer."); -        } -        buffers[i].length = buf.length; -        buffers[i].start = mmap(NULL, buf.length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, buf.m.offset); -        if (MAP_FAILED == buffers[i].start) { -            CAP_ERROR_RET("failed to mmap buffer."); -        } -    } - -    for (i = 0; i < nbf; i++) { -        CLEAR(buf); -        buf.index = i; -        buf.memory = V4L2_MEMORY_MMAP; -        buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; - -        if (xioctl(fd, VIDIOC_QBUF, &buf) == -1) { -            CAP_ERROR_RET("failed to queue buffer."); -        } -    } - -    type = V4L2_BUF_TYPE_VIDEO_CAPTURE; -    if (xioctl(fd, VIDIOC_STREAMON, &type) == -1) { -        CAP_ERROR_RET("failed to stream on."); -    } - -    *buffers_count = nbf; - -    return CAP_OK; -} - -int v4l2_retrieve_frame(int fd, int buffers_count, vpx_image_t *raw, vpx_codec_ctx_t *codec, int frame_index, int kframe_interval) -{ -    int sz; -    fd_set fds; -    unsigned char *frame_yuv; -    struct timeval tv; -    struct v4l2_buffer buf; -    int rc; -    char err_msg[128]; -    int flags = 0; - -    CLEAR(tv); -    CLEAR(buf); - -    rc = 1; -    while (rc > 0) { -        FD_ZERO(&fds); -        FD_SET(fd, &fds); - -        tv.tv_sec = 2; -        tv.tv_usec = 0; - -        rc = select(fd + 1, &fds, NULL, NULL, &tv); -        if (-1 == rc) { -            if (EINTR == errno) { -                rc = 1;         // try again -                continue; -            } -            CAP_ERROR_RET("failed to select frame."); -        } -        /* we got something */ -        break; -    } -    if (rc <= 0) { -        sprintf(err_msg, "errno: %d - check sensor, something wrong.", errno); -        CAP_ERROR_RET(err_msg); -    } - -    buf.memory = V4L2_MEMORY_MMAP; -    buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; -    if (xioctl(fd, VIDIOC_DQBUF, &buf) == -1) { -        CAP_ERROR_RET("failed to retrieve frame."); -    } - -    // fprintf(stderr, "Length: %d \tBytesused: %d \tAddress: %p\n", buf.length, buf.bytesused, &buffers[buf.index]); - -    sz = ALIGN_16B(width) * height * 3 / 2; -    if (!vpx_img_read(raw, buffers[buf.index].start, sz)) { -        die_codec(NULL, "Failed to read image."); -    } -    if (frame_index % kframe_interval == 0) flags |= VPX_EFLAG_FORCE_KF; -    vpx_encode_frame(codec, raw, frame_index, flags); - -    if (xioctl(fd, VIDIOC_QBUF, &buf) == -1) { -        CAP_ERROR_RET("failed to queue buffer."); -    } - -    return CAP_OK; -} - -int v4l2_close_camera(int fd, int buffers_count) -{ -    int i; -    enum v4l2_buf_type type; - -    type = V4L2_BUF_TYPE_VIDEO_CAPTURE; -    if (xioctl(fd, VIDIOC_STREAMOFF, &type) == -1) { -        CAP_ERROR_RET("failed to stream off."); -    } - -    for (i = 0; i < buffers_count; i++) -        munmap(buffers[i].start, buffers[i].length); - -    close(fd); -} - -int main(int argc, char *argv[]) -{ -    int fd; -    int target_bitrate = 200; -    double after; -    double before; -    double avg, fps; -    int buffers_count; -    int kframe_interval; -    vpx_codec_er_flags_t err_resilient; -    char *address; -    char *my_key; -    char *vcs_key; - -    if (argc != 14) { -        CAP_ERROR_RET("./cap <width> <height> <buffers [4,8]> <video mode [0,1]> <exposure [-4,4]> <hflip [0,1]> <vflip [0,1]> <kframe interval> <bitrate> <err resilient> <address> <my key> <vcs pub key>") -    } -    width = (int) atoi(argv[1]); -    height = (int) atoi(argv[2]); -    n_buffers = (int) atoi(argv[3]); -    if (n_buffers < 4) -        n_buffers = 4; -    if (n_buffers > 8) -        n_buffers = 8;          // enough in VIDEO MODE!!! - -    sensor_video_mode = (int) atoi(argv[4]); -    if (!sensor_video_mode) -        n_buffers = 1; -    sensor_exposure = (int) atoi(argv[5]); -    sensor_hflip = (int) atoi(argv[6]); -    sensor_vflip = (int) atoi(argv[7]); -    kframe_interval = (int) atoi(argv[8]); -    target_bitrate = (int) atoi(argv[9]); -    err_resilient = strtoul(argv[10], NULL, 0); -    address = argv[11]; -    my_key = argv[12]; -    vcs_key = argv[13]; - -    fprintf(stderr, "---- cap parameters -----\nwidth: %d\nheight: %d\nv4l2 buffers: %d\nexposure: %d\nhflip: %d\nvflip: %d\nMode: %s\n", width, height, n_buffers, sensor_exposure, sensor_hflip, sensor_vflip, sensor_video_mode ? "V4L2_MODE_VIDEO" : "V4L2_MODE_IMAGE"); - -    fd = open("/dev/video0", O_RDWR | O_NONBLOCK); -    if (fd == -1) { -        CAP_ERROR_RET("failed to open the camera."); -    } - -    if (v4l2_init_camera(fd) == -1) { -        CAP_ERROR_RET("failed to init camera."); -    } -#ifdef _V4L2_KERNEL_ -    if (sensor_exposure != -999) { -        v4l2_set_exposure(fd, sensor_exposure); -    } -    if (sensor_hflip != -1) { -        v4l2_set_hflip(fd, sensor_hflip); -    } - -    if (sensor_vflip != -1) { -        v4l2_set_vflip(fd, sensor_vflip); -    } -#endif - -    if (v4l2_set_mmap(fd, &buffers_count) == -1) { -        CAP_ERROR_RET("failed to mmap."); -    } - -    int n = 0; -    int _fps = 30; -    const char *codec_arg = "vp9"; -    vpx_codec_ctx_t codec; -    vpx_image_t raw; - -    vpx_open(codec_arg, width, height, _fps, target_bitrate, err_resilient, &codec, &raw); -    init_server(address, my_key, vcs_key); - -    while (1) { -        if (!conn_is_open()) { -            sleep(1); -            continue; -        } -        before = get_wall_time(); -        if (v4l2_retrieve_frame(fd, buffers_count, &raw, &codec, n, kframe_interval)) { -            CAP_ERROR_RET("failed to retrieve frame."); -        } -        after = get_wall_time(); -        fps = 1.0 / (after - before); -        avg += fps; -        n++; -        // fprintf(stderr, "FPS[%d]: %.2f\n", i, fps); -    } - -    vpx_close(&codec, &raw); -    v4l2_close_camera(fd, buffers_count); -     -    if (n) { -        fprintf(stderr, "\n------- Avg FPS: %.2f --------\n\n", (double) (avg / (double) n)); -    } - -    return CAP_OK; -} diff --git a/ecp/test/vid/cap.sh b/ecp/test/vid/cap.sh deleted file mode 100755 index bfd933e..0000000 --- a/ecp/test/vid/cap.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/sh - -./cap 640 480 8 1 -999 -1 -1 25 256 1 0.0.0.0:3000 ../../keys/pine64-home.priv ../../keys/majstor.org.pub diff --git a/ecp/test/vid/client.c b/ecp/test/vid/client.c deleted file mode 100644 index 017967b..0000000 --- a/ecp/test/vid/client.c +++ /dev/null @@ -1,123 +0,0 @@ -#include <stdio.h> -#include <string.h> -#include <unistd.h> -#include <stdlib.h> - -#include "vpx/vpx_decoder.h" -#include "vpx/vp8cx.h" -#include "tools.h" -#include "display.h" - -#include "core.h" -#include "vconn/vconn.h" -#include "util.h" - -#define CTYPE_TEST      0 -#define MTYPE_MSG       8 - -#define FRAG_BUF_SIZE   8192 -#define RBUF_MSG_SIZE   128 - -ECPContext ctx; -ECPSocket sock; -ECPConnHandler handler; - -ECPNode node; -ECPConnection conn; - -ECPVConnection vconn; -ECPNode vconn_node; - -vpx_codec_ctx_t codec; - -ECPRBRecv rbuf_recv; -ECPRBMessage rbuf_r_msg[RBUF_MSG_SIZE]; -ECPFragIter frag_iter; -unsigned char frag_buffer[FRAG_BUF_SIZE]; - -SDLCanvas sdl_canvas; - -ssize_t handle_msg(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *f, ssize_t sz, ECP2Buffer *b) { -    vpx_codec_iter_t iter = NULL; -    vpx_image_t *img = NULL; - -    if (vpx_codec_decode(&codec, f, (unsigned int)sz, NULL, 0)) { -        fprintf(stderr, "\n%lu\n", sz); -        fprintf(stderr, "ERROR!\n"); -        // die_codec(&codec, "Failed to decode frame."); -    } - -    while ((img = vpx_codec_get_frame(&codec, &iter)) != NULL) { -        if (!vpx_img_write(img, sdl_canvas.yuvBuffer, sdl_canvas.yPlaneSz + 2 * sdl_canvas.uvPlaneSz)) die_codec(NULL, "Failed to write image."); -    } -    sdl_display_frame(&sdl_canvas); -     -    return sz; -} - -static void usage(char *arg) { -    fprintf(stderr, "Usage: %s <node.pub> <vcs.pub>\n", arg); -    exit(1); -} - -int main(int argc, char *argv[]) { -    int rv; -     -    if (argc != 3) usage(argv[0]); -     -    rv = ecp_init(&ctx); -    fprintf(stderr, "ecp_init RV:%d\n", rv); - -    if (!rv) rv = ecp_conn_handler_init(&handler); -    if (!rv) { -        handler.msg[MTYPE_MSG] = handle_msg; -        ctx.handler[CTYPE_TEST] = &handler; -    } -     -    if (!rv) rv = ecp_sock_create(&sock, &ctx, NULL); -    fprintf(stderr, "ecp_sock_create RV:%d\n", rv); - -    if (!rv) rv = ecp_sock_open(&sock, NULL); -    fprintf(stderr, "ecp_sock_open RV:%d\n", rv); -     -    if (!rv) rv = ecp_start_receiver(&sock); -    fprintf(stderr, "ecp_start_receiver RV:%d\n", rv); - -    if (!rv) rv = ecp_util_node_load(&ctx, &node, argv[1]); -    fprintf(stderr, "ecp_util_node_load RV:%d\n", rv); - -    if (!rv) rv = ecp_util_node_load(&ctx, &vconn_node, argv[2]); -    printf("ecp_util_node_load RV:%d\n", rv); - -    if (!rv) rv = ecp_conn_create(&conn, &sock, CTYPE_TEST); -    fprintf(stderr, "ecp_conn_create RV:%d\n", rv); - -    if (!rv) rv = ecp_rbuf_create(&conn, NULL, NULL, 0, &rbuf_recv, rbuf_r_msg, RBUF_MSG_SIZE); -    fprintf(stderr, "ecp_rbuf_create RV:%d\n", rv); -     -    if (!rv) { -        ecp_frag_iter_init(&frag_iter, frag_buffer, FRAG_BUF_SIZE); -        rbuf_recv.frag_iter = &frag_iter; -    } - -    const char *codec_arg = "vp9"; -    const VpxInterface *decoder = get_vpx_decoder_by_name(codec_arg); -    if (!decoder) die_codec(NULL, "Unknown input codec."); - -    fprintf(stderr, "Using %s\n", vpx_codec_iface_name(decoder->codec_interface())); - -    if (vpx_codec_dec_init(&codec, decoder->codec_interface(), NULL, 0)) -      die_codec(&codec, "Failed to initialize decoder."); -     -    sdl_open(&sdl_canvas, 640, 480); -     -    // if (!rv) rv = ecp_conn_open(&conn, &node); -    // fprintf(stderr, "ecp_conn_open RV:%d\n", rv); - -    if (!rv) rv = ecp_vconn_open(&conn, &node, &vconn, &vconn_node, 1); -    printf("ecp_vconn_open RV:%d\n", rv); - -    sdl_loop(); -    sdl_close(&sdl_canvas); -    if (vpx_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec"); -}
\ No newline at end of file diff --git a/ecp/test/vid/display.c b/ecp/test/vid/display.c deleted file mode 100644 index d378431..0000000 --- a/ecp/test/vid/display.c +++ /dev/null @@ -1,68 +0,0 @@ -#include <stdio.h> - -#include "display.h" - -void sdl_open(SDLCanvas *o, int img_width, int img_height) { -    // Initialize the SDL -    if (SDL_Init(SDL_INIT_VIDEO) != 0) { -        fprintf(stderr, "SDL_Init() Failed: %s\n", SDL_GetError()); -        exit(1); -    } - -    o->display = SDL_CreateWindow("SDL Tutorial", -                              SDL_WINDOWPOS_UNDEFINED, -                              SDL_WINDOWPOS_UNDEFINED, -                              img_width, img_height, 0); -    if (o->display == NULL) { -        fprintf(stderr, "SDL_SetVideoMode() Failed: %s\n", SDL_GetError()); -        exit(1); -    } - -    o->renderer = SDL_CreateRenderer(o->display, -1, 0); -    if (o->renderer == NULL) { -        fprintf(stderr, "SDL_CreateRenderer() Failed: %s\n", SDL_GetError()); -        exit(1); -    } -     -    o->texture = SDL_CreateTexture(o->renderer, SDL_PIXELFORMAT_YV12, SDL_TEXTUREACCESS_STREAMING, img_width, img_height); -    if (o->texture == NULL) { -        fprintf(stderr, "SDL_CreateTextureFromSurface() Failed: %s\n", SDL_GetError()); -        exit(1); -    } - -    o->yPlaneSz = img_width * img_height; -    o->uvPlaneSz = img_width * img_height / 4; -    o->yuvBuffer = (Uint8*)malloc(o->yPlaneSz + 2 * o->uvPlaneSz); -    o->yPitch = img_width; -    o->uvPitch = img_width / 2; -} - -void sdl_close(SDLCanvas *o) { -    free(o->yuvBuffer); -    SDL_DestroyTexture(o->texture); -    SDL_DestroyRenderer(o->renderer); -    SDL_DestroyWindow(o->display); -    SDL_Quit(); -} - -void sdl_display_frame(SDLCanvas *o) { -    SDL_UpdateYUVTexture(o->texture, NULL, o->yuvBuffer, o->yPitch, o->yuvBuffer + o->yPlaneSz, o->uvPitch,  o->yuvBuffer + o->yPlaneSz + o->uvPlaneSz, o->uvPitch); -    SDL_RenderClear(o->renderer); -    SDL_RenderCopy(o->renderer, o->texture, NULL, NULL); -    SDL_RenderPresent(o->renderer); -} - -void sdl_loop(void) { -    SDL_Event event; -     -    while(1) { -        // Check for messages -        if (SDL_PollEvent(&event)) { -            // Check for the quit message -            if (event.type == SDL_QUIT) { -                // Quit the program -                break; -            } -        } -    } -} diff --git a/ecp/test/vid/display.h b/ecp/test/vid/display.h deleted file mode 100644 index 38e07be..0000000 --- a/ecp/test/vid/display.h +++ /dev/null @@ -1,17 +0,0 @@ -#include "SDL.h" - -typedef struct SDLCanvas { -    SDL_Window *display; -    SDL_Renderer *renderer; -    SDL_Texture *texture; -    Uint8 *yuvBuffer; -    size_t yPlaneSz; -    size_t uvPlaneSz; -    int yPitch; -    int uvPitch; -} SDLCanvas; - -void sdl_open(SDLCanvas *o, int img_width, int img_height); -void sdl_close(SDLCanvas *o); -void sdl_display_frame(SDLCanvas *o); -void sdl_loop(void); diff --git a/ecp/test/vid/enc.c b/ecp/test/vid/enc.c deleted file mode 100644 index 34d0be8..0000000 --- a/ecp/test/vid/enc.c +++ /dev/null @@ -1,168 +0,0 @@ -/* - *  Copyright (c) 2010 The WebM project authors. All Rights Reserved. - * - *  Use of this source code is governed by a BSD-style license - *  that can be found in the LICENSE file in the root of the source - *  tree. An additional intellectual property rights grant can be found - *  in the file PATENTS.  All contributing project authors may - *  be found in the AUTHORS file in the root of the source tree. - */ - -// Simple Encoder -// ============== -// -// This is an example of a simple encoder loop. It takes an input file in -// YV12 format, passes it through the encoder, and writes the compressed -// frames to disk in IVF format. Other decoder examples build upon this -// one. -// -// The details of the IVF format have been elided from this example for -// simplicity of presentation, as IVF files will not generally be used by -// your application. In general, an IVF file consists of a file header, -// followed by a variable number of frames. Each frame consists of a frame -// header followed by a variable length payload. The length of the payload -// is specified in the first four bytes of the frame header. The payload is -// the raw compressed data. -// -// Standard Includes -// ----------------- -// For encoders, you only have to include `vpx_encoder.h` and then any -// header files for the specific codecs you use. In this case, we're using -// vp8. -// -// Getting The Default Configuration -// --------------------------------- -// Encoders have the notion of "usage profiles." For example, an encoder -// may want to publish default configurations for both a video -// conferencing application and a best quality offline encoder. These -// obviously have very different default settings. Consult the -// documentation for your codec to see if it provides any default -// configurations. All codecs provide a default configuration, number 0, -// which is valid for material in the vacinity of QCIF/QVGA. -// -// Updating The Configuration -// --------------------------------- -// Almost all applications will want to update the default configuration -// with settings specific to their usage. Here we set the width and height -// of the video file to that specified on the command line. We also scale -// the default bitrate based on the ratio between the default resolution -// and the resolution specified on the command line. -// -// Initializing The Codec -// ---------------------- -// The encoder is initialized by the following code. -// -// Encoding A Frame -// ---------------- -// The frame is read as a continuous block (size width * height * 3 / 2) -// from the input file. If a frame was read (the input file has not hit -// EOF) then the frame is passed to the encoder. Otherwise, a NULL -// is passed, indicating the End-Of-Stream condition to the encoder. The -// `frame_cnt` is reused as the presentation time stamp (PTS) and each -// frame is shown for one frame-time in duration. The flags parameter is -// unused in this example. The deadline is set to VPX_DL_REALTIME to -// make the example run as quickly as possible. - -// Forced Keyframes -// ---------------- -// Keyframes can be forced by setting the VPX_EFLAG_FORCE_KF bit of the -// flags passed to `vpx_codec_control()`. In this example, we force a -// keyframe every <keyframe-interval> frames. Note, the output stream can -// contain additional keyframes beyond those that have been forced using the -// VPX_EFLAG_FORCE_KF flag because of automatic keyframe placement by the -// encoder. -// -// Processing The Encoded Data -// --------------------------- -// Each packet of type `VPX_CODEC_CX_FRAME_PKT` contains the encoded data -// for this frame. We write a IVF frame header, followed by the raw data. -// -// Cleanup -// ------- -// The `vpx_codec_destroy` call frees any memory allocated by the codec. -// -// Error Handling -// -------------- -// This example does not special case any error return codes. If there was -// an error, a descriptive message is printed and the program exits. With -// few exeptions, vpx_codec functions return an enumerated error status, -// with the value `0` indicating success. -// -// Error Resiliency Features -// ------------------------- -// Error resiliency is controlled by the g_error_resilient member of the -// configuration structure. Use the `decode_with_drops` example to decode with -// frames 5-10 dropped. Compare the output for a file encoded with this example -// versus one encoded with the `simple_encoder` example. - -#include <stdio.h> -#include <stdlib.h> -#include <string.h> - -#include "enc.h" -#include "server.h" - -int vpx_encode_frame(vpx_codec_ctx_t *codec, vpx_image_t *img, int frame_index, int flags) { -  int got_pkts = 0; -  vpx_codec_iter_t iter = NULL; -  const vpx_codec_cx_pkt_t *pkt = NULL; -  const vpx_codec_err_t res = -      vpx_codec_encode(codec, img, frame_index, 1, flags, VPX_DL_REALTIME); -  if (res != VPX_CODEC_OK) die_codec(codec, "Failed to encode frame"); - -  while ((pkt = vpx_codec_get_cx_data(codec, &iter)) != NULL) { -    got_pkts = 1; - -    if (pkt->kind == VPX_CODEC_CX_FRAME_PKT) { -      const int keyframe = (pkt->data.frame.flags & VPX_FRAME_IS_KEY) != 0; -      if (send_frame(pkt->data.frame.buf, pkt->data.frame.sz, pkt->data.frame.pts) < 0) { -        die_codec(codec, "Failed to write compressed frame"); -      } -      fprintf(stderr, keyframe ? "K" : "."); -      fflush(stdout); -    } -  } - -  return got_pkts; -} - - -void vpx_open(const char *codec_arg, int width, int height, int fps, int bitrate, vpx_codec_er_flags_t err_resilient, vpx_codec_ctx_t *codec, vpx_image_t *raw) { -  vpx_codec_enc_cfg_t cfg; -  vpx_codec_err_t res; - -  const VpxInterface *encoder = get_vpx_encoder_by_name(codec_arg); -  if (!encoder) die_codec(NULL, "Unsupported codec."); - -  fprintf(stderr, "Using %s\n", vpx_codec_iface_name(encoder->codec_interface())); - -  if (!vpx_img_alloc(raw, VPX_IMG_FMT_I420, width, height, 1)) -    die_codec(NULL, "Failed to allocate image."); - -  res = vpx_codec_enc_config_default(encoder->codec_interface(), &cfg, 0); -  if (res) die_codec(NULL, "Failed to get default codec config."); - -  cfg.g_w = width; -  cfg.g_h = height; -  cfg.g_timebase.num = 1; -  cfg.g_timebase.den = fps; -  cfg.rc_target_bitrate = bitrate; -  cfg.g_error_resilient = err_resilient; -  cfg.g_lag_in_frames = 0; -  cfg.rc_end_usage = VPX_CBR; - -  if (vpx_codec_enc_init(codec, encoder->codec_interface(), &cfg, 0)) -    die_codec(codec, "Failed to initialize encoder"); - -  if (vpx_codec_control(codec, VP8E_SET_CPUUSED, 8)) -    die_codec(codec, "Failed to initialize cpuused"); -} - -void vpx_close(vpx_codec_ctx_t *codec, vpx_image_t *raw) { -  // Flush encoder. -  while (vpx_encode_frame(codec, NULL, -1, 0)) { -  } - -  vpx_img_free(raw); -  if (vpx_codec_destroy(codec)) die_codec(codec, "Failed to destroy codec."); -} diff --git a/ecp/test/vid/enc.h b/ecp/test/vid/enc.h deleted file mode 100644 index 5acd8ce..0000000 --- a/ecp/test/vid/enc.h +++ /dev/null @@ -1,7 +0,0 @@ -#include "vpx/vpx_encoder.h" -#include "vpx/vp8cx.h" -#include "tools.h" - -int vpx_encode_frame(vpx_codec_ctx_t *codec, vpx_image_t *img, int frame_index, int flags); -void vpx_open(const char *codec_arg, int width, int height, int fps, int bitrate, vpx_codec_er_flags_t err_resilient, vpx_codec_ctx_t *codec, vpx_image_t *raw); -void vpx_close(vpx_codec_ctx_t *codec, vpx_image_t *raw); diff --git a/ecp/test/vid/server.c b/ecp/test/vid/server.c deleted file mode 100644 index 9d43b7a..0000000 --- a/ecp/test/vid/server.c +++ /dev/null @@ -1,76 +0,0 @@ -#include <stdio.h> -#include <string.h> -#include <unistd.h> -#include <stdlib.h> - -#include "server.h" - -static ECPContext ctx; -static ECPSocket sock; -static ECPDHKey key_perma; -static ECPConnHandler handler; - -static ECPConnection *conn_in; -static int is_open = 0; - -#define CTYPE_TEST  0 -#define MTYPE_MSG   8 - -ECPNode node; -ECPConnection conn; - -static ssize_t handle_open(ECPConnection *c, ecp_seq_t sq, unsigned char t, unsigned char *m, ssize_t sz, ECP2Buffer *b) { -    fprintf(stderr, "IS OPEN!\n"); - -    ssize_t rv = ecp_conn_handle_open(c, sq, t, m, sz, b); -    if (rv < 0) return rv; - -    conn_in = c; -    is_open = 1; -     -    return rv; -} - -ssize_t send_frame(unsigned char *buffer, size_t size, ecp_pts_t pts) { -    return ecp_send(conn_in, MTYPE_MSG, buffer, size); -} - -int conn_is_open(void) { -    return is_open; -} - -int init_server(char *address, char *my_key, char *vcs_key) { -    int rv; -     -    rv = ecp_init(&ctx); -    fprintf(stderr, "ecp_init RV:%d\n", rv); -     -    if (!rv) rv = ecp_conn_handler_init(&handler); -    if (!rv) { -        handler.msg[ECP_MTYPE_OPEN] = handle_open; -        ctx.handler[CTYPE_TEST] = &handler; -    } -     -    if (!rv) rv = ecp_util_key_load(&ctx, &key_perma, my_key); -    fprintf(stderr, "ecp_util_key_load RV:%d\n", rv); -     -    if (!rv) rv = ecp_sock_create(&sock, &ctx, &key_perma); -    fprintf(stderr, "ecp_sock_create RV:%d\n", rv); - -    if (!rv) rv = ecp_sock_open(&sock, address); -    fprintf(stderr, "ecp_sock_open RV:%d\n", rv); -     -    if (!rv) rv = ecp_start_receiver(&sock); -    fprintf(stderr, "ecp_start_receiver RV:%d\n", rv); - -    if (!rv) rv = ecp_util_node_load(&ctx, &node, vcs_key); -    printf("ecp_util_node_load RV:%d\n", rv); - -    if (!rv) rv = ecp_conn_create(&conn, &sock, ECP_CTYPE_VLINK); -    printf("ecp_conn_create RV:%d\n", rv); - -    if (!rv) rv = ecp_conn_open(&conn, &node); -    printf("ecp_conn_open RV:%d\n", rv); - -    return rv; -} diff --git a/ecp/test/vid/server.h b/ecp/test/vid/server.h deleted file mode 100644 index 2158b38..0000000 --- a/ecp/test/vid/server.h +++ /dev/null @@ -1,7 +0,0 @@ -#include "core.h" -#include "vconn/vconn.h" -#include "util.h" - -ssize_t send_frame(unsigned char *buffer, size_t size, ecp_pts_t pts); -int conn_is_open(void); -int init_server(char *address, char *my_key, char *vcs_key); diff --git a/ecp/test/vid/tools.c b/ecp/test/vid/tools.c deleted file mode 100644 index 0307ef6..0000000 --- a/ecp/test/vid/tools.c +++ /dev/null @@ -1,165 +0,0 @@ -#include <stdio.h> -#include <stdarg.h> -#include <stdlib.h> -#include <string.h> - -#include "tools.h" - -static const VpxInterface vpx_encoders[] = { -  { "vp8", VP8_FOURCC, &vpx_codec_vp8_cx }, -  { "vp9", VP9_FOURCC, &vpx_codec_vp9_cx }, -}; - -int get_vpx_encoder_count(void) { -  return sizeof(vpx_encoders) / sizeof(vpx_encoders[0]); -} - -const VpxInterface *get_vpx_encoder_by_index(int i) { return &vpx_encoders[i]; } - -const VpxInterface *get_vpx_encoder_by_name(const char *name) { -  int i; - -  for (i = 0; i < get_vpx_encoder_count(); ++i) { -    const VpxInterface *encoder = get_vpx_encoder_by_index(i); -    if (strcmp(encoder->name, name) == 0) return encoder; -  } - -  return NULL; -} - -static const VpxInterface vpx_decoders[] = { -  { "vp8", VP8_FOURCC, &vpx_codec_vp8_dx }, -  { "vp9", VP9_FOURCC, &vpx_codec_vp9_dx }, -}; - -int get_vpx_decoder_count(void) { -  return sizeof(vpx_decoders) / sizeof(vpx_decoders[0]); -} - -const VpxInterface *get_vpx_decoder_by_index(int i) { return &vpx_decoders[i]; } - -const VpxInterface *get_vpx_decoder_by_name(const char *name) { -  int i; - -  for (i = 0; i < get_vpx_decoder_count(); ++i) { -    const VpxInterface *const decoder = get_vpx_decoder_by_index(i); -    if (strcmp(decoder->name, name) == 0) return decoder; -  } - -  return NULL; -} - -void die_codec(vpx_codec_ctx_t *ctx, const char *s) { -  if (ctx) { -    const char *detail = vpx_codec_error_detail(ctx); - -    fprintf(stderr, "%s: %s\n", s, vpx_codec_error(ctx)); -    if (detail) fprintf(stderr, "    %s\n", detail); -  } else { -    fprintf(stderr, "%s", s); -  } -  exit(EXIT_FAILURE); -} - -// TODO(dkovalev): move this function to vpx_image.{c, h}, so it will be part -// of vpx_image_t support -int vpx_img_plane_width(const vpx_image_t *img, int plane) { -  if (plane > 0 && img->x_chroma_shift > 0) -    return (img->d_w + 1) >> img->x_chroma_shift; -  else -    return img->d_w; -} - -int vpx_img_plane_height(const vpx_image_t *img, int plane) { -  if (plane > 0 && img->y_chroma_shift > 0) -    return (img->d_h + 1) >> img->y_chroma_shift; -  else -    return img->d_h; -} - -int vpx_img_read(vpx_image_t *img, void *img_buf, int sz) { -  int plane; -  int off = 0; - -  for (plane = 0; plane < 3; ++plane) { -    unsigned char *buf = img->planes[plane]; -    const int stride = img->stride[plane]; -    const int w = vpx_img_plane_width(img, plane) * -                  ((img->fmt & VPX_IMG_FMT_HIGHBITDEPTH) ? 2 : 1); -    const int h = vpx_img_plane_height(img, plane); -    int y; - -    for (y = 0; y < h; ++y) { -      if (off + w > sz) return 0; -      memcpy(buf, img_buf + off, w); -      off += w; -      buf += stride; -    } -  } - -  return 1; -} - -int vpx_img_write(const vpx_image_t *img, void *img_buf, int sz) { -  int plane; -  int off = 0; - -  for (plane = 0; plane < 3; ++plane) { -    const unsigned char *buf = img->planes[plane]; -    const int stride = img->stride[plane]; -    const int w = vpx_img_plane_width(img, plane) * -                  ((img->fmt & VPX_IMG_FMT_HIGHBITDEPTH) ? 2 : 1); -    const int h = vpx_img_plane_height(img, plane); -    int y; - -    for (y = 0; y < h; ++y) { -      if (off + w > sz) return 0; -      memcpy(img_buf + off, buf, w); -      off += w; -      buf += stride; -    } -  } -   -  return 1; -} - -int vpx_img_read_f(vpx_image_t *img, FILE *file) { -  int plane; - -  for (plane = 0; plane < 3; ++plane) { -    unsigned char *buf = img->planes[plane]; -    const int stride = img->stride[plane]; -    const int w = vpx_img_plane_width(img, plane) * -                  ((img->fmt & VPX_IMG_FMT_HIGHBITDEPTH) ? 2 : 1); -    const int h = vpx_img_plane_height(img, plane); -    int y; - -    for (y = 0; y < h; ++y) { -      if (fread(buf, 1, w, file) != (size_t)w) return 0; -      buf += stride; -    } -  } - -  return 1; -} - -int vpx_img_write_f(const vpx_image_t *img, FILE *file) { -  int plane; - -  for (plane = 0; plane < 3; ++plane) { -    const unsigned char *buf = img->planes[plane]; -    const int stride = img->stride[plane]; -    const int w = vpx_img_plane_width(img, plane) * -                  ((img->fmt & VPX_IMG_FMT_HIGHBITDEPTH) ? 2 : 1); -    const int h = vpx_img_plane_height(img, plane); -    int y; - -    for (y = 0; y < h; ++y) { -      if (fwrite(buf, 1, w, file) != (size_t)w) return 0; -      buf += stride; -    } -  } -   -  return 1; -} - diff --git a/ecp/test/vid/tools.h b/ecp/test/vid/tools.h deleted file mode 100644 index 645d5ba..0000000 --- a/ecp/test/vid/tools.h +++ /dev/null @@ -1,37 +0,0 @@ -#ifndef TOOLS_COMMON_H_ -#define TOOLS_COMMON_H_ - -#include "vpx/vpx_codec.h" -#include "vpx/vpx_image.h" -#include "vpx/vp8cx.h" -#include "vpx/vp8dx.h" - -#define VP8_FOURCC 0x30385056 -#define VP9_FOURCC 0x30395056 - -struct VpxRational { -  int numerator; -  int denominator; -}; - -typedef struct VpxInterface { -  const char *const name; -  const uint32_t fourcc; -  vpx_codec_iface_t *(*const codec_interface)(); -} VpxInterface; - -const VpxInterface *get_vpx_encoder_by_index(int i); -const VpxInterface *get_vpx_encoder_by_name(const char *name); - -const VpxInterface *get_vpx_decoder_by_index(int i); -const VpxInterface *get_vpx_decoder_by_name(const char *name); - -void die_codec(vpx_codec_ctx_t *ctx, const char *s); - -int vpx_img_read(vpx_image_t *img, void *img_buf, int sz); -int vpx_img_write(const vpx_image_t *img, void *img_buf, int sz); - -int vpx_img_read_f(vpx_image_t *img, FILE *file); -int vpx_img_write_f(const vpx_image_t *img, FILE *file); - -#endif
\ No newline at end of file diff --git a/ecp/test/voip.c b/ecp/test/voip.c deleted file mode 100644 index b0a9207..0000000 --- a/ecp/test/voip.c +++ /dev/null @@ -1,196 +0,0 @@ -#include <stdio.h> -#include <string.h> -#include <unistd.h> -#include <stdlib.h> -#include <alsa/asoundlib.h> - -#include <opus.h> - -#include "core.h" -#include "util.h" - -ECPContext ctx_c; -ECPSocket sock_c; -ECPConnHandler handler_c; - -ECPNode node; -ECPConnection conn; -int open_done = 0; - -snd_pcm_t *handle_plb; -snd_pcm_t *handle_cpt; -OpusEncoder *opus_enc; -OpusDecoder *opus_dec; -// 2.5, 5, 10, 20, 40 or 60 ms -snd_pcm_uframes_t alsa_frames = 160; -unsigned char *alsa_out_buf = NULL; -unsigned char *alsa_in_buf = NULL; - -#define CTYPE_TEST  0 -#define MTYPE_MSG   8 - -int a_open(char *dev_name, snd_pcm_t **handle, snd_pcm_hw_params_t **hw_params, snd_pcm_stream_t stream, snd_pcm_format_t format, unsigned int *nchannels, unsigned int *sample_rate, snd_pcm_uframes_t *frames, size_t *buf_size) { -	int bits, err = 0; -	unsigned int fragments = 2; -	unsigned int frame_size; - -	err = snd_pcm_open(handle, dev_name, stream, 0); -	if (!err) err = snd_pcm_hw_params_malloc(hw_params); -	if (!err) err = snd_pcm_hw_params_any(*handle, *hw_params); -	if (!err) err = snd_pcm_hw_params_set_access(*handle, *hw_params, SND_PCM_ACCESS_RW_INTERLEAVED); -	if (!err) err = snd_pcm_hw_params_set_format(*handle, *hw_params, format); -	if (!err) err = snd_pcm_hw_params_set_rate_near(*handle, *hw_params, sample_rate, 0); -	if (!err) err = snd_pcm_hw_params_set_channels_near(*handle, *hw_params, nchannels); -	if (!err) err = snd_pcm_hw_params_set_periods_near(*handle, *hw_params, &fragments, 0); -	if (!err) err = snd_pcm_hw_params_set_period_size_near(*handle, *hw_params, frames, 0); -	if (!err) err = snd_pcm_hw_params(*handle, *hw_params); -	if (err) return err; -	 -	bits = snd_pcm_hw_params_get_sbits(*hw_params); -	if (bits < 0) return bits; -	 -	frame_size = *nchannels * (bits / 8); -	*buf_size = frame_size * *frames; - -	return ECP_OK; -} - -int a_prepare(snd_pcm_t *handle, snd_pcm_hw_params_t *hw_params, unsigned char *buf, snd_pcm_uframes_t frames) { -	snd_pcm_drop(handle); -	snd_pcm_prepare(handle); - -	if (snd_pcm_stream(handle) == SND_PCM_STREAM_PLAYBACK) { -		int i, err; -		unsigned int fragments; - -		err = snd_pcm_hw_params_get_periods(hw_params, &fragments, NULL); -		if (err) return err; - -		for (i=0; i<fragments; i++) snd_pcm_writei(handle, buf, frames); -	} -	return ECP_OK; -} - -opus_int32 a_read(snd_pcm_t *handle, unsigned char *buf, snd_pcm_uframes_t frames, OpusEncoder *enc, unsigned char *opus_buf, opus_int32 opus_size) { -	snd_pcm_sframes_t frames_in; - -	while ((frames_in = snd_pcm_readi(handle, buf, frames)) < 0) { -		if (frames_in == -EAGAIN) -			continue; -		snd_pcm_prepare(handle); -	} -	return opus_encode(enc, (opus_int16 *)buf, frames_in, opus_buf, opus_size); -} - -snd_pcm_sframes_t a_write(OpusDecoder *dec, unsigned char *opus_buf, opus_int32 opus_size, snd_pcm_t *handle, unsigned char *buf, snd_pcm_uframes_t frames) { -	snd_pcm_sframes_t frames_in, frames_out; - -	frames_in = opus_decode(dec, opus_buf, opus_size, (opus_int16 *)buf, frames, 0);	 -	while ((frames_out = snd_pcm_writei(handle, buf, frames_in)) < 0) { -		if (frames_out == -EAGAIN) -			continue; -		snd_pcm_prepare(handle); -	} -	return frames_out; -} - -int a_init(void) { -	char *dev_name  = "hw:1,0"; -	unsigned int nchannels = 1; -	unsigned int sample_rate = 16000; - -	snd_pcm_hw_params_t *hw_params_plb; -	snd_pcm_hw_params_t *hw_params_cpt; -	size_t buf_size; - -	a_open(dev_name, &handle_plb, &hw_params_plb, SND_PCM_STREAM_PLAYBACK, SND_PCM_FORMAT_S16_LE, &nchannels, &sample_rate, &alsa_frames, &buf_size); -	alsa_out_buf = malloc(buf_size); -	memset(alsa_out_buf, 0, buf_size); -	a_prepare(handle_plb, hw_params_plb, alsa_out_buf, alsa_frames); - -	a_open(dev_name, &handle_cpt, &hw_params_cpt, SND_PCM_STREAM_CAPTURE, SND_PCM_FORMAT_S16_LE, &nchannels, &sample_rate, &alsa_frames, &buf_size); -	alsa_in_buf = malloc(buf_size); -	memset(alsa_in_buf, 0, buf_size); -	a_prepare(handle_cpt, hw_params_cpt, alsa_in_buf, alsa_frames); - -	int size; -	size = opus_encoder_get_size(nchannels); -	opus_enc = malloc(size); -	opus_encoder_init(opus_enc, sample_rate, nchannels, OPUS_APPLICATION_VOIP); - -	size = opus_decoder_get_size(nchannels); -	opus_dec = malloc(size); -	opus_decoder_init(opus_dec, sample_rate, nchannels); -     -    return ECP_OK; -} - -ssize_t handle_open_c(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) { -    uint32_t seq = 0; -     -    ecp_conn_handle_open(conn, sq, t, p, s, b); -    if (s < 0) { -        printf("OPEN ERR:%ld\n", s); -        return s; -    } -     -	a_init(); -	open_done = 1; -    return s; -} - -ssize_t handle_msg_c(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) { -	a_write(opus_dec, p, s, handle_plb, alsa_out_buf, alsa_frames); -    return s; -} - - -static void usage(char *arg) { -    fprintf(stderr, "Usage: %s <node.pub>\n", arg); -    exit(1); -} - -int main(int argc, char *argv[]) { -    int rv; -     -    if (argc != 2) usage(argv[0]); -     -    rv = ecp_init(&ctx_c); -    printf("ecp_init RV:%d\n", rv); - -    rv = ecp_conn_handler_init(&handler_c); -    handler_c.msg[ECP_MTYPE_OPEN] = handle_open_c; -    handler_c.msg[MTYPE_MSG] = handle_msg_c; -    ctx_c.handler[CTYPE_TEST] = &handler_c; -     -    rv = ecp_sock_init(&sock_c, &ctx_c, NULL); -    printf("ecp_sock_init RV:%d\n", rv); - -    rv = ecp_sock_open(&sock_c, NULL); -    printf("ecp_sock_open RV:%d\n", rv); -     -    rv = ecp_start_receiver(&sock_c); -    printf("ecp_start_receiver RV:%d\n", rv); - -    rv = ecp_util_node_load(&ctx_c, &node, argv[1]); -    printf("ecp_util_node_load RV:%d\n", rv); - -    rv = ecp_conn_init(&conn, &sock_c, CTYPE_TEST); -    printf("ecp_conn_init RV:%d\n", rv); - -    rv = ecp_conn_open(&conn, &node); -    printf("ecp_conn_open RV:%d\n", rv); - -    while(!open_done) sleep(1); - -	unsigned char payload[ECP_MAX_PLD]; -	unsigned char *opus_buf; - -	while(1) { -		ecp_pld_set_type(payload, MTYPE_MSG); -		opus_buf = ecp_pld_get_buf(payload, 0); -		opus_int32 len = a_read(handle_cpt, alsa_in_buf, alsa_frames, opus_enc, opus_buf, ECP_MAX_MSG); -		if (len < 0) continue; -	    ssize_t _rv = ecp_pld_send(&conn, payload, len); -	} -}
\ No newline at end of file diff --git a/ecp/util/mknode.c b/ecp/util/mknode.c index 885a4c0..764ac18 100644 --- a/ecp/util/mknode.c +++ b/ecp/util/mknode.c @@ -4,7 +4,8 @@  #include <stdio.h>  #include <string.h> -#include "core.h" +#include <core.h> +  #include "util.h"  #define FN_LEN  256 @@ -18,12 +19,16 @@ static void usage(char *arg) {  }  int main(int argc, char *argv[]) { +    char *addr;      ECPDHKey key;      ECPNode node;      int rv;      if ((argc < 2) || (argc > 3)) usage(argv[0]); +    addr = NULL; +    if (argc == 3) addr = argv[2]; +      if (strlen(argv[1]) > FN_LEN - 6) usage(argv[0]);      strcpy(fn_node, argv[1]);      strcpy(fn_key, argv[1]); @@ -33,14 +38,19 @@ int main(int argc, char *argv[]) {      rv = ecp_dhkey_gen(&key);      if (rv) goto err; -    rv = ecp_node_init(&node, &key.public, (argc == 3) ? argv[2] : NULL); +    rv = ecp_node_init(&node, &key.public, addr);      if (rv) goto err; -    rv = ecp_util_key_save(&key, fn_key); +    rv = ecp_util_save_key(&key.public, &key.private, fn_key);      if (rv) goto err; -    rv = ecp_util_node_save(&node, fn_node); -    if (rv) goto err; +    if (addr) { +        rv = ecp_util_save_node(&node, fn_node); +        if (rv) goto err; +    } else { +        rv = ecp_util_save_pub(&key.public, fn_node); +        if (rv) goto err; +    }      return 0; diff --git a/ecp/util/util.c b/ecp/util/util.c index ee51c73..2b67127 100644 --- a/ecp/util/util.c +++ b/ecp/util/util.c @@ -3,24 +3,22 @@  #include <unistd.h>  #include <sys/stat.h> -#include "core.h" -#include "cr.h" +#include <core.h> +  #include "util.h" -int ecp_util_key_save(ECPDHKey *key, char *filename) { +int ecp_util_load_key(ecp_ecdh_public_t *public, ecp_ecdh_private_t *private, char *filename) {      int fd;      ssize_t rv; -    if (!key->valid) return ECP_ERR; - -    if ((fd = open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) < 0) return ECP_ERR; -    rv = write(fd, &key->public, sizeof(key->public)); -    if (rv != sizeof(key->public)) { +    if ((fd = open(filename, O_RDONLY)) < 0) return ECP_ERR; +    rv = read(fd, public, sizeof(ecp_ecdh_public_t)); +    if (rv != sizeof(ecp_ecdh_public_t)) {          close(fd);          return ECP_ERR;      } -    rv = write(fd, &key->private, sizeof(key->private)); -    if (rv != sizeof(key->private)) { +    rv = read(fd, private, sizeof(ecp_ecdh_private_t)); +    if (rv != sizeof(ecp_ecdh_private_t)) {          close(fd);          return ECP_ERR;      } @@ -28,41 +26,46 @@ int ecp_util_key_save(ECPDHKey *key, char *filename) {      return ECP_OK;  } -int ecp_util_key_load(ECPDHKey *key, char *filename) { +int ecp_util_save_key(ecp_ecdh_public_t *public, ecp_ecdh_private_t *private, char *filename) {      int fd;      ssize_t rv; -    if ((fd = open(filename, O_RDONLY)) < 0) return ECP_ERR; -    rv = read(fd, &key->public, sizeof(key->public)); -    if (rv != sizeof(key->public)) { +    if ((fd = open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) < 0) return ECP_ERR; +    rv = write(fd, public, sizeof(ecp_ecdh_public_t)); +    if (rv != sizeof(ecp_ecdh_public_t)) {          close(fd);          return ECP_ERR;      } -    rv = read(fd, &key->private, sizeof(key->private)); -    if (rv != sizeof(key->private)) { +    rv = write(fd, private, sizeof(ecp_ecdh_private_t)); +    if (rv != sizeof(ecp_ecdh_private_t)) {          close(fd);          return ECP_ERR;      }      close(fd); - -    key->valid = 1;      return ECP_OK;  } -int ecp_util_node_save(ECPNode *node, char *filename) { +int ecp_util_load_pub(ecp_ecdh_public_t *public, char *filename) {      int fd;      ssize_t rv; -    if (!node->key_perma.valid) return ECP_ERR; - -    if ((fd = open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) < 0) return ECP_ERR; -    rv = write(fd, &node->key_perma.public, sizeof(node->key_perma.public)); -    if (rv != sizeof(node->key_perma.public)) { +    if ((fd = open(filename, O_RDONLY)) < 0) return ECP_ERR; +    rv = read(fd, public, sizeof(ecp_ecdh_public_t)); +    if (rv != sizeof(ecp_ecdh_public_t)) {          close(fd);          return ECP_ERR;      } -    rv = write(fd, &node->addr, sizeof(node->addr)); -    if (rv != sizeof(node->addr)) { +    close(fd); +    return ECP_OK; +} + +int ecp_util_save_pub(ecp_ecdh_public_t *public, char *filename) { +    int fd; +    ssize_t rv; + +    if ((fd = open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) < 0) return ECP_ERR; +    rv = write(fd, public, sizeof(ecp_ecdh_public_t)); +    if (rv != sizeof(ecp_ecdh_public_t)) {          close(fd);          return ECP_ERR;      } @@ -70,7 +73,7 @@ int ecp_util_node_save(ECPNode *node, char *filename) {      return ECP_OK;  } -int ecp_util_node_load(ECPNode *node, char *filename) { +int ecp_util_load_node(ECPNode *node, char *filename) {      int fd;      ssize_t rv; @@ -89,4 +92,25 @@ int ecp_util_node_load(ECPNode *node, char *filename) {      node->key_perma.valid = 1;      return ECP_OK; -}
\ No newline at end of file +} + +int ecp_util_save_node(ECPNode *node, char *filename) { +    int fd; +    ssize_t rv; + +    if (!node->key_perma.valid) return ECP_ERR; + +    if ((fd = open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) < 0) return ECP_ERR; +    rv = write(fd, &node->key_perma.public, sizeof(node->key_perma.public)); +    if (rv != sizeof(node->key_perma.public)) { +        close(fd); +        return ECP_ERR; +    } +    rv = write(fd, &node->addr, sizeof(node->addr)); +    if (rv != sizeof(node->addr)) { +        close(fd); +        return ECP_ERR; +    } +    close(fd); +    return ECP_OK; +} diff --git a/ecp/util/util.h b/ecp/util/util.h index 631a808..83c4c14 100644 --- a/ecp/util/util.h +++ b/ecp/util/util.h @@ -1,5 +1,8 @@ -int ecp_util_key_save(ECPDHKey *key, char *filename); -int ecp_util_key_load(ECPDHKey *key, char *filename); +int ecp_util_load_key(ecp_ecdh_public_t *public, ecp_ecdh_private_t *private, char *filename); +int ecp_util_save_key(ecp_ecdh_public_t *public, ecp_ecdh_private_t *private, char *filename); -int ecp_util_node_save(ECPNode *node, char *filename); -int ecp_util_node_load(ECPNode *node, char *filename);
\ No newline at end of file +int ecp_util_load_pub(ecp_ecdh_public_t *public, char *filename); +int ecp_util_save_pub(ecp_ecdh_public_t *public, char *filename); + +int ecp_util_load_node(ECPNode *node, char *filename); +int ecp_util_save_node(ECPNode *node, char *filename);  | 
