aboutsummaryrefslogtreecommitdiff
path: root/src/util_rt.c
diff options
context:
space:
mode:
authorDavid Timber <mieabby@gmail.com>2020-01-11 18:03:47 +1100
committerDavid Timber <mieabby@gmail.com>2020-01-11 18:03:47 +1100
commit10512fc50e6184397206a41c157c09d9b02e9e1a (patch)
tree5f0779032edf0f0710400ae6c99b9a3f2977735d /src/util_rt.c
parent129e12d7685a6ea99fde514ad104a0368a19033d (diff)
staged resolv_worker
* replaced RNG from `rnd` to `mbedtls_ctr_drbg` * use of `uint_fastN_t` where appropriate * heartbeat protocol draft * improved worker scheduling mech
Diffstat (limited to 'src/util_rt.c')
-rw-r--r--src/util_rt.c109
1 files changed, 92 insertions, 17 deletions
diff --git a/src/util_rt.c b/src/util_rt.c
index 6a4d139..e0ebafc 100644
--- a/src/util_rt.c
+++ b/src/util_rt.c
@@ -5,17 +5,59 @@
#include <ctype.h>
#include <errno.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/ioctl.h>
+#include <sys/socket.h>
#include <mbedtls/base64.h>
-void prne_succeed_or_die (const int ret) {
+void prne_ok_or_die (const int ret) {
if (ret < 0) {
abort();
}
}
-void prne_empty_func () {}
+void prne_true_or_die (const bool ret) {
+ if (!ret) {
+ abort();
+ }
+}
+
+void prne_empty_func (void) {}
+
+bool prne_is_nonblock_errno (void) {
+ switch (errno) {
+#if EAGAIN == EWOULDBLOCK
+ case EAGAIN:
+#else
+ case EAGAIN:
+ case EWOULDBLOCK:
+#endif
+ case EINPROGRESS:
+ return true;
+ }
+ return false;
+}
+
+void prne_die_not_nonblock_err (void) {
+ if (!prne_is_nonblock_errno()) {
+ abort();
+ }
+}
+
+void prne_close (const int fd) {
+ if (fd >= 0) {
+ close(fd);
+ }
+}
+
+void prne_shutdown (const int fd, const int how) {
+ if (fd >= 0) {
+ shutdown(fd, how);
+ }
+}
void *prne_malloc (const size_t se, const size_t cnt) {
if (SIZE_MAX / se < cnt) {
@@ -41,14 +83,14 @@ void prne_free (void *ptr) {
free(ptr);
}
-void prne_rnd_anum_str (prne_rnd_engine_t *rnd_engine, char *str, const size_t len) {
+void prne_rnd_anum_str (mbedtls_ctr_drbg_context *rnd, char *str, const size_t len) {
static const char SET[] = "qwertyuiopasdfghjklzxcvbnm0123456789";
size_t i = 0;
uint32_t n;
if (len >= 4) {
for (; i < len / 4 * 4; i += 4) {
- n = prne_rnd_gen_int(rnd_engine);
+ mbedtls_ctr_drbg_random(rnd, (uint8_t*)&n, sizeof(n));
str[i + 0] = SET[((uint8_t*)&n)[0] % sizeof(SET)];
str[i + 1] = SET[((uint8_t*)&n)[1] % sizeof(SET)];
str[i + 2] = SET[((uint8_t*)&n)[2] % sizeof(SET)];
@@ -56,13 +98,28 @@ void prne_rnd_anum_str (prne_rnd_engine_t *rnd_engine, char *str, const size_t l
}
}
if (i < len) {
- n = prne_rnd_gen_int(rnd_engine);
+ mbedtls_ctr_drbg_random(rnd, (uint8_t*)&n, sizeof(n));
for (; i < len; i += 1) {
str[i] = SET[((uint8_t*)&n)[i % 4] % sizeof(SET)];
}
}
}
+char *prne_strnchr (const char *p, const char c, const size_t n) {
+ size_t i;
+
+ for (i = 0; i < n; i += 1) {
+ if (p[i] == c) {
+ return (char*)p + i;
+ }
+ else if (p[i] == 0) {
+ return NULL;
+ }
+ }
+
+ return NULL;
+}
+
size_t prne_str_shift_spaces (char *str, const size_t len) {
size_t i, ret = len;
@@ -85,34 +142,42 @@ size_t prne_str_shift_spaces (char *str, const size_t len) {
}
-struct timespec prne_sub_timespec (const struct timespec *a, const struct timespec *b) {
+struct timespec prne_sub_timespec (const struct timespec a, const struct timespec b) {
struct timespec ret;
- if (a->tv_nsec < b->tv_nsec) {
- ret.tv_sec = a->tv_sec - 1 - b->tv_sec;
- ret.tv_nsec = 1000000000 + a->tv_nsec - b->tv_nsec;
+ if (a.tv_nsec < b.tv_nsec) {
+ ret.tv_sec = a.tv_sec - 1 - b.tv_sec;
+ ret.tv_nsec = 1000000000 + a.tv_nsec - b.tv_nsec;
}
else {
- ret.tv_sec = a->tv_sec - b->tv_sec;
- ret.tv_nsec = a->tv_nsec - b->tv_nsec;
+ ret.tv_sec = a.tv_sec - b.tv_sec;
+ ret.tv_nsec = a.tv_nsec - b.tv_nsec;
}
return ret;
}
-double prne_real_timespec (const struct timespec *ts) {
- return (double)ts->tv_sec + (double)ts->tv_nsec / 1000000000.0;
+double prne_real_timespec (const struct timespec ts) {
+ return (double)ts.tv_sec + (double)ts.tv_nsec / 1000000000.0;
}
-int prne_cmp_timespec (const struct timespec *a, const struct timespec *b) {
- if (a->tv_sec < b->tv_sec) {
+int prne_cmp_timespec (const struct timespec a, const struct timespec b) {
+ if (a.tv_sec < b.tv_sec) {
return -1;
}
- else if (a->tv_sec > b->tv_sec) {
+ else if (a.tv_sec > b.tv_sec) {
return 1;
}
- return a->tv_nsec < b->tv_nsec ? -1 : a->tv_nsec > b->tv_nsec ? 1 : 0;
+ return a.tv_nsec < b.tv_nsec ? -1 : a.tv_nsec > b.tv_nsec ? 1 : 0;
+}
+
+struct timespec prne_min_timespec (const struct timespec a, const struct timespec b) {
+ return prne_cmp_timespec(a, b) < 0 ? a : b;
+}
+
+struct timespec prne_max_timespec (const struct timespec a, const struct timespec b) {
+ return prne_cmp_timespec(a, b) > 0 ? a : b;
}
char *prne_enc_base64_mem (const uint8_t *data, const size_t size) {
@@ -161,3 +226,13 @@ bool prne_dec_base64_mem (const char *str, const size_t str_len, uint8_t **data,
*size = ret_size;
return true;
}
+
+bool prne_set_pipe_size (const int fd, const int size) {
+ return
+#if defined(F_SETPIPE_SZ)
+ fcntl(fd, F_SETPIPE_SZ, size) == 0
+#elif defined(FIONREAD)
+ ioctl(fd, FIONREAD, &size) == 0
+#endif
+ ;
+}