mirror of
https://gitlab.com/chrony/chrony.git
synced 2025-12-03 18:45:07 -05:00
socket: avoid unnecessary bind() call
Don't call bind() if the specified local address of a socket has port 0 and the "any" address. It will be bound automatically on connect() or sendmsg().
This commit is contained in:
15
socket.c
15
socket.c
@@ -258,6 +258,18 @@ set_ip_options(int sock_fd, int family, int flags)
|
|||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
|
|
||||||
|
static int
|
||||||
|
is_any_address(IPAddr *addr)
|
||||||
|
{
|
||||||
|
IPAddr any_addr;
|
||||||
|
|
||||||
|
SCK_GetAnyLocalIPAddress(addr->family, &any_addr);
|
||||||
|
|
||||||
|
return UTI_CompareIPs(&any_addr, addr, NULL) == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
static int
|
static int
|
||||||
bind_ip_address(int sock_fd, IPSockAddr *addr, int flags)
|
bind_ip_address(int sock_fd, IPSockAddr *addr, int flags)
|
||||||
{
|
{
|
||||||
@@ -352,8 +364,9 @@ open_ip_socket(IPSockAddr *remote_addr, IPSockAddr *local_addr, int type, int fl
|
|||||||
if (!set_ip_options(sock_fd, family, flags))
|
if (!set_ip_options(sock_fd, family, flags))
|
||||||
goto error;
|
goto error;
|
||||||
|
|
||||||
/* Bind the socket if a local address was specified */
|
/* Bind the socket if a non-any local address/port was specified */
|
||||||
if (local_addr && local_addr->ip_addr.family != IPADDR_UNSPEC &&
|
if (local_addr && local_addr->ip_addr.family != IPADDR_UNSPEC &&
|
||||||
|
(local_addr->port != 0 || !is_any_address(&local_addr->ip_addr)) &&
|
||||||
!bind_ip_address(sock_fd, local_addr, flags))
|
!bind_ip_address(sock_fd, local_addr, flags))
|
||||||
goto error;
|
goto error;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user