From c18393b52b6ec3e9c4581631ac9155aa2dddc930 Mon Sep 17 00:00:00 2001 From: goldsimon Date: Wed, 22 Mar 2017 22:29:11 +0100 Subject: [PATCH] altcp: added 'addrinfo' and 'tcp_state' functions --- src/apps/altcp_tls/altcp_mbedtls.c | 27 ++++++++++++++++++++++++++- src/core/altcp.c | 20 ++++++++++++++++++++ src/core/altcp_tcp.c | 29 ++++++++++++++++++++++++++++- src/core/tcp.c | 24 ++++++++++++++++++++++++ src/include/lwip/altcp.h | 17 ++++++++++++----- src/include/lwip/priv/altcp_priv.h | 9 +++++++++ src/include/lwip/tcp.h | 3 ++- src/include/lwip/tcpbase.h | 2 ++ 8 files changed, 123 insertions(+), 8 deletions(-) diff --git a/src/apps/altcp_tls/altcp_mbedtls.c b/src/apps/altcp_tls/altcp_mbedtls.c index eaa38807..4da0ac00 100644 --- a/src/apps/altcp_tls/altcp_mbedtls.c +++ b/src/apps/altcp_tls/altcp_mbedtls.c @@ -840,6 +840,27 @@ altcp_mbedtls_dealloc(struct altcp_pcb *conn) } } +err_t +altcp_mbedtls_get_tcp_addrinfo(struct altcp_pcb *conn, int local, ip_addr_t *addr, u16_t *port) +{ + if (conn) { + return altcp_get_tcp_addrinfo(conn->inner_conn, local, addr, port); + } + return ERR_VAL; +} + +#ifdef LWIP_DEBUG +enum tcp_state +altcp_mbedtls_dbg_get_tcp_state(struct altcp_pcb *conn) +{ + if (conn) { + return altcp_dbg_get_tcp_state(conn->inner_conn); + } + return CLOSED; +} +#endif + + const struct altcp_functions altcp_mbedtls_functions = { altcp_mbedtls_set_poll, altcp_mbedtls_recved, @@ -855,7 +876,11 @@ const struct altcp_functions altcp_mbedtls_functions = { altcp_mbedtls_sndbuf, altcp_mbedtls_sndqueuelen, altcp_mbedtls_setprio, - altcp_mbedtls_dealloc + altcp_mbedtls_dealloc, + altcp_mbedtls_get_tcp_addrinfo +#ifdef LWIP_DEBUG + ,altcp_mbedtls_dbg_get_tcp_state +#endif }; #endif /* LWIP_ALTCP_TLS && LWIP_ALTCP_TLS_MBEDTLS */ diff --git a/src/core/altcp.c b/src/core/altcp.c index 147e45cd..2df66528 100644 --- a/src/core/altcp.c +++ b/src/core/altcp.c @@ -242,4 +242,24 @@ altcp_setprio(struct altcp_pcb *conn, u8_t prio) } } +err_t +altcp_get_tcp_addrinfo(struct altcp_pcb *conn, int local, ip_addr_t *addr, u16_t *port) +{ + if (conn && conn->fns && conn->fns->addrinfo) { + return conn->fns->addrinfo(conn, local, addr, port); + } + return ERR_VAL; +} + +#ifdef LWIP_DEBUG +enum tcp_state +altcp_dbg_get_tcp_state(struct altcp_pcb *conn) +{ + if (conn && conn->fns && conn->fns->dbg_get_tcp_state) { + return conn->fns->dbg_get_tcp_state(conn); + } + return CLOSED; +} +#endif + #endif /* LWIP_ALTCP */ diff --git a/src/core/altcp_tcp.c b/src/core/altcp_tcp.c index 7dab943e..2cb28986 100644 --- a/src/core/altcp_tcp.c +++ b/src/core/altcp_tcp.c @@ -343,6 +343,29 @@ altcp_tcp_dealloc(struct altcp_pcb *conn) /* no private state to clean up */ } +err_t +altcp_tcp_get_tcp_addrinfo(struct altcp_pcb *conn, int local, ip_addr_t *addr, u16_t *port) +{ + if (conn) { + struct tcp_pcb *pcb = (struct tcp_pcb *)conn->inner_conn; + return tcp_tcp_get_tcp_addrinfo(pcb, local, addr, port); + } + return ERR_VAL; +} + +#ifdef LWIP_DEBUG +enum tcp_state +altcp_tcp_dbg_get_tcp_state(struct altcp_pcb *conn) +{ + if (conn) { + struct tcp_pcb *pcb = (struct tcp_pcb *)conn->inner_conn; + if (pcb) { + return pcb->state; + } + } + return CLOSED; +} +#endif const struct altcp_functions altcp_tcp_functions = { altcp_tcp_set_poll, altcp_tcp_recved, @@ -358,7 +381,11 @@ const struct altcp_functions altcp_tcp_functions = { altcp_tcp_sndbuf, altcp_tcp_sndqueuelen, altcp_tcp_setprio, - altcp_tcp_dealloc + altcp_tcp_dealloc, + altcp_tcp_get_tcp_addrinfo +#ifdef LWIP_DEBUG + ,altcp_tcp_dbg_get_tcp_state +#endif }; #endif /* LWIP_ALTCP */ diff --git a/src/core/tcp.c b/src/core/tcp.c index 8c136817..9289f7f4 100644 --- a/src/core/tcp.c +++ b/src/core/tcp.c @@ -2055,6 +2055,30 @@ tcp_debug_state_str(enum tcp_state s) return tcp_state_str[s]; } +err_t +tcp_tcp_get_tcp_addrinfo(struct tcp_pcb *pcb, int local, ip_addr_t *addr, u16_t *port) +{ + if (pcb) { + if (local) { + if (addr) { + *addr = pcb->local_ip; + } + if (port) { + *port = pcb->local_port; + } + } else { + if (addr) { + *addr = pcb->remote_ip; + } + if (port) { + *port = pcb->remote_port; + } + } + return ERR_OK; + } + return ERR_VAL; +} + #if TCP_DEBUG || TCP_INPUT_DEBUG || TCP_OUTPUT_DEBUG /** * Print a tcp header for debugging purposes. diff --git a/src/include/lwip/altcp.h b/src/include/lwip/altcp.h index 282615c7..663cf5e6 100644 --- a/src/include/lwip/altcp.h +++ b/src/include/lwip/altcp.h @@ -39,8 +39,6 @@ * * Author: Simon Goldschmidt * - * @todo: - * - access to local/remote ip/port */ #ifndef LWIP_HDR_ALTCP_H #define LWIP_HDR_ALTCP_H @@ -112,11 +110,14 @@ u16_t altcp_mss(struct altcp_pcb *conn); u16_t altcp_sndbuf(struct altcp_pcb *conn); u16_t altcp_sndqueuelen(struct altcp_pcb *conn); -#define TCP_PRIO_MIN 1 -#define TCP_PRIO_NORMAL 64 -#define TCP_PRIO_MAX 127 void altcp_setprio(struct altcp_pcb *conn, u8_t prio); +err_t altcp_get_tcp_addrinfo(struct altcp_pcb *conn, int local, ip_addr_t *addr, u16_t *port); + +#ifdef LWIP_DEBUG +enum tcp_state altcp_dbg_get_tcp_state(struct altcp_pcb *conn); +#endif + #ifdef __cplusplus } #endif @@ -157,6 +158,12 @@ void altcp_setprio(struct altcp_pcb *conn, u8_t prio); #define altcp_sndqueuelen tcp_sndqueuelen #define altcp_setprio tcp_setprio +#define altcp_get_tcp_addrinfo tcp_get_tcp_addrinfo + +#ifdef LWIP_DEBUG +#define altcp_dbg_get_tcp_state tcp_dbg_get_tcp_state +#endif + #endif /* LWIP_ALTCP */ #endif /* LWIP_HDR_ALTCP_H */ diff --git a/src/include/lwip/priv/altcp_priv.h b/src/include/lwip/priv/altcp_priv.h index 529777e8..4bcbd281 100644 --- a/src/include/lwip/priv/altcp_priv.h +++ b/src/include/lwip/priv/altcp_priv.h @@ -78,6 +78,11 @@ typedef void (*altcp_setprio_fn)(struct altcp_pcb *conn, u8_t prio); typedef void (*altcp_dealloc_fn)(struct altcp_pcb *conn); +typedef err_t (*altcp_get_tcp_addrinfo_fn)(struct altcp_pcb *conn, int local, ip_addr_t *addr, u16_t *port); +#ifdef LWIP_DEBUG +typedef enum tcp_state (*altcp_dbg_get_tcp_state_fn)(struct altcp_pcb *conn); +#endif + struct altcp_functions { altcp_set_poll_fn set_poll; altcp_recved_fn recved; @@ -94,6 +99,10 @@ struct altcp_functions { altcp_sndqueuelen_fn sndqueuelen; altcp_setprio_fn setprio; altcp_dealloc_fn dealloc; + altcp_get_tcp_addrinfo_fn addrinfo; +#ifdef LWIP_DEBUG + altcp_dbg_get_tcp_state_fn dbg_get_tcp_state; +#endif }; diff --git a/src/include/lwip/tcp.h b/src/include/lwip/tcp.h index 74b08ae4..deedaf98 100644 --- a/src/include/lwip/tcp.h +++ b/src/include/lwip/tcp.h @@ -395,8 +395,9 @@ void tcp_setprio (struct tcp_pcb *pcb, u8_t prio); err_t tcp_output (struct tcp_pcb *pcb); +err_t tcp_tcp_get_tcp_addrinfo(struct tcp_pcb *pcb, int local, ip_addr_t *addr, u16_t *port); -const char* tcp_debug_state_str(enum tcp_state s); +#define tcp_dbg_get_tcp_state(pcb) ((pcb)->state) /* for compatibility with older implementation */ #define tcp_new_ip6() tcp_new_ip_type(IPADDR_TYPE_V6) diff --git a/src/include/lwip/tcpbase.h b/src/include/lwip/tcpbase.h index f30664b0..50c28536 100644 --- a/src/include/lwip/tcpbase.h +++ b/src/include/lwip/tcpbase.h @@ -75,6 +75,8 @@ enum tcp_state { #define TCP_PRIO_NORMAL 64 #define TCP_PRIO_MAX 127 +const char* tcp_debug_state_str(enum tcp_state s); + #ifdef __cplusplus } #endif