socket_helper.c
Go to the documentation of this file.
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 #undef _POSIX_C_SOURCE
16 #define _POSIX_C_SOURCE 200112L
17 
18 #include <openssl/bio.h>
19 #include <openssl/err.h>
20 
21 #if !defined(OPENSSL_TRUSTY)
22 
23 #include <fcntl.h>
24 #include <string.h>
25 #include <sys/types.h>
26 
27 #if !defined(OPENSSL_WINDOWS)
28 #include <netdb.h>
29 #include <unistd.h>
30 #else
32 #include <winsock2.h>
33 #include <ws2tcpip.h>
35 #endif
36 
37 #include "internal.h"
38 #include "../internal.h"
39 
40 
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) {
79  goto out;
80  }
81 
82  ret = 1;
83  break;
84  }
85 
86 out:
87  freeaddrinfo(result);
88  return ret;
89 }
90 
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 
111 
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 
122 #endif // OPENSSL_TRUSTY
bio_ip_and_port_to_socket_and_addr
int 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)
Definition: socket_helper.c:41
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
internal.h
gen_build_yaml.out
dictionary out
Definition: src/benchmark/gen_build_yaml.py:24
check_version.warning
string warning
Definition: check_version.py:46
OPENSSL_PUT_ERROR
#define OPENSSL_PUT_ERROR(library, reason)
Definition: err.h:423
bio.h
string.h
error
grpc_error_handle error
Definition: retry_filter.cc:499
OPENSSL_memset
static void * OPENSSL_memset(void *dst, int c, size_t n)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:835
bio_socket_nbio
int bio_socket_nbio(int sock, int on)
Definition: socket_helper.c:91
OPENSSL_memcpy
static void * OPENSSL_memcpy(void *dst, const void *src, size_t n)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:819
err.h
arg
Definition: cmdline.cc:40
OPENSSL_MSVC_PRAGMA
OPENSSL_MSVC_PRAGMA(warning(disable:4702))
Definition: e_aes.c:69
addrinfo::ai_family
int ai_family
Definition: ares_ipv6.h:46
bio_sock_error
int bio_sock_error(int sock)
Definition: socket_helper.c:112
push
int push(void *desc, unsigned char *buf, unsigned len)
Definition: bloaty/third_party/zlib/test/infcover.c:463
memory_diff.cur
def cur
Definition: memory_diff.py:83
absl::flags_internal
Definition: abseil-cpp/absl/flags/commandlineflag.h:40
ret
UniquePtr< SSL_SESSION > ret
Definition: ssl_x509.cc:1029
OPENSSL_PUT_SYSTEM_ERROR
#define OPENSSL_PUT_SYSTEM_ERROR()
Definition: err.h:429
ERR_add_error_data
#define ERR_add_error_data
Definition: boringssl_prefix_symbols.h:1411
addrinfo::ai_socktype
int ai_socktype
Definition: ares_ipv6.h:47
test_server.socket
socket
Definition: test_server.py:65
bio_clear_socket_error
void bio_clear_socket_error(void)
Definition: socket_helper.c:110
addrinfo
Definition: ares_ipv6.h:43


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:19