Skip to content

Commit

Permalink
Provide Darwin compatible implementation for InetLayer (project-chip#…
Browse files Browse the repository at this point in the history
…1121)

* Use --with-target-network=Network.framework as a build target. Does nothing so far

* UDPEndpoint basic client working in order to send a message to a server

* UDPEndPoint basic client can send/receive message from a server

* UDPEndPoint basic client can send from a different port than the remote server

* Add missing methods to EndPointBasis.h and move some code from UDPEndpoint to IPEndPoint

* Prepare the client code to support multiple connections

* UDP basic client can connect to a different server after the first connection

* UDP Basic server over Network.framework

* Restyle the code and add a IPEndPointBasis::GetEndPoint method

* Map error code to INET_ERROR codes

* Configure the protocol stack accordingly to the requested address type (ipv4/ipv6)

* Fix some crashes and deadlocks into the Network.framework backend

* Remove printf from IPEndPointBasis.cpp and use ChipLogging instead
  • Loading branch information
vivien-apple authored Jun 18, 2020
1 parent e3a7632 commit 7962596
Show file tree
Hide file tree
Showing 12 changed files with 598 additions and 33 deletions.
6 changes: 6 additions & 0 deletions Makefile-Standalone
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ DEBUG ?= 0
TIMESTAMP ?= 0
TUNNEL_FAILOVER ?= 0
USE_LWIP ?= 0
USE_NW ?= 0
NO_OPENSSL ?= 0
BLUEZ ?= 0
USE_FUZZING ?= 0
Expand Down Expand Up @@ -178,6 +179,11 @@ configure_OPTIONS += --with-target-network=lwip --with-lwip=intern
TargetTuple := $(TargetTuple)-lwip
endif

ifeq ($(USE_NW),1)
configure_OPTIONS += --with-target-network=Network.framework
TargetTuple := $(TargetTuple)-nw
endif

