Imported Upstream version 1.1.0
[platform/upstream/iotivity.git] / build_common / android / compatibility / c_compat.c
index b76c739..1a36575 100644 (file)
@@ -1,4 +1,5 @@
 #include <stdlib.h>
+#include <sys/socket.h>
 
 /* from stdlib.h */
 float strtof(const char *nptr, char **endptr)
@@ -45,3 +46,25 @@ void srandom(unsigned int __s)
 {
     srand48(__s);
 }
+
+/* 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)
+{
+    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;
+}