From 62105f16ea1cdf6e593efd6991eefc366bbe2c4b Mon Sep 17 00:00:00 2001 From: bluhm <> Date: Sat, 18 Jul 2015 21:50:47 +0000 Subject: [PATCH] Handle short writes and TLS_{READ,WRITE}_AGAIN around tls_write(). input doug@; OK beck@ --- src/usr.sbin/ntpd/constraint.c | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/usr.sbin/ntpd/constraint.c b/src/usr.sbin/ntpd/constraint.c index e688b9f7..cb20d621 100644 --- a/src/usr.sbin/ntpd/constraint.c +++ b/src/usr.sbin/ntpd/constraint.c @@ -1,4 +1,4 @@ -/* $OpenBSD: constraint.c,v 1.13 2015/07/18 20:32:38 bcook Exp $ */ +/* $OpenBSD: constraint.c,v 1.14 2015/07/18 21:50:47 bluhm Exp $ */ /* * Copyright (c) 2015 Reyk Floeter @@ -641,8 +641,9 @@ httpsdate_free(void *arg) int httpsdate_request(struct httpsdate *httpsdate, struct timeval *when) { - size_t outlen = 0, maxlength = CONSTRAINT_MAXHEADERLENGTH; - char *line, *p; + size_t outlen = 0, maxlength = CONSTRAINT_MAXHEADERLENGTH, len; + char *line, *p, *buf; + int ret; if ((httpsdate->tls_ctx = tls_client()) == NULL) goto fail; @@ -657,10 +658,17 @@ httpsdate_request(struct httpsdate *httpsdate, struct timeval *when) goto fail; } - if (tls_write(httpsdate->tls_ctx, - httpsdate->tls_request, strlen(httpsdate->tls_request), - &outlen) == -1) - goto fail; + buf = httpsdate->tls_request; + len = strlen(httpsdate->tls_request); + while (len > 0) { + ret = tls_write(httpsdate->tls_ctx, buf, len, &outlen); + if (ret == TLS_READ_AGAIN || ret == TLS_WRITE_AGAIN) + continue; + if (ret < 0) + goto fail; + buf += outlen; + len -= outlen; + } while ((line = tls_readline(httpsdate->tls_ctx, &outlen, &maxlength, when)) != NULL) {