ifeq ($(LONG_TESTS),1)
configure_OPTIONS += --enable-long-tests=yes
endif
Expand Down
23 changes: 21 additions & 2 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -1100,14 +1100,14 @@ CONFIG_TARGET_NETWORKS=sockets
AC_MSG_CHECKING([target network])
AC_ARG_WITH(target-network,
[AS_HELP_STRING([--with-target-network=NETWORK],
[Specify the target network stack from one or more of, separated by commas: sockets or lwip @<:@default=sockets@:>@.])],
[Specify the target network stack from one or more of, separated by commas: sockets, lwip or Network.framework @<:@default=sockets@:>@.])],
[
CONFIG_TARGET_NETWORKS=`echo ${with_target_network} | sed -e 's/,/ /g'`
for target_network in ${CONFIG_TARGET_NETWORKS}; do
case "${target_network}" in
lwip|sockets)
lwip|sockets|Network.framework)
;;
*)
Expand All @@ -1124,6 +1124,7 @@ AC_DEFINE_UNQUOTED([CONFIG_TARGET_NETWORKS], "${CONFIG_TARGET_NETWORKS}", [CHIP

CHIP_SYSTEM_CONFIG_USE_LWIP=0
CHIP_SYSTEM_CONFIG_USE_SOCKETS=0
CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK=0

for target_network in ${CONFIG_TARGET_NETWORKS}; do
case ${target_network} in
Expand All @@ -1136,6 +1137,10 @@ for target_network in ${CONFIG_TARGET_NETWORKS}; do
CHIP_SYSTEM_CONFIG_USE_SOCKETS=1
;;

Network.framework)
CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK=1
;;

esac
done

Expand All @@ -1149,6 +1154,12 @@ AM_CONDITIONAL([CHIP_SYSTEM_CONFIG_USE_SOCKETS], [test "${CHIP_SYSTEM_CONFIG_USE
AC_DEFINE_UNQUOTED([CHIP_SYSTEM_CONFIG_USE_SOCKETS], [${CHIP_SYSTEM_CONFIG_USE_SOCKETS}],
[Define to 1 if you want to use BSD sockets with CHIP System Layer.])

AC_SUBST(CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK)
AM_CONDITIONAL([CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK], [test "${CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK}" = 1])
AC_DEFINE_UNQUOTED([CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK], [${CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK}],
[Define to 1 if you want to use Network.framework with CHIP System Layer.])

#
#
# Internet Protocol Network Endpoints
#
Expand Down Expand Up @@ -2028,6 +2039,14 @@ AX_PTHREAD([], [])
AC_CHECK_DECL([PTHREAD_NULL], [], [AC_DEFINE([PTHREAD_NULL],[0], [Approximation of PTHREAD_NULL since pthread.h does not define one])],
[[#include <pthread.h>]])

#
# Check for Network.framework
#
if test "${CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK}" = "1"; then
AC_CHECK_HEADER([Network/Network.h])
LDFLAGS="${LDFLAGS} -lnetwork"
fi

#
# Check for <new>
#
Expand Down
25 changes: 25 additions & 0 deletions src/inet/EndPointBasis.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@

#include <support/DLLUtil.h>

#if CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK
#include <Network/Network.h>
#endif // CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK

//--- Declaration of LWIP protocol control buffer structure names
#if CHIP_SYSTEM_CONFIG_USE_LWIP
#if INET_CONFIG_ENABLE_RAW_ENDPOINT
Expand Down Expand Up @@ -68,6 +72,11 @@ class DLL_EXPORT EndPointBasis : public InetLayerBasis
kBasisState_Closed = 0 /**< Encapsulated descriptor is not valid. */
};

#if CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK
/** Test whether endpoint is a Network.framework endpoint */
bool IsNetworkFrameworkEndPoint(void) const;
#endif

#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
/** Test whether endpoint is a POSIX socket */
bool IsSocketsEndPoint(void) const;
Expand All @@ -82,6 +91,11 @@ class DLL_EXPORT EndPointBasis : public InetLayerBasis
bool IsOpenEndPoint(void) const;

protected:
#if CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK
nw_parameters_t mParameters;
IPAddressType mAddrType; /**< Protocol family, i.e. IPv4 or IPv6. */
#endif

#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
int mSocket; /**< Encapsulated socket descriptor. */
IPAddressType mAddrType; /**< Protocol family, i.e. IPv4 or IPv6. */
Expand Down Expand Up @@ -125,6 +139,13 @@ class DLL_EXPORT EndPointBasis : public InetLayerBasis
void InitEndPointBasis(InetLayer & aInetLayer, void * aAppState = NULL);
};

#if CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK
inline bool EndPointBasis::IsNetworkFrameworkEndPoint(void) const
{
return mParameters != NULL;
}
#endif // CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK

#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
inline bool EndPointBasis::IsSocketsEndPoint(void) const
{
Expand All @@ -151,6 +172,10 @@ inline bool EndPointBasis::IsOpenEndPoint(void) const
lResult = (lResult || IsSocketsEndPoint());
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS

#if CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK
lResult = (lResult || IsNetworkFrameworkEndPoint());
#endif // CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK

return lResult;
}

Expand Down
4 changes: 2 additions & 2 deletions src/inet/IPAddress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ IPAddress IPAddress::FromIPv6(const ip6_addr_t & ipv6Addr)

#endif // CHIP_SYSTEM_CONFIG_USE_LWIP

#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
#if CHIP_SYSTEM_CONFIG_USE_SOCKETS || CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK

#if INET_CONFIG_ENABLE_IPV4
struct in_addr IPAddress::ToIPv4() const
Expand Down Expand Up @@ -231,7 +231,7 @@ IPAddress IPAddress::FromSockAddr(const struct sockaddr & sockaddr)
return Any;
}

#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS || CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK

// Is address an IPv4 address encoded in IPv6 format?
bool IPAddress::IsIPv4() const
Expand Down
12 changes: 9 additions & 3 deletions src/inet/IPAddress.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#ifndef IPADDRESS_H
#define IPADDRESS_H

#include <stddef.h>
#include <stdint.h>

#include <support/DLLUtil.h>
Expand All @@ -46,8 +47,13 @@
#include <lwip/inet.h>
#endif // CHIP_SYSTEM_CONFIG_USE_LWIP

#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
#if CHIP_SYSTEM_CONFIG_USE_SOCKETS || CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK
#include <ifaddrs.h>
#include <net/if.h>
#include <netinet/in.h>
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS || CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK

#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
#include <sys/socket.h>
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS

Expand Down Expand Up @@ -531,7 +537,7 @@ class DLL_EXPORT IPAddress

#endif // CHIP_SYSTEM_CONFIG_USE_LWIP

#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
#if CHIP_SYSTEM_CONFIG_USE_SOCKETS || CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK

struct in6_addr ToIPv6(void) const;
static IPAddress FromIPv6(const struct in6_addr & addr);
Expand All @@ -552,7 +558,7 @@ class DLL_EXPORT IPAddress
*/
static IPAddress FromSockAddr(const struct sockaddr & sockaddr);

#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS || CHIP_SYSTEM_USE_NETWORK_FRAMEWORK

/**
* @brief Construct an IPv6 unique-local address (ULA) from its parts.
Expand Down
Loading

0 comments on commit 7962596

Please sign in to comment.