From 21f09b44ced7e74996be7b7a045d9c6ecf99698d Mon Sep 17 00:00:00 2001 From: Cyrill Gorcunov Date: Sat, 9 Jun 2018 16:06:49 +0300 Subject: [PATCH] sockets: Add sock_type_name and tcp_state_name helpers For readable printings. Signed-off-by: Cyrill Gorcunov Signed-off-by: Andrei Vagin --- criu/include/sockets.h | 3 +++ criu/sockets.c | 44 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/criu/include/sockets.h b/criu/include/sockets.h index db3304288..1d0e1f293 100644 --- a/criu/include/sockets.h +++ b/criu/include/sockets.h @@ -97,4 +97,7 @@ extern int set_netns(uint32_t ns_id); extern int kerndat_socket_netns(void); extern int kerndat_socket_unix_file(void); +extern const char *tcp_state_name(unsigned int state); +extern const char *socket_type_name(unsigned int type); + #endif /* __CR_SOCKETS_H__ */ diff --git a/criu/sockets.c b/criu/sockets.c index f8504d9c5..eec282027 100644 --- a/criu/sockets.c +++ b/criu/sockets.c @@ -39,6 +39,50 @@ #define SO_GET_FILTER SO_ATTACH_FILTER #endif +const char *socket_type_name(unsigned int type) +{ + static const char *types[] = { + [SOCK_STREAM] = __stringify_1(SOCK_STREAM), + [SOCK_DGRAM] = __stringify_1(SOCK_DGRAM), + [SOCK_RAW] = __stringify_1(SOCK_RAW), + [SOCK_SEQPACKET] = __stringify_1(SOCK_SEQPACKET), + [SOCK_PACKET] = __stringify_1(SOCK_PACKET), + }; + size_t i; + + for (i = 0; i < ARRAY_SIZE(types); i++) { + if (type == i) + return types[i]; + } + + return "UNKNOWN"; +} + +const char *tcp_state_name(unsigned int state) +{ + static const char *states[] = { + [TCP_ESTABLISHED] = __stringify_1(TCP_ESTABLISHED), + [TCP_SYN_SENT] = __stringify_1(TCP_SYN_SENT), + [TCP_SYN_RECV] = __stringify_1(TCP_SYN_RECV), + [TCP_FIN_WAIT1] = __stringify_1(TCP_FIN_WAIT1), + [TCP_FIN_WAIT2] = __stringify_1(TCP_FIN_WAIT2), + [TCP_TIME_WAIT] = __stringify_1(TCP_TIME_WAIT), + [TCP_CLOSE] = __stringify_1(TCP_CLOSE), + [TCP_CLOSE_WAIT] = __stringify_1(TCP_CLOSE_WAIT), + [TCP_LAST_ACK] = __stringify_1(TCP_LAST_ACK), + [TCP_LISTEN] = __stringify_1(TCP_LISTEN), + [TCP_CLOSING] = __stringify_1(TCP_CLOSING), + }; + size_t i; + + for (i = 0; i < ARRAY_SIZE(states); i++) { + if (state == i) + return states[i]; + } + + return "UNKNOWN"; +} + struct sock_diag_greq { u8 family; u8 protocol;