net: net_pkt_append: Refactor to return length of data actually added

For stream-based protocols (TCP), adding less data than requested
("short write") is generally not a problem - the rest of data can
be sent in the next packet. So, make net_pkt_append() return length
of written data instead of just bool flag, which makes it closer
to the behavior of POSIX send()/write() calls.

There're many users of older net_pkt_append() in the codebase
however, so net_pkt_append_all() convenience function is added which
keeps returning a boolean flag. All current users were converted to
this function, except for two:

samples/net/http_server/src/ssl_utils.c
samples/net/mbedtls_sslclient/src/tcp.c

Both are related to TLS and implement mbedTLS "tx callback", which
follows POSIX short-write semantics. Both cases also had a code to
workaround previous boolean-only behavior of net_pkt_append() - after
calling it, they measured length of the actual data added (but only
in case of successful return of net_pkt_append(), so that didn't
really help). So, these 2 cases are already improved.

Jira: ZEP-1984

Change-Id: Ibaf7c029b15e91b516d73dab3612eed190ee982b
Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
This commit is contained in:
Paul Sokolovsky 2017-04-21 13:55:45 +03:00 committed by Jukka Rissanen
commit 25307d5331
25 changed files with 99 additions and 80 deletions

View file

@ -27,35 +27,35 @@ int http_request(struct net_context *net_ctx, s32_t timeout,
return -ENOMEM;
}
if (!net_pkt_append(tx, strlen(req->method), (u8_t *)req->method,
if (!net_pkt_append_all(tx, strlen(req->method), (u8_t *)req->method,
timeout)) {
goto lb_exit;
}
if (!net_pkt_append(tx, strlen(req->url), (u8_t *)req->url,
if (!net_pkt_append_all(tx, strlen(req->url), (u8_t *)req->url,
timeout)) {
goto lb_exit;
}
if (!net_pkt_append(tx, strlen(req->protocol),
if (!net_pkt_append_all(tx, strlen(req->protocol),
(u8_t *)req->protocol, timeout)) {
goto lb_exit;
}
if (!net_pkt_append(tx, strlen(req->header_fields),
if (!net_pkt_append_all(tx, strlen(req->header_fields),
(u8_t *)req->header_fields,
timeout)) {
goto lb_exit;
}
if (req->content_type_value) {
if (!net_pkt_append(tx, strlen(HTTP_CONTENT_TYPE),
if (!net_pkt_append_all(tx, strlen(HTTP_CONTENT_TYPE),
(u8_t *)HTTP_CONTENT_TYPE,
timeout)) {
goto lb_exit;
}
if (!net_pkt_append(tx, strlen(req->content_type_value),
if (!net_pkt_append_all(tx, strlen(req->content_type_value),
(u8_t *)req->content_type_value,
timeout)) {
goto lb_exit;
@ -73,13 +73,13 @@ int http_request(struct net_context *net_ctx, s32_t timeout,
goto lb_exit;
}
if (!net_pkt_append(tx, rc, (u8_t *)content_len_str,
if (!net_pkt_append_all(tx, rc, (u8_t *)content_len_str,
timeout)) {
rc = -ENOMEM;
goto lb_exit;
}
if (!net_pkt_append(tx, req->payload_size,
if (!net_pkt_append_all(tx, req->payload_size,
(u8_t *)req->payload,
timeout)) {
rc = -ENOMEM;
@ -87,7 +87,7 @@ int http_request(struct net_context *net_ctx, s32_t timeout,
}
} else {
if (!net_pkt_append(tx, strlen(HTTP_EOF),
if (!net_pkt_append_all(tx, strlen(HTTP_EOF),
(u8_t *)HTTP_EOF,
timeout)) {
goto lb_exit;