diff --git a/sys/net/gnrc/transport_layer/tcp/gnrc_tcp.c b/sys/net/gnrc/transport_layer/tcp/gnrc_tcp.c index e1b4b0e1ff..d03057a04c 100644 --- a/sys/net/gnrc/transport_layer/tcp/gnrc_tcp.c +++ b/sys/net/gnrc/transport_layer/tcp/gnrc_tcp.c @@ -49,16 +49,6 @@ */ static evtimer_t _tcp_mbox_timer; -/** - * @brief Head of liked TCB list. - */ -gnrc_tcp_tcb_t *_list_tcb_head; - -/** - * @brief Mutex for TCB list synchronization. - */ -mutex_t _list_tcb_lock; - static void _sched_mbox(evtimer_mbox_event_t *event, uint32_t offset, uint16_t type, mbox_t *mbox) { @@ -352,11 +342,7 @@ int gnrc_tcp_ep_from_str(gnrc_tcp_ep_t *ep, const char *str) int gnrc_tcp_init(void) { - /* Initialize mutex for TCB list synchronization */ - mutex_init(&(_list_tcb_lock)); - /* Initialize TCB list */ - _list_tcb_head = NULL; _rcvbuf_init(); /* Initialize timers */ diff --git a/sys/net/gnrc/transport_layer/tcp/gnrc_tcp_common.c b/sys/net/gnrc/transport_layer/tcp/gnrc_tcp_common.c new file mode 100644 index 0000000000..cf161cced1 --- /dev/null +++ b/sys/net/gnrc/transport_layer/tcp/gnrc_tcp_common.c @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2020 Simon Brummer + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup net_gnrc + * @{ + * + * @file + * @brief GNRC TCP common function implementation + * + * @author Simon Brummer + * @} + */ + +#include "internal/common.h" + +static tcb_list_t _list = {NULL, MUTEX_INIT}; + +tcb_list_t *_gnrc_tcp_common_get_tcb_list(void) +{ + return &_list; +} diff --git a/sys/net/gnrc/transport_layer/tcp/gnrc_tcp_eventloop.c b/sys/net/gnrc/transport_layer/tcp/gnrc_tcp_eventloop.c index 16c2934703..cbe37b57b4 100644 --- a/sys/net/gnrc/transport_layer/tcp/gnrc_tcp_eventloop.c +++ b/sys/net/gnrc/transport_layer/tcp/gnrc_tcp_eventloop.c @@ -216,8 +216,9 @@ static int _receive(gnrc_pktsnip_t *pkt) } /* Find TCB to for this packet */ - mutex_lock(&_list_tcb_lock); - tcb = _list_tcb_head; + tcb_list_t *list = _gnrc_tcp_common_get_tcb_list(); + mutex_lock(&list->lock); + tcb = list->head; while (tcb) { #ifdef MODULE_GNRC_IPV6 /* Check if current TCB is fitting for the incoming packet */ @@ -250,7 +251,7 @@ static int _receive(gnrc_pktsnip_t *pkt) #endif tcb = tcb->next; } - mutex_unlock(&_list_tcb_lock); + mutex_unlock(&list->lock); /* Call FSM with event RCVD_PKT if a fitting TCB was found */ /* cppcheck-suppress knownConditionTrueFalse diff --git a/sys/net/gnrc/transport_layer/tcp/gnrc_tcp_fsm.c b/sys/net/gnrc/transport_layer/tcp/gnrc_tcp_fsm.c index 7aef6701d8..3cb2f29bce 100644 --- a/sys/net/gnrc/transport_layer/tcp/gnrc_tcp_fsm.c +++ b/sys/net/gnrc/transport_layer/tcp/gnrc_tcp_fsm.c @@ -56,7 +56,8 @@ static int _is_local_port_in_use(const uint16_t port_number) { gnrc_tcp_tcb_t *iter = NULL; - LL_SEARCH_SCALAR(_list_tcb_head, iter, local_port, port_number); + tcb_list_t *list = _gnrc_tcp_common_get_tcb_list(); + LL_SEARCH_SCALAR(list->head, iter, local_port, port_number); return (iter != NULL); } @@ -124,6 +125,7 @@ static int _transition_to(gnrc_tcp_tcb_t *tcb, fsm_state_t state) DEBUG("_transition_to: %d\n", state); gnrc_tcp_tcb_t *iter = NULL; + tcb_list_t *list = _gnrc_tcp_common_get_tcb_list(); switch (state) { case FSM_STATE_CLOSED: @@ -131,9 +133,9 @@ static int _transition_to(gnrc_tcp_tcb_t *tcb, fsm_state_t state) _clear_retransmit(tcb); /* Remove connection from active connections */ - mutex_lock(&_list_tcb_lock); - LL_DELETE(_list_tcb_head, tcb); - mutex_unlock(&_list_tcb_lock); + mutex_lock(&list->lock); + LL_DELETE(list->head, tcb); + mutex_unlock(&list->lock); /* Free potentially allocated receive buffer */ _rcvbuf_release_buffer(tcb); @@ -153,25 +155,25 @@ static int _transition_to(gnrc_tcp_tcb_t *tcb, fsm_state_t state) tcb->peer_port = PORT_UNSPEC; /* Add connection to active connections (if not already active) */ - mutex_lock(&_list_tcb_lock); - LL_SEARCH(_list_tcb_head, iter, tcb, TCB_EQUAL); + mutex_lock(&list->lock); + LL_SEARCH(list->head, iter, tcb, TCB_EQUAL); if (iter == NULL) { - LL_PREPEND(_list_tcb_head, tcb); + LL_PREPEND(list->head, tcb); } - mutex_unlock(&_list_tcb_lock); + mutex_unlock(&list->lock); break; case FSM_STATE_SYN_SENT: /* Add connection to active connections (if not already active) */ - mutex_lock(&_list_tcb_lock); - LL_SEARCH(_list_tcb_head, iter, tcb, TCB_EQUAL); + mutex_lock(&list->lock); + LL_SEARCH(list->head, iter, tcb, TCB_EQUAL); /* If connection is not already active: Check port number, append TCB */ if (iter == NULL) { /* Check if port number was specified */ if (tcb->local_port != PORT_UNSPEC) { /* Check if given port number is in use: return error */ if (_is_local_port_in_use(tcb->local_port)) { - mutex_unlock(&_list_tcb_lock); + mutex_unlock(&list->lock); return -EADDRINUSE; } } @@ -179,9 +181,9 @@ static int _transition_to(gnrc_tcp_tcb_t *tcb, fsm_state_t state) else { tcb->local_port = _get_random_local_port(); } - LL_PREPEND(_list_tcb_head, tcb); + LL_PREPEND(list->head, tcb); } - mutex_unlock(&_list_tcb_lock); + mutex_unlock(&list->lock); break; case FSM_STATE_SYN_RCVD: @@ -443,7 +445,7 @@ static int _fsm_rcvd_pkt(gnrc_tcp_tcb_t *tcb, gnrc_pktsnip_t *in_pkt) uint16_t dst = byteorder_ntohs(tcp_hdr->dst_port); /* Check if SYN request is handled by another connection */ - lst = _list_tcb_head; + lst = _gnrc_tcp_common_get_tcb_list()->head; while (lst) { /* Compare port numbers and network layer addresses */ if (lst->local_port == dst && lst->peer_port == src) { diff --git a/sys/net/gnrc/transport_layer/tcp/internal/common.h b/sys/net/gnrc/transport_layer/tcp/internal/common.h index da646954d1..c564e37b57 100644 --- a/sys/net/gnrc/transport_layer/tcp/internal/common.h +++ b/sys/net/gnrc/transport_layer/tcp/internal/common.h @@ -115,14 +115,19 @@ extern "C" { #define GET_OFFSET( x ) (((x) & MSK_OFFSET) >> 12) /** - * @brief Head of linked TCB list. + * @brief TCB list type. */ -extern gnrc_tcp_tcb_t *_list_tcb_head; +typedef struct { + gnrc_tcp_tcb_t *head; /**< Head of TCB list */ + mutex_t lock; /**< Lock of TCB list */ +} tcb_list_t; /** - * @brief Mutex to protect TCB list. + * @brief Function to access to TCB list + * + * @returns Pointer to global TCB list. */ -extern mutex_t _list_tcb_lock; +tcb_list_t *_gnrc_tcp_common_get_tcb_list(void); #ifdef __cplusplus }