Debian Codegraph @

crt/s2n/docs/examples/s2n_send.c

raw symbols SCIP
1 /*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License").
5 * You may not use this file except in compliance with the License.
6 * A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0
9 *
10 * or in the "license" file accompanying this file. This file is distributed
11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 * express or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 */
15
16 #include "s2n.h"
17
18 int s2n_example_send(struct s2n_connection *conn, uint8_t *data, size_t data_size)
19 {
20 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
21 int bytes_written = 0;
22 while (bytes_written < data_size) {
23 int w = s2n_send(conn, data + bytes_written, data_size - bytes_written, &blocked);
24 if (w >= 0) {
25 /* `w` bytes were successfully sent. Update `bytes_written` so the next call to
26 * `s2n_send()` will send data from the correct offset in the buffer.
27 */
28 bytes_written += w;
29 } else if (s2n_error_get_type(s2n_errno) == S2N_ERR_T_BLOCKED) {
30 /* The return of `s2n_send()` indicates an error. If this error is `S2N_ERR_T_BLOCKED`,
31 * the socket is blocked waiting to send more data.
32 *
33 * In a typical non-blocking environment, `poll()` or `select()` would be used re-enter
34 * `s2n_example_send()` and call `s2n_send()` again after the socket is ready to send
35 * more data. But this example just busy-waits, so we immediately call `s2n_send()`
36 * again.
37 */
38 continue;
39 } else {
40 /* `s2n_send()` encountered an irrecoverable error. Log the error and return. */
41 fprintf(stderr, "Error: %s. %s\n", s2n_strerror(s2n_errno, NULL), s2n_strerror_debug(s2n_errno, NULL));
42 return -1;
43 }
44 }
45 return 0;
46 }
47
48 /* s2n_sendv_with_offset is not part of the public API on Windows. */
49 #ifndef _WIN32
50 int s2n_example_sendv(struct s2n_connection *conn, uint8_t *data, size_t data_size)
51 {
52 struct iovec iov[1] = { 0 };
53 iov[0].iov_base = data;
54 iov[0].iov_len = data_size;
55
56 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
57 int bytes_written = 0;
58 while (bytes_written < data_size) {
59 int w = s2n_sendv_with_offset(conn, iov, 1, bytes_written, &blocked);
60 if (w >= 0) {
61 bytes_written += w;
62 } else if (s2n_error_get_type(s2n_errno) != S2N_ERR_T_BLOCKED) {
63 fprintf(stderr, "Error: %s. %s\n", s2n_strerror(s2n_errno, NULL), s2n_strerror_debug(s2n_errno, NULL));
64 return -1;
65 }
66 }
67 return 0;
68 }
69 #endif