1 // Copyright 2014 The BoringSSL Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
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 #include <winsock2.h>
34 #include <ws2tcpip.h>
35 #endif
36 
37 #include "internal.h"
38 #include "../internal.h"
39 
40 
bio_ip_and_port_to_socket_and_addr(int * out_sock,struct sockaddr_storage * out_addr,socklen_t * out_addr_length,const char * hostname,const char * port_str)41 int bio_ip_and_port_to_socket_and_addr(int *out_sock,
42                                        struct sockaddr_storage *out_addr,
43                                        socklen_t *out_addr_length,
44                                        const char *hostname,
45                                        const char *port_str) {
46   struct addrinfo hint, *result, *cur;
47   int ret;
48 
49   *out_sock = -1;
50 
51   OPENSSL_memset(&hint, 0, sizeof(hint));
52   hint.ai_family = AF_UNSPEC;
53   hint.ai_socktype = SOCK_STREAM;
54 
55   ret = getaddrinfo(hostname, port_str, &hint, &result);
56   if (ret != 0) {
57     OPENSSL_PUT_ERROR(SYS, 0);
58 #if defined(OPENSSL_WINDOWS)
59     ERR_add_error_data(1, gai_strerrorA(ret));
60 #else
61     ERR_add_error_data(1, gai_strerror(ret));
62 #endif
63     return 0;
64   }
65 
66   ret = 0;
67 
68   for (cur = result; cur; cur = cur->ai_next) {
69     if ((size_t) cur->ai_addrlen > sizeof(struct sockaddr_storage)) {
70       continue;
71     }
72     OPENSSL_memset(out_addr, 0, sizeof(struct sockaddr_storage));
73     OPENSSL_memcpy(out_addr, cur->ai_addr, cur->ai_addrlen);
74     *out_addr_length = cur->ai_addrlen;
75 
76     *out_sock = socket(cur->ai_family, cur->ai_socktype, cur->ai_protocol);
77     if (*out_sock < 0) {
78       OPENSSL_PUT_SYSTEM_ERROR();
79       goto out;
80     }
81 
82     ret = 1;
83     break;
84   }
85 
86 out:
87   freeaddrinfo(result);
88   return ret;
89 }
90 
bio_socket_nbio(int sock,int on)91 int bio_socket_nbio(int sock, int on) {
92 #if defined(OPENSSL_WINDOWS)
93   u_long arg = on;
94 
95   return 0 == ioctlsocket(sock, FIONBIO, &arg);
96 #else
97   int flags = fcntl(sock, F_GETFL, 0);
98   if (flags < 0) {
99     return 0;
100   }
101   if (!on) {
102     flags &= ~O_NONBLOCK;
103   } else {
104     flags |= O_NONBLOCK;
105   }
106   return fcntl(sock, F_SETFL, flags) == 0;
107 #endif
108 }
109 
bio_clear_socket_error(void)110 void bio_clear_socket_error(void) {}
111 
bio_sock_error(int sock)112 int bio_sock_error(int sock) {
113   int error;
114   socklen_t error_size = sizeof(error);
115 
116   if (getsockopt(sock, SOL_SOCKET, SO_ERROR, (char *)&error, &error_size) < 0) {
117     return 1;
118   }
119   return error;
120 }
121 
bio_socket_should_retry(int return_value)122 int bio_socket_should_retry(int return_value) {
123 #if defined(OPENSSL_WINDOWS)
124   return return_value == -1 && WSAGetLastError() == WSAEWOULDBLOCK;
125 #else
126   // On POSIX platforms, sockets and fds are the same.
127   return bio_errno_should_retry(return_value);
128 #endif
129 }
130 
131 #endif  // OPENSSL_NO_SOCK
132