Skip to content
Permalink
Browse files

net: sntp: Handle case of request timeout

Previously, a case when poll() call timed out wasn't handled, and
recv() was called unconditionally. In the case of timeout, recv()
itself would hang indefinitely.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
  • Loading branch information...
pfalcon authored and jukkar committed Apr 20, 2019
1 parent c0a15c8 commit e8e814c6311b49f2cc6af2d4a5dceae339dddb3f
Showing with 8 additions and 3 deletions.
  1. +2 −2 include/net/sntp.h
  2. +6 −1 subsys/net/lib/sntp/sntp.c
@@ -49,9 +49,9 @@ int sntp_init(struct sntp_ctx *ctx, struct sockaddr *addr,
*
* @param ctx Address of sntp context.
* @param timeout Timeout of waiting for sntp response (in milliseconds).
* @param epoch_time Seconds since 1 January 1970.
* @param epoch_time Seconds since 1 January 1970 (output).
*
* @return 0 if ok, <0 if error.
* @return 0 if ok, <0 if error (-ETIMEDOUT if timeout).
*/
int sntp_request(struct sntp_ctx *ctx, u32_t timeout, u64_t *epoch_time);

@@ -105,11 +105,16 @@ static int sntp_recv_response(struct sntp_ctx *sntp, u32_t timeout,
int status;
int rcvd;

if (poll(sntp->sock.fds, sntp->sock.nfds, timeout) < 0) {
status = poll(sntp->sock.fds, sntp->sock.nfds, timeout);
if (status < 0) {
NET_ERR("Error in poll:%d", errno);
return -errno;
}

if (status == 0) {
return -ETIMEDOUT;
}

rcvd = recv(sntp->sock.fd, (u8_t *)&buf, sizeof(buf), 0);
if (rcvd < 0) {
return -errno;

0 comments on commit e8e814c

Please sign in to comment.
You can’t perform that action at this time.