From 710e9e8961cf5039b425e66d2042942b7e4af0c8 Mon Sep 17 00:00:00 2001 From: Determinant Date: Fri, 28 Jun 2019 15:37:22 -0400 Subject: clean up code --- include/salticidae/netaddr.h | 6 +++--- include/salticidae/network.h | 3 ++- include/salticidae/stream.h | 2 ++ src/netaddr.cpp | 17 ++++++++++++++--- src/network.cpp | 28 ++++++++++++++++------------ src/stream.cpp | 16 ++++++++++++++++ test/test_msgnet.cpp | 4 ++-- test/test_msgnet_c.c | 4 ++-- test/test_msgnet_tls.cpp | 4 ++-- 9 files changed, 59 insertions(+), 25 deletions(-) diff --git a/include/salticidae/netaddr.h b/include/salticidae/netaddr.h index e4e8c6c..7d04d43 100644 --- a/include/salticidae/netaddr.h +++ b/include/salticidae/netaddr.h @@ -138,9 +138,9 @@ extern "C" { netaddr_t *netaddr_new(); void netaddr_free(const netaddr_t *self); -netaddr_t *netaddr_new_from_ip_port(uint32_t ip, uint16_t port); -netaddr_t *netaddr_new_from_sip_port(const char *ip, uint16_t port); -netaddr_t *netaddr_new_from_sipport(const char *ip_port_addr); +netaddr_t *netaddr_new_from_ip_port(uint32_t ip, uint16_t port, SalticidaeCError *err); +netaddr_t *netaddr_new_from_sip_port(const char *ip, uint16_t port, SalticidaeCError *err); +netaddr_t *netaddr_new_from_sipport(const char *ip_port_addr, SalticidaeCError *err); netaddr_t *netaddr_copy(const netaddr_t *self); bool netaddr_is_eq(const netaddr_t *a, const netaddr_t *b); bool netaddr_is_null(const netaddr_t *self); diff --git a/include/salticidae/network.h b/include/salticidae/network.h index 092c969..f8b3353 100644 --- a/include/salticidae/network.h +++ b/include/salticidae/network.h @@ -1186,7 +1186,8 @@ typedef enum msgnetwork_conn_mode_t { } msgnetwork_conn_mode_t; typedef enum peernetwork_id_mode_t { - ID_MODE_ADDR_BASED + ID_MODE_ADDR_BASED, + ID_MODE_CERT_BASED } peernetwork_id_mode_t; #ifdef __cplusplus diff --git a/include/salticidae/stream.h b/include/salticidae/stream.h index 2efc532..2f41782 100644 --- a/include/salticidae/stream.h +++ b/include/salticidae/stream.h @@ -546,7 +546,9 @@ int64_t datastream_get_i64(datastream_t *self, bool *succ); const uint8_t *datastream_get_data_inplace(datastream_t *self, size_t len); uint256_t *datastream_get_hash(const datastream_t *self); bytearray_t *bytearray_new_moved_from_datastream(datastream_t *_moved_self); +bytearray_t *bytearray_new_copied_from_datastream(datastream_t *src); bytearray_t *bytearray_new_from_hex(const char *hex_str); +bytearray_t *bytearray_new_from_bytes(const uint8_t *arr, size_t len); char *datastream_get_hex(datastream_t *self); diff --git a/src/netaddr.cpp b/src/netaddr.cpp index cb8a3d2..736d606 100644 --- a/src/netaddr.cpp +++ b/src/netaddr.cpp @@ -2,22 +2,33 @@ #ifdef SALTICIDAE_CBINDINGS #include "salticidae/netaddr.h" +using namespace salticidae; + extern "C" { netaddr_t *netaddr_new() { return new netaddr_t(); } void netaddr_free(const netaddr_t *self) { delete self; } -netaddr_t *netaddr_new_from_ip_port(uint32_t ip, uint16_t port) { +netaddr_t *netaddr_new_from_ip_port(uint32_t ip, uint16_t port, SalticidaeCError *cerror) { + SALTICIDAE_CERROR_TRY(cerror) return new netaddr_t(ip, port); + SALTICIDAE_CERROR_CATCH(cerror) + return nullptr; } -netaddr_t *netaddr_new_from_sip_port(const char *ip, uint16_t port) { +netaddr_t *netaddr_new_from_sip_port(const char *ip, uint16_t port, SalticidaeCError *cerror) { + SALTICIDAE_CERROR_TRY(cerror) return new netaddr_t(ip, port); + SALTICIDAE_CERROR_CATCH(cerror) + return nullptr; } -netaddr_t *netaddr_new_from_sipport(const char *ip_port_addr) { +netaddr_t *netaddr_new_from_sipport(const char *ip_port_addr, SalticidaeCError *cerror) { + SALTICIDAE_CERROR_TRY(cerror) return new netaddr_t(ip_port_addr); + SALTICIDAE_CERROR_CATCH(cerror) + return nullptr; } netaddr_t *netaddr_copy(const netaddr_t *self) { diff --git a/src/network.cpp b/src/network.cpp index a02ead1..2dc7d1f 100644 --- a/src/network.cpp +++ b/src/network.cpp @@ -190,19 +190,23 @@ peernetwork_t *peernetwork_new(const eventcontext_t *ec, const peernetwork_confi void peernetwork_free(const peernetwork_t *self) { delete self; } -void peernetwork_add_peer(peernetwork_t *self, const netaddr_t *paddr) { - self->add_peer(*paddr); +void peernetwork_add_peer(peernetwork_t *self, const netaddr_t *addr) { + self->add_peer(*addr); } -bool peernetwork_has_peer(const peernetwork_t *self, const netaddr_t *paddr) { - return self->has_peer(*paddr); +void peernetwork_del_peer(peernetwork_t *self, const netaddr_t *addr) { + self->del_peer(*addr); +} + +bool peernetwork_has_peer(const peernetwork_t *self, const netaddr_t *addr) { + return self->has_peer(*addr); } const peernetwork_conn_t *peernetwork_get_peer_conn(const peernetwork_t *self, - const netaddr_t *paddr, + const netaddr_t *addr, SalticidaeCError *cerror) { SALTICIDAE_CERROR_TRY(cerror) - return new peernetwork_conn_t(self->get_peer_conn(*paddr)); + return new peernetwork_conn_t(self->get_peer_conn(*addr)); SALTICIDAE_CERROR_CATCH(cerror) return nullptr; } @@ -227,18 +231,18 @@ peernetwork_conn_t *peernetwork_conn_copy(const peernetwork_conn_t *self) { void peernetwork_conn_free(const peernetwork_conn_t *self) { delete self; } -void peernetwork_send_msg(peernetwork_t *self, const msg_t * msg, const netaddr_t *paddr) { - self->_send_msg(*msg, *paddr); +void peernetwork_send_msg(peernetwork_t *self, const msg_t * msg, const netaddr_t *addr) { + self->_send_msg(*msg, *addr); } void peernetwork_send_msg_deferred_by_move(peernetwork_t *self, - msg_t *_moved_msg, const netaddr_t *paddr) { - self->_send_msg_deferred(std::move(*_moved_msg), *paddr); + msg_t *_moved_msg, const netaddr_t *addr) { + self->_send_msg_deferred(std::move(*_moved_msg), *addr); } void peernetwork_multicast_msg_by_move(peernetwork_t *self, - msg_t *_moved_msg, const netaddr_array_t *paddrs) { - self->_multicast_msg(std::move(*_moved_msg), *paddrs); + msg_t *_moved_msg, const netaddr_array_t *addrs) { + self->_multicast_msg(std::move(*_moved_msg), *addrs); } void peernetwork_listen(peernetwork_t *self, const netaddr_t *listen_addr, SalticidaeCError *cerror) { diff --git a/src/stream.cpp b/src/stream.cpp index f5d4ac9..6b208aa 100644 --- a/src/stream.cpp +++ b/src/stream.cpp @@ -133,6 +133,14 @@ bytearray_t *bytearray_new_moved_from_datastream(datastream_t *_moved_src) { } } +bytearray_t *bytearray_new_copied_from_datastream(datastream_t *src) { + try { + return new bytearray_t(*src); + } catch (...) { + return nullptr; + } +} + bytearray_t *bytearray_new_from_hex(const char *hex_str) { try { return new bytearray_t(salticidae::from_hex(hex_str)); @@ -141,6 +149,14 @@ bytearray_t *bytearray_new_from_hex(const char *hex_str) { } } +bytearray_t *bytearray_new_from_bytes(const uint8_t *arr, size_t len) { + try { + return new bytearray_t(arr, arr + len); + } catch (...) { + return nullptr; + } +} + char *datastream_get_hex(datastream_t *self) { std::string tmp = self->get_hex(); auto res = (char *)malloc(tmp.length() + 1); diff --git a/test/test_msgnet.cpp b/test/test_msgnet.cpp index 5578031..50e35dc 100644 --- a/test/test_msgnet.cpp +++ b/test/test_msgnet.cpp @@ -94,7 +94,7 @@ struct MyNet: public MsgNetworkByteOp { { if (conn->get_mode() == ConnPool::Conn::ACTIVE) { - printf("[%s] Connected, sending hello.\n", + printf("[%s] connected, sending hello.\n", this->name.c_str()); /* send the first message through this connection */ send_msg(MsgHello(this->name, "Hello there!"), @@ -106,7 +106,7 @@ struct MyNet: public MsgNetworkByteOp { } else { - printf("[%s] Disconnected, retrying.\n", this->name.c_str()); + printf("[%s] disconnected, retrying.\n", this->name.c_str()); /* try to reconnect to the same address */ connect(conn->get_addr(), false); } diff --git a/test/test_msgnet_c.c b/test/test_msgnet_c.c index 753c70f..1417d2b 100644 --- a/test/test_msgnet_c.c +++ b/test/test_msgnet_c.c @@ -170,8 +170,8 @@ void on_term_signal(int sig, void *userdata) { int main() { ec = eventcontext_new(); - netaddr_t *alice_addr = netaddr_new_from_sipport("127.0.0.1:12345"); - netaddr_t *bob_addr = netaddr_new_from_sipport("127.0.0.1:12346"); + netaddr_t *alice_addr = netaddr_new_from_sipport("127.0.0.1:12345", &err); + netaddr_t *bob_addr = netaddr_new_from_sipport("127.0.0.1:12346", &err); /* test two nodes in the same main loop */ alice = gen_mynet(ec, "Alice"); diff --git a/test/test_msgnet_tls.cpp b/test/test_msgnet_tls.cpp index b839497..a779ba5 100644 --- a/test/test_msgnet_tls.cpp +++ b/test/test_msgnet_tls.cpp @@ -105,7 +105,7 @@ struct MyNet: public MsgNetworkByteOp { res = peer_fingerprint == cert_hash; if (conn->get_mode() == ConnPool::Conn::ACTIVE) { - printf("[%s] Connected, sending hello.\n", + printf("[%s] connected, sending hello.\n", this->name.c_str()); /* send the first message through this connection */ send_msg(MsgHello(this->name, "Hello there!"), @@ -119,7 +119,7 @@ struct MyNet: public MsgNetworkByteOp { } else { - printf("[%s] Disconnected, retrying.\n", this->name.c_str()); + printf("[%s] disconnected, retrying.\n", this->name.c_str()); /* try to reconnect to the same address */ connect(conn->get_addr(), false); } -- cgit v1.2.3-70-g09d2