mirror of
https://github.com/lunarmodules/luasocket.git
synced 2024-12-26 04:28:20 +01:00
add getsockname api for unix {udp,tcp} socket
This commit is contained in:
parent
c87f953d81
commit
2205c2053c
@ -33,6 +33,7 @@ static int meth_setfd(lua_State *L);
|
|||||||
static int meth_dirty(lua_State *L);
|
static int meth_dirty(lua_State *L);
|
||||||
static int meth_getstats(lua_State *L);
|
static int meth_getstats(lua_State *L);
|
||||||
static int meth_setstats(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_tryconnect(p_unix un, const char *path);
|
||||||
static const char *unixtcp_trybind(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},
|
{"setoption", meth_setoption},
|
||||||
{"setpeername", meth_connect},
|
{"setpeername", meth_connect},
|
||||||
{"setsockname", meth_bind},
|
{"setsockname", meth_bind},
|
||||||
|
{"getsockname", meth_getsockname},
|
||||||
{"settimeout", meth_settimeout},
|
{"settimeout", meth_settimeout},
|
||||||
{"shutdown", meth_shutdown},
|
{"shutdown", meth_shutdown},
|
||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
@ -215,6 +217,22 @@ static int meth_bind(lua_State *L) {
|
|||||||
return 1;
|
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.
|
* Turns a master unixtcp object into a client object.
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
|
@ -34,6 +34,7 @@ static int meth_setfd(lua_State *L);
|
|||||||
static int meth_dirty(lua_State *L);
|
static int meth_dirty(lua_State *L);
|
||||||
static int meth_receivefrom(lua_State *L);
|
static int meth_receivefrom(lua_State *L);
|
||||||
static int meth_sendto(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_tryconnect(p_unix un, const char *path);
|
||||||
static const char *unixudp_trybind(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},
|
{"setoption", meth_setoption},
|
||||||
{"setpeername", meth_connect},
|
{"setpeername", meth_connect},
|
||||||
{"setsockname", meth_bind},
|
{"setsockname", meth_bind},
|
||||||
|
{"getsockname", meth_getsockname},
|
||||||
{"settimeout", meth_settimeout},
|
{"settimeout", meth_settimeout},
|
||||||
{"gettimeout", meth_gettimeout},
|
{"gettimeout", meth_gettimeout},
|
||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
@ -290,6 +292,22 @@ static int meth_bind(lua_State *L)
|
|||||||
return 1;
|
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.
|
* Turns a master unixudp object into a client object.
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
|
Loading…
Reference in New Issue
Block a user