diff options
| author | Nicolas BESNARD | 2024-10-29 10:06:15 +0000 |
|---|---|---|
| committer | Álvaro Fernández Rojas | 2025-11-03 15:14:48 +0000 |
| commit | 6466314e7f623fffd2a9d00a87e8902fad0dfadf (patch) | |
| tree | 8c9e27031a6a2cf3b89fd9d2e03f32506ed73cc5 | |
| parent | 1df65f0caf4647288972b637c89aa4f226c92a7a (diff) | |
| download | odhcp6c-6466314e7f623fffd2a9d00a87e8902fad0dfadf.tar.gz | |
odhcp6c: enable Non-Blocking DHCPv6 Socket
Problem:
The DHCPv6 socket is currently set to blocking mode, which causes
the program to pause execution whenever it attempts to read from the
socket. This can lead to delays and hinder the overall
responsiveness of the application.
Solution:
Add the set_nonblocking() function, using fcntl() to set the
O_NONBLOCK flag, allowing the socket to operate in non-blocking
mode.
Signed-off-by: Nicolas BESNARD <nico.besnard31@gmail.com>
Signed-off-by: Paul Donald <newtwen+github@gmail.com>
Link: https://github.com/openwrt/odhcp6c/pull/106
Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>
| -rw-r--r-- | src/dhcpv6.c | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/src/dhcpv6.c b/src/dhcpv6.c index 42309c7..410a2f5 100644 --- a/src/dhcpv6.c +++ b/src/dhcpv6.c @@ -193,6 +193,27 @@ static char *dhcpv6_status_code_to_str(uint16_t code) return "Unknown"; } +static int fd_set_nonblocking(int sockfd) +{ + int flags = fcntl(sockfd, F_GETFL, 0); + if (flags == -1) { + syslog(LOG_ERR, + "Failed to get the dhcpv6 socket flags: fcntl F_GETFL failed (%s)", + strerror(errno)); + return -1; + } + + // Set the socket to non-blocking + if (fcntl(sockfd, F_SETFL, flags | O_NONBLOCK) == -1) { + syslog(LOG_ERR, + "Failed to set the dhcpv6 socket to non-blocking: fcntl F_SETFL failed (%s)", + strerror(errno)); + return -1; + } + + return 0; +} + int init_dhcpv6(const char *ifname, unsigned int options, int sk_prio, int sol_timeout, unsigned int dscp) { client_options = options; @@ -211,6 +232,10 @@ int init_dhcpv6(const char *ifname, unsigned int options, int sk_prio, int sol_t ifindex = ifr.ifr_ifindex; + // Set the socket to non-blocking mode + if (fd_set_nonblocking(sock) < 0) + goto failure; + // Create client DUID size_t client_id_len; odhcp6c_get_state(STATE_CLIENT_ID, &client_id_len); |