1 /* MIT License 2 * 3 * Copyright (c) 2024 Brad House 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining a copy 6 * of this software and associated documentation files (the "Software"), to deal 7 * in the Software without restriction, including without limitation the rights 8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 * copies of the Software, and to permit persons to whom the Software is 10 * furnished to do so, subject to the following conditions: 11 * 12 * The above copyright notice and this permission notice (including the next 13 * paragraph) shall be included in all copies or substantial portions of the 14 * Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 * SOFTWARE. 23 * 24 * SPDX-License-Identifier: MIT 25 */ 26 27 #ifndef __ARES_SOCKET_H 28 #define __ARES_SOCKET_H 29 30 /* Macro SOCKERRNO / SET_SOCKERRNO() returns / sets the *socket-related* errno 31 * (or equivalent) on this platform to hide platform details to code using it. 32 */ 33 #ifdef USE_WINSOCK 34 # define SOCKERRNO ((int)WSAGetLastError()) 35 # define SET_SOCKERRNO(x) (WSASetLastError((int)(x))) 36 #else 37 # define SOCKERRNO (errno) 38 # define SET_SOCKERRNO(x) (errno = (x)) 39 #endif 40 41 /* Portable error number symbolic names defined to Winsock error codes. */ 42 #ifdef USE_WINSOCK 43 # undef EBADF /* override definition in errno.h */ 44 # define EBADF WSAEBADF 45 # undef EINTR /* override definition in errno.h */ 46 # define EINTR WSAEINTR 47 # undef EINVAL /* override definition in errno.h */ 48 # define EINVAL WSAEINVAL 49 # undef EWOULDBLOCK /* override definition in errno.h */ 50 # define EWOULDBLOCK WSAEWOULDBLOCK 51 # undef EINPROGRESS /* override definition in errno.h */ 52 # define EINPROGRESS WSAEINPROGRESS 53 # undef EALREADY /* override definition in errno.h */ 54 # define EALREADY WSAEALREADY 55 # undef ENOTSOCK /* override definition in errno.h */ 56 # define ENOTSOCK WSAENOTSOCK 57 # undef EDESTADDRREQ /* override definition in errno.h */ 58 # define EDESTADDRREQ WSAEDESTADDRREQ 59 # undef EMSGSIZE /* override definition in errno.h */ 60 # define EMSGSIZE WSAEMSGSIZE 61 # undef EPROTOTYPE /* override definition in errno.h */ 62 # define EPROTOTYPE WSAEPROTOTYPE 63 # undef ENOPROTOOPT /* override definition in errno.h */ 64 # define ENOPROTOOPT WSAENOPROTOOPT 65 # undef EPROTONOSUPPORT /* override definition in errno.h */ 66 # define EPROTONOSUPPORT WSAEPROTONOSUPPORT 67 # define ESOCKTNOSUPPORT WSAESOCKTNOSUPPORT 68 # undef EOPNOTSUPP /* override definition in errno.h */ 69 # define EOPNOTSUPP WSAEOPNOTSUPP 70 # undef ENOSYS /* override definition in errno.h */ 71 # define ENOSYS WSAEOPNOTSUPP 72 # define EPFNOSUPPORT WSAEPFNOSUPPORT 73 # undef EAFNOSUPPORT /* override definition in errno.h */ 74 # define EAFNOSUPPORT WSAEAFNOSUPPORT 75 # undef EADDRINUSE /* override definition in errno.h */ 76 # define EADDRINUSE WSAEADDRINUSE 77 # undef EADDRNOTAVAIL /* override definition in errno.h */ 78 # define EADDRNOTAVAIL WSAEADDRNOTAVAIL 79 # undef ENETDOWN /* override definition in errno.h */ 80 # define ENETDOWN WSAENETDOWN 81 # undef ENETUNREACH /* override definition in errno.h */ 82 # define ENETUNREACH WSAENETUNREACH 83 # undef ENETRESET /* override definition in errno.h */ 84 # define ENETRESET WSAENETRESET 85 # undef ECONNABORTED /* override definition in errno.h */ 86 # define ECONNABORTED WSAECONNABORTED 87 # undef ECONNRESET /* override definition in errno.h */ 88 # define ECONNRESET WSAECONNRESET 89 # undef ENOBUFS /* override definition in errno.h */ 90 # define ENOBUFS WSAENOBUFS 91 # undef EISCONN /* override definition in errno.h */ 92 # define EISCONN WSAEISCONN 93 # undef ENOTCONN /* override definition in errno.h */ 94 # define ENOTCONN WSAENOTCONN 95 # define ESHUTDOWN WSAESHUTDOWN 96 # define ETOOMANYREFS WSAETOOMANYREFS 97 # undef ETIMEDOUT /* override definition in errno.h */ 98 # define ETIMEDOUT WSAETIMEDOUT 99 # undef ECONNREFUSED /* override definition in errno.h */ 100 # define ECONNREFUSED WSAECONNREFUSED 101 # undef ELOOP /* override definition in errno.h */ 102 # define ELOOP WSAELOOP 103 # ifndef ENAMETOOLONG /* possible previous definition in errno.h */ 104 # define ENAMETOOLONG WSAENAMETOOLONG 105 # endif 106 # define EHOSTDOWN WSAEHOSTDOWN 107 # undef EHOSTUNREACH /* override definition in errno.h */ 108 # define EHOSTUNREACH WSAEHOSTUNREACH 109 # ifndef ENOTEMPTY /* possible previous definition in errno.h */ 110 # define ENOTEMPTY WSAENOTEMPTY 111 # endif 112 # define EPROCLIM WSAEPROCLIM 113 # define EUSERS WSAEUSERS 114 # define EDQUOT WSAEDQUOT 115 # define ESTALE WSAESTALE 116 # define EREMOTE WSAEREMOTE 117 #endif 118 119 /*! Socket errors */ 120 typedef enum { 121 ARES_CONN_ERR_SUCCESS = 0, /*!< Success */ 122 ARES_CONN_ERR_WOULDBLOCK = 1, /*!< Operation would block */ 123 ARES_CONN_ERR_CONNCLOSED = 2, /*!< Connection closed (gracefully) */ 124 ARES_CONN_ERR_CONNABORTED = 3, /*!< Connection Aborted */ 125 ARES_CONN_ERR_CONNRESET = 4, /*!< Connection Reset */ 126 ARES_CONN_ERR_CONNREFUSED = 5, /*!< Connection Refused */ 127 ARES_CONN_ERR_CONNTIMEDOUT = 6, /*!< Connection Timed Out */ 128 ARES_CONN_ERR_HOSTDOWN = 7, /*!< Host Down */ 129 ARES_CONN_ERR_HOSTUNREACH = 8, /*!< Host Unreachable */ 130 ARES_CONN_ERR_NETDOWN = 9, /*!< Network Down */ 131 ARES_CONN_ERR_NETUNREACH = 10, /*!< Network Unreachable */ 132 ARES_CONN_ERR_INTERRUPT = 11, /*!< Call interrupted by signal, repeat */ 133 ARES_CONN_ERR_AFNOSUPPORT = 12, /*!< Address family not supported */ 134 ARES_CONN_ERR_BADADDR = 13, /*!< Bad Address / Unavailable */ 135 ARES_CONN_ERR_NOMEM = 14, /*!< Out of memory */ 136 ARES_CONN_ERR_INVALID = 15, /*!< Invalid Usage */ 137 ARES_CONN_ERR_TOOLARGE = 16, /*!< Request size too large */ 138 ARES_CONN_ERR_NOTIMP = 17, /*!< Not implemented */ 139 ARES_CONN_ERR_FAILURE = 99 /*!< Generic failure */ 140 } ares_conn_err_t; 141 142 ares_bool_t ares_sockaddr_addr_eq(const struct sockaddr *sa, 143 const struct ares_addr *aa); 144 ares_status_t ares_socket_configure(ares_channel_t *channel, int family, 145 ares_bool_t is_tcp, ares_socket_t fd); 146 ares_conn_err_t ares_socket_enable_tfo(const ares_channel_t *channel, 147 ares_socket_t fd); 148 ares_conn_err_t ares_socket_open(ares_socket_t *sock, ares_channel_t *channel, 149 int af, int type, int protocol); 150 ares_bool_t ares_socket_try_again(int errnum); 151 void ares_socket_close(ares_channel_t *channel, ares_socket_t s); 152 ares_conn_err_t ares_socket_connect(ares_channel_t *channel, 153 ares_socket_t sockfd, ares_bool_t is_tfo, 154 const struct sockaddr *addr, 155 ares_socklen_t addrlen); 156 ares_bool_t ares_sockaddr_to_ares_addr(struct ares_addr *ares_addr, 157 unsigned short *port, 158 const struct sockaddr *sockaddr); 159 ares_conn_err_t ares_socket_write(ares_channel_t *channel, ares_socket_t fd, 160 const void *data, size_t len, size_t *written, 161 const struct sockaddr *sa, 162 ares_socklen_t salen); 163 #endif 164