diff --git a/src/tbox/platform/posix/socket.c b/src/tbox/platform/posix/socket.c index e7079f40c..895b14539 100644 --- a/src/tbox/platform/posix/socket.c +++ b/src/tbox/platform/posix/socket.c @@ -492,6 +492,22 @@ tb_bool_t tb_socket_local(tb_socket_ref_t sock, tb_ipaddr_ref_t addr) // ok return tb_true; } +tb_bool_t tb_socket_peer(tb_socket_ref_t sock, tb_ipaddr_ref_t addr) +{ + // check + tb_assert_and_check_return_val(sock, tb_false); + + // get peer address + struct sockaddr_storage d; + tb_int_t n = sizeof(d); + if (getpeername(tb_sock2fd(sock), (struct sockaddr *)&d, (socklen_t *)&n) == -1) return tb_false; + + // save address + if (addr) tb_sockaddr_save(addr, &d); + + // ok + return tb_true; +} tb_bool_t tb_socket_kill(tb_socket_ref_t sock, tb_size_t mode) { // check diff --git a/src/tbox/platform/socket.h b/src/tbox/platform/socket.h index 65b1174f5..50ea51676 100644 --- a/src/tbox/platform/socket.h +++ b/src/tbox/platform/socket.h @@ -192,6 +192,15 @@ tb_socket_ref_t tb_socket_accept(tb_socket_ref_t sock, tb_ipaddr_ref_t addr) */ tb_bool_t tb_socket_local(tb_socket_ref_t sock, tb_ipaddr_ref_t addr); +/*! get peer address + * + * @param sock the socket + * @param addr the peer address + * + * @return tb_true or tb_false + */ +tb_bool_t tb_socket_peer(tb_socket_ref_t sock, tb_ipaddr_ref_t addr); + /*! kill socket * * @param sock the socket diff --git a/src/tbox/platform/windows/interface/ws2_32.c b/src/tbox/platform/windows/interface/ws2_32.c index 9533ed288..8de7be1ed 100644 --- a/src/tbox/platform/windows/interface/ws2_32.c +++ b/src/tbox/platform/windows/interface/ws2_32.c @@ -67,6 +67,7 @@ static tb_bool_t tb_ws2_32_instance_init(tb_ws2_32_ref_t ws2_32) TB_INTERFACE_LOAD(ws2_32, select); TB_INTERFACE_LOAD(ws2_32, connect); TB_INTERFACE_LOAD(ws2_32, shutdown); + TB_INTERFACE_LOAD(ws2_32, getpeername); TB_INTERFACE_LOAD(ws2_32, getsockname); TB_INTERFACE_LOAD(ws2_32, getsockopt); TB_INTERFACE_LOAD(ws2_32, setsockopt); diff --git a/src/tbox/platform/windows/interface/ws2_32.h b/src/tbox/platform/windows/interface/ws2_32.h index 7ef0c6f2e..8585593e6 100644 --- a/src/tbox/platform/windows/interface/ws2_32.h +++ b/src/tbox/platform/windows/interface/ws2_32.h @@ -108,6 +108,9 @@ typedef tb_int_t (WSAAPI* tb_ws2_32_shutdown_t)(SOCKET s, tb_int_t how); // the getsockname func type typedef tb_int_t (WSAAPI* tb_ws2_32_getsockname_t)(SOCKET s, struct sockaddr* name, tb_int_t* namelen); +// the getpeername func type +typedef tb_int_t (WSAAPI* tb_ws2_32_getpeername_t)(SOCKET s, struct sockaddr* name, tb_int_t* namelen); + // the getsockopt func type typedef tb_int_t (WSAAPI* tb_ws2_32_getsockopt_t)(SOCKET s, tb_int_t level, tb_int_t optname, tb_char_t* optval, tb_int_t* optlen); @@ -207,6 +210,9 @@ typedef struct __tb_ws2_32_t // getsockname tb_ws2_32_getsockname_t getsockname; + // getpeername + tb_ws2_32_getpeername_t getpeername; + // getsockopt tb_ws2_32_getsockopt_t getsockopt; diff --git a/src/tbox/platform/windows/socket.c b/src/tbox/platform/windows/socket.c index 1d964d36c..eae7554a4 100644 --- a/src/tbox/platform/windows/socket.c +++ b/src/tbox/platform/windows/socket.c @@ -582,6 +582,22 @@ tb_bool_t tb_socket_local(tb_socket_ref_t sock, tb_ipaddr_ref_t addr) // ok return tb_true; } +tb_bool_t tb_socket_peer(tb_socket_ref_t sock, tb_ipaddr_ref_t addr) +{ + // check + tb_assert_and_check_return_val(sock, tb_false); + + // get peer address + struct sockaddr_storage d = {0}; + tb_int_t n = sizeof(d); + if (tb_ws2_32()->getpeername(tb_sock2fd(sock), (struct sockaddr *)&d, &n) == -1) return tb_false; + + // save address + if (addr) tb_sockaddr_save(addr, &d); + + // ok + return tb_true; +} tb_bool_t tb_socket_kill(tb_socket_ref_t sock, tb_size_t mode) { // check