You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
7.4 KiB

  1. From 75a19e9059e671442abe40908ba80c2da614118d Mon Sep 17 00:00:00 2001
  2. From: Hauke Mehrtens <hauke@hauke-m.de>
  3. Date: Mon, 26 Oct 2015 22:42:40 +0100
  4. Subject: [PATCH] connectivity: fix getnameinfo() call for musl libc
  5. The musl libc and also the libc from net/freebsd are currently checking
  6. if the size of the sockaddr given to getnameinfo() matches the size for
  7. this family and it fails when giving sizeof(struct sockaddr_storage).
  8. This patch fixes this problem by making the caller also provide the
  9. size of the struct sockaddr_storage.
  10. I am currently trying to get a fix for this problem into musl libc, but
  11. IoTivity should still fix this.
  12. This also fixes a wrong error message.
  13. Change-Id: Ie9b89523b5ba20e536c03e3ad85c65bd5ff2ba53
  14. Signed-off-by: Hauke Mehrtens <hauke@hauke-m.de>
  15. ---
  16. resource/csdk/connectivity/inc/caadapterutils.h | 12 ++++++++----
  17. .../csdk/connectivity/src/adapter_util/caadapternetdtls.c | 10 +++++-----
  18. resource/csdk/connectivity/src/adapter_util/caadapterutils.c | 9 +++++----
  19. resource/csdk/connectivity/src/ip_adapter/caipserver.c | 2 +-
  20. resource/csdk/connectivity/src/tcp_adapter/catcpserver.c | 2 +-
  21. 5 files changed, 20 insertions(+), 15 deletions(-)
  22. --- a/resource/csdk/connectivity/inc/caadapterutils.h
  23. +++ b/resource/csdk/connectivity/inc/caadapterutils.h
  24. @@ -212,13 +212,16 @@ void CAClearNetInterfaceInfoList(u_array
  25. */
  26. void CAClearServerInfoList(u_arraylist_t *serverInfoList);
  27. +#ifndef WITH_ARDUINO
  28. /**
  29. * Convert address from binary to string.
  30. - * @param[in] ipaddr IP address info.
  31. - * @param[out] host address string (must be CA_IPADDR_SIZE).
  32. - * @param[out] port host order port number.
  33. + * @param[in] sockAddr IP address info.
  34. + * @param[in] sockAddrLen size of sockAddr.
  35. + * @param[out] host address string (must be CA_IPADDR_SIZE).
  36. + * @param[out] port host order port number.
  37. */
  38. -void CAConvertAddrToName(const struct sockaddr_storage *sockaddr, char *host, uint16_t *port);
  39. +void CAConvertAddrToName(const struct sockaddr_storage *sockAddr, socklen_t sockAddrLen,
  40. + char *host, uint16_t *port);
  41. /**
  42. * Convert address from string to binary.
  43. @@ -227,6 +230,7 @@ void CAConvertAddrToName(const struct so
  44. * @param[out] ipaddr IP address info.
  45. */
  46. void CAConvertNameToAddr(const char *host, uint16_t port, struct sockaddr_storage *sockaddr);
  47. +#endif /* WITH_ARDUINO */
  48. #ifdef __ANDROID__
  49. /**
  50. --- a/resource/csdk/connectivity/src/adapter_util/caadapternetdtls.c
  51. +++ b/resource/csdk/connectivity/src/adapter_util/caadapternetdtls.c
  52. @@ -417,7 +417,7 @@ static int32_t CAReadDecryptedPayload(dt
  53. ((addrInfo->addr.st.ss_family == AF_INET) ? CA_IPV4 : CA_IPV6) | CA_SECURE, .port = 0 },
  54. .identity =
  55. { 0 } };
  56. - CAConvertAddrToName(&(addrInfo->addr.st), sep.endpoint.addr, &sep.endpoint.port);
  57. + CAConvertAddrToName(&(addrInfo->addr.st), addrInfo->size, sep.endpoint.addr, &sep.endpoint.port);
  58. if (NULL == g_caDtlsContext)
  59. {
  60. @@ -468,7 +468,7 @@ static int32_t CASendSecureData(dtls_con
  61. CAEndpoint_t endpoint = {.adapter = CA_DEFAULT_ADAPTER};
  62. - CAConvertAddrToName(&(addrInfo->addr.st), endpoint.addr, &endpoint.port);
  63. + CAConvertAddrToName(&(addrInfo->addr.st), addrInfo->size, endpoint.addr, &endpoint.port);
  64. endpoint.flags = addrInfo->addr.st.ss_family == AF_INET ? CA_IPV4 : CA_IPV6;
  65. endpoint.flags |= CA_SECURE;
  66. endpoint.adapter = CA_ADAPTER_IP;
  67. @@ -515,7 +515,7 @@ static int32_t CAHandleSecureEvent(dtls_
  68. stCADtlsAddrInfo_t *addrInfo = (stCADtlsAddrInfo_t *)session;
  69. char peerAddr[MAX_ADDR_STR_SIZE_CA] = { 0 };
  70. uint16_t port = 0;
  71. - CAConvertAddrToName(&(addrInfo->addr.st), peerAddr, &port);
  72. + CAConvertAddrToName(&(addrInfo->addr.st), addrInfo->size, peerAddr, &port);
  73. CARemovePeerFromPeerInfoList(peerAddr, port);
  74. }
  75. @@ -553,7 +553,7 @@ static int32_t CAGetPskCredentials(dtls_
  76. stCADtlsAddrInfo_t *addrInfo = (stCADtlsAddrInfo_t *)session;
  77. char peerAddr[MAX_ADDR_STR_SIZE_CA] = { 0 };
  78. uint16_t port = 0;
  79. - CAConvertAddrToName(&(addrInfo->addr.st), peerAddr, &port);
  80. + CAConvertAddrToName(&(addrInfo->addr.st), addrInfo->size, peerAddr, &port);
  81. if(CA_STATUS_OK != CAAddIdToPeerInfoList(peerAddr, port, desc, descLen) )
  82. {
  83. @@ -933,7 +933,7 @@ static int CAVerifyCertificate(struct dt
  84. stCADtlsAddrInfo_t *addrInfo = (stCADtlsAddrInfo_t *)session;
  85. char peerAddr[MAX_ADDR_STR_SIZE_CA] = { 0 };
  86. uint16_t port = 0;
  87. - CAConvertAddrToName(&(addrInfo->addr.st), peerAddr, &port);
  88. + CAConvertAddrToName(&(addrInfo->addr.st), addrInfo->size, peerAddr, &port);
  89. CAResult_t result = CAAddIdToPeerInfoList(peerAddr, port,
  90. crtChain[0].subject.data + DER_SUBJECT_HEADER_LEN + 2, crtChain[0].subject.data[DER_SUBJECT_HEADER_LEN + 1]);
  91. --- a/resource/csdk/connectivity/src/adapter_util/caadapterutils.c
  92. +++ b/resource/csdk/connectivity/src/adapter_util/caadapterutils.c
  93. @@ -122,14 +122,15 @@ CAResult_t CAParseIPv4AddressInternal(co
  94. * These two conversion functions return void because errors can't happen
  95. * (because of NI_NUMERIC), and there's nothing to do if they do happen.
  96. */
  97. -void CAConvertAddrToName(const struct sockaddr_storage *sockAddr, char *host, uint16_t *port)
  98. +void CAConvertAddrToName(const struct sockaddr_storage *sockAddr, socklen_t sockAddrLen,
  99. + char *host, uint16_t *port)
  100. {
  101. VERIFY_NON_NULL_VOID(sockAddr, CA_ADAPTER_UTILS_TAG, "sockAddr is null");
  102. VERIFY_NON_NULL_VOID(host, CA_ADAPTER_UTILS_TAG, "host is null");
  103. VERIFY_NON_NULL_VOID(port, CA_ADAPTER_UTILS_TAG, "port is null");
  104. int r = getnameinfo((struct sockaddr *)sockAddr,
  105. - sizeof (struct sockaddr_storage),
  106. + sockAddrLen,
  107. host, MAX_ADDR_STR_SIZE_CA,
  108. NULL, 0,
  109. NI_NUMERICHOST|NI_NUMERICSERV);
  110. @@ -138,12 +139,12 @@ void CAConvertAddrToName(const struct so
  111. if (EAI_SYSTEM == r)
  112. {
  113. OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG,
  114. - "getaddrinfo failed: errno %s", strerror(errno));
  115. + "getnameinfo failed: errno %s", strerror(errno));
  116. }
  117. else
  118. {
  119. OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG,
  120. - "getaddrinfo failed: %s", gai_strerror(r));
  121. + "getnameinfo failed: %s", gai_strerror(r));
  122. }
  123. return;
  124. }
  125. --- a/resource/csdk/connectivity/src/ip_adapter/caipserver.c
  126. +++ b/resource/csdk/connectivity/src/ip_adapter/caipserver.c
  127. @@ -299,7 +299,7 @@ static CAResult_t CAReceiveMessage(int f
  128. }
  129. }
  130. - CAConvertAddrToName(&srcAddr, sep.endpoint.addr, &sep.endpoint.port);
  131. + CAConvertAddrToName(&srcAddr, msg.msg_namelen, sep.endpoint.addr, &sep.endpoint.port);
  132. if (flags & CA_SECURE)
  133. {
  134. --- a/resource/csdk/connectivity/src/tcp_adapter/catcpserver.c
  135. +++ b/resource/csdk/connectivity/src/tcp_adapter/catcpserver.c
  136. @@ -478,7 +478,7 @@ static void CAAcceptHandler(void *data)
  137. }
  138. svritem->u4tcp.fd = sockfd;
  139. - CAConvertAddrToName((struct sockaddr_storage *)&clientaddr,
  140. + CAConvertAddrToName((struct sockaddr_storage *)&clientaddr, clientlen,
  141. (char *) &svritem->addr, &svritem->u4tcp.port);
  142. ca_mutex_lock(g_mutexObjectList);