@@ -1,7 +1,7 @@ /* - * Copyright (c) 2008-2022 Jonathan Schleifer + * Copyright (c) 2008-2024 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 @@ -59,12 +59,10 @@ # ifdef OF_HAVE_APPLETALK # include # endif #endif -/** @file */ - #ifdef OF_WII # include #endif #ifdef OF_PSP @@ -72,10 +70,12 @@ #endif #import "macros.h" OF_ASSUME_NONNULL_BEGIN + +/** @file */ #ifndef OF_WINDOWS typedef int OFSocketHandle; static const OFSocketHandle OFInvalidSocketHandle = -1; #else @@ -134,51 +134,54 @@ sa_family_t sun_family; char sun_path[108]; }; #endif -#ifndef OF_HAVE_IPX +#ifndef IPX_NODE_LEN # define IPX_NODE_LEN 6 +#endif +#if !defined(OF_HAVE_IPX) struct sockaddr_ipx { sa_family_t sipx_family; uint32_t sipx_network; unsigned char sipx_node[IPX_NODE_LEN]; uint16_t sipx_port; uint8_t sipx_type; }; -#endif -#ifdef OF_WINDOWS +#elif defined(OF_WINDOWS) # define IPX_NODE_LEN 6 # define sipx_family sa_family # define sipx_network sa_netnum # define sipx_node sa_nodenum # define sipx_port sa_socket +#elif defined(OF_FREEBSD) +# define sipx_network sipx_addr.x_net.c_net +# define sipx_node sipx_addr.x_host.c_host #endif #ifndef OF_HAVE_APPLETALK struct sockaddr_at { sa_family_t sat_family; uint8_t sat_port; - struct at_addr { - uint16_t s_net; - uint8_t s_node; - } sat_addr; + uint16_t sat_net; + uint8_t sat_node; }; -#endif -#ifdef OF_WINDOWS -# define sat_port sat_socket #else -# define sat_net sat_addr.s_net -# define sat_node sat_addr.s_node +# ifdef OF_WINDOWS +# define sat_port sat_socket +# else +# define sat_net sat_addr.s_net +# define sat_node sat_addr.s_node +# endif #endif /** * @struct OFSocketAddress OFSocket.h ObjFW/OFSocket.h * * @brief A struct which represents a host / port pair for a socket. */ -typedef struct OF_BOXABLE { +typedef struct OF_BOXABLE OFSocketAddress { OFSocketAddressFamily family; /* * We can't use struct sockaddr as it can contain variable length * arrays. */ @@ -186,10 +189,17 @@ struct sockaddr_in in; struct sockaddr_in6 in6; struct sockaddr_un un; struct sockaddr_ipx ipx; struct sockaddr_at at; +#ifdef OF_HAVE_SOCKADDR_STORAGE + /* + * Required to make the ABI stable in case we want to add more + * address types later. + */ + struct sockaddr_storage storage; +#endif } sockaddr; socklen_t length; } OFSocketAddress; #ifdef __cplusplus @@ -278,15 +288,26 @@ /** * @brief Converts the specified @ref OFSocketAddress to a string. * * @param address The address to convert to a string - * @return The address as an IP string + * @return The address as a string, without the port */ extern OFString *_Nonnull OFSocketAddressString( const OFSocketAddress *_Nonnull address); +/** + * @brief Returns a description for the specified @ref OFSocketAddress. + * + * This is similar to @ref OFSocketAddressString, but it also contains the port. + * + * @param address The address to return a description for + * @return The address as an string, with the port + */ +extern OFString *_Nonnull OFSocketAddressDescription( + const OFSocketAddress *_Nonnull address); + /** * @brief Sets the IP port of the specified @ref OFSocketAddress. * * @param address The address on which to set the port * @param port The port to set on the address @@ -306,11 +327,11 @@ * @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( +extern OFString *OFSocketAddressUNIXPath( const OFSocketAddress *_Nonnull address); /** * @brief Sets the IPX network of the specified @ref OFSocketAddress. *