xref: /aosp_15_r20/external/boringssl/src/crypto/bio/socket_helper.c (revision 8fb009dc861624b67b6cdb62ea21f0f22d0c584b)
1 /* Copyright (c) 2014, Google Inc.
2  *
3  * Permission to use, copy, modify, and/or distribute this software for any
4  * purpose with or without fee is hereby granted, provided that the above
5  * copyright notice and this permission notice appear in all copies.
6  *
7  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14 
15 #if defined(__linux__)
16 #undef _POSIX_C_SOURCE
17 #define _POSIX_C_SOURCE 200112L
18 #endif
19 
20 #include <openssl/bio.h>
21 #include <openssl/err.h>
22 
23 #if !defined(OPENSSL_NO_SOCK)
24 
25 #include <fcntl.h>
26 #include <string.h>
27 #include <sys/types.h>
28 
29 #if !defined(OPENSSL_WINDOWS)
30 #include <netdb.h>
31 #include <unistd.h>
32 #else
33 OPENSSL_MSVC_PRAGMA(warning(push, 3))
34 #include <winsock2.h>
35 #include <ws2tcpip.h>
OPENSSL_MSVC_PRAGMA(warning (pop))36 OPENSSL_MSVC_PRAGMA(warning(pop))
37 #endif
38 
39 #include "internal.h"
40 #include "../internal.h"
41 
42 
43 int bio_ip_and_port_to_socket_and_addr(int *out_sock,
44                                        struct sockaddr_storage *out_addr,
45                                        socklen_t *out_addr_length,
46                                        const char *hostname,
47                                        const char *port_str) {
48   struct addrinfo hint, *result, *cur;
49   int ret;
50 
51   *out_sock = -1;
52 
53   OPENSSL_memset(&hint, 0, sizeof(hint));
54   hint.ai_family = AF_UNSPEC;
55   hint.ai_socktype = SOCK_STREAM;
56 
57   ret = getaddrinfo(hostname, port_str, &hint, &result);
58   if (ret != 0) {
59     OPENSSL_PUT_ERROR(SYS, 0);
60 #if defined(OPENSSL_WINDOWS)
61     ERR_add_error_data(1, gai_strerrorA(ret));
62 #else
63     ERR_add_error_data(1, gai_strerror(ret));
64 #endif
65     return 0;
66   }
67 
68   ret = 0;
69 
70   for (cur = result; cur; cur = cur->ai_next) {
71     if ((size_t) cur->ai_addrlen > sizeof(struct sockaddr_storage)) {
72       continue;
73     }
74     OPENSSL_memset(out_addr, 0, sizeof(struct sockaddr_storage));
75     OPENSSL_memcpy(out_addr, cur->ai_addr, cur->ai_addrlen);
76     *out_addr_length = cur->ai_addrlen;
77 
78     *out_sock = socket(cur->ai_family, cur->ai_socktype, cur->ai_protocol);
79     if (*out_sock < 0) {
80       OPENSSL_PUT_SYSTEM_ERROR();
81       goto out;
82     }
83 
84     ret = 1;
85     break;
86   }
87 
88 out:
89   freeaddrinfo(result);
90   return ret;
91 }
92 
bio_socket_nbio(int sock,int on)93 int bio_socket_nbio(int sock, int on) {
94 #if defined(OPENSSL_WINDOWS)
95   u_long arg = on;
96 
97   return 0 == ioctlsocket(sock, FIONBIO, &arg);
98 #else
99   int flags = fcntl(sock, F_GETFL, 0);
100   if (flags < 0) {
101     return 0;
102   }
103   if (!on) {
104     flags &= ~O_NONBLOCK;
105   } else {
106     flags |= O_NONBLOCK;
107   }
108   return fcntl(sock, F_SETFL, flags) == 0;
109 #endif
110 }
111 
bio_clear_socket_error(void)112 void bio_clear_socket_error(void) {}
113 
bio_sock_error(int sock)114 int bio_sock_error(int sock) {
115   int error;
116   socklen_t error_size = sizeof(error);
117 
118   if (getsockopt(sock, SOL_SOCKET, SO_ERROR, (char *)&error, &error_size) < 0) {
119     return 1;
120   }
121   return error;
122 }
123 
bio_socket_should_retry(int return_value)124 int bio_socket_should_retry(int return_value) {
125 #if defined(OPENSSL_WINDOWS)
126   return return_value == -1 && WSAGetLastError() == WSAEWOULDBLOCK;
127 #else
128   // On POSIX platforms, sockets and fds are the same.
129   return bio_errno_should_retry(return_value);
130 #endif
131 }
132 
133 #endif  // OPENSSL_NO_SOCK
134