Debian Codegraph @

src/vendor/azure-uamqp-c/deps/azure-c-shared-utility/pal/socket_async.c

raw
1 // Copyright (c) Microsoft. All rights reserved.
2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
4 #include <stdbool.h>
5 #include <stdint.h>
6
7 // This file is OS-specific, and is identified by setting include directories
8 // in the project
9 #include "socket_async_os.h"
10
11 #include "socket_async.h"
12 #include "azure_c_shared_utility/xlogging.h"
13
14
15 static int get_socket_errno(int file_descriptor)
16 {
17 int sock_errno = 0;
18 socklen_t optlen = sizeof(sock_errno);
19 getsockopt(file_descriptor, SOL_SOCKET, SO_ERROR, &sock_errno, &optlen);
20 return sock_errno;
21 }
22
23 SOCKET_ASYNC_HANDLE socket_async_create(uint32_t serverIPv4, uint16_t port,
24 bool is_UDP, SOCKET_ASYNC_OPTIONS_HANDLE options)
25 {
26 SOCKET_ASYNC_HANDLE result;
27 struct sockaddr_in sock_addr;
28
29 /* Codes_SRS_SOCKET_ASYNC_30_013: [ The is_UDP parameter shall be true for a UDP connection, and false for TCP. ]*/
30 int sock = socket(AF_INET, is_UDP ? SOCK_DGRAM : SOCK_STREAM, 0);
31 if (sock < 0)
32 {
33 /* Codes_SRS_SOCKET_ASYNC_30_010: [ If socket option creation fails, socket_async_create shall log an error and return SOCKET_ASYNC_INVALID_SOCKET. ]*/
34 // An essentially impossible failure, not worth logging errno()
35 LogError("create socket failed");
36 result = SOCKET_ASYNC_INVALID_SOCKET;
37 }
38 else
39 {
40 bool setopt_ok;
41 int setopt_return;
42 // None of the currently defined options apply to UDP
43 /* Codes_SRS_SOCKET_ASYNC_30_015: [ If is_UDP is true, the optional options parameter shall be ignored. ]*/
44 if (!is_UDP)
45 {
46 if (options != NULL)
47 {
48 /* Codes_SRS_SOCKET_ASYNC_30_014: [ If the optional options parameter is non-NULL and is_UDP is false, and options->keep_alive is non-negative, socket_async_create shall set the socket options to the provided options values. ]*/
49 if (options->keep_alive >= 0)
50 {
51 int keepAlive = 1; //enable keepalive
52 setopt_ok = 0 == (setopt_return = setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (void *)&keepAlive, sizeof(keepAlive)));
53 setopt_ok = setopt_ok && 0 == (setopt_return = setsockopt(sock, IPPROTO_TCP, TCP_KEEPIDLE, (void *)&(options->keep_idle), sizeof((options->keep_idle))));
54 setopt_ok = setopt_ok && 0 == (setopt_return = setsockopt(sock, IPPROTO_TCP, TCP_KEEPINTVL, (void *)&(options->keep_interval), sizeof((options->keep_interval))));
55 setopt_ok = setopt_ok && 0 == (setopt_return = setsockopt(sock, IPPROTO_TCP, TCP_KEEPCNT, (void *)&(options->keep_count), sizeof((options->keep_count))));
56 }
57 else
58 {
59 /* Codes_SRS_SOCKET_ASYNC_30_015: [ If the optional options parameter is non-NULL and is_UDP is false, and options->keep_alive is negative, socket_async_create not set the socket keep-alive options. ]*/
60 // < 0 means use system defaults, so do nothing
61 setopt_ok = true;
62 setopt_return = 0;
63 }
64 }
65 else
66 {
67 /* Codes_SRS_SOCKET_ASYNC_30_017: [ If the optional options parameter is NULL and is_UDP is false, socket_async_create shall disable TCP keep-alive. ]*/
68 int keepAlive = 0; //disable keepalive
69 setopt_ok = 0 == setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (void *)&keepAlive, sizeof(keepAlive));
70 setopt_return = 0;
71 }
72 }
73 else
74 {
75 setopt_ok = true;
76 setopt_return = 0;
77 }
78
79 // NB: On full-sized (multi-process) systems it would be necessary to use the SO_REUSEADDR option to
80 // grab the socket from any earlier (dying) invocations of the process and then deal with any
81 // residual junk in the connection stream. This doesn't happen with embedded, so it doesn't need
82 // to be defended against.
83
84 if (!setopt_ok)
85 {
86 /* Codes_SRS_SOCKET_ASYNC_30_020: [ If socket option setting fails, socket_async_create shall log an error and return SOCKET_ASYNC_INVALID_SOCKET. ]*/
87 // setsockopt has no real possibility of failing due to the way it's being used here, so there's no need
88 // to spend memory trying to log the not-really-possible errno.
89 LogError("setsockopt failed: %d", setopt_return);
90 result = SOCKET_ASYNC_INVALID_SOCKET;
91 }
92 else
93 {
94 /* Codes_SRS_SOCKET_ASYNC_30_019: [ The socket returned shall be non-blocking. ]*/
95 // When supplied with either F_GETFL and F_SETFL parameters, the fcntl function
96 // does simple bit flips which have no error path, so it is not necessary to
97 // check for errors. (Source checked for linux and lwIP).
98 int originalFlags = fcntl(sock, F_GETFL, 0);
99 int bind_ret;
100
101 (void)fcntl(sock, F_SETFL, originalFlags | O_NONBLOCK);
102
103 memset(&sock_addr, 0, sizeof(sock_addr));
104 sock_addr.sin_family = AF_INET;
105 sock_addr.sin_addr.s_addr = 0;
106 sock_addr.sin_port = 0; // random local port
107
108 bind_ret = bind(sock, (const struct sockaddr*)&sock_addr, sizeof(sock_addr));
109
110 if (bind_ret != 0)
111 {
112 /* Codes_SRS_SOCKET_ASYNC_30_021: [ If socket binding fails, socket_async_create shall log an error and return SOCKET_ASYNC_INVALID_SOCKET. ]*/
113 LogError("bind socket failed: %d", get_socket_errno(sock));
114 result = SOCKET_ASYNC_INVALID_SOCKET;
115 }
116 else
117 {
118 int connect_ret;
119
120 memset(&sock_addr, 0, sizeof(sock_addr));
121 sock_addr.sin_family = AF_INET;
122 /* Codes_SRS_SOCKET_ASYNC_30_011: [ The host_ipv4 parameter shall be the 32-bit IP V4 of the target server. ]*/
123 sock_addr.sin_addr.s_addr = serverIPv4;
124 /* Codes_SRS_SOCKET_ASYNC_30_012: [ The port parameter shall be the port number for the target server. ]*/
125 sock_addr.sin_port = htons(port);
126
127 connect_ret = connect(sock, (const struct sockaddr*)&sock_addr, sizeof(sock_addr));
128 if (connect_ret == -1)
129 {
130 int sockErr = get_socket_errno(sock);
131 if (sockErr != EINPROGRESS)
132 {
133 /* Codes_SRS_SOCKET_ASYNC_30_022: [ If socket connection fails, socket_async_create shall log an error and return SOCKET_ASYNC_INVALID_SOCKET. ]*/
134 LogError("Socket connect failed, not EINPROGRESS: %d", sockErr);
135 result = SOCKET_ASYNC_INVALID_SOCKET;
136 }
137 else
138 {
139 // This is the normally expected code path for our non-blocking socket
140 /* Codes_SRS_SOCKET_ASYNC_30_018: [ On success, socket_async_create shall return the created and configured SOCKET_ASYNC_HANDLE. ]*/
141 result = sock;
142 }
143 }
144 else
145 {
146 /* Codes_SRS_SOCKET_ASYNC_30_018: [ On success, socket_async_create shall return the created and configured SOCKET_ASYNC_HANDLE. ]*/
147 // This result would be a surprise because a non-blocking socket
148 // returns EINPROGRESS. But it could happen if this thread got
149 // blocked for a while by the system while the handshake proceeded,
150 // or for a UDP socket.
151 result = sock;
152 }
153 }
154 }
155 }
156
157 return result;
158 }
159
160 int socket_async_is_create_complete(SOCKET_ASYNC_HANDLE sock, bool* is_complete)
161 {
162 int result;
163 if (is_complete == NULL)
164 {
165 /* Codes_SRS_SOCKET_ASYNC_30_026: [ If the is_complete parameter is NULL, socket_async_is_create_complete shall log an error and return FAILURE. ]*/
166 LogError("is_complete is NULL");
167 result = MU_FAILURE;
168 }
169 else
170 {
171 struct timeval tv;
172 int select_ret;
173
174 // Check if the socket is ready to perform a write.
175 fd_set writeset;
176 fd_set errset;
177 FD_ZERO(&writeset);
178 FD_ZERO(&errset);
179 FD_SET(sock, &writeset);
180 FD_SET(sock, &errset);
181
182 tv.tv_sec = 0;
183 tv.tv_sec = 0;
184 select_ret = select(sock + 1, NULL, &writeset, &errset, &tv);
185 if (select_ret < 0)
186 {
187 /* Codes_SRS_SOCKET_ASYNC_30_028: [ On failure, the is_complete value shall be set to false and socket_async_create shall return FAILURE. ]*/
188 LogError("Socket select failed: %d", get_socket_errno(sock));
189 result = MU_FAILURE;
190 }
191 else
192 {
193 if (FD_ISSET(sock, &errset))
194 {
195 /* Codes_SRS_SOCKET_ASYNC_30_028: [ On failure, the is_complete value shall be set to false and socket_async_create shall return FAILURE. ]*/
196 LogError("Socket select errset non-empty: %d", get_socket_errno(sock));
197 result = MU_FAILURE;
198 }
199 else if (FD_ISSET(sock, &writeset))
200 {
201 /* Codes_SRS_SOCKET_ASYNC_30_027: [ On success, the is_complete value shall be set to the completion state and socket_async_create shall return 0. ]*/
202 // Ready to read
203 result = 0;
204 *is_complete = true;
205 }
206 else
207 {
208 /* Codes_SRS_SOCKET_ASYNC_30_027: [ On success, the is_complete value shall be set to the completion state and socket_async_create shall return 0. ]*/
209 // Not ready yet
210 result = 0;
211 *is_complete = false;
212 }
213 }
214 }
215 return result;
216 }
217
218 int socket_async_send(SOCKET_ASYNC_HANDLE sock, const void* buffer, size_t size, size_t* sent_count)
219 {
220 int result;
221 if (buffer == NULL)
222 {
223 /* Codes_SRS_SOCKET_ASYNC_30_033: [ If the buffer parameter is NULL, socket_async_send shall log the error return FAILURE. ]*/
224 LogError("buffer is NULL");
225 result = MU_FAILURE;
226 }
227 else
228 {
229 if (sent_count == NULL)
230 {
231 /* Codes_SRS_SOCKET_ASYNC_30_034: [ If the sent_count parameter is NULL, socket_async_send shall log the error return FAILURE. ]*/
232 LogError("sent_count is NULL");
233 result = MU_FAILURE;
234 }
235 else
236 if (size == 0)
237 {
238 /* Codes_SRS_SOCKET_ASYNC_30_073: [ If the size parameter is 0, socket_async_send shall set sent_count to 0 and return 0. ]*/
239 // This behavior is not always defined by the underlying API, so we make it predictable here
240 *sent_count = 0;
241 result = 0;
242 }
243 else
244 {
245 ssize_t send_result = send(sock, buffer, size, 0);
246 if (send_result < 0)
247 {
248 int sock_err = get_socket_errno(sock);
249 if (sock_err == EAGAIN || sock_err == EWOULDBLOCK)
250 {
251 /* Codes_SRS_SOCKET_ASYNC_30_036: [ If the underlying socket is unable to accept any bytes for transmission because its buffer is full, socket_async_send shall return 0 and the sent_count parameter shall receive the value 0. ]*/
252 // Nothing sent, try again later
253 *sent_count = 0;
254 result = 0;
255 }
256 else
257 {
258 /* Codes_SRS_SOCKET_ASYNC_30_037: [ If socket_async_send fails unexpectedly, socket_async_send shall log the error return FAILURE. ]*/
259 // Something bad happened
260 LogError("Unexpected send error: %d", sock_err);
261 result = MU_FAILURE;
262 }
263 }
264 else
265 {
266 /* Codes_SRS_SOCKET_ASYNC_30_035: [ If the underlying socket accepts one or more bytes for transmission, socket_async_send shall return 0 and the sent_count parameter shall receive the number of bytes accepted for transmission. ]*/
267 // Sent at least part of the message
268 result = 0;
269 *sent_count = (size_t)send_result;
270 }
271 }
272 }
273 return result;
274 }
275
276 int socket_async_receive(SOCKET_ASYNC_HANDLE sock, void* buffer, size_t size, size_t* received_count)
277 {
278 int result;
279 if (buffer == NULL)
280 {
281 /* Codes_SRS_SOCKET_ASYNC_30_052: [ If the buffer parameter is NULL, socket_async_receive shall log the error and return FAILURE. ]*/
282 LogError("buffer is NULL");
283 result = MU_FAILURE;
284 }
285 else
286 {
287 if (received_count == NULL)
288 {
289 /* Codes_SRS_SOCKET_ASYNC_30_053: [ If the received_count parameter is NULL, socket_async_receive shall log the error and return FAILURE. ]*/
290 LogError("received_count is NULL");
291 result = MU_FAILURE;
292 }
293 else
294 if (size == 0)
295 {
296 /* Codes_SRS_SOCKET_ASYNC_30_072: [ If the size parameter is 0, socket_async_receive shall log an error and return FAILURE. ]*/
297 LogError("size is 0");
298 result = MU_FAILURE;
299 }
300 else
301 {
302 ssize_t recv_result = recv(sock, buffer, size, 0);
303 if (recv_result < 0)
304 {
305 int sock_err = get_socket_errno(sock);
306 if (sock_err == EAGAIN || sock_err == EWOULDBLOCK)
307 {
308 /* Codes_SRS_SOCKET_ASYNC_30_055: [ If the underlying socket has no received bytes available, socket_async_receive shall return 0 and the received_count parameter shall receive the value 0. ]*/
309 // Nothing received, try again later
310 *received_count = 0;
311 result = 0;
312 }
313 else
314 {
315 /* Codes_SRS_SOCKET_ASYNC_30_056: [ If the underlying socket fails unexpectedly, socket_async_receive shall log the error and return FAILURE. ]*/
316 // Something bad happened
317 LogError("Unexpected recv error: %d", sock_err);
318 result = MU_FAILURE;
319 }
320 }
321 else
322 {
323 /* Codes_SRS_SOCKET_ASYNC_30_054: [ On success, the underlying socket shall set one or more received bytes into buffer, socket_async_receive shall return 0, and the received_count parameter shall receive the number of bytes received into buffer. ]*/
324 // Received some stuff
325 *received_count = (size_t)recv_result;
326 result = 0;
327 }
328 }
329 }
330 return result;
331 }
332
333 void socket_async_destroy(SOCKET_ASYNC_HANDLE sock)
334 {
335 /* Codes_SRS_SOCKET_ASYNC_30_071: [ socket_async_destroy shall call the underlying close method on the supplied socket. ]*/
336 close(sock);
337 }
338
339