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