@@ -1,7 +1,7 @@ /* - * Copyright (c) 2008-2021 Jonathan Schleifer + * Copyright (c) 2008-2022 Jonathan Schleifer * * All rights reserved. * * This file is part of ObjFW. It may be distributed under the terms of the * Q Public License 1.0, which can be found in the file LICENSE.QPL included in @@ -39,10 +39,16 @@ # include #endif #ifdef OF_HAVE_NETIPX_IPX_H # include #endif +#ifdef OF_HAVE_SYS_UN_H +# include +#endif +#ifdef OF_HAVE_AFUNIX_H +# include +#endif #ifdef OF_WINDOWS # include # include # ifdef OF_HAVE_IPX @@ -69,10 +75,14 @@ static const OFSocketHandle OFInvalidSocketHandle = -1; #else typedef SOCKET OFSocketHandle; static const OFSocketHandle OFInvalidSocketHandle = INVALID_SOCKET; #endif + +#ifdef OF_WINDOWS +typedef short sa_family_t; +#endif #ifdef OF_WII typedef u8 sa_family_t; #endif @@ -90,10 +100,12 @@ OFSocketAddressFamilyUnknown, /** IPv4 */ OFSocketAddressFamilyIPv4, /** IPv6 */ OFSocketAddressFamilyIPv6, + /** UNIX */ + OFSocketAddressFamilyUNIX, /** IPX */ OFSocketAddressFamilyIPX, /** Any address family */ OFSocketAddressFamilyAny = 255 } OFSocketAddressFamily; @@ -107,10 +119,17 @@ uint8_t s6_addr[16]; } sin6_addr; uint32_t sin6_scope_id; }; #endif + +#if !defined(OF_HAVE_UNIX_SOCKETS) && !defined(OF_MORPHOS) && !defined(OF_MINT) +struct sockaddr_un { + sa_family_t sun_family; + char sun_path[108]; +}; +#endif #ifndef OF_HAVE_IPX # define IPX_NODE_LEN 6 struct sockaddr_ipx { sa_family_t sipx_family; @@ -132,22 +151,19 @@ * @struct OFSocketAddress OFSocket.h ObjFW/OFSocket.h * * @brief A struct which represents a host / port pair for a socket. */ typedef struct OF_BOXABLE { + OFSocketAddressFamily family; /* - * Even though struct sockaddr contains the family, we need to use our - * own family, as we need to support storing an IPv6 address on systems - * that don't support IPv6. These may not have AF_INET6 defined and we - * can't just define it, as the value is system-dependent and might - * clash with an existing value. + * We can't use struct sockaddr as it can contain variable length + * arrays. */ - OFSocketAddressFamily family; union { - struct sockaddr sockaddr; struct sockaddr_in in; struct sockaddr_in6 in6; + struct sockaddr_un un; struct sockaddr_ipx ipx; } sockaddr; socklen_t length; } OFSocketAddress; @@ -181,19 +197,27 @@ * @return The parsed IPv6 and port as an OFSocketAddress */ extern OFSocketAddress OFSocketAddressParseIPv6(OFString *IP, uint16_t port); /** - * @brief Creates an IPX address for the specified network, node and port. + * @brief Creates a UNIX socket address from the specified path. * - * @param node The node in the IPX network + * @param path The path of the UNIX socket + * @return A UNIX socket address with the specified path + */ +extern OFSocketAddress OFSocketAddressMakeUNIX(OFString *path); + +/** + * @brief Creates an IPX address for the specified node, network and port. + * * @param network The IPX network + * @param node The node in the IPX network * @param port The IPX port (sometimes called socket number) on the node + * @return An IPX socket address with the specified node, network and port. */ -extern OFSocketAddress OFSocketAddressMakeIPX( - const unsigned char node[_Nonnull IPX_NODE_LEN], uint32_t network, - uint16_t port); +extern OFSocketAddress OFSocketAddressMakeIPX(uint32_t network, + const unsigned char node[_Nonnull IPX_NODE_LEN], uint16_t port); /** * @brief Compares two OFSocketAddress for equality. * * @param address1 The address to compare with the second address @@ -238,10 +262,19 @@ * @param address The address on which to get the port * @return The port of the address */ extern uint16_t OFSocketAddressPort(const OFSocketAddress *_Nonnull address); +/** + * @brief Gets the UNIX socket path of the specified @ref OFSocketAddress. + * + * @param address The address on which to get the UNIX socket path + * @return The UNIX socket path + */ +extern OFString *_Nullable OFSocketAddressUNIXPath( + const OFSocketAddress *_Nonnull address); + /** * @brief Sets the IPX network of the specified @ref OFSocketAddress. * * @param address The address on which to set the IPX network * @param network The IPX network to set on the address