Imported Upstream version 1.1.0
[platform/upstream/iotivity.git] / build_common / android / compatibility / c_compat.c
index 000428a..1a36575 100644 (file)
@@ -1,4 +1,5 @@
 #include <stdlib.h>
+#include <sys/socket.h>
 
 /* from stdlib.h */
 float strtof(const char *nptr, char **endptr)
@@ -46,15 +47,24 @@ void srandom(unsigned int __s)
     srand48(__s);
 }
 
-/* from unistd.h */
-int getpagesize(void)
+/* from __cmsg_nxthdr.cpp */
+/*
+ * The function __cmsg_nxthd() is missing in Android 4.4, but the Android NDK
+ * header files in the version we are using are referencing it and we use it in
+ * our code, this functions was added in version 5.0. To make IoTivity
+ * dynamically loadable at load time on Android KitKat 4.4 add this functions
+ * as a weak symbol, so it will be used if the c lib does not provide it, like
+ * on Android < 5.0 This code was taken from these two resources:
+ * https://raw.githubusercontent.com/android/platform_bionic/master/libc/bionic/__cmsg_nxthdr.cpp
+ * https://github.com/android/platform_bionic/commit/ff64831b0965c16c95c9f81a148f30a6ef3a6c64
+ */
+struct cmsghdr* __attribute__((weak)) __cmsg_nxthdr(struct msghdr* msg, struct cmsghdr* cmsg)
 {
-  extern unsigned int __page_size;
-  return __page_size;
-}
-
-int __getpageshift(void)
-{
-  extern unsigned int __page_shift;
-  return __page_shift;
+    struct cmsghdr* ptr;
+    ptr = (struct cmsghdr*)(((unsigned char*) cmsg) + CMSG_ALIGN(cmsg->cmsg_len));
+    size_t len = (unsigned long)((char*)(ptr+1) - (char*) msg->msg_control);
+    if (len > msg->msg_controllen) {
+        return NULL;
+    }
+    return ptr;
 }