diff options
| author | enginix <wnd1024@gmail.com> | 2016-07-22 22:46:45 +0800 |
|---|---|---|
| committer | enginix <wnd1024@gmail.com> | 2016-07-22 22:52:13 +0800 |
| commit | 2205c2053c9bdd270c90f93d23fe44c9674bad3e (patch) | |
| tree | e23e0e39cb27a23c68233758dd69ea9574d71ec8 /src | |
| parent | c87f953d81420e64cb5bb324ae3ca15f1bb28b1a (diff) | |
| download | luasocket-2205c2053c9bdd270c90f93d23fe44c9674bad3e.tar.gz luasocket-2205c2053c9bdd270c90f93d23fe44c9674bad3e.tar.bz2 luasocket-2205c2053c9bdd270c90f93d23fe44c9674bad3e.zip | |
add getsockname api for unix {udp,tcp} socket
Diffstat (limited to 'src')
| -rw-r--r-- | src/unixtcp.c | 18 | ||||
| -rw-r--r-- | src/unixudp.c | 18 |
2 files changed, 36 insertions, 0 deletions
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); | |||
| 33 | static int meth_dirty(lua_State *L); | 33 | static int meth_dirty(lua_State *L); |
| 34 | static int meth_getstats(lua_State *L); | 34 | static int meth_getstats(lua_State *L); |
| 35 | static int meth_setstats(lua_State *L); | 35 | static int meth_setstats(lua_State *L); |
| 36 | static int meth_getsockname(lua_State *L); | ||
| 36 | 37 | ||
| 37 | static const char *unixtcp_tryconnect(p_unix un, const char *path); | 38 | static const char *unixtcp_tryconnect(p_unix un, const char *path); |
| 38 | static const char *unixtcp_trybind(p_unix un, const char *path); | 39 | static const char *unixtcp_trybind(p_unix un, const char *path); |
| @@ -56,6 +57,7 @@ static luaL_Reg unixtcp_methods[] = { | |||
| 56 | {"setoption", meth_setoption}, | 57 | {"setoption", meth_setoption}, |
| 57 | {"setpeername", meth_connect}, | 58 | {"setpeername", meth_connect}, |
| 58 | {"setsockname", meth_bind}, | 59 | {"setsockname", meth_bind}, |
| 60 | {"getsockname", meth_getsockname}, | ||
| 59 | {"settimeout", meth_settimeout}, | 61 | {"settimeout", meth_settimeout}, |
| 60 | {"shutdown", meth_shutdown}, | 62 | {"shutdown", meth_shutdown}, |
| 61 | {NULL, NULL} | 63 | {NULL, NULL} |
| @@ -215,6 +217,22 @@ static int meth_bind(lua_State *L) { | |||
| 215 | return 1; | 217 | return 1; |
| 216 | } | 218 | } |
| 217 | 219 | ||
| 220 | static int meth_getsockname(lua_State *L) | ||
| 221 | { | ||
| 222 | p_unix un = (p_unix) auxiliar_checkgroup(L, "unixtcp{any}", 1); | ||
| 223 | struct sockaddr_un peer = {0}; | ||
| 224 | socklen_t peer_len = sizeof(peer); | ||
| 225 | |||
| 226 | if (getsockname(un->sock, (SA *) &peer, &peer_len) < 0) { | ||
| 227 | lua_pushnil(L); | ||
| 228 | lua_pushstring(L, socket_strerror(errno)); | ||
| 229 | return 2; | ||
| 230 | } | ||
| 231 | |||
| 232 | lua_pushstring(L, peer.sun_path); | ||
| 233 | return 1; | ||
| 234 | } | ||
| 235 | |||
| 218 | /*-------------------------------------------------------------------------*\ | 236 | /*-------------------------------------------------------------------------*\ |
| 219 | * Turns a master unixtcp object into a client object. | 237 | * Turns a master unixtcp object into a client object. |
| 220 | \*-------------------------------------------------------------------------*/ | 238 | \*-------------------------------------------------------------------------*/ |
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); | |||
| 34 | static int meth_dirty(lua_State *L); | 34 | static int meth_dirty(lua_State *L); |
| 35 | static int meth_receivefrom(lua_State *L); | 35 | static int meth_receivefrom(lua_State *L); |
| 36 | static int meth_sendto(lua_State *L); | 36 | static int meth_sendto(lua_State *L); |
| 37 | static int meth_getsockname(lua_State *L); | ||
| 37 | 38 | ||
| 38 | static const char *unixudp_tryconnect(p_unix un, const char *path); | 39 | static const char *unixudp_tryconnect(p_unix un, const char *path); |
| 39 | static const char *unixudp_trybind(p_unix un, const char *path); | 40 | static const char *unixudp_trybind(p_unix un, const char *path); |
| @@ -55,6 +56,7 @@ static luaL_Reg unixudp_methods[] = { | |||
| 55 | {"setoption", meth_setoption}, | 56 | {"setoption", meth_setoption}, |
| 56 | {"setpeername", meth_connect}, | 57 | {"setpeername", meth_connect}, |
| 57 | {"setsockname", meth_bind}, | 58 | {"setsockname", meth_bind}, |
| 59 | {"getsockname", meth_getsockname}, | ||
| 58 | {"settimeout", meth_settimeout}, | 60 | {"settimeout", meth_settimeout}, |
| 59 | {"gettimeout", meth_gettimeout}, | 61 | {"gettimeout", meth_gettimeout}, |
| 60 | {NULL, NULL} | 62 | {NULL, NULL} |
| @@ -290,6 +292,22 @@ static int meth_bind(lua_State *L) | |||
| 290 | return 1; | 292 | return 1; |
| 291 | } | 293 | } |
| 292 | 294 | ||
| 295 | static int meth_getsockname(lua_State *L) | ||
| 296 | { | ||
| 297 | p_unix un = (p_unix) auxiliar_checkgroup(L, "unixudp{any}", 1); | ||
| 298 | struct sockaddr_un peer = {0}; | ||
| 299 | socklen_t peer_len = sizeof(peer); | ||
| 300 | |||
| 301 | if (getsockname(un->sock, (SA *) &peer, &peer_len) < 0) { | ||
| 302 | lua_pushnil(L); | ||
| 303 | lua_pushstring(L, socket_strerror(errno)); | ||
| 304 | return 2; | ||
| 305 | } | ||
| 306 | |||
| 307 | lua_pushstring(L, peer.sun_path); | ||
| 308 | return 1; | ||
| 309 | } | ||
| 310 | |||
| 293 | /*-------------------------------------------------------------------------*\ | 311 | /*-------------------------------------------------------------------------*\ |
| 294 | * Turns a master unixudp object into a client object. | 312 | * Turns a master unixudp object into a client object. |
| 295 | \*-------------------------------------------------------------------------*/ | 313 | \*-------------------------------------------------------------------------*/ |
