aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDeterminant <[email protected]>2019-06-22 01:04:36 -0400
committerDeterminant <[email protected]>2019-06-22 01:04:36 -0400
commit95257d41d0b946197093ad67657fb3d4f7f9030f (patch)
tree48986fe06085b83e45c633da0be97e586be722c7 /src
parent729eb87a12bb0efd3c2a4cc381f39e13dfd4fbf9 (diff)
update C API; fix bugs
Diffstat (limited to 'src')
-rw-r--r--src/conn.cpp63
-rw-r--r--src/network.cpp8
-rw-r--r--src/stream.cpp43
-rw-r--r--src/util.cpp1
4 files changed, 71 insertions, 44 deletions
diff --git a/src/conn.cpp b/src/conn.cpp
index 535803b..931e915 100644
--- a/src/conn.cpp
+++ b/src/conn.cpp
@@ -305,10 +305,7 @@ void ConnPool::accept_client(int fd, int) {
conn->on_setup();
worker.feed(conn, client_fd);
}
- } catch (ConnPoolError &e) {
- SALTICIDAE_LOG_ERROR("%s", e.what());
- throw e;
- }
+ } catch (...) { recoverable_error(std::current_exception()); }
}
void ConnPool::Conn::conn_server(int fd, int events) {
@@ -338,31 +335,26 @@ void ConnPool::_listen(NetAddr listen_addr) {
ev_listen.clear();
close(listen_fd);
}
- try {
- if ((listen_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
- throw ConnPoolError(SALTI_ERROR_LISTEN, errno);
- if (setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&one, sizeof(one)) < 0 ||
- setsockopt(listen_fd, SOL_TCP, TCP_NODELAY, (const char *)&one, sizeof(one)) < 0)
- throw ConnPoolError(SALTI_ERROR_LISTEN, errno);
- if (fcntl(listen_fd, F_SETFL, O_NONBLOCK) == -1)
- throw ConnPoolError(SALTI_ERROR_LISTEN, errno);
+ if ((listen_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
+ throw ConnPoolError(SALTI_ERROR_LISTEN, errno);
+ if (setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&one, sizeof(one)) < 0 ||
+ setsockopt(listen_fd, SOL_TCP, TCP_NODELAY, (const char *)&one, sizeof(one)) < 0)
+ throw ConnPoolError(SALTI_ERROR_LISTEN, errno);
+ if (fcntl(listen_fd, F_SETFL, O_NONBLOCK) == -1)
+ throw ConnPoolError(SALTI_ERROR_LISTEN, errno);
- struct sockaddr_in sockin;
- memset(&sockin, 0, sizeof(struct sockaddr_in));
- sockin.sin_family = AF_INET;
- sockin.sin_addr.s_addr = INADDR_ANY;
- sockin.sin_port = listen_addr.port;
+ struct sockaddr_in sockin;
+ memset(&sockin, 0, sizeof(struct sockaddr_in));
+ sockin.sin_family = AF_INET;
+ sockin.sin_addr.s_addr = INADDR_ANY;
+ sockin.sin_port = listen_addr.port;
- if (bind(listen_fd, (struct sockaddr *)&sockin, sizeof(sockin)) < 0)
- throw ConnPoolError(SALTI_ERROR_LISTEN, errno);
- if (::listen(listen_fd, max_listen_backlog) < 0)
- throw ConnPoolError(SALTI_ERROR_LISTEN, errno);
- } catch (ConnPoolError &e) {
- SALTICIDAE_LOG_ERROR("%s", e.what());
- throw e;
- }
+ if (bind(listen_fd, (struct sockaddr *)&sockin, sizeof(sockin)) < 0)
+ throw ConnPoolError(SALTI_ERROR_LISTEN, errno);
+ if (::listen(listen_fd, max_listen_backlog) < 0)
+ throw ConnPoolError(SALTI_ERROR_LISTEN, errno);
ev_listen = FdEvent(disp_ec, listen_fd,
- std::bind(&ConnPool::accept_client, this, _1, _2));
+ std::bind(&ConnPool::accept_client, this, _1, _2));
ev_listen.add(FdEvent::READ);
SALTICIDAE_LOG_INFO("listening to %u", ntohs(listen_addr.port));
}
@@ -370,18 +362,13 @@ void ConnPool::_listen(NetAddr listen_addr) {
ConnPool::conn_t ConnPool::_connect(const NetAddr &addr) {
int fd;
int one = 1;
- try {
- if ((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
- throw ConnPoolError(SALTI_ERROR_CONNECT, errno);
- if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&one, sizeof(one)) < 0 ||
- setsockopt(fd, SOL_TCP, TCP_NODELAY, (const char *)&one, sizeof(one)) < 0)
- throw ConnPoolError(SALTI_ERROR_CONNECT, errno);
- if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1)
- throw ConnPoolError(SALTI_ERROR_CONNECT, errno);
- } catch (ConnPoolError &e) {
- SALTICIDAE_LOG_ERROR("%s", e.what());
- throw e;
- }
+ if ((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
+ throw ConnPoolError(SALTI_ERROR_CONNECT, errno);
+ if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&one, sizeof(one)) < 0 ||
+ setsockopt(fd, SOL_TCP, TCP_NODELAY, (const char *)&one, sizeof(one)) < 0)
+ throw ConnPoolError(SALTI_ERROR_CONNECT, errno);
+ if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1)
+ throw ConnPoolError(SALTI_ERROR_CONNECT, errno);
conn_t conn = create_conn();
conn->self_ref = conn;
conn->send_buffer.set_capacity(queue_capacity);
diff --git a/src/network.cpp b/src/network.cpp
index bc539f4..2f84f25 100644
--- a/src/network.cpp
+++ b/src/network.cpp
@@ -76,9 +76,9 @@ void msgnetwork_send_msg_deferred_by_move(msgnetwork_t *self,
self->_send_msg_deferred(std::move(*_moved_msg), *conn);
}
-msgnetwork_conn_t *msgnetwork_connect(msgnetwork_t *self, const netaddr_t *addr, SalticidaeCError *cerror) {
+msgnetwork_conn_t *msgnetwork_connect(msgnetwork_t *self, const netaddr_t *addr, bool blocking, SalticidaeCError *cerror) {
SALTICIDAE_CERROR_TRY(cerror)
- return new msgnetwork_conn_t(self->connect(*addr));
+ return new msgnetwork_conn_t(self->connect(*addr, blocking));
SALTICIDAE_CERROR_CATCH(cerror)
return nullptr;
}
@@ -151,6 +151,10 @@ const netaddr_t *msgnetwork_conn_get_addr(const msgnetwork_conn_t *conn) {
return &(*conn)->get_addr();
}
+const x509_t *msgnetwork_conn_get_peer_cert(const msgnetwork_conn_t *conn) {
+ return (*conn)->get_peer_cert();
+}
+
// PeerNetwork
peernetwork_config_t *peernetwork_config_new() {
diff --git a/src/stream.cpp b/src/stream.cpp
index 71a5b27..f5d4ac9 100644
--- a/src/stream.cpp
+++ b/src/stream.cpp
@@ -6,8 +6,21 @@ extern "C" {
uint256_t *uint256_new() { return new uint256_t(); }
uint256_t *uint256_new_from_bytes(const uint8_t *arr) {
- return new uint256_t(arr);
+ try {
+ return new uint256_t(arr);
+ } catch (...) {
+ return nullptr;
+ }
}
+
+uint256_t *uint256_new_from_bytearray(const bytearray_t *bytes) {
+ try {
+ return new uint256_t(*bytes);
+ } catch (...) {
+ return nullptr;
+ }
+}
+
void uint256_free(const uint256_t *self) { delete self; }
bool uint256_is_null(const uint256_t *self) { return self->is_null(); }
@@ -39,6 +52,22 @@ datastream_t *datastream_new_from_bytes(const uint8_t *base, size_t size) {
}
}
+datastream_t *datastream_new_from_bytearray(const bytearray_t *bytes) {
+ try {
+ return new datastream_t(*bytes);
+ } catch (...) {
+ return nullptr;
+ }
+}
+
+datastream_t *datastream_new_moved_from_bytearray(bytearray_t *bytes) {
+ try {
+ return new datastream_t(std::move(*bytes));
+ } catch (...) {
+ return nullptr;
+ }
+}
+
void datastream_free(const datastream_t *self) { delete self; }
datastream_t *datastream_copy(const datastream_t *self) {
@@ -98,9 +127,15 @@ uint256_t *datastream_get_hash(const datastream_t *self) {
bytearray_t *bytearray_new_moved_from_datastream(datastream_t *_moved_src) {
try {
- auto res = new bytearray_t(std::move(*_moved_src));
- //delete _moved_src;
- return res;
+ return new bytearray_t(std::move(*_moved_src));
+ } catch (...) {
+ return nullptr;
+ }
+}
+
+bytearray_t *bytearray_new_from_hex(const char *hex_str) {
+ try {
+ return new bytearray_t(salticidae::from_hex(hex_str));
} catch (...) {
return nullptr;
}
diff --git a/src/util.cpp b/src/util.cpp
index 1493b20..f70cb32 100644
--- a/src/util.cpp
+++ b/src/util.cpp
@@ -55,6 +55,7 @@ const char *SALTICIDAE_ERROR_STRINGS[] = {
"EVP_PKEY error",
"tls key does not match the cert",
"tls fail to get peer cert",
+ "fd error",
"unknown error"
};