diff --git a/HISTORY b/HISTORY index a151414..155c536 100644 --- a/HISTORY +++ b/HISTORY @@ -1,6 +1,7 @@ -Version 1.65 2022-12-20 +Version 1.65 2022-12-30 * locked_list.h: add functions locked_list_move and locked_list_move_tail + * add function tcp_socket_connected Version 1.64 2022-11-19 * shared_func.[hc]: normalize_path use type string_t for general purpose diff --git a/src/locked_list.h b/src/locked_list.h index 3584942..dae110c 100644 --- a/src/locked_list.h +++ b/src/locked_list.h @@ -13,6 +13,9 @@ typedef struct fc_locked_list { extern "C" { #endif +#define LOCKED_LIST_LOCK(list) PTHREAD_MUTEX_LOCK(&(list)->lock) +#define LOCKED_LIST_UNLOCK(list) PTHREAD_MUTEX_UNLOCK(&(list)->lock) + static inline int locked_list_init(FCLockedList *list) { int result; diff --git a/src/sockopt.c b/src/sockopt.c index 541d43c..6f02311 100644 --- a/src/sockopt.c +++ b/src/sockopt.c @@ -2690,3 +2690,36 @@ int fc_get_net_type_by_name(const char *net_type) } } +bool tcp_socket_connected(int sock) +{ + socklen_t len; +#if defined(OS_LINUX) || defined(OS_FREEBSD) + +#ifdef OS_LINUX + struct tcp_info info; +#else +#include +#define TCP_INFO TCP_CONNECTION_INFO +#define TCP_ESTABLISHED TCPS_ESTABLISHED + struct tcp_connection_info info; +#endif + + len = sizeof(info); + if (getsockopt(sock, IPPROTO_TCP, TCP_INFO, &info, &len) < 0) { + return false; + } + if (info.tcpi_state == TCP_ESTABLISHED) { + return true; + } else { + return false; + } +#else + int result; + len = sizeof(result); + if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &result, &len) < 0) { + return false; + } else { + return (result == 0); + } +#endif +} diff --git a/src/sockopt.h b/src/sockopt.h index 6418e47..bd1ea37 100644 --- a/src/sockopt.h +++ b/src/sockopt.h @@ -702,6 +702,8 @@ static inline void tcp_dont_try_again_when_interrupt() void tcp_set_quick_ack(const bool value); +bool tcp_socket_connected(int sock); + int fc_get_net_type_by_name(const char *net_type); int fc_get_net_type_by_ip(const char *ip);