From 75a19e9059e671442abe40908ba80c2da614118d Mon Sep 17 00:00:00 2001 From: Hauke Mehrtens Date: Mon, 26 Oct 2015 22:42:40 +0100 Subject: [PATCH] connectivity: fix getnameinfo() call for musl libc The musl libc and also the libc from net/freebsd are currently checking if the size of the sockaddr given to getnameinfo() matches the size for this family and it fails when giving sizeof(struct sockaddr_storage). This patch fixes this problem by making the caller also provide the size of the struct sockaddr_storage. I am currently trying to get a fix for this problem into musl libc, but IoTivity should still fix this. This also fixes a wrong error message. Change-Id: Ie9b89523b5ba20e536c03e3ad85c65bd5ff2ba53 Signed-off-by: Hauke Mehrtens --- resource/csdk/connectivity/inc/caadapterutils.h | 12 ++++++++---- .../csdk/connectivity/src/adapter_util/caadapternetdtls.c | 10 +++++----- resource/csdk/connectivity/src/adapter_util/caadapterutils.c | 9 +++++---- resource/csdk/connectivity/src/ip_adapter/caipserver.c | 2 +- resource/csdk/connectivity/src/tcp_adapter/catcpserver.c | 2 +- 5 files changed, 20 insertions(+), 15 deletions(-) --- a/resource/csdk/connectivity/inc/caadapterutils.h +++ b/resource/csdk/connectivity/inc/caadapterutils.h @@ -212,13 +212,16 @@ void CAClearNetInterfaceInfoList(u_array */ void CAClearServerInfoList(u_arraylist_t *serverInfoList); +#ifndef WITH_ARDUINO /** * Convert address from binary to string. - * @param[in] ipaddr IP address info. - * @param[out] host address string (must be CA_IPADDR_SIZE). - * @param[out] port host order port number. + * @param[in] sockAddr IP address info. + * @param[in] sockAddrLen size of sockAddr. + * @param[out] host address string (must be CA_IPADDR_SIZE). + * @param[out] port host order port number. */ -void CAConvertAddrToName(const struct sockaddr_storage *sockaddr, char *host, uint16_t *port); +void CAConvertAddrToName(const struct sockaddr_storage *sockAddr, socklen_t sockAddrLen, + char *host, uint16_t *port); /** * Convert address from string to binary. @@ -227,6 +230,7 @@ void CAConvertAddrToName(const struct so * @param[out] ipaddr IP address info. */ void CAConvertNameToAddr(const char *host, uint16_t port, struct sockaddr_storage *sockaddr); +#endif /* WITH_ARDUINO */ #ifdef __ANDROID__ /** --- a/resource/csdk/connectivity/src/adapter_util/caadapternetdtls.c +++ b/resource/csdk/connectivity/src/adapter_util/caadapternetdtls.c @@ -417,7 +417,7 @@ static int32_t CAReadDecryptedPayload(dt ((addrInfo->addr.st.ss_family == AF_INET) ? CA_IPV4 : CA_IPV6) | CA_SECURE, .port = 0 }, .identity = { 0 } }; - CAConvertAddrToName(&(addrInfo->addr.st), sep.endpoint.addr, &sep.endpoint.port); + CAConvertAddrToName(&(addrInfo->addr.st), addrInfo->size, sep.endpoint.addr, &sep.endpoint.port); if (NULL == g_caDtlsContext) { @@ -468,7 +468,7 @@ static int32_t CASendSecureData(dtls_con CAEndpoint_t endpoint = {.adapter = CA_DEFAULT_ADAPTER}; - CAConvertAddrToName(&(addrInfo->addr.st), endpoint.addr, &endpoint.port); + CAConvertAddrToName(&(addrInfo->addr.st), addrInfo->size, endpoint.addr, &endpoint.port); endpoint.flags = addrInfo->addr.st.ss_family == AF_INET ? CA_IPV4 : CA_IPV6; endpoint.flags |= CA_SECURE; endpoint.adapter = CA_ADAPTER_IP; @@ -515,7 +515,7 @@ static int32_t CAHandleSecureEvent(dtls_ stCADtlsAddrInfo_t *addrInfo = (stCADtlsAddrInfo_t *)session; char peerAddr[MAX_ADDR_STR_SIZE_CA] = { 0 }; uint16_t port = 0; - CAConvertAddrToName(&(addrInfo->addr.st), peerAddr, &port); + CAConvertAddrToName(&(addrInfo->addr.st), addrInfo->size, peerAddr, &port); CARemovePeerFromPeerInfoList(peerAddr, port); } @@ -553,7 +553,7 @@ static int32_t CAGetPskCredentials(dtls_ stCADtlsAddrInfo_t *addrInfo = (stCADtlsAddrInfo_t *)session; char peerAddr[MAX_ADDR_STR_SIZE_CA] = { 0 }; uint16_t port = 0; - CAConvertAddrToName(&(addrInfo->addr.st), peerAddr, &port); + CAConvertAddrToName(&(addrInfo->addr.st), addrInfo->size, peerAddr, &port); if(CA_STATUS_OK != CAAddIdToPeerInfoList(peerAddr, port, desc, descLen) ) { @@ -933,7 +933,7 @@ static int CAVerifyCertificate(struct dt stCADtlsAddrInfo_t *addrInfo = (stCADtlsAddrInfo_t *)session; char peerAddr[MAX_ADDR_STR_SIZE_CA] = { 0 }; uint16_t port = 0; - CAConvertAddrToName(&(addrInfo->addr.st), peerAddr, &port); + CAConvertAddrToName(&(addrInfo->addr.st), addrInfo->size, peerAddr, &port); CAResult_t result = CAAddIdToPeerInfoList(peerAddr, port, crtChain[0].subject.data + DER_SUBJECT_HEADER_LEN + 2, crtChain[0].subject.data[DER_SUBJECT_HEADER_LEN + 1]); --- a/resource/csdk/connectivity/src/adapter_util/caadapterutils.c +++ b/resource/csdk/connectivity/src/adapter_util/caadapterutils.c @@ -122,14 +122,15 @@ CAResult_t CAParseIPv4AddressInternal(co * These two conversion functions return void because errors can't happen * (because of NI_NUMERIC), and there's nothing to do if they do happen. */ -void CAConvertAddrToName(const struct sockaddr_storage *sockAddr, char *host, uint16_t *port) +void CAConvertAddrToName(const struct sockaddr_storage *sockAddr, socklen_t sockAddrLen, + char *host, uint16_t *port) { VERIFY_NON_NULL_VOID(sockAddr, CA_ADAPTER_UTILS_TAG, "sockAddr is null"); VERIFY_NON_NULL_VOID(host, CA_ADAPTER_UTILS_TAG, "host is null"); VERIFY_NON_NULL_VOID(port, CA_ADAPTER_UTILS_TAG, "port is null"); int r = getnameinfo((struct sockaddr *)sockAddr, - sizeof (struct sockaddr_storage), + sockAddrLen, host, MAX_ADDR_STR_SIZE_CA, NULL, 0, NI_NUMERICHOST|NI_NUMERICSERV); @@ -138,12 +139,12 @@ void CAConvertAddrToName(const struct so if (EAI_SYSTEM == r) { OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG, - "getaddrinfo failed: errno %s", strerror(errno)); + "getnameinfo failed: errno %s", strerror(errno)); } else { OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG, - "getaddrinfo failed: %s", gai_strerror(r)); + "getnameinfo failed: %s", gai_strerror(r)); } return; } --- a/resource/csdk/connectivity/src/ip_adapter/caipserver.c +++ b/resource/csdk/connectivity/src/ip_adapter/caipserver.c @@ -299,7 +299,7 @@ static CAResult_t CAReceiveMessage(int f } } - CAConvertAddrToName(&srcAddr, sep.endpoint.addr, &sep.endpoint.port); + CAConvertAddrToName(&srcAddr, msg.msg_namelen, sep.endpoint.addr, &sep.endpoint.port); if (flags & CA_SECURE) { --- a/resource/csdk/connectivity/src/tcp_adapter/catcpserver.c +++ b/resource/csdk/connectivity/src/tcp_adapter/catcpserver.c @@ -478,7 +478,7 @@ static void CAAcceptHandler(void *data) } svritem->u4tcp.fd = sockfd; - CAConvertAddrToName((struct sockaddr_storage *)&clientaddr, + CAConvertAddrToName((struct sockaddr_storage *)&clientaddr, clientlen, (char *) &svritem->addr, &svritem->u4tcp.port); ca_mutex_lock(g_mutexObjectList);