Skip to content

Commit e8e814c

Browse files
pfalconjukkar
authored andcommitted
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 <[email protected]>
1 parent c0a15c8 commit e8e814c

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

include/net/sntp.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ int sntp_init(struct sntp_ctx *ctx, struct sockaddr *addr,
4949
*
5050
* @param ctx Address of sntp context.
5151
* @param timeout Timeout of waiting for sntp response (in milliseconds).
52-
* @param epoch_time Seconds since 1 January 1970.
52+
* @param epoch_time Seconds since 1 January 1970 (output).
5353
*
54-
* @return 0 if ok, <0 if error.
54+
* @return 0 if ok, <0 if error (-ETIMEDOUT if timeout).
5555
*/
5656
int sntp_request(struct sntp_ctx *ctx, u32_t timeout, u64_t *epoch_time);
5757

subsys/net/lib/sntp/sntp.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,16 @@ static int sntp_recv_response(struct sntp_ctx *sntp, u32_t timeout,
105105
int status;
106106
int rcvd;
107107

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

114+
if (status == 0) {
115+
return -ETIMEDOUT;
116+
}
117+
113118
rcvd = recv(sntp->sock.fd, (u8_t *)&buf, sizeof(buf), 0);
114119
if (rcvd < 0) {
115120
return -errno;

0 commit comments

Comments
 (0)