From c54071e654062e9f6934feddbeff17f7c1e16a2d Mon Sep 17 00:00:00 2001 From: Viktor Korsun Date: Wed, 9 Dec 2015 14:38:01 +1000 Subject: [PATCH 1/5] changing check order as here C++ uses a left-to-right order and accessing over the array is an UB --- Source/SocketLayer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/SocketLayer.cpp b/Source/SocketLayer.cpp index d111e5630..8cd9e6147 100644 --- a/Source/SocketLayer.cpp +++ b/Source/SocketLayer.cpp @@ -594,7 +594,7 @@ bool SocketLayer::GetFirstBindableIP(char firstBindable[128], int ipProto) break; } - if (ipList[l]==UNASSIGNED_SYSTEM_ADDRESS || l==MAXIMUM_NUMBER_OF_INTERNAL_IDS) + if (l==MAXIMUM_NUMBER_OF_INTERNAL_IDS || ipList[l]==UNASSIGNED_SYSTEM_ADDRESS) return false; // RAKNET_DEBUG_PRINTF("%i %i %i %i\n", // ((char*)(&ipList[l].address.addr4.sin_addr.s_addr))[0], From 6c6f4fe98ffc0a84681cbfbbe638c6310fcb5f4c Mon Sep 17 00:00:00 2001 From: Viktor Korsun Date: Wed, 9 Dec 2015 14:42:28 +1000 Subject: [PATCH 2/5] adding va_end()'s to elliminate possible memory leaks, stack overflow or other UB --- Source/RakString.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Source/RakString.cpp b/Source/RakString.cpp index 5d8583ecd..6db9a2de2 100644 --- a/Source/RakString.cpp +++ b/Source/RakString.cpp @@ -76,11 +76,13 @@ RakString::RakString(const unsigned char *format, ...){ va_list ap; va_start(ap, format); Assign((const char*) format,ap); + va_end(ap); } RakString::RakString(const char *format, ...){ va_list ap; va_start(ap, format); Assign(format,ap); + va_end(ap); } RakString::RakString( const RakString & rhs) { @@ -377,6 +379,7 @@ void RakString::Set(const char *format, ...) va_start(ap, format); Clear(); Assign(format,ap); + va_end(ap); } bool RakString::IsEmpty(void) const { From 5d4011b14f95fd0944dbae49dce9c3751c854edf Mon Sep 17 00:00:00 2001 From: Viktor Korsun Date: Wed, 9 Dec 2015 14:47:14 +1000 Subject: [PATCH 3/5] fixing buffer overflow: accessing arrays at index 512 --- Source/PacketizedTCP.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Source/PacketizedTCP.cpp b/Source/PacketizedTCP.cpp index e84dcd2b5..75e135743 100644 --- a/Source/PacketizedTCP.cpp +++ b/Source/PacketizedTCP.cpp @@ -87,8 +87,8 @@ bool PacketizedTCP::SendList( const char **data, const unsigned int *lengths, co #endif - unsigned int lengthsArray[512]; - const char *dataArray[512]; + unsigned int lengthsArray[513]; + const char *dataArray[513]; dataArray[0]=(char*) &dataLength; lengthsArray[0]=sizeof(dataLength); for (int i=0; i < 512 && i < numParameters; i++) From cbc02de308cd58684657ecfa143d8f8cbbbba4a3 Mon Sep 17 00:00:00 2001 From: Viktor Korsun Date: Wed, 9 Dec 2015 14:50:13 +1000 Subject: [PATCH 4/5] closing fd2 as here it should be valid (non-negative) --- Source/SocketLayer.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Source/SocketLayer.cpp b/Source/SocketLayer.cpp index 8cd9e6147..3a2ac410d 100644 --- a/Source/SocketLayer.cpp +++ b/Source/SocketLayer.cpp @@ -233,7 +233,8 @@ RakNet::RakString SocketLayer::GetSubNetForSocketAndIp(__UDPSOCKET__ inSock, Rak ifc.ifc_buf = buf; if(ioctl(fd2, SIOCGIFCONF, &ifc) < 0) { - return ""; + close(fd2); + return ""; } struct ifreq *ifr; From 54d5d1d4fdb71084a3b681576ec586fa9e56db6a Mon Sep 17 00:00:00 2001 From: Viktor Korsun Date: Thu, 10 Dec 2015 14:06:39 +1000 Subject: [PATCH 5/5] adding Android target library project --- README.md | 40 +-------------------------- RakNet_Android_NDK/README.TXT | 31 +++++++++++++++++++++ RakNet_Android_NDK/jni/Android.mk | 8 ++++++ RakNet_Android_NDK/jni/Application.mk | 3 ++ 4 files changed, 43 insertions(+), 39 deletions(-) create mode 100644 RakNet_Android_NDK/README.TXT create mode 100644 RakNet_Android_NDK/jni/Android.mk create mode 100644 RakNet_Android_NDK/jni/Application.mk diff --git a/README.md b/README.md index d68299c87..e738f2b9e 100644 --- a/README.md +++ b/README.md @@ -191,45 +191,7 @@ Depending on what version you target, you may have to change two defines to not Android ----------------------------------------- - -You will need the latest CYWGIN and the android SDK to build native code on the android. Under CYWGIN, you will need to run ndk-build on a directory for RakNet. - -1. Under cygwin, create the RakNet directory somewhere, such as under samples. -For example, if you create the path `\cygwin\home\Kevin\android-ndk-r4b\samples\RakNet` - -2. I copied the Android.Manifest.xml and other files from another sample - -3. Under jni, you will need the following Android.mk - - LOCAL_PATH := $(call my-dir) - include $(CLEAR_VARS) - LOCAL_MODULE := RakNet - MY_PREFIX := $(LOCAL_PATH)/RakNetSources/ - MY_SOURCES := $(wildcard $(MY_PREFIX)*.cpp) - LOCAL_SRC_FILES += $(MY_SOURCES:$(MY_PREFIX)%=RakNetSources/%) - include $(BUILD_SHARED_LIBRARY) - -This version of Android.mk assumes there is a directory called RakNetSources, for example -`cygwin/home/Kevin/android-ndk-r4b/samples/RakNet/jni/RakNetSources` - -Under RakNetSources should be the /Source directory to RakNet. Rather than copy the files I used junction.exe -http://technet.microsoft.com/en-us/sysinternals/bb896768.aspx - -The command I used to create the junction was: - - D:/cygwin/home/Kevin/android-ndk-r4b/samples/RakNet/jni/junction.exe -s D:/cygwin/home/Kevin/android-ndk-r4b/samples/RakNet/jni/RakNetSources D:/RakNet4/Source - -To unjunction I used: - - D:/cygwin/home/Kevin/android-ndk-r4b/samples/RakNet/jni/junction.exe -d D:/cygwin/home/Kevin/android-ndk-r4b/samples/RakNet/jni/RakNetSources - -From within the CYWGIN enviroment, navigate to home/Kevin/android-ndk-r4b/samples/RakNet. Then type - - ../../ndk-build - -Everything should build and you should end up with a .so file. - -You should then be able to create a project in eclipse, and import cygwin/home/Kevin/android-ndk-r4b/samples/RakNet +Android project with build instructionsis is located in RakNet_Android_NDK folder. Native client ----------------------------------------- diff --git a/RakNet_Android_NDK/README.TXT b/RakNet_Android_NDK/README.TXT new file mode 100644 index 000000000..c1be44ce5 --- /dev/null +++ b/RakNet_Android_NDK/README.TXT @@ -0,0 +1,31 @@ +Android project for Oculus RakNet library. +Initial project version: Viktor Korsun, bitekas@gmail.com + +Copyright (c) 2015, Oculus VR, Inc. +All rights reserved. + +This source code is licensed under the BSD-style license found in the +LICENSE file in the root directory of this source tree. An additional grant +of patent rights can be found in the PATENTS file in the same directory. +--------------------------------------------------------------------------- + +To build Android native libraries run "ndk-build" from THIS directory, or +merge the content with your project. + +Windows build command example: +D:\Soft\android-ndk-r10e-64\bin\ndk-build + +Linux/OS X build command example: +$ ~/soft/android-ndk-r10e-32/ndk-build + +This will produce "libs" folder with shared libraries for platforms: + armeabi + armeabi-v7a + x86 + +The project uses android-9 API, that covers almost 100% of Android devices +by the end of 2015. To raise API (and possibly build additional ABIs) +please edit jni/Application.mk. + +The project has been tested with Android NDK 10e both 32- and 64- bit +versions. \ No newline at end of file diff --git a/RakNet_Android_NDK/jni/Android.mk b/RakNet_Android_NDK/jni/Android.mk new file mode 100644 index 000000000..27b060e7e --- /dev/null +++ b/RakNet_Android_NDK/jni/Android.mk @@ -0,0 +1,8 @@ +LOCAL_PATH:= $(call my-dir) +include $(CLEAR_VARS) +LOCAL_MODULE := libraknet + +FILE_LIST := $(wildcard $(LOCAL_PATH)/../../Source/*.cpp) +LOCAL_SRC_FILES := $(FILE_LIST:$(LOCAL_PATH)/%=%) + +include $(BUILD_SHARED_LIBRARY) \ No newline at end of file diff --git a/RakNet_Android_NDK/jni/Application.mk b/RakNet_Android_NDK/jni/Application.mk new file mode 100644 index 000000000..8ffe14073 --- /dev/null +++ b/RakNet_Android_NDK/jni/Application.mk @@ -0,0 +1,3 @@ +APP_STL := c++_static +APP_ABI := armeabi armeabi-v7a x86 +APP_PLATFORM := android-9