add function tcp_socket_connected

remotes/origin/fstore_storage_engine
YuQing 2022-12-30 17:15:42 +08:00
parent aa2fc62cbb
commit 8ab3420bce
4 changed files with 40 additions and 1 deletions

View File

@ -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

View File

@ -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;

View File

@ -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 <netinet/tcp_fsm.h>
#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
}

View File

@ -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);