From 2b33717dc402bdd8f1ab9e3f89ecc09d52b2df60 Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Fri, 12 Aug 2011 10:26:33 +0000 Subject: [PATCH] libs/nixio: implement getproto(), getprotobyname() and getprotobynumber() --- libs/nixio/Makefile | 4 +-- libs/nixio/docsrc/nixio.lua | 35 +++++++++++++++++++++- libs/nixio/src/address.c | 59 ++++++++++++++++++++++++++++++++----- libs/nixio/src/nixio.c | 3 +- libs/nixio/src/nixio.h | 1 + 5 files changed, 91 insertions(+), 11 deletions(-) diff --git a/libs/nixio/Makefile b/libs/nixio/Makefile index fb91388d0..42d366652 100644 --- a/libs/nixio/Makefile +++ b/libs/nixio/Makefile @@ -21,8 +21,8 @@ else endif NIXIO_OBJ = src/nixio.o src/socket.o src/sockopt.o src/bind.o src/address.o \ - src/poll.o src/io.o src/file.o src/splice.o src/process.o src/syslog.o \ - src/bit.o src/binary.o src/fs.o src/user.o \ + src/protoent.o src/poll.o src/io.o src/file.o src/splice.o src/process.o \ + src/syslog.o src/bit.o src/binary.o src/fs.o src/user.o \ $(if $(NIXIO_TLS),src/tls-crypto.o src/tls-context.o src/tls-socket.o,) ifeq ($(NIXIO_TLS),axtls) diff --git a/libs/nixio/docsrc/nixio.lua b/libs/nixio/docsrc/nixio.lua index 90331cf23..1b434d76d 100644 --- a/libs/nixio/docsrc/nixio.lua +++ b/libs/nixio/docsrc/nixio.lua @@ -37,6 +37,39 @@ module "nixio" --
  • ifindex = Interface Index (Linux, "packet"-family)
  • -- +--- Get protocol entry by name. +-- @usage This function returns nil if the given protocol is unknown. +-- @class function +-- @name nixio.getprotobyname +-- @param name protocol name to lookup +-- @return Table containing the following fields: + +--- Get protocol entry by number. +-- @usage This function returns nil if the given protocol is unknown. +-- @class function +-- @name nixio.getprotobynumber +-- @param proto protocol number to lookup +-- @return Table containing the following fields: + +--- Get all or a specifc proto entry. +-- @class function +-- @name nixio.getproto +-- @param proto protocol number or name to lookup (optional) +-- @return Table (or if no parameter is given, a table of tables) +-- containing the following fields: + --- Create a new socket and bind it to a network address. -- This function is a shortcut for calling nixio.socket and then bind() -- on the socket object. @@ -431,4 +464,4 @@ module "nixio" -- @class function -- @name nixio.tls -- @param mode TLS-Mode ["client", "server"] --- @return TLSContext Object \ No newline at end of file +-- @return TLSContext Object diff --git a/libs/nixio/src/address.c b/libs/nixio/src/address.c index eff05e3a0..98bba01d0 100644 --- a/libs/nixio/src/address.c +++ b/libs/nixio/src/address.c @@ -493,22 +493,67 @@ static int nixio_getifaddrs(lua_State *L) { } #endif +/** + * protoent conversion helper + */ +static int nixio__pushprotoent(lua_State *L, struct protoent *e) { + int i; + if (e) { + lua_newtable(L); + + lua_pushstring(L, e->p_name); + lua_setfield(L, -2, "name"); + + lua_pushnumber(L, e->p_proto); + lua_setfield(L, -2, "proto"); + + lua_newtable(L); + for (i = 0; e->p_aliases[i]; i++) { + lua_pushstring(L, e->p_aliases[i]); + lua_rawseti(L, -2, i+1); + } + lua_setfield(L, -2, "aliases"); + return 1; + } else { + return 0; + } +} + +/** + * getprotobyname(name) + */ +static int nixio_getprotobyname(lua_State *L) { + const char *name = luaL_checkstring(L, 1); + struct protoent *res = getprotobyname(name); + return nixio__pushprotoent(L, res); +} + +/** + * getprotobynumber(proto) + */ +static int nixio_getprotobynumber(lua_State *L) { + int proto = luaL_checkinteger(L, 1); + struct protoent *res = getprotobynumber(proto); + return nixio__pushprotoent(L, res); +} /* module table */ static const luaL_reg R[] = { #if defined(__linux__) || defined(BSD) - {"getifaddrs", nixio_getifaddrs}, + {"getifaddrs", nixio_getifaddrs}, #endif - {"getaddrinfo", nixio_getaddrinfo}, - {"getnameinfo", nixio_getnameinfo}, - {NULL, NULL} + {"getaddrinfo", nixio_getaddrinfo}, + {"getnameinfo", nixio_getnameinfo}, + {"getprotobyname", nixio_getprotobyname}, + {"getprotobynumber", nixio_getprotobynumber}, + {NULL, NULL} }; /* object table */ static const luaL_reg M[] = { - {"getsockname", nixio_sock_getsockname}, - {"getpeername", nixio_sock_getpeername}, - {NULL, NULL} + {"getsockname", nixio_sock_getsockname}, + {"getpeername", nixio_sock_getpeername}, + {NULL, NULL} }; void nixio_open_address(lua_State *L) { diff --git a/libs/nixio/src/nixio.c b/libs/nixio/src/nixio.c index 70a77b4c9..ae81c6f96 100644 --- a/libs/nixio/src/nixio.c +++ b/libs/nixio/src/nixio.c @@ -22,7 +22,7 @@ #include #include -#define VERSION 0.3 +#define VERSION 0.4 /* pushes nil, error number and errstring on the stack */ @@ -133,6 +133,7 @@ NIXIO_API int luaopen_nixio(lua_State *L) { nixio_open_sockopt(L); nixio_open_bind(L); nixio_open_address(L); + nixio_open_protoent(L); nixio_open_poll(L); nixio_open_io(L); nixio_open_splice(L); diff --git a/libs/nixio/src/nixio.h b/libs/nixio/src/nixio.h index 2be197f13..8802e9204 100644 --- a/libs/nixio/src/nixio.h +++ b/libs/nixio/src/nixio.h @@ -111,6 +111,7 @@ void nixio_open_socket(lua_State *L); void nixio_open_sockopt(lua_State *L); void nixio_open_bind(lua_State *L); void nixio_open_address(lua_State *L); +void nixio_open_protoent(lua_State *L); void nixio_open_poll(lua_State *L); void nixio_open_io(lua_State *L); void nixio_open_splice(lua_State *L); -- 2.25.1