aboutsummaryrefslogtreecommitdiffstats
path: root/util-src
diff options
context:
space:
mode:
Diffstat (limited to 'util-src')
-rw-r--r--util-src/crand.c64
-rw-r--r--util-src/net.c63
-rw-r--r--util-src/ringbuffer.c42
3 files changed, 149 insertions, 20 deletions
diff --git a/util-src/crand.c b/util-src/crand.c
index 7eea1f2b..160ac1f6 100644
--- a/util-src/crand.c
+++ b/util-src/crand.c
@@ -21,19 +21,22 @@
#define _DEFAULT_SOURCE
-#include "lualib.h"
-#include "lauxlib.h"
-
+#include <stdlib.h>
#include <string.h>
#include <errno.h>
+#include "lualib.h"
+#include "lauxlib.h"
+
#if defined(WITH_GETRANDOM)
#ifndef __GLIBC_PREREQ
+/* Not compiled with glibc at all */
#define __GLIBC_PREREQ(a,b) 0
#endif
#if ! __GLIBC_PREREQ(2,25)
+/* Not compiled with a glibc that provides getrandom() */
#include <unistd.h>
#include <sys/syscall.h>
@@ -49,45 +52,66 @@ int getrandom(void *buf, size_t buflen, unsigned int flags) {
#include <sys/random.h>
#endif
-#elif defined(WITH_ARC4RANDOM)
-#include <stdlib.h>
#elif defined(WITH_OPENSSL)
#include <openssl/rand.h>
+#elif defined(WITH_ARC4RANDOM)
+#ifdef __linux__
+#include <bsd/stdlib.h>
+#endif
#else
#error util.crand compiled without a random source
#endif
+#ifndef SMALLBUFSIZ
+#define SMALLBUFSIZ 32
+#endif
+
int Lrandom(lua_State *L) {
- int ret = 0;
- size_t len = (size_t)luaL_checkinteger(L, 1);
- void *buf = lua_newuserdata(L, len);
+ char smallbuf[SMALLBUFSIZ];
+ char *buf = &smallbuf[0];
+ const lua_Integer l = luaL_checkinteger(L, 1);
+ const size_t len = l;
+ luaL_argcheck(L, l >= 0, 1, "must be > 0");
+
+ if(len == 0) {
+ lua_pushliteral(L, "");
+ return 1;
+ }
+
+ if(len > SMALLBUFSIZ) {
+ buf = lua_newuserdata(L, len);
+ }
#if defined(WITH_GETRANDOM)
/*
* This acts like a read from /dev/urandom with the exception that it
* *does* block if the entropy pool is not yet initialized.
*/
- ret = getrandom(buf, len, 0);
+ int left = len;
+ char *p = buf;
- if(ret < 0) {
- lua_pushstring(L, strerror(errno));
- return lua_error(L);
- }
+ do {
+ int ret = getrandom(p, left, 0);
+
+ if(ret < 0) {
+ lua_pushstring(L, strerror(errno));
+ return lua_error(L);
+ }
+
+ p += ret;
+ left -= ret;
+ } while(left > 0);
#elif defined(WITH_ARC4RANDOM)
arc4random_buf(buf, len);
- ret = len;
#elif defined(WITH_OPENSSL)
+
if(!RAND_status()) {
lua_pushliteral(L, "OpenSSL PRNG not seeded");
return lua_error(L);
}
- ret = RAND_bytes(buf, len);
-
- if(ret == 1) {
- ret = len;
- } else {
+ if(RAND_bytes((unsigned char *)buf, len) != 1) {
/* TODO ERR_get_error() */
lua_pushstring(L, "RAND_bytes() failed");
return lua_error(L);
@@ -95,7 +119,7 @@ int Lrandom(lua_State *L) {
#endif
- lua_pushlstring(L, buf, ret);
+ lua_pushlstring(L, buf, len);
return 1;
}
diff --git a/util-src/net.c b/util-src/net.c
index bb159d57..9ff01a71 100644
--- a/util-src/net.c
+++ b/util-src/net.c
@@ -125,12 +125,75 @@ static int lc_local_addresses(lua_State *L) {
return 1;
}
+static int lc_pton(lua_State *L) {
+ char buf[16];
+ const char *ipaddr = luaL_checkstring(L, 1);
+ int errno_ = 0;
+ int family = strchr(ipaddr, ':') ? AF_INET6 : AF_INET;
+
+ switch(inet_pton(family, ipaddr, &buf)) {
+ case 1:
+ lua_pushlstring(L, buf, family == AF_INET6 ? 16 : 4);
+ return 1;
+
+ case -1:
+ errno_ = errno;
+ lua_pushnil(L);
+ lua_pushstring(L, strerror(errno_));
+ lua_pushinteger(L, errno_);
+ return 3;
+
+ default:
+ case 0:
+ lua_pushnil(L);
+ lua_pushstring(L, strerror(EINVAL));
+ lua_pushinteger(L, EINVAL);
+ return 3;
+ }
+
+}
+
+static int lc_ntop(lua_State *L) {
+ char buf[INET6_ADDRSTRLEN];
+ int family;
+ int errno_;
+ size_t l;
+ const char *ipaddr = luaL_checklstring(L, 1, &l);
+
+ if(l == 16) {
+ family = AF_INET6;
+ }
+ else if(l == 4) {
+ family = AF_INET;
+ }
+ else {
+ lua_pushnil(L);
+ lua_pushstring(L, strerror(EAFNOSUPPORT));
+ lua_pushinteger(L, EAFNOSUPPORT);
+ return 3;
+ }
+
+ if(!inet_ntop(family, ipaddr, buf, INET6_ADDRSTRLEN))
+ {
+ errno_ = errno;
+ lua_pushnil(L);
+ lua_pushstring(L, strerror(errno_));
+ lua_pushinteger(L, errno_);
+ return 3;
+ }
+
+ lua_pushstring(L, (const char *)(&buf));
+ return 1;
+}
+
int luaopen_util_net(lua_State *L) {
#if (LUA_VERSION_NUM > 501)
luaL_checkversion(L);
#endif
luaL_Reg exports[] = {
{ "local_addresses", lc_local_addresses },
+ { "pton", lc_pton },
+ { "ntop", lc_ntop },
{ NULL, NULL }
};
diff --git a/util-src/ringbuffer.c b/util-src/ringbuffer.c
index 8d9e49e6..10fe0209 100644
--- a/util-src/ringbuffer.c
+++ b/util-src/ringbuffer.c
@@ -39,10 +39,12 @@ int find(ringbuffer *b, const char *s, size_t l) {
return 0;
}
+ /* look for a matching first byte */
for(i = 0; i <= b->blen - l; i++) {
if(b->buffer[(b->rpos + i) % b->alen] == *s) {
m = 1;
+ /* check if the following byte also match */
for(j = 1; j < l; j++)
if(b->buffer[(b->rpos + i + j) % b->alen] != s[j]) {
m = 0;
@@ -58,6 +60,10 @@ int find(ringbuffer *b, const char *s, size_t l) {
return 0;
}
+/*
+ * Find first position of a substring in buffer
+ * (buffer, string) -> number
+ */
int rb_find(lua_State *L) {
size_t l, m;
ringbuffer *b = luaL_checkudata(L, 1, "ringbuffer_mt");
@@ -72,6 +78,31 @@ int rb_find(lua_State *L) {
return 0;
}
+/*
+ * Move read position forward without returning the data
+ * (buffer, number) -> boolean
+ */
+int rb_discard(lua_State *L) {
+ ringbuffer *b = luaL_checkudata(L, 1, "ringbuffer_mt");
+ size_t r = luaL_checkinteger(L, 2);
+
+ if(r > b->blen) {
+ lua_pushboolean(L, 0);
+ return 1;
+ }
+
+ b->blen -= r;
+ b->rpos += r;
+ modpos(b);
+
+ lua_pushboolean(L, 1);
+ return 1;
+}
+
+/*
+ * Read bytes from buffer
+ * (buffer, number, boolean?) -> string
+ */
int rb_read(lua_State *L) {
ringbuffer *b = luaL_checkudata(L, 1, "ringbuffer_mt");
size_t r = luaL_checkinteger(L, 2);
@@ -83,6 +114,7 @@ int rb_read(lua_State *L) {
}
if((b->rpos + r) > b->alen) {
+ /* Substring wraps around to the beginning of the buffer */
lua_pushlstring(L, &b->buffer[b->rpos], b->alen - b->rpos);
lua_pushlstring(L, b->buffer, r - (b->alen - b->rpos));
lua_concat(L, 2);
@@ -99,6 +131,10 @@ int rb_read(lua_State *L) {
return 1;
}
+/*
+ * Read buffer until first occurence of a substring
+ * (buffer, string) -> string
+ */
int rb_readuntil(lua_State *L) {
size_t l, m;
ringbuffer *b = luaL_checkudata(L, 1, "ringbuffer_mt");
@@ -114,6 +150,10 @@ int rb_readuntil(lua_State *L) {
return 0;
}
+/*
+ * Write bytes into the buffer
+ * (buffer, string) -> integer
+ */
int rb_write(lua_State *L) {
size_t l, w = 0;
ringbuffer *b = luaL_checkudata(L, 1, "ringbuffer_mt");
@@ -191,6 +231,8 @@ int luaopen_util_ringbuffer(lua_State *L) {
{
lua_pushcfunction(L, rb_find);
lua_setfield(L, -2, "find");
+ lua_pushcfunction(L, rb_discard);
+ lua_setfield(L, -2, "discard");
lua_pushcfunction(L, rb_read);
lua_setfield(L, -2, "read");
lua_pushcfunction(L, rb_readuntil);