libs/nixio: fix sendto(), implement support for unix domain sockets (#140)
This commit is contained in:
parent
a2e224b6aa
commit
716fd7a2d1
1 changed files with 56 additions and 18 deletions
|
@ -29,15 +29,20 @@
|
||||||
*/
|
*/
|
||||||
static int nixio_sock__sendto(lua_State *L, int to) {
|
static int nixio_sock__sendto(lua_State *L, int to) {
|
||||||
nixio_sock *sock = nixio__checksock(L);
|
nixio_sock *sock = nixio__checksock(L);
|
||||||
|
struct sockaddr_storage addr_in;
|
||||||
|
#ifndef __WINNT__
|
||||||
|
struct sockaddr_un addr_un;
|
||||||
|
#endif
|
||||||
struct sockaddr *addr = NULL;
|
struct sockaddr *addr = NULL;
|
||||||
socklen_t alen = 0;
|
socklen_t alen = 0;
|
||||||
int argoff = 2;
|
int argoff = 2;
|
||||||
|
|
||||||
if (to) {
|
if (to) {
|
||||||
argoff += 2;
|
argoff += 2;
|
||||||
|
if (sock->domain == AF_INET || sock->domain == AF_INET6) {
|
||||||
const char *address = luaL_checkstring(L, 3);
|
const char *address = luaL_checkstring(L, 3);
|
||||||
struct sockaddr_storage addrstor;
|
addr = (struct sockaddr*)&addr_in;
|
||||||
addr = (struct sockaddr*)&addrstor;
|
alen = sizeof(addr_in);
|
||||||
|
|
||||||
nixio_addr naddr;
|
nixio_addr naddr;
|
||||||
memset(&naddr, 0, sizeof(naddr));
|
memset(&naddr, 0, sizeof(naddr));
|
||||||
|
@ -50,6 +55,21 @@ static int nixio_sock__sendto(lua_State *L, int to) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifndef __WINNT__
|
||||||
|
else if (sock->domain == AF_UNIX) {
|
||||||
|
size_t pathlen;
|
||||||
|
const char *path = luaL_checklstring(L, 3, &pathlen);
|
||||||
|
|
||||||
|
addr_un.sun_family = AF_UNIX;
|
||||||
|
luaL_argcheck(L, pathlen < sizeof(addr_un.sun_path), 3, "out of range");
|
||||||
|
strncpy(addr_un.sun_path, path, sizeof(addr_un.sun_path));
|
||||||
|
|
||||||
|
addr = (struct sockaddr*)&addr_un;
|
||||||
|
alen = sizeof(addr_un);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
size_t len;
|
size_t len;
|
||||||
ssize_t sent;
|
ssize_t sent;
|
||||||
const char *data = luaL_checklstring(L, 2, &len);
|
const char *data = luaL_checklstring(L, 2, &len);
|
||||||
|
@ -103,16 +123,25 @@ static int nixio_sock_sendto(lua_State *L) {
|
||||||
static int nixio_sock__recvfrom(lua_State *L, int from) {
|
static int nixio_sock__recvfrom(lua_State *L, int from) {
|
||||||
nixio_sock *sock = nixio__checksock(L);
|
nixio_sock *sock = nixio__checksock(L);
|
||||||
char buffer[NIXIO_BUFFERSIZE];
|
char buffer[NIXIO_BUFFERSIZE];
|
||||||
struct sockaddr_storage addrobj;
|
struct sockaddr_storage addr_in;
|
||||||
|
#ifndef __WINNT__
|
||||||
|
struct sockaddr_un addr_un;
|
||||||
|
#endif
|
||||||
|
struct sockaddr *addr = NULL;
|
||||||
|
socklen_t alen = 0;
|
||||||
uint req = luaL_checkinteger(L, 2);
|
uint req = luaL_checkinteger(L, 2);
|
||||||
int readc;
|
int readc;
|
||||||
|
|
||||||
if (from && sock->domain != AF_INET && sock->domain != AF_INET6) {
|
if (sock->domain == AF_INET || sock->domain == AF_INET6) {
|
||||||
return luaL_argerror(L, 1, "supported families: inet, inet6");
|
addr = (from) ? (struct sockaddr*)&addr_in : NULL;
|
||||||
|
alen = (from) ? sizeof(addr_in) : 0;
|
||||||
}
|
}
|
||||||
|
#ifndef __WINNT__
|
||||||
struct sockaddr *addr = (from) ? (struct sockaddr*)&addrobj : NULL;
|
else if (sock->domain == AF_UNIX) {
|
||||||
socklen_t alen = (from) ? sizeof(addrobj) : 0;
|
addr = (from) ? (struct sockaddr*)&addr_un : NULL;
|
||||||
|
alen = (from) ? sizeof(addr_un) : 0;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
/* We limit the readsize to NIXIO_BUFFERSIZE */
|
/* We limit the readsize to NIXIO_BUFFERSIZE */
|
||||||
req = (req > NIXIO_BUFFERSIZE) ? NIXIO_BUFFERSIZE : req;
|
req = (req > NIXIO_BUFFERSIZE) ? NIXIO_BUFFERSIZE : req;
|
||||||
|
@ -137,9 +166,11 @@ static int nixio_sock__recvfrom(lua_State *L, int from) {
|
||||||
|
|
||||||
if (!from) {
|
if (!from) {
|
||||||
return 1;
|
return 1;
|
||||||
} else {
|
}
|
||||||
|
/* push address. */
|
||||||
|
if (sock->domain == AF_INET || sock->domain == AF_INET6) {
|
||||||
nixio_addr naddr;
|
nixio_addr naddr;
|
||||||
if (!nixio__addr_parse(&naddr, (struct sockaddr *)&addrobj)) {
|
if (!nixio__addr_parse(&naddr, (struct sockaddr *)&addr_in)) {
|
||||||
lua_pushstring(L, naddr.host);
|
lua_pushstring(L, naddr.host);
|
||||||
lua_pushinteger(L, naddr.port);
|
lua_pushinteger(L, naddr.port);
|
||||||
return 3;
|
return 3;
|
||||||
|
@ -147,7 +178,14 @@ static int nixio_sock__recvfrom(lua_State *L, int from) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#ifndef __WINNT__
|
||||||
|
else if (sock->domain == AF_UNIX) {
|
||||||
|
lua_pushstring(L, addr_un.sun_path);
|
||||||
|
return 2;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue