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