Commit 5c691491 authored by Christopher Friedt's avatar Christopher Friedt Committed by Andrew Boie
Browse files

net: context: add net_context api to check if a port is bound



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: default avatarChristopher Friedt <chrisfriedt@gmail.com>
parent df43f178
Loading
Loading
Loading
Loading
+16 −0
Original line number Diff line number Diff line
@@ -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
+7 −0
Original line number Diff line number Diff line
@@ -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,