From patchwork Mon May 21 20:05:54 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: [v6,2/9] sockets: Add sock_type_name and tcp_state_name helpers From: Cyrill Gorcunov X-Patchwork-Id: 8377 Message-Id: <20180521200601.26297-3-gorcunov@gmail.com> To: crml Cc: Andrey Vagin Date: Mon, 21 May 2018 23:05:54 +0300 For readable printings. Signed-off-by: Cyrill Gorcunov --- 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 db330428850c..1d0e1f29304c 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 f8504d9c5050..eec282027298 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;