Skip to content

Commit

Permalink
add peer name for socket
Browse files Browse the repository at this point in the history
  • Loading branch information
waruqi committed Apr 30, 2022
1 parent e168999 commit 4556329
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/tbox/platform/posix/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions src/tbox/platform/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/tbox/platform/windows/interface/ws2_32.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions src/tbox/platform/windows/interface/ws2_32.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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;

Expand Down
16 changes: 16 additions & 0 deletions src/tbox/platform/windows/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4556329

Please sign in to comment.