diff options
author | Uros Majstorovic <majstor@majstor.org> | 2024-05-07 18:55:09 +0200 |
---|---|---|
committer | Uros Majstorovic <majstor@majstor.org> | 2024-05-07 18:55:09 +0200 |
commit | 34eca987e2b3f8c08cf386a644beb7465324f638 (patch) | |
tree | ba285f895966946ded0741f116a16652f97ea244 | |
parent | a538ccbb5102a8c5b7bb03004c3327bf543c7e16 (diff) |
better error handling
-rw-r--r-- | ecp/src/ecp/core.c | 18 | ||||
-rw-r--r-- | ecp/src/ecp/core.h | 12 |
2 files changed, 16 insertions, 14 deletions
diff --git a/ecp/src/ecp/core.c b/ecp/src/ecp/core.c index 7d61def..03fa815 100644 --- a/ecp/src/ecp/core.c +++ b/ecp/src/ecp/core.c @@ -1699,7 +1699,7 @@ ssize_t ecp_handle_init_req(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_ if (msg_size < ECP_SIZE_ZPAD_PLD) return ECP_ERR_SIZE; for (i=0; i<ECP_SIZE_ZPAD_PLD; i++) { - if (msg[i] != 0) return ECP_ERR; + if (msg[i] != 0) return ECP_ERR_ZPAD; } memcpy(_public_buf, public_buf, sizeof(_public_buf)); @@ -2211,7 +2211,7 @@ ssize_t ecp_unpack(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t *addr, ssize_t rv; int _rv = ECP_OK; - if (pkt_size < ECP_MIN_PKT) return ECP_ERR_SIZE; + if (pkt_size < ECP_MIN_PKT) return ECP_ERR_PKT; *_conn = NULL; *_payload = NULL; @@ -2258,7 +2258,7 @@ ssize_t ecp_unpack(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t *addr, packet += ECP_SIZE_PKT_HDR_IREP; pkt_size -= ECP_SIZE_PKT_HDR_IREP; } else { - _rv = ECP_ERR; + _rv = ECP_ERR_PKT; } if (!_rv) _rv = conn_shkey_get(conn, s_idx, c_idx, &shkey); if (!_rv) conn->refcount++; @@ -2278,7 +2278,7 @@ ssize_t ecp_unpack(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t *addr, unsigned char *zpad_buf; int i; - if (pkt_size < (ECP_MIN_PKT+ECP_SIZE_ZPAD_HDR+ECP_SIZE_ZPAD_PLD)) return ECP_ERR_SIZE; + if (pkt_size < (ECP_MIN_PKT+ECP_SIZE_ZPAD_HDR+ECP_SIZE_ZPAD_PLD)) return ECP_ERR_PKT; zpad_buf = public_buf+ECP_SIZE_ECDH_PUB; nonce_buf = zpad_buf+ECP_SIZE_ZPAD_HDR; @@ -2286,16 +2286,18 @@ ssize_t ecp_unpack(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t *addr, pkt_size -= ECP_SIZE_PKT_HDR_IREQ; for (i=0; i<ECP_SIZE_ZPAD_HDR; i++) { - if (zpad_buf[i] != 0) return ECP_ERR; + if (zpad_buf[i] != 0) return ECP_ERR_ZPAD; } _rv = ecp_sock_dhkey_get(sock, s_idx, &key); if (_rv) return _rv; ecp_ecdh_shkey(&shkey, (ecp_ecdh_public_t *)public_buf, &key.private); - } else if (pkt_size >= (ECP_MIN_PKT+ECP_SIZE_COOKIE)) { + } else { /* open request message */ unsigned char *cookie_buf; + if (pkt_size < (ECP_MIN_PKT+ECP_SIZE_COOKIE)) return ECP_ERR_PKT; + cookie_buf = public_buf+ECP_SIZE_ECDH_PUB; nonce_buf = cookie_buf+ECP_SIZE_COOKIE; packet += ECP_SIZE_PKT_HDR_OREQ; @@ -2308,11 +2310,9 @@ ssize_t ecp_unpack(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t *addr, if (_rv) return _rv; ecp_ecdh_shkey(&shkey, (ecp_ecdh_public_t *)public_buf, &key.private); - } else { - return ECP_ERR; } } else { - return ECP_ERR; + return ECP_ERR_PKT; } ecp_buf2nonce(&nonce_pkt, nonce_buf); diff --git a/ecp/src/ecp/core.h b/ecp/src/ecp/core.h index 5d8cd47..8b76895 100644 --- a/ecp/src/ecp/core.h +++ b/ecp/src/ecp/core.h @@ -21,13 +21,15 @@ #define ECP_ERR_MTYPE -9 #define ECP_ERR_CTYPE -10 #define ECP_ERR_HANDLER -11 -#define ECP_ERR_COOKIE -12 +#define ECP_ERR_PKT -12 +#define ECP_ERR_ZPAD -13 +#define ECP_ERR_COOKIE -14 -#define ECP_ERR_NET_ADDR -13 -#define ECP_ERR_MAX_PARENT -14 -#define ECP_ERR_NEXT -15 +#define ECP_ERR_NET_ADDR -20 +#define ECP_ERR_MAX_PARENT -21 +#define ECP_ERR_NEXT -22 -#define ECP_ERR_ECDH_IDX -22 +#define ECP_ERR_ECDH_IDX -25 #define ECP_ERR_ENCRYPT -26 #define ECP_ERR_DECRYPT -27 #define ECP_ERR_SIGN -28 |