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#include <fcntl.h>
22#include <string.h>
23#include <sys/types.h>
24
25#if !defined(OPENSSL_WINDOWS)
26#include <netdb.h>
27#include <unistd.h>
28#else
29OPENSSL_MSVC_PRAGMA(warning(push, 3))
30#include <winsock2.h>
31#include <ws2tcpip.h>
32OPENSSL_MSVC_PRAGMA(warning(pop))
33#endif
34
35#include "internal.h"
36#include "../internal.h"
37
38
39int bio_ip_and_port_to_socket_and_addr(int *out_sock,
40                                       struct sockaddr_storage *out_addr,
41                                       socklen_t *out_addr_length,
42                                       const char *hostname,
43                                       const char *port_str) {
44  struct addrinfo hint, *result, *cur;
45  int ret;
46
47  *out_sock = -1;
48
49  OPENSSL_memset(&hint, 0, sizeof(hint));
50  hint.ai_family = AF_UNSPEC;
51  hint.ai_socktype = SOCK_STREAM;
52
53  ret = getaddrinfo(hostname, port_str, &hint, &result);
54  if (ret != 0) {
55    OPENSSL_PUT_ERROR(SYS, 0);
56    ERR_add_error_data(1, gai_strerror(ret));
57    return 0;
58  }
59
60  ret = 0;
61
62  for (cur = result; cur; cur = cur->ai_next) {
63    if ((size_t) cur->ai_addrlen > sizeof(struct sockaddr_storage)) {
64      continue;
65    }
66    OPENSSL_memset(out_addr, 0, sizeof(struct sockaddr_storage));
67    OPENSSL_memcpy(out_addr, cur->ai_addr, cur->ai_addrlen);
68    *out_addr_length = cur->ai_addrlen;
69
70    *out_sock = socket(cur->ai_family, cur->ai_socktype, cur->ai_protocol);
71    if (*out_sock < 0) {
72      OPENSSL_PUT_SYSTEM_ERROR();
73      goto out;
74    }
75
76    ret = 1;
77    break;
78  }
79
80out:
81  freeaddrinfo(result);
82  return ret;
83}
84
85int bio_socket_nbio(int sock, int on) {
86#if defined(OPENSSL_WINDOWS)
87  u_long arg = on;
88
89  return 0 == ioctlsocket(sock, FIONBIO, &arg);
90#else
91  int flags = fcntl(sock, F_GETFL, 0);
92  if (flags < 0) {
93    return 0;
94  }
95  if (!on) {
96    flags &= ~O_NONBLOCK;
97  } else {
98    flags |= O_NONBLOCK;
99  }
100  return fcntl(sock, F_SETFL, flags) == 0;
101#endif
102}
103
104void bio_clear_socket_error(void) {}
105
106int bio_sock_error(int sock) {
107  int error;
108  socklen_t error_size = sizeof(error);
109
110  if (getsockopt(sock, SOL_SOCKET, SO_ERROR, (char *)&error, &error_size) < 0) {
111    return 1;
112  }
113  return error;
114}
115