Home | History | Annotate | Line # | Download | only in internal
      1 /*
      2  * Copyright 1995-2025 The OpenSSL Project Authors. All Rights Reserved.
      3  *
      4  * Licensed under the Apache License 2.0 (the "License").  You may not use
      5  * this file except in compliance with the License.  You can obtain a copy
      6  * in the file LICENSE in the source distribution or at
      7  * https://www.openssl.org/source/license.html
      8  */
      9 
     10 #ifndef OSSL_INTERNAL_SOCKETS_H
     11 #define OSSL_INTERNAL_SOCKETS_H
     12 #pragma once
     13 
     14 #include <openssl/opensslconf.h>
     15 #include "internal/common.h"
     16 
     17 #if defined(OPENSSL_SYS_VXWORKS) || defined(OPENSSL_SYS_UEFI)
     18 #define NO_SYS_PARAM_H
     19 #endif
     20 #ifdef WIN32
     21 #define NO_SYS_UN_H
     22 #endif
     23 #ifdef OPENSSL_SYS_VMS
     24 #define NO_SYS_PARAM_H
     25 #define NO_SYS_UN_H
     26 #endif
     27 
     28 #ifdef OPENSSL_NO_SOCK
     29 
     30 #elif defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)
     31 #if defined(__DJGPP__)
     32 #define WATT32
     33 #define WATT32_NO_OLDIES
     34 #include <sys/socket.h>
     35 #include <sys/un.h>
     36 #include <tcp.h>
     37 #include <netdb.h>
     38 #include <arpa/inet.h>
     39 #include <netinet/tcp.h>
     40 #elif defined(_WIN32_WCE) && _WIN32_WCE < 410
     41 #define getservbyname _masked_declaration_getservbyname
     42 #endif
     43 #if !defined(IPPROTO_IP)
     44 /* winsock[2].h was included already? */
     45 #include "internal/e_winsock.h"
     46 #endif
     47 #ifdef getservbyname
     48 /* this is used to be wcecompat/include/winsock_extras.h */
     49 #undef getservbyname
     50 struct servent *PASCAL getservbyname(const char *, const char *);
     51 #endif
     52 
     53 #ifdef _WIN64
     54 /*
     55  * Even though sizeof(SOCKET) is 8, it's safe to cast it to int, because
     56  * the value constitutes an index in per-process table of limited size
     57  * and not a real pointer. And we also depend on fact that all processors
     58  * Windows run on happen to be two's complement, which allows to
     59  * interchange INVALID_SOCKET and -1.
     60  */
     61 #define socket(d, t, p) ((int)socket(d, t, p))
     62 #define accept(s, f, l) ((int)accept(s, f, l))
     63 #endif
     64 
     65 /* Windows have other names for shutdown() reasons */
     66 #ifndef SHUT_RD
     67 #define SHUT_RD SD_RECEIVE
     68 #endif
     69 #ifndef SHUT_WR
     70 #define SHUT_WR SD_SEND
     71 #endif
     72 #ifndef SHUT_RDWR
     73 #define SHUT_RDWR SD_BOTH
     74 #endif
     75 
     76 #else
     77 #if defined(__APPLE__)
     78 /*
     79  * This must be defined before including <netinet/in6.h> to get
     80  * IPV6_RECVPKTINFO
     81  */
     82 #define __APPLE_USE_RFC_3542
     83 #endif
     84 
     85 #ifndef NO_SYS_PARAM_H
     86 #include <sys/param.h>
     87 #endif
     88 #ifdef OPENSSL_SYS_VXWORKS
     89 #include <time.h>
     90 #endif
     91 
     92 #include <netdb.h>
     93 #if defined(OPENSSL_SYS_VMS)
     94 typedef size_t socklen_t; /* Currently appears to be missing on VMS */
     95 #endif
     96 #if defined(OPENSSL_SYS_VMS_NODECC)
     97 #include <socket.h>
     98 #include <in.h>
     99 #include <inet.h>
    100 #else
    101 #include <sys/socket.h>
    102 #if !defined(NO_SYS_UN_H) && defined(AF_UNIX) && !defined(OPENSSL_NO_UNIX_SOCK)
    103 #include <sys/un.h>
    104 #ifndef UNIX_PATH_MAX
    105 #define UNIX_PATH_MAX sizeof(((struct sockaddr_un *)NULL)->sun_path)
    106 #endif
    107 #endif
    108 #ifdef FILIO_H
    109 #include <sys/filio.h> /* FIONBIO in some SVR4, e.g. unixware, solaris */
    110 #endif
    111 #include <netinet/in.h>
    112 #include <arpa/inet.h>
    113 #include <netinet/tcp.h>
    114 #endif
    115 
    116 #ifdef OPENSSL_SYS_AIX
    117 #include <sys/select.h>
    118 #endif
    119 
    120 #ifdef OPENSSL_SYS_UNIX
    121 #ifndef OPENSSL_SYS_TANDEM
    122 #include <poll.h>
    123 #endif
    124 #include <errno.h>
    125 #endif
    126 
    127 #ifndef VMS
    128 #include <sys/ioctl.h>
    129 #else
    130 #if !defined(TCPIP_TYPE_SOCKETSHR) && defined(__VMS_VER) && (__VMS_VER > 70000000)
    131 /* ioctl is only in VMS > 7.0 and when socketshr is not used */
    132 #include <sys/ioctl.h>
    133 #endif
    134 #include <unixio.h>
    135 #if defined(TCPIP_TYPE_SOCKETSHR)
    136 #include <socketshr.h>
    137 #endif
    138 #endif
    139 
    140 #ifndef INVALID_SOCKET
    141 #define INVALID_SOCKET (-1)
    142 #endif
    143 #endif
    144 
    145 /*
    146  * Some IPv6 implementations are broken, you can disable them in known
    147  * bad versions.
    148  */
    149 #if !defined(OPENSSL_USE_IPV6)
    150 #if defined(AF_INET6)
    151 #define OPENSSL_USE_IPV6 1
    152 #else
    153 #define OPENSSL_USE_IPV6 0
    154 #endif
    155 #endif
    156 
    157 /*
    158  * Some platforms define AF_UNIX, but don't support it
    159  */
    160 #if !defined(OPENSSL_NO_UNIX_SOCK)
    161 #if !defined(AF_UNIX) || defined(NO_SYS_UN_H)
    162 #define OPENSSL_NO_UNIX_SOCK
    163 #endif
    164 #endif
    165 
    166 #define get_last_socket_error() errno
    167 #define clear_socket_error() errno = 0
    168 #define get_last_socket_error_is_eintr() (get_last_socket_error() == EINTR)
    169 
    170 #if defined(OPENSSL_SYS_WINDOWS)
    171 #undef get_last_socket_error
    172 #undef clear_socket_error
    173 #undef get_last_socket_error_is_eintr
    174 #define get_last_socket_error() WSAGetLastError()
    175 #define clear_socket_error() WSASetLastError(0)
    176 #define get_last_socket_error_is_eintr() (get_last_socket_error() == WSAEINTR)
    177 #define readsocket(s, b, n) recv((s), (b), (n), 0)
    178 #define writesocket(s, b, n) send((s), (b), (n), 0)
    179 #elif defined(__DJGPP__)
    180 #define closesocket(s) close_s(s)
    181 #define readsocket(s, b, n) read_s(s, b, n)
    182 #define writesocket(s, b, n) send(s, b, n, 0)
    183 #elif defined(OPENSSL_SYS_VMS)
    184 #define ioctlsocket(a, b, c) ioctl(a, b, c)
    185 #define closesocket(s) close(s)
    186 #define readsocket(s, b, n) recv((s), (b), (n), 0)
    187 #define writesocket(s, b, n) send((s), (b), (n), 0)
    188 #elif defined(OPENSSL_SYS_VXWORKS)
    189 #define ioctlsocket(a, b, c) ioctl((a), (b), (int)(c))
    190 #define closesocket(s) close(s)
    191 #define readsocket(s, b, n) read((s), (b), (n))
    192 #define writesocket(s, b, n) write((s), (char *)(b), (n))
    193 #elif defined(OPENSSL_SYS_TANDEM)
    194 #define readsocket(s, b, n) read((s), (b), (n))
    195 #define writesocket(s, b, n) write((s), (b), (n))
    196 #define ioctlsocket(a, b, c) ioctl(a, b, c)
    197 #define closesocket(s) close(s)
    198 #else
    199 #define ioctlsocket(a, b, c) ioctl(a, b, c)
    200 #define closesocket(s) close(s)
    201 #define readsocket(s, b, n) read((s), (b), (n))
    202 #define writesocket(s, b, n) write((s), (b), (n))
    203 #endif
    204 
    205 /* also in apps/include/apps.h */
    206 #if defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_WINCE)
    207 #define openssl_fdset(a, b) FD_SET((unsigned int)(a), b)
    208 #else
    209 #define openssl_fdset(a, b) FD_SET(a, b)
    210 #endif
    211 
    212 #endif
    213