summaryrefslogtreecommitdiffhomepage
path: root/libs/nixio
diff options
context:
space:
mode:
authorSteven Barth <steven@midlink.org>2009-02-10 17:35:07 +0000
committerSteven Barth <steven@midlink.org>2009-02-10 17:35:07 +0000
commit4327f9250e6d0f4a81ca949289d5e1444a360d4e (patch)
tree5766c5d9e127cc97d256ed31e5ee77beba895d17 /libs/nixio
parentb5026e65c0334f9d8afddb0a3ca1797ef2baff5c (diff)
nixio + io = nix2io
Diffstat (limited to 'libs/nixio')
-rw-r--r--libs/nixio/Makefile2
-rw-r--r--libs/nixio/src/io.c181
-rw-r--r--libs/nixio/src/nixio.c1
-rw-r--r--libs/nixio/src/nixio.h2
-rw-r--r--libs/nixio/src/socket.c4
5 files changed, 187 insertions, 3 deletions
diff --git a/libs/nixio/Makefile b/libs/nixio/Makefile
index b6d9485e4..6cc7254c8 100644
--- a/libs/nixio/Makefile
+++ b/libs/nixio/Makefile
@@ -5,7 +5,7 @@ include ../../build/gccconfig.mk
%.o: %.c
$(COMPILE) $(LUA_CFLAGS) $(FPIC) -c -o $@ $<
-compile: src/nixio.o src/socket.o src/sockopt.o src/bind.o src/address.o src/poll.o
+compile: src/nixio.o src/socket.o src/sockopt.o src/bind.o src/address.o src/poll.o src/io.o
$(LINK) $(SHLIB_FLAGS) -o src/nixio.so src/*.o
mkdir -p dist$(LUA_LIBRARYDIR)
cp src/nixio.so dist$(LUA_LIBRARYDIR)/nixio.so
diff --git a/libs/nixio/src/io.c b/libs/nixio/src/io.c
new file mode 100644
index 000000000..30327b1c0
--- /dev/null
+++ b/libs/nixio/src/io.c
@@ -0,0 +1,181 @@
+/*
+ * nixio - Linux I/O library for lua
+ *
+ * Copyright (C) 2009 Steven Barth <steven@midlink.org>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <lua.h>
+#include <lualib.h>
+#include <lauxlib.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <arpa/inet.h>
+#include "nixio.h"
+
+
+
+/**
+ * send() / sendto() helper
+ */
+static int nixio_sock__sendto(lua_State *L, int to) {
+ nixio_sock *sock = nixio__checksock(L);
+ struct sockaddr *addr = NULL;
+ socklen_t alen = 0;
+
+ if (to) {
+ const char *address = luaL_checklstring(L, 2, NULL);
+ uint16_t port = (uint16_t)luaL_checkinteger(L, 3);
+ struct sockaddr_storage addrstor;
+ addr = (struct sockaddr*)&addrstor;
+ if (sock->domain == AF_INET) {
+ struct sockaddr_in *inetaddr = (struct sockaddr_in *)addr;
+ if (inet_pton(sock->domain, address, &inetaddr->sin_addr) < 0) {
+ return luaL_argerror(L, 3, "invalid address");
+ }
+ inetaddr->sin_port = htons(port);
+ alen = sizeof(*inetaddr);
+ } else if (sock->domain == AF_INET6) {
+ struct sockaddr_in6 *inet6addr = (struct sockaddr_in6 *)addr;
+ if (inet_pton(sock->domain, address, &inet6addr->sin6_addr) < 0) {
+ return luaL_argerror(L, 3, "invalid address");
+ }
+ inet6addr->sin6_port = htons(port);
+ alen = sizeof(*inet6addr);
+ } else {
+ return luaL_argerror(L, 1, "supported families: inet, inet6");
+ }
+ }
+
+ size_t len;
+ ssize_t sent;
+ const char *data = luaL_checklstring(L, 2, &len);
+ do {
+ sent = sendto(sock->fd, data, len, 0, addr, alen);
+ } while(sent == -1 && errno == EINTR);
+ if (len >= 0) {
+ lua_pushinteger(L, sent);
+ return 1;
+ } else {
+ return nixio__perror(L);
+ }
+}
+
+/**
+ * send(data)
+ */
+static int nixio_sock_send(lua_State *L) {
+ return nixio_sock__sendto(L, 0);
+}
+
+/**
+ * sendto(data, address, port)
+ */
+static int nixio_sock_sendto(lua_State *L) {
+ return nixio_sock__sendto(L, 1);
+}
+
+
+/**
+ * recv() / recvfrom() helper
+ */
+static int nixio_sock__recvfrom(lua_State *L, int from) {
+ nixio_sock *sock = nixio__checksock(L);
+ char buffer[NIXIO_BUFFERSIZE];
+ struct sockaddr_storage addrobj;
+ int req = luaL_checkinteger(L, 2);
+ int readc;
+
+ if (from && sock->domain != AF_INET && sock->domain != AF_INET6) {
+ return luaL_argerror(L, 1, "supported families: inet, inet6");
+ }
+
+ struct sockaddr *addr = (from) ? (struct sockaddr*)&addrobj : NULL;
+ socklen_t alen = (from) ? sizeof(addrobj) : 0;
+
+ /* We limit the readsize to NIXIO_BUFFERSIZE */
+ req = (req > NIXIO_BUFFERSIZE) ? NIXIO_BUFFERSIZE : req;
+
+ do {
+ readc = recvfrom(sock->fd, buffer, req, 0, addr, &alen);
+ } while (readc == -1 && errno == EINTR);
+
+ if (readc < 0) {
+ return nixio__perror(L);
+ } else {
+ lua_pushlstring(L, buffer, readc);
+
+ if (!from) {
+ return 1;
+ } else {
+ char ipaddr[INET6_ADDRSTRLEN];
+ void *binaddr;
+ uint16_t port;
+
+ if (addrobj.ss_family == AF_INET) {
+ struct sockaddr_in *inetaddr = (struct sockaddr_in*)addr;
+ port = inetaddr->sin_port;
+ binaddr = &inetaddr->sin_addr;
+ } else if (addrobj.ss_family == AF_INET6) {
+ struct sockaddr_in6 *inet6addr = (struct sockaddr_in6*)addr;
+ port = inet6addr->sin6_port;
+ binaddr = &inet6addr->sin6_addr;
+ } else {
+ return luaL_error(L, "unknown address family");
+ }
+
+ if (!inet_ntop(addrobj.ss_family, binaddr, ipaddr, sizeof(ipaddr))) {
+ return nixio__perror(L);
+ }
+
+ lua_pushstring(L, ipaddr);
+ lua_pushinteger(L, ntohs(port));
+
+ return 3;
+ }
+ }
+}
+
+/**
+ * recv(count)
+ */
+static int nixio_sock_recv(lua_State *L) {
+ return nixio_sock__recvfrom(L, 0);
+}
+
+/**
+ * recvfrom(count)
+ */
+static int nixio_sock_recvfrom(lua_State *L) {
+ return nixio_sock__recvfrom(L, 1);
+}
+
+
+/* module table */
+static const luaL_reg M[] = {
+ {"send", nixio_sock_send},
+ {"sendto", nixio_sock_sendto},
+ {"recv", nixio_sock_recv},
+ {"recvfrom",nixio_sock_recvfrom},
+ {NULL, NULL}
+};
+
+void nixio_open_io(lua_State *L) {
+ lua_pushvalue(L, -2);
+ luaL_register(L, NULL, M);
+ lua_pop(L, 1);
+}
diff --git a/libs/nixio/src/nixio.c b/libs/nixio/src/nixio.c
index 6f3e0793f..f152e05ea 100644
--- a/libs/nixio/src/nixio.c
+++ b/libs/nixio/src/nixio.c
@@ -107,6 +107,7 @@ LUALIB_API int luaopen_nixio(lua_State *L) {
nixio_open_bind(L);
nixio_open_address(L);
nixio_open_poll(L);
+ nixio_open_io(L);
/* module version */
lua_pushnumber(L, VERSION);
diff --git a/libs/nixio/src/nixio.h b/libs/nixio/src/nixio.h
index d39c31b6d..4b037b25e 100644
--- a/libs/nixio/src/nixio.h
+++ b/libs/nixio/src/nixio.h
@@ -2,6 +2,7 @@
#define NIXIO_H_
#define NIXIO_META "nixio.socket"
+#define NIXIO_BUFFERSIZE 8096
struct nixio_socket {
int fd;
@@ -25,6 +26,7 @@ void nixio_open_sockopt(lua_State *L);
void nixio_open_bind(lua_State *L);
void nixio_open_address(lua_State *L);
void nixio_open_poll(lua_State *L);
+void nixio_open_io(lua_State *L);
/* Method functions */
diff --git a/libs/nixio/src/socket.c b/libs/nixio/src/socket.c
index acfa9aeb2..9d4648671 100644
--- a/libs/nixio/src/socket.c
+++ b/libs/nixio/src/socket.c
@@ -43,8 +43,8 @@ static int nixio_socket(lua_State *L) {
sock->domain = AF_INET6;
} else if (!strcmp(domain, "unix")) {
sock->domain = AF_UNIX;
- } else if (!strcmp(domain, "packet")) {
- sock->domain = AF_PACKET;
+ /*} else if (!strcmp(domain, "packet")) {
+ sock->domain = AF_PACKET;*/
} else {
return luaL_argerror(L, 1,
"supported values: inet, inet6, unix, packet"