aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/network.cpp49
-rw-r--r--src/stream.cpp97
-rw-r--r--src/util.cpp25
3 files changed, 131 insertions, 40 deletions
diff --git a/src/network.cpp b/src/network.cpp
index 30ef0eb..91fc770 100644
--- a/src/network.cpp
+++ b/src/network.cpp
@@ -50,8 +50,17 @@ void msgnetwork_send_msg_by_move(msgnetwork_t *self,
delete _moved_msg;
}
-msgnetwork_conn_t *msgnetwork_connect(msgnetwork_t *self, const netaddr_t *addr) {
- return new msgnetwork_conn_t(self->connect(*addr));
+msgnetwork_conn_t *msgnetwork_connect(msgnetwork_t *self, const netaddr_t *addr, SalticidaeCError *cerror) {
+ try {
+ auto res = new msgnetwork_conn_t(self->connect(*addr));
+ *cerror = salticidae_cerror_normal();
+ return res;
+ } catch (const SalticidaeError &err) {
+ *cerror = err.get_cerr();
+ } catch (const std::exception &err) {
+ *cerror = salticidae_cerror_unknown();
+ }
+ return nullptr;
}
msgnetwork_conn_t *msgnetwork_conn_copy(const msgnetwork_conn_t *self) {
@@ -61,8 +70,15 @@ msgnetwork_conn_t *msgnetwork_conn_copy(const msgnetwork_conn_t *self) {
void msgnetwork_conn_free(const msgnetwork_conn_t *self) { delete self; }
-void msgnetwork_listen(msgnetwork_t *self, const netaddr_t *listen_addr) {
- self->listen(*listen_addr);
+void msgnetwork_listen(msgnetwork_t *self, const netaddr_t *listen_addr, SalticidaeCError *cerror) {
+ try {
+ self->listen(*listen_addr);
+ *cerror = salticidae_cerror_normal();
+ } catch (const SalticidaeError &err) {
+ *cerror = err.get_cerr();
+ } catch (const std::exception &err) {
+ *cerror = salticidae_cerror_unknown();
+ }
}
void msgnetwork_reg_handler(msgnetwork_t *self,
@@ -143,8 +159,18 @@ bool peernetwork_has_peer(const peernetwork_t *self, const netaddr_t *paddr) {
}
const peernetwork_conn_t *peernetwork_get_peer_conn(const peernetwork_t *self,
- const netaddr_t *paddr) {
- return new peernetwork_conn_t(self->get_peer_conn(*paddr));
+ const netaddr_t *paddr,
+ SalticidaeCError *cerror) {
+ try {
+ auto res = new peernetwork_conn_t(self->get_peer_conn(*paddr));
+ *cerror = salticidae_cerror_normal();
+ return res;
+ } catch (const SalticidaeError &err) {
+ *cerror = err.get_cerr();
+ } catch (const std::exception &err) {
+ *cerror = salticidae_cerror_unknown();
+ }
+ return nullptr;
}
msgnetwork_t *peernetwork_as_msgnetwork(peernetwork_t *self) { return self; }
@@ -172,8 +198,15 @@ void peernetwork_multicast_msg_by_move(peernetwork_t *self,
delete _moved_msg;
}
-void peernetwork_listen(peernetwork_t *self, const netaddr_t *listen_addr) {
- self->listen(*listen_addr);
+void peernetwork_listen(peernetwork_t *self, const netaddr_t *listen_addr, SalticidaeCError *cerror) {
+ try {
+ self->listen(*listen_addr);
+ *cerror = salticidae_cerror_normal();
+ } catch (const SalticidaeError &err) {
+ *cerror = err.get_cerr();
+ } catch (const std::exception &err) {
+ *cerror = salticidae_cerror_unknown();
+ }
}
}
diff --git a/src/stream.cpp b/src/stream.cpp
index b14bd6a..fe715f1 100644
--- a/src/stream.cpp
+++ b/src/stream.cpp
@@ -23,19 +23,37 @@ void uint256_unserialize(uint256_t *self, datastream_t *s) {
self->unserialize(*s);
}
-datastream_t *datastream_new() { return new datastream_t(); }
+datastream_t *datastream_new() {
+ try {
+ return new datastream_t();
+ } catch (...) {
+ return nullptr;
+ }
+}
+
datastream_t *datastream_new_from_bytes(const uint8_t *base, size_t size) {
- return new datastream_t(base, base + size);
+ try {
+ return new datastream_t(base, base + size);
+ } catch (...) {
+ return nullptr;
+ }
}
+
void datastream_free(const datastream_t *self) { delete self; }
-void datastream_assign_by_copy(datastream_t *dst, const datastream_t *src) {
- *dst = *src;
+bool datastream_assign_by_copy(datastream_t *dst, const datastream_t *src) {
+ try {
+ *dst = *src;
+ } catch (...) { return false; }
+ return true;
}
-void datastream_assign_by_move(datastream_t *dst, datastream_t *_moved_src) {
- *dst = std::move(*_moved_src);
- delete _moved_src;
+bool datastream_assign_by_move(datastream_t *dst, datastream_t *_moved_src) {
+ try {
+ *dst = std::move(*_moved_src);
+ delete _moved_src;
+ } catch (...) { return false; }
+ return true;
}
uint8_t *datastream_data(datastream_t *self) { return self->data(); }
@@ -44,42 +62,57 @@ void datastream_clear(datastream_t *self) { self->clear(); }
size_t datastream_size(const datastream_t *self) { return self->size(); }
-void datastream_put_u8(datastream_t *self, uint8_t val) { *self << val; }
-void datastream_put_u16(datastream_t *self, uint16_t val) { *self << val; }
-void datastream_put_u32(datastream_t *self, uint32_t val) { *self << val; }
-void datastream_put_u64(datastream_t *self, uint64_t val) { *self << val; }
-
-void datastream_put_i8(datastream_t *self, int8_t val) { *self << val; }
-void datastream_put_i16(datastream_t *self, int16_t val) { *self << val; }
-void datastream_put_i32(datastream_t *self, int32_t val) { *self << val; }
-void datastream_put_i64(datastream_t *self, int64_t val) { *self << val; }
-
-void datastream_put_data(datastream_t *self, const uint8_t *base, size_t size) {
- self->put_data(base, base + size);
+bool datastream_put_u8(datastream_t *self, uint8_t val) { try {*self << val; } catch (...) { return false; } return true; }
+bool datastream_put_u16(datastream_t *self, uint16_t val) { try {*self << val; } catch (...) { return false; } return true; }
+bool datastream_put_u32(datastream_t *self, uint32_t val) { try {*self << val; } catch (...) { return false; } return true; }
+bool datastream_put_u64(datastream_t *self, uint64_t val) { try {*self << val; } catch (...) { return false; } return true; }
+
+bool datastream_put_i8(datastream_t *self, int8_t val) { try {*self << val; } catch (...) { return false; } return true; }
+bool datastream_put_i16(datastream_t *self, int16_t val) { try {*self << val; } catch (...) { return false; } return true; }
+bool datastream_put_i32(datastream_t *self, int32_t val) { try {*self << val; } catch (...) { return false; } return true; }
+bool datastream_put_i64(datastream_t *self, int64_t val) { try {*self << val; } catch (...) { return false; } return true; }
+
+bool datastream_put_data(datastream_t *self, const uint8_t *base, size_t size) {
+ try {
+ self->put_data(base, base + size);
+ } catch (...) { return false; }
+ return true;
}
-uint8_t datastream_get_u8(datastream_t *self) { uint8_t val; *self >> val; return val; }
-uint16_t datastream_get_u16(datastream_t *self) { uint16_t val; *self >> val; return val; }
-uint32_t datastream_get_u32(datastream_t *self) { uint32_t val; *self >> val; return val; }
-uint64_t datastream_get_u64(datastream_t *self) { uint64_t val; *self >> val; return val; }
+uint8_t datastream_get_u8(datastream_t *self, bool *succ) { uint8_t val; try {*self >> val;} catch (...) {*succ = false;} *succ = true; return val; }
+uint16_t datastream_get_u16(datastream_t *self, bool *succ) { uint16_t val; try {*self >> val;} catch (...) {*succ = false;} *succ = true; return val; }
+uint32_t datastream_get_u32(datastream_t *self, bool *succ) { uint32_t val; try {*self >> val;} catch (...) {*succ = false;} *succ = true; return val; }
+uint64_t datastream_get_u64(datastream_t *self, bool *succ) { uint64_t val; try {*self >> val;} catch (...) {*succ = false;} *succ = true; return val; }
-int8_t datastream_get_i8(datastream_t *self) { int8_t val; *self >> val; return val; }
-int16_t datastream_get_i16(datastream_t *self) { int16_t val; *self >> val; return val; }
-int32_t datastream_get_i32(datastream_t *self) { int32_t val; *self >> val; return val; }
-int64_t datastream_get_i64(datastream_t *self) { int64_t val; *self >> val; return val; }
+int8_t datastream_get_i8(datastream_t *self, bool *succ) {int8_t val; try {*self >> val;} catch (...) {*succ = false;} *succ = true; return val; }
+int16_t datastream_get_i16(datastream_t *self, bool *succ) {int16_t val; try {*self >> val;} catch (...) {*succ = false;} *succ = true; return val; }
+int32_t datastream_get_i32(datastream_t *self, bool *succ) {int32_t val; try {*self >> val;} catch (...) {*succ = false;} *succ = true; return val; }
+int64_t datastream_get_i64(datastream_t *self, bool *succ) {int64_t val; try {*self >> val;} catch (...) {*succ = false;} *succ = true; return val; }
const uint8_t *datastream_get_data_inplace(datastream_t *self, size_t len) {
- return self->get_data_inplace(len);
+ try {
+ return self->get_data_inplace(len);
+ } catch (...) {
+ return nullptr;
+ }
}
uint256_t *datastream_get_hash(const datastream_t *self) {
- return new uint256_t(self->get_hash());
+ try {
+ return new uint256_t(self->get_hash());
+ } catch (...) {
+ return nullptr;
+ }
}
bytearray_t *bytearray_new_moved_from_datastream(datastream_t *_moved_src) {
- auto res = new bytearray_t(std::move(*_moved_src));
- delete _moved_src;
- return res;
+ try {
+ auto res = new bytearray_t(std::move(*_moved_src));
+ delete _moved_src;
+ return res;
+ } catch (...) {
+ return nullptr;
+ }
}
char *datastream_get_hex(datastream_t *self) {
diff --git a/src/util.cpp b/src/util.cpp
index f762b4c..874eb41 100644
--- a/src/util.cpp
+++ b/src/util.cpp
@@ -58,6 +58,31 @@ const char *TTY_COLOR_MAGENTA = "\x1b[35m";
const char *TTY_COLOR_CYAN = "\x1b[36m";
const char *TTY_COLOR_RESET = "\x1b[0m";
+extern "C" {
+
+SalticidaeCError salticidae_cerror_normal() {
+ SalticidaeCError res;
+ res.code = SALTI_NORMAL;
+ res.oscode = 0;
+ return res;
+}
+
+SalticidaeCError salticidae_cerror_unknown() {
+ SalticidaeCError res;
+ res.code = SALTI_ERROR_UNKNOWN;
+ res.oscode = 0;
+ return res;
+}
+
+const char *salticidae_strerror(int code) {
+ if (code < 0 ||
+ code >= (int)(sizeof(SALTICIDAE_ERROR_STRINGS) / sizeof(const char *)))
+ return "invalid";
+ return SALTICIDAE_ERROR_STRINGS[code];
+}
+
+}
+
void sec2tv(double t, struct timeval &tv) {
tv.tv_sec = trunc(t);
tv.tv_usec = trunc((t - tv.tv_sec) * 1e6);