Skip to content

Commit

Permalink
net: context: add net_context api to check if a port is bound
Browse files Browse the repository at this point in the history
This change adds net_context_port_in_use(), which is a simple
wrapper around net_context_check_port() and is used to check
if a particular socket is bound to a given IP address.

Fixes #29649

Signed-off-by: Christopher Friedt <chrisfriedt@gmail.com>
  • Loading branch information
cfriedt authored and andrewboie committed Nov 10, 2020
1 parent df43f17 commit 5c69149
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
16 changes: 16 additions & 0 deletions include/net/net_context.h
Expand Up @@ -1125,6 +1125,22 @@ static inline void net_context_setup_pools(struct net_context *context,
#define net_context_setup_pools(context, tx_pool, data_pool)
#endif

/**
* @brief Check if a port is in use (bound)
*
* This function checks if a port is bound with respect to the specified
* @p ip_proto and @p local_addr.
*
* @param ip_proto the IP protocol
* @param local_port the port to check
* @param local_addr the network address
*
* @return true if the port is bound
* @return false if the port is not bound
*/
bool net_context_port_in_use(enum net_ip_protocol ip_proto,
uint16_t local_port, const struct sockaddr *local_addr);

#ifdef __cplusplus
}
#endif
Expand Down
7 changes: 7 additions & 0 deletions subsys/net/ip/net_context.c
Expand Up @@ -122,6 +122,13 @@ static uint16_t find_available_port(struct net_context *context,
#define find_available_port(...) 0
#endif

bool net_context_port_in_use(enum net_ip_protocol ip_proto,
uint16_t local_port,
const struct sockaddr *local_addr)
{
return check_used_port(ip_proto, htons(local_port), local_addr) != 0;
}

int net_context_get(sa_family_t family,
enum net_sock_type type,
uint16_t ip_proto,
Expand Down

0 comments on commit 5c69149

Please sign in to comment.