Skip to content

Commit

Permalink
FIXUP: Cleanup after review part1
Browse files Browse the repository at this point in the history
  • Loading branch information
brummer-simon committed Aug 9, 2021
1 parent 2aa08a4 commit 8f62149
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 102 deletions.
10 changes: 10 additions & 0 deletions sys/include/net/gnrc/tcp.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,23 @@
#include "net/gnrc/pkt.h"
#include "net/gnrc/tcp/tcb.h"

#ifdef SOCK_HAS_IPV6
#include "net/sock.h"
#else
#ifdef MODULE_GNRC_IPV6
#include "net/gnrc/ipv6.h"
#endif
#endif

#ifdef __cplusplus
extern "C" {
#endif

#ifdef SOCK_HAS_IPV6
// Re-use sock endpoint if sock is available and supporting IPv6.
typedef struct _sock_tl_ep gnrc_tcp_ep_t;

#else
/**
* @brief Address information for a single TCP connection endpoint.
* @extends sock_tcp_ep_t
Expand All @@ -49,6 +58,7 @@ typedef struct {
uint16_t netif; /**< Network interface ID */
uint16_t port; /**< Port number (in host byte order) */
} gnrc_tcp_ep_t;
#endif

/**
* @brief Initialize TCP connection endpoint.
Expand Down
6 changes: 3 additions & 3 deletions sys/include/net/gnrc/tcp/tcb.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ extern "C" {
/**
* @brief Transmission control block of GNRC TCP.
*/
typedef struct _transmission_control_block {
typedef struct sock_tcp {
uint8_t address_family; /**< Address Family of local_addr / peer_addr */
#ifdef MODULE_GNRC_IPV6
uint8_t local_addr[sizeof(ipv6_addr_t)]; /**< Local IP address */
Expand Down Expand Up @@ -76,13 +76,13 @@ typedef struct _transmission_control_block {
ringbuffer_t rcv_buf; /**< Receive buffer data structure */
mutex_t fsm_lock; /**< Mutex for FSM access synchronization */
mutex_t function_lock; /**< Mutex for function call synchronization */
struct _transmission_control_block *next; /**< Pointer next TCB */
struct sock_tcp *next; /**< Pointer next TCB */
} gnrc_tcp_tcb_t;

/**
* @brief Transmission control block queue.
*/
typedef struct _transmission_control_block_queue {
typedef struct sock_tcp_queue {
mutex_t lock; /**< Mutex for access synchronization */
gnrc_tcp_tcb_t *tcbs; /**< Pointer to TCB sequence */
size_t tcbs_len; /**< Number of TCBs behind member tcbs */
Expand Down
16 changes: 0 additions & 16 deletions sys/net/gnrc/sock/include/sock_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,22 +138,6 @@ struct sock_udp {
uint16_t flags; /**< option flags */
};

/**
* @brief TCP sock type
* @internal
*/
struct sock_tcp {
gnrc_tcp_tcb_t tcb; /**< tcb */
};

/**
* @brief TCP queue sock type
* @internal
*/
struct sock_tcp_queue {
gnrc_tcp_tcb_queue_t tcb_queue; /**< tcb queue*/
};

#ifdef __cplusplus
}
#endif
Expand Down
96 changes: 13 additions & 83 deletions sys/net/gnrc/sock/tcp/gnrc_sock_tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,13 @@ int sock_tcp_connect(sock_tcp_t *sock, const sock_tcp_ep_t *remote,

/* Asserts to protect GNRC_TCP. Flags are not supported. */
assert(flags == 0);
assert(sizeof(sock_tcp_t) == sizeof(gnrc_tcp_tcb_t));
assert(sizeof(sock_tcp_ep_t) == sizeof(gnrc_tcp_ep_t));

/* NOTE: GNRC_TCP and GNRC_SOCK_TCP types must have the same memory representation. */
gnrc_tcp_tcb_t *tcb = (gnrc_tcp_tcb_t *) sock;
gnrc_tcp_ep_t *ep = (gnrc_tcp_ep_t *) remote;

/* Initialize given TCB and try to open a connection */
gnrc_tcp_tcb_init(tcb);
gnrc_tcp_tcb_init(sock);

/* Forward call to gnrc_tcp_open. Return codes are identical except those that are
* not generated by gnrc_tcp_open: -ENETUNREACH and -EPERM */
return gnrc_tcp_open(tcb, ep, local_port);
return gnrc_tcp_open(sock, remote, local_port);
}

int sock_tcp_listen(sock_tcp_queue_t *queue, const sock_tcp_ep_t *local,
Expand All @@ -58,95 +52,51 @@ int sock_tcp_listen(sock_tcp_queue_t *queue, const sock_tcp_ep_t *local,

/* Asserts to protect GNRC_TCP. Flags are not supported. */
assert(flags == 0);
assert(sizeof(sock_tcp_queue_t) == sizeof(gnrc_tcp_tcb_queue_t));
assert(sizeof(sock_tcp_t) == sizeof(gnrc_tcp_tcb_t));
assert(sizeof(sock_tcp_ep_t) == sizeof(gnrc_tcp_ep_t));

/* NOTE: GNRC_TCP and GNRC_SOCK_TCP types must have the same memory representation. */
gnrc_tcp_tcb_queue_t *tcb_queue = (gnrc_tcp_tcb_queue_t *) queue;
gnrc_tcp_tcb_t *tcbs = (gnrc_tcp_tcb_t *) queue_array;
gnrc_tcp_ep_t *ep = (gnrc_tcp_ep_t *) local;

/* Initialize given TCB queue, all given tcbs and forward call */
gnrc_tcp_tcb_queue_init(tcb_queue);
gnrc_tcp_tcb_queue_init(queue);
for (unsigned i = 0; i < queue_len; ++i) {
gnrc_tcp_tcb_init(&tcbs[i]);
gnrc_tcp_tcb_init(&queue_array[i]);
}
return gnrc_tcp_listen(tcb_queue, tcbs, queue_len, ep);
return gnrc_tcp_listen(queue, queue_array, queue_len, local);
}

void sock_tcp_disconnect(sock_tcp_t *sock)
{
/* Asserts defined by API. */
assert(sock != NULL);

/* Asserts to protect GNRC_TCP. */
assert(sizeof(sock_tcp_t) == sizeof(gnrc_tcp_tcb_t));

/* NOTE: GNRC_TCP and GNRC_SOCK_TCP types must have the same memory representation. */
gnrc_tcp_tcb_t *tcb = (gnrc_tcp_tcb_t *) sock;
gnrc_tcp_close(tcb);
gnrc_tcp_close(sock);
}

void sock_tcp_stop_listen(sock_tcp_queue_t *queue)
{
/* Asserts defined by API. */
assert(queue != NULL);

/* Asserts to protect GNRC_TCP. */
assert(sizeof(sock_tcp_queue_t) == sizeof(gnrc_tcp_tcb_queue_t));

/* NOTE: GNRC_TCP and GNRC_SOCK_TCP types must have the same memory representation. */
gnrc_tcp_tcb_queue_t *tcb_queue = (gnrc_tcp_tcb_queue_t *) queue;
gnrc_tcp_stop_listen(tcb_queue);
gnrc_tcp_stop_listen(queue);
}

int sock_tcp_get_local(sock_tcp_t *sock, sock_tcp_ep_t *ep)
{
/* Asserts defined by API. */
assert(sock != NULL);
assert(ep != NULL);

/* Asserts to protect GNRC_TCP. */
assert(sizeof(sock_tcp_t) == sizeof(gnrc_tcp_tcb_t));
assert(sizeof(sock_tcp_ep_t) == sizeof(gnrc_tcp_ep_t));

/* NOTE: GNRC_TCP and GNRC_SOCK_TCP types must have the same memory representation. */
gnrc_tcp_tcb_t *tcb = (gnrc_tcp_tcb_t *) sock;
gnrc_tcp_ep_t *local = (gnrc_tcp_ep_t *) ep;
return gnrc_tcp_get_local(tcb, local);
return gnrc_tcp_get_local(sock, ep);
}

int sock_tcp_get_remote(sock_tcp_t *sock, sock_tcp_ep_t *ep)
{
/* Asserts defined by API. */
assert(sock != NULL);
assert(ep != NULL);

/* Asserts to protect GNRC_TCP. */
assert(sizeof(sock_tcp_t) == sizeof(gnrc_tcp_tcb_t));
assert(sizeof(sock_tcp_ep_t) == sizeof(gnrc_tcp_ep_t));

/* NOTE: GNRC_TCP and GNRC_SOCK_TCP types must have the same memory representation. */
gnrc_tcp_tcb_t *tcb = (gnrc_tcp_tcb_t *) sock;
gnrc_tcp_ep_t *remote = (gnrc_tcp_ep_t *) ep;
return gnrc_tcp_get_remote(tcb, remote);
return gnrc_tcp_get_remote(sock, ep);
}

int sock_tcp_queue_get_local(sock_tcp_queue_t *queue, sock_tcp_ep_t *ep)
{
/* Asserts defined by API. */
assert(queue != NULL);
assert(ep != NULL);

/* Asserts to protect GNRC_TCP. */
assert(sizeof(sock_tcp_queue_t) == sizeof(gnrc_tcp_tcb_queue_t));
assert(sizeof(sock_tcp_ep_t) == sizeof(gnrc_tcp_ep_t));

/* NOTE: GNRC_TCP and GNRC_SOCK_TCP types must have the same memory representation. */
gnrc_tcp_tcb_queue_t *tcb_queue = (gnrc_tcp_tcb_queue_t *) queue;
gnrc_tcp_ep_t *local = (gnrc_tcp_ep_t *) ep;
return gnrc_tcp_queue_get_local(tcb_queue, local);
return gnrc_tcp_queue_get_local(queue, ep);
}

int sock_tcp_accept(sock_tcp_queue_t *queue, sock_tcp_t **sock, uint32_t timeout)
Expand All @@ -155,14 +105,6 @@ int sock_tcp_accept(sock_tcp_queue_t *queue, sock_tcp_t **sock, uint32_t timeout
assert(queue != NULL);
assert(sock != NULL);

/* Asserts to protect GNRC_TCP. */
assert(sizeof(sock_tcp_queue_t) == sizeof(gnrc_tcp_tcb_queue_t));
assert(sizeof(sock_tcp_t) == sizeof(gnrc_tcp_tcb_t));

/* NOTE: GNRC_TCP and GNRC_SOCK_TCP types must have the same memory representation. */
gnrc_tcp_tcb_queue_t *tcb_queue = (gnrc_tcp_tcb_queue_t *) queue;
gnrc_tcp_tcb_t **tcb = (gnrc_tcp_tcb_t **) sock;

/* Map SOCK_NO_TIMEOUT to 0. 0 is used in GNRC_TCP for no timeout */
if (timeout == SOCK_NO_TIMEOUT) {
timeout = 0;
Expand All @@ -171,7 +113,7 @@ int sock_tcp_accept(sock_tcp_queue_t *queue, sock_tcp_t **sock, uint32_t timeout
/* Forward call to gnrc_tcp_accept.
* NOTE: Errorcodes -ECONNABORTED, -EPERM are not returned by
* gnrc_tcp_accept. All other error codes share the same semantics. */
return gnrc_tcp_accept(tcb_queue, tcb, timeout);
return gnrc_tcp_accept(queue, sock, timeout);
}

ssize_t sock_tcp_read(sock_tcp_t *sock, void *data, size_t max_len, uint32_t timeout)
Expand All @@ -181,19 +123,13 @@ ssize_t sock_tcp_read(sock_tcp_t *sock, void *data, size_t max_len, uint32_t tim
assert(data != NULL);
assert(max_len > 0);

/* Asserts to protect GNRC_TCP. */
assert(sizeof(sock_tcp_t) == sizeof(gnrc_tcp_tcb_t));

/* NOTE: GNRC_TCP and GNRC_SOCK_TCP types must have the same memory representation. */
gnrc_tcp_tcb_t *tcb = (gnrc_tcp_tcb_t *) sock;

/* Map SOCK_NO_TIMEOUT to 0. 0 is used in GNRC_TCP for no timeout */
if (timeout == SOCK_NO_TIMEOUT) {
timeout = 0;
}

/* Forward call to gnrc_tcp_recv: All error codes share the same semantics */
return gnrc_tcp_recv(tcb, data, max_len, timeout);
return gnrc_tcp_recv(sock, data, max_len, timeout);
}

ssize_t sock_tcp_write(sock_tcp_t *sock, const void *data, size_t len)
Expand All @@ -203,14 +139,8 @@ ssize_t sock_tcp_write(sock_tcp_t *sock, const void *data, size_t len)
assert(data != NULL);
assert(len > 0);

/* Asserts to protect GNRC_TCP. */
assert(sizeof(sock_tcp_t) == sizeof(gnrc_tcp_tcb_t));

/* NOTE: GNRC_TCP and GNRC_SOCK_TCP types must have the same memory representation. */
gnrc_tcp_tcb_t *tcb = (gnrc_tcp_tcb_t *) sock;

/* Forward call to gnrc_tcp_send.
* NOTE: gnrc_tcp_send offers a timeout. By setting it to 0, the call blocks
* until at least some data was transmitted. */
return gnrc_tcp_send(tcb, data, len, 0);
return gnrc_tcp_send(sock, data, len, 0);
}

0 comments on commit 8f62149

Please sign in to comment.