Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / boringssl / src / crypto / bio / socket_helper.c
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 #define _POSIX_SOURCE
16
17 #include <openssl/bio.h>
18 #include <openssl/err.h>
19
20 #include <fcntl.h>
21 #include <sys/types.h>
22
23 #if !defined(OPENSSL_WINDOWS)
24 #include <netdb.h>
25 #include <unistd.h>
26 #else
27 #include <WinSock2.h>
28 #include <WS2tcpip.h>
29 typedef int socklen_t;
30 #endif
31
32 #include "internal.h"
33
34
35 int bio_ip_and_port_to_socket_and_addr(int *out_sock,
36                                        struct sockaddr_storage *out_addr,
37                                        socklen_t *out_addr_length,
38                                        const char *hostname,
39                                        const char *port_str) {
40   struct addrinfo hint, *result, *cur;
41   int ret;
42
43   *out_sock = -1;
44
45   memset(&hint, 0, sizeof(hint));
46   hint.ai_family = AF_UNSPEC;
47   hint.ai_socktype = SOCK_STREAM;
48
49   ret = getaddrinfo(hostname, port_str, &hint, &result);
50   if (ret != 0) {
51     OPENSSL_PUT_ERROR(SYS, getaddrinfo, 0);
52     ERR_add_error_data(2, gai_strerror(ret));
53     return 0;
54   }
55
56   ret = 0;
57
58   for (cur = result; cur; cur = cur->ai_next) {
59     if (cur->ai_addrlen > sizeof(struct sockaddr_storage)) {
60       continue;
61     }
62     memset(out_addr, 0, sizeof(struct sockaddr_storage));
63     memcpy(out_addr, cur->ai_addr, cur->ai_addrlen);
64     *out_addr_length = cur->ai_addrlen;
65
66     *out_sock = socket(cur->ai_family, cur->ai_socktype, cur->ai_protocol);
67     if (*out_sock < 0) {
68       OPENSSL_PUT_SYSTEM_ERROR(socket);
69       goto out;
70     }
71
72     ret = 1;
73     break;
74   }
75
76 out:
77   freeaddrinfo(result);
78   return ret;
79 }
80
81 int bio_socket_nbio(int sock, int on) {
82 #if defined(OPENSSL_WINDOWS)
83   u_long arg = on;
84
85   return 0 == ioctlsocket(sock, FIONBIO, &arg);
86 #else
87   int flags = fcntl(sock, F_GETFL, 0);
88   if (flags < 0) {
89     return 0;
90   }
91   if (!on) {
92     flags &= ~O_NONBLOCK;
93   } else {
94     flags |= O_NONBLOCK;
95   }
96   return fcntl(sock, F_SETFL, flags) == 0;
97 #endif
98 }
99
100 void bio_clear_socket_error(void) {}
101
102 int bio_sock_error(int sock) {
103   int error;
104   socklen_t error_size = sizeof(error);
105
106   if (getsockopt(sock, SOL_SOCKET, SO_ERROR, (char *)&error, &error_size) < 0) {
107     return 1;
108   }
109   return error;
110 }