/* * lib/idiag/idiag.c Inet Diag Netlink * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation version 2.1 * of the License. * * Copyright (c) 2013 Sassano Systems LLC */ /** * @defgroup idiag Inet Diag library (libnl-idiag) * @brief * @{ */ #include #include #include #include #include /** * @name Socket Creation * @{ */ /** * Create and connect idiag netlink socket. * @arg sk Netlink socket. * * Creates a NETLINK_INET_DIAG socket, binds the socket, and issues a connection * attemp. * * @see nl_connect() * * @return 0 on success or a negative error code. */ int idiagnl_connect(struct nl_sock *sk) { return nl_connect(sk, NETLINK_INET_DIAG); } /** @} */ /** * @name Sending * @{ */ /** * Send trivial idiag netlink message * @arg sk Netlink socket. * @arg flags Message flags * @arg family Address family * @arg states Socket states to query * @arg ext Inet Diag attribute extensions to query * * @return Newly allocated netlink message or NULL. */ int idiagnl_send_simple(struct nl_sock *sk, int flags, uint8_t family, uint16_t states, uint16_t ext) { struct inet_diag_req req; memset(&req, 0, sizeof(req)); flags |= NLM_F_ROOT; req.idiag_family = family; req.idiag_states = states; req.idiag_ext = ext; return nl_send_simple(sk, TCPDIAG_GETSOCK, flags, &req, sizeof(req)); } /** @} */ /** * @name Inet Diag flag and attribute conversions * @{ */ static const struct trans_tbl idiag_states[] = { __ADD(IDIAG_SS_UNKNOWN, unknown) __ADD(IDIAG_SS_ESTABLISHED, established) __ADD(IDIAG_SS_SYN_SENT, syn_sent) __ADD(IDIAG_SS_SYN_RECV, syn_recv) __ADD(IDIAG_SS_FIN_WAIT1, fin_wait) __ADD(IDIAG_SS_FIN_WAIT2, fin_wait2) __ADD(IDIAG_SS_TIME_WAIT, time_wait) __ADD(IDIAG_SS_CLOSE, close) __ADD(IDIAG_SS_CLOSE_WAIT, close_wait) __ADD(IDIAG_SS_LAST_ACK, last_ack) __ADD(IDIAG_SS_LISTEN, listen) __ADD(IDIAG_SS_CLOSING, closing) __ADD(IDIAG_SS_MAX, max) { ((1<shutdown) * @arg buf Ouput buffer to hold string representation * @arg len Length in bytes of output buffer * * @return string representation of shutdown state or NULL */ char * idiagnl_shutdown2str(uint8_t shutdown, char *buf, size_t len) { if (shutdown == 0) { snprintf(buf, len, " "); return buf; } else if (shutdown == 1) { snprintf(buf, len, "receive shutdown"); return buf; } else if (shutdown == 2) { snprintf(buf, len, "send shutdown"); return buf; } return NULL; } static const struct trans_tbl idiag_exts[] = { __ADD(IDIAG_ATTR_NONE, none) __ADD(IDIAG_ATTR_MEMINFO, meminfo) __ADD(IDIAG_ATTR_INFO, info) __ADD(IDIAG_ATTR_VEGASINFO, vegasinfo) __ADD(IDIAG_ATTR_CONG, congestion) __ADD(IDIAG_ATTR_TOS, tos) __ADD(IDIAG_ATTR_TCLASS, tclass) }; /** * Convert inet diag extension flags to a string. * @arg attrs inet diag extension flags (e.g., (IDIAG_ATTR_MEMINFO | * IDIAG_ATTR_CONG | IDIAG_ATTR_TOS)) * @arg buf Output buffer to hold string representation * @arg len length in bytes of the output buffer */ char *idiagnl_exts2str(uint8_t attrs, char *buf, size_t len) { return __flags2str(attrs, buf, len, idiag_exts, ARRAY_SIZE(idiag_exts)); } /** @} */ /** @} */