diff --git a/src/unixtcp.c b/src/unixtcp.c index a9c4962..747ef5f 100644 --- a/src/unixtcp.c +++ b/src/unixtcp.c @@ -33,6 +33,7 @@ static int meth_setfd(lua_State *L); static int meth_dirty(lua_State *L); static int meth_getstats(lua_State *L); static int meth_setstats(lua_State *L); +static int meth_getsockname(lua_State *L); static const char *unixtcp_tryconnect(p_unix un, const char *path); static const char *unixtcp_trybind(p_unix un, const char *path); @@ -56,6 +57,7 @@ static luaL_Reg unixtcp_methods[] = { {"setoption", meth_setoption}, {"setpeername", meth_connect}, {"setsockname", meth_bind}, + {"getsockname", meth_getsockname}, {"settimeout", meth_settimeout}, {"shutdown", meth_shutdown}, {NULL, NULL} @@ -215,6 +217,22 @@ static int meth_bind(lua_State *L) { return 1; } +static int meth_getsockname(lua_State *L) +{ + p_unix un = (p_unix) auxiliar_checkgroup(L, "unixtcp{any}", 1); + struct sockaddr_un peer = {0}; + socklen_t peer_len = sizeof(peer); + + if (getsockname(un->sock, (SA *) &peer, &peer_len) < 0) { + lua_pushnil(L); + lua_pushstring(L, socket_strerror(errno)); + return 2; + } + + lua_pushstring(L, peer.sun_path); + return 1; +} + /*-------------------------------------------------------------------------*\ * Turns a master unixtcp object into a client object. \*-------------------------------------------------------------------------*/ diff --git a/src/unixudp.c b/src/unixudp.c index 1088c6b..0e0a19a 100644 --- a/src/unixudp.c +++ b/src/unixudp.c @@ -34,6 +34,7 @@ static int meth_setfd(lua_State *L); static int meth_dirty(lua_State *L); static int meth_receivefrom(lua_State *L); static int meth_sendto(lua_State *L); +static int meth_getsockname(lua_State *L); static const char *unixudp_tryconnect(p_unix un, const char *path); static const char *unixudp_trybind(p_unix un, const char *path); @@ -55,6 +56,7 @@ static luaL_Reg unixudp_methods[] = { {"setoption", meth_setoption}, {"setpeername", meth_connect}, {"setsockname", meth_bind}, + {"getsockname", meth_getsockname}, {"settimeout", meth_settimeout}, {"gettimeout", meth_gettimeout}, {NULL, NULL} @@ -290,6 +292,22 @@ static int meth_bind(lua_State *L) return 1; } +static int meth_getsockname(lua_State *L) +{ + p_unix un = (p_unix) auxiliar_checkgroup(L, "unixudp{any}", 1); + struct sockaddr_un peer = {0}; + socklen_t peer_len = sizeof(peer); + + if (getsockname(un->sock, (SA *) &peer, &peer_len) < 0) { + lua_pushnil(L); + lua_pushstring(L, socket_strerror(errno)); + return 2; + } + + lua_pushstring(L, peer.sun_path); + return 1; +} + /*-------------------------------------------------------------------------*\ * Turns a master unixudp object into a client object. \*-------------------------------------------------------------------------*/