net: tcp: Add better type safety for connection handlers

This creates a `struct net_conn_handle` that's only used as an opaque
pointer.  The purpose is to avoid assigning a void* to a void** in
connection registering/unregistering functions, avoiding a
previously-caught bug by issuing an incompatible pointer types warning:

	warning: passing argument 7 of 'net_tcp_register' from
		incompatible pointer type [-Wincompatible-pointer-types]

Suggested by Andy Ross in 5beec6.  This particular commit didn't catch
any bugs, but the one caught in 5beec6 wouldn't exist if this were in
place at the time.

Change-Id: I5c13fb4c5826adce6397feb7b400d36e426c4a87
Signed-off-by: Leandro Pereira <leandro.pereira@intel.com>
This commit is contained in:
Leandro Pereira 2016-11-03 14:18:35 -07:00 committed by Jukka Rissanen
parent 2d4e7b6c6e
commit ad1a9d088d
7 changed files with 19 additions and 15 deletions

View file

@ -125,6 +125,8 @@ typedef void (*net_context_accept_cb_t)(struct net_context *new_context,
struct net_tcp;
struct net_conn_handle;
/**
* Note that we do not store the actual source IP address in the context
* because the address is already be set in the network interface struct.
@ -141,7 +143,7 @@ struct net_context {
struct sockaddr remote;
/** Connection handle */
void *conn_handler;
struct net_conn_handle *conn_handler;
/** Receive callback to be called when desired packet
* has been received.

View file

@ -347,7 +347,7 @@ static inline enum net_verdict cache_check(enum net_ip_protocol proto,
#define cache_check(...) NET_CONTINUE
#endif /* CONFIG_NET_CONN_CACHE */
int net_conn_unregister(void *handle)
int net_conn_unregister(struct net_conn_handle *handle)
{
struct net_conn *conn = (struct net_conn *)handle;
@ -427,7 +427,7 @@ int net_conn_register(enum net_ip_protocol proto,
uint16_t local_port,
net_conn_cb_t cb,
void *user_data,
void **handle)
struct net_conn_handle **handle)
{
int i;
uint8_t rank = 0;
@ -556,7 +556,7 @@ int net_conn_register(enum net_ip_protocol proto,
#endif /* NET_DEBUG */
if (handle) {
*handle = &conns[i];
*handle = (struct net_conn_handle *)&conns[i];
}
return 0;

View file

@ -37,6 +37,8 @@ extern "C" {
struct net_conn;
struct net_conn_handle;
/**
* @brief Function that is called by connection subsystem when UDP/TCP
* packet is received and which matches local and remote IP address
@ -95,7 +97,7 @@ struct net_conn {
* @param local_port Local port of the connection end point.
* @param cb Callback to be called
* @param user_data User data supplied by caller.
* @param handle UDP handle that can be used when unregistering
* @param handle Connection handle that can be used when unregistering
*
* @return Return 0 if the registration succeed, <0 otherwise.
*/
@ -106,7 +108,7 @@ int net_conn_register(enum net_ip_protocol proto,
uint16_t local_port,
net_conn_cb_t cb,
void *user_data,
void **handle);
struct net_conn_handle **handle);
/**
* @brief Unregister connection handler.
@ -115,7 +117,7 @@ int net_conn_register(enum net_ip_protocol proto,
*
* @return Return 0 if the unregistration succeed, <0 otherwise.
*/
int net_conn_unregister(void *handle);
int net_conn_unregister(struct net_conn_handle *handle);
/**
* @brief Called by net_core.c when a network packet is received.

View file

@ -162,7 +162,7 @@ static inline int net_tcp_register(const struct sockaddr *remote_addr,
uint16_t local_port,
net_conn_cb_t cb,
void *user_data,
void **handle)
struct net_conn_handle **handle)
{
return net_conn_register(IPPROTO_TCP, remote_addr, local_addr,
remote_port, local_port, cb, user_data,
@ -176,7 +176,7 @@ static inline int net_tcp_register(const struct sockaddr *remote_addr,
*
* @return Return 0 if the unregistration succeed, <0 otherwise.
*/
static inline int net_tcp_unregister(void *handle)
static inline int net_tcp_unregister(struct net_conn_handle *handle)
{
return net_conn_unregister(handle);
}

View file

@ -91,7 +91,7 @@ static inline int net_udp_register(const struct sockaddr *remote_addr,
uint16_t local_port,
net_conn_cb_t cb,
void *user_data,
void **handle)
struct net_conn_handle **handle)
{
return net_conn_register(IPPROTO_UDP, remote_addr, local_addr,
remote_port, local_port, cb, user_data,
@ -105,7 +105,7 @@ static inline int net_udp_register(const struct sockaddr *remote_addr,
*
* @return Return 0 if the unregistration succeed, <0 otherwise.
*/
static inline int net_udp_unregister(void *handle)
static inline int net_udp_unregister(struct net_conn_handle *handle)
{
return net_conn_unregister(handle);
}

View file

@ -222,7 +222,7 @@ struct ud {
uint16_t remote_port;
uint16_t local_port;
char *test;
void *handle;
struct net_conn_handle *handle;
};
static struct ud *returned_ud;
@ -446,7 +446,7 @@ static void set_port(sa_family_t family, struct sockaddr *raddr,
static bool test_register(void)
{
void *handlers[CONFIG_NET_MAX_CONN];
struct net_conn_handle *handlers[CONFIG_NET_MAX_CONN];
struct net_if *iface = net_if_get_default();
struct net_if_addr *ifaddr;
struct ud *ud;

View file

@ -363,8 +363,8 @@ static void set_port(sa_family_t family, struct sockaddr *raddr,
static bool run_tests(void)
{
void *handlers[CONFIG_NET_MAX_CONN];
struct net_if *iface = net_if_get_default();;
struct net_conn_handle *handlers[CONFIG_NET_MAX_CONN];
struct net_if *iface = net_if_get_default();
struct net_if_addr *ifaddr;
struct ud *ud;
int ret, i = 0;