Index: src/OFSocket.h ================================================================== --- src/OFSocket.h +++ src/OFSocket.h @@ -33,25 +33,31 @@ # include #endif #ifdef OF_HAVE_NETINET_TCP_H # 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_HAVE_NETIPX_IPX_H +# include +#endif +#ifdef OF_HAVE_NETATALK_AT_H +# include #endif #ifdef OF_WINDOWS # include # include # ifdef OF_HAVE_IPX # include +# endif +# ifdef OF_HAVE_APPLETALK +# include # endif #endif /** @file */ @@ -101,10 +107,12 @@ OFSocketAddressFamilyIPv6, /** UNIX */ OFSocketAddressFamilyUNIX, /** IPX */ OFSocketAddressFamilyIPX, + /** AppleTalk */ + OFSocketAddressFamilyAppleTalk, /** Any address family */ OFSocketAddressFamilyAny = 255 } OFSocketAddressFamily; #ifndef OF_HAVE_IPV6 @@ -142,10 +150,25 @@ # define sipx_network sa_netnum # define sipx_node sa_nodenum # define sipx_port sa_socket #endif +#ifndef OF_HAVE_APPLETALK +struct sockaddr_at { + sa_family_t sat_family; + unsigned short sat_net; + unsigned char sat_node; + unsigned char sat_port; +}; +#endif +#ifdef OF_WINDOWS +# define sat_port sat_socket +#else +# define sat_net sat_addr.s_net +# define sat_node sat_addr.s_node +#endif + /** * @struct OFSocketAddress OFSocket.h ObjFW/OFSocket.h * * @brief A struct which represents a host / port pair for a socket. */ @@ -158,10 +181,11 @@ union { struct sockaddr_in in; struct sockaddr_in6 in6; struct sockaddr_un un; struct sockaddr_ipx ipx; + struct sockaddr_at at; } sockaddr; socklen_t length; } OFSocketAddress; #ifdef __cplusplus @@ -205,20 +229,32 @@ * @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. + * @brief Creates an IPX address for the specified network, node 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(uint32_t network, const unsigned char node[_Nonnull IPX_NODE_LEN], uint16_t port); +/** + * @brief Creates an AppleTalk address for the specified network, node and port. + * + * @param network The AppleTalk network + * @param node The node in the AppleTalk network + * @param port The AppleTalk (sometimes called socket number) on the node + * @return An AppleTalk socket address with the specified node, network and + * port. + */ +extern OFSocketAddress OFSocketAddressMakeAppleTalk(uint16_t network, + uint8_t node, uint8_t port); + /** * @brief Compares two OFSocketAddress for equality. * * @param address1 The address to compare with the second address * @param address2 The second address @@ -307,10 +343,46 @@ * @param node A byte array to store the IPX node of the address */ extern void OFSocketAddressIPXNode(const OFSocketAddress *_Nonnull address, unsigned char node[_Nonnull IPX_NODE_LEN]); +/** + * @brief Sets the AppleTalk network of the specified @ref OFSocketAddress. + * + * @param address The address on which to set the AppleTalk network + * @param network The AppleTalk network to set on the address + */ +extern void OFSocketAddressSetAppleTalkNetwork( + OFSocketAddress *_Nonnull address, uint16_t network); + +/** + * @brief Returns the AppleTalk network of the specified @ref OFSocketAddress. + * + * @param address The address on which to get the AppleTalk network + * @return The AppleTalk network of the address + */ +extern uint16_t OFSocketAddressAppleTalkNetwork( + const OFSocketAddress *_Nonnull address); + +/** + * @brief Sets the AppleTalk node of the specified @ref OFSocketAddress. + * + * @param address The address on which to set the AppleTalk node + * @param node The AppleTalk node to set on the address + */ +extern void OFSocketAddressSetAppleTalkNode(OFSocketAddress *_Nonnull address, + uint8_t node); + +/** + * @brief Gets the AppleTalk node of the specified @ref OFSocketAddress. + * + * @param address The address on which to get the AppleTalk node + * @return The AppleTalk node of the address + */ +extern uint8_t OFSocketAddressAppleTalkNode( + const OFSocketAddress *_Nonnull address); + extern bool OFSocketInit(void); #if defined(OF_HAVE_THREADS) && defined(OF_AMIGAOS) && !defined(OF_MORPHOS) extern void OFSocketDeinit(void); #endif extern int OFSocketErrNo(void); Index: src/OFSocket.m ================================================================== --- src/OFSocket.m +++ src/OFSocket.m @@ -570,18 +570,40 @@ memcpy(ret.sockaddr.ipx.sipx_node, node, IPX_NODE_LEN); ret.sockaddr.ipx.sipx_port = OFToBigEndian16(port); return ret; } + +OFSocketAddress +OFSocketAddressMakeAppleTalk(uint16_t network, uint8_t node, uint8_t port) +{ + OFSocketAddress ret; + + memset(&ret, '\0', sizeof(ret)); + ret.family = OFSocketAddressFamilyAppleTalk; + ret.length = sizeof(ret.sockaddr.at); + +#ifdef AF_APPLETALK + ret.sockaddr.at.sat_family = AF_APPLETALK; +#else + ret.sockaddr.at.sat_family = AF_UNSPEC; +#endif + ret.sockaddr.at.sat_net = OFToBigEndian16(network); + ret.sockaddr.at.sat_node = node; + ret.sockaddr.at.sat_port = port; + + return ret; +} bool OFSocketAddressEqual(const OFSocketAddress *address1, const OFSocketAddress *address2) { const struct sockaddr_in *addrIn1, *addrIn2; const struct sockaddr_in6 *addrIn6_1, *addrIn6_2; const struct sockaddr_ipx *addrIPX1, *addrIPX2; + const struct sockaddr_at *addrAT1, *addrAT2; void *pool; OFString *path1, *path2; bool ret; if (address1->family != address2->family) @@ -655,10 +677,26 @@ return false; if (memcmp(addrIPX1->sipx_node, addrIPX2->sipx_node, IPX_NODE_LEN) != 0) return false; + return true; + case OFSocketAddressFamilyAppleTalk: + if (address1->length < (socklen_t)sizeof(struct sockaddr_at) || + address2->length < (socklen_t)sizeof(struct sockaddr_at)) + @throw [OFInvalidArgumentException exception]; + + addrAT1 = &address1->sockaddr.at; + addrAT2 = &address2->sockaddr.at; + + if (addrAT1->sat_net != addrAT2->sat_net) + return false; + if (addrAT1->sat_node != addrAT2->sat_node) + return false; + if (addrAT1->sat_port != addrAT2->sat_port) + return false; + return true; default: @throw [OFInvalidArgumentException exception]; } } @@ -731,10 +769,19 @@ for (size_t i = 0; i < IPX_NODE_LEN; i++) OFHashAddByte(&hash, address->sockaddr.ipx.sipx_node[i]); + break; + case OFSocketAddressFamilyAppleTalk: + if (address->length < (socklen_t)sizeof(struct sockaddr_at)) + @throw [OFInvalidArgumentException exception]; + + OFHashAddByte(&hash, address->sockaddr.at.sat_net >> 8); + OFHashAddByte(&hash, address->sockaddr.at.sat_net); + OFHashAddByte(&hash, address->sockaddr.at.sat_port); + break; default: @throw [OFInvalidArgumentException exception]; } @@ -848,10 +895,15 @@ address->sockaddr.in6.sin6_port = OFToBigEndian16(port); break; case OFSocketAddressFamilyIPX: address->sockaddr.ipx.sipx_port = OFToBigEndian16(port); break; + case OFSocketAddressFamilyAppleTalk: + if (port > UINT8_MAX) + @throw [OFOutOfRangeException exception]; + + address->sockaddr.at.sat_port = (uint8_t)port; default: @throw [OFInvalidArgumentException exception]; } } @@ -863,10 +915,12 @@ return OFFromBigEndian16(address->sockaddr.in.sin_port); case OFSocketAddressFamilyIPv6: return OFFromBigEndian16(address->sockaddr.in6.sin6_port); case OFSocketAddressFamilyIPX: return OFFromBigEndian16(address->sockaddr.ipx.sipx_port); + case OFSocketAddressFamilyAppleTalk: + return address->sockaddr.at.sat_port; default: @throw [OFInvalidArgumentException exception]; } } @@ -933,5 +987,41 @@ if (address->family != OFSocketAddressFamilyIPX) @throw [OFInvalidArgumentException exception]; memcpy(node, address->sockaddr.ipx.sipx_node, IPX_NODE_LEN); } + +void +OFSocketAddressSetAppleTalkNetwork(OFSocketAddress *address, uint16_t network) +{ + if (address->family != OFSocketAddressFamilyAppleTalk) + @throw [OFInvalidArgumentException exception]; + + address->sockaddr.at.sat_net = OFToBigEndian16(network); +} + +uint16_t +OFSocketAddressAppleTalkNetwork(const OFSocketAddress *address) +{ + if (address->family != OFSocketAddressFamilyAppleTalk) + @throw [OFInvalidArgumentException exception]; + + return OFFromBigEndian16(address->sockaddr.at.sat_net); +} + +void +OFSocketAddressSetAppleTalkNode(OFSocketAddress *address, uint8_t node) +{ + if (address->family != OFSocketAddressFamilyAppleTalk) + @throw [OFInvalidArgumentException exception]; + + address->sockaddr.at.sat_node = node; +} + +uint8_t +OFSocketAddressAppleTalkNode(const OFSocketAddress *address) +{ + if (address->family != OFSocketAddressFamilyAppleTalk) + @throw [OFInvalidArgumentException exception]; + + return address->sockaddr.at.sat_node; +}