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.

252 lines
7.8 KiB

  1. From 0e09c2b5c573ad1fce7c8f13b6bf255e1f21d390 Mon Sep 17 00:00:00 2001
  2. From: Luca <deri@ntop.org>
  3. Date: Tue, 11 Sep 2018 10:02:34 +0300
  4. Subject: [PATCH] Fixes #600 Backport of recent fixes (e.g. #601)
  5. ---
  6. Makefile.am | 5 ++-
  7. autogen.sh | 2 +-
  8. configure.seed | 13 +++++--
  9. src/lib/Makefile.in | 54 ++++++++++++++++++++++++++++++
  10. src/lib/ndpi_main.c | 2 --
  11. src/lib/protocols/ssl.c | 36 ++++++++++++++------
  12. src/lib/third_party/include/hash.h | 1 +
  13. 7 files changed, 94 insertions(+), 19 deletions(-)
  14. create mode 100644 src/lib/Makefile.in
  15. diff --git a/Makefile.am b/Makefile.am
  16. index 17c6748..37f0849 100644
  17. --- a/Makefile.am
  18. +++ b/Makefile.am
  19. @@ -1,8 +1,7 @@
  20. ACLOCAL_AMFLAGS = -I m4
  21. -
  22. SUBDIRS = src/lib example tests
  23. -pkgconfigdir = $(libdir)/pkgconfig
  24. +pkgconfigdir = $(prefix)/libdata/pkgconfig
  25. pkgconfig_DATA = libndpi.pc
  26. -EXTRA_DIST = libndpi.sym autogen.sh
  27. +EXTRA_DIST = autogen.sh
  28. diff --git a/autogen.sh b/autogen.sh
  29. index 6596b2f..efeffc4 100755
  30. --- a/autogen.sh
  31. +++ b/autogen.sh
  32. @@ -5,7 +5,7 @@ NDPI_MINOR="4"
  33. NDPI_PATCH="0"
  34. NDPI_VERSION_SHORT="$NDPI_MAJOR.$NDPI_MINOR.$NDPI_PATCH"
  35. -rm -f configure config.h config.h.in src/lib/Makefile.in
  36. +rm -f configure config.h config.h.in
  37. AUTOCONF=$(command -v autoconf)
  38. AUTOMAKE=$(command -v automake)
  39. diff --git a/configure.seed b/configure.seed
  40. index 6b85c66..8f8817f 100644
  41. --- a/configure.seed
  42. +++ b/configure.seed
  43. @@ -10,6 +10,7 @@ AC_PROG_CC
  44. AM_PROG_CC_C_O
  45. AX_PTHREAD
  46. +NDPI_VERSION_SHORT="@NDPI_VERSION_SHORT@"
  47. NDPI_MAJOR="@NDPI_MAJOR@"
  48. NDPI_MINOR="@NDPI_MINOR@"
  49. NDPI_PATCH="@NDPI_PATCH@"
  50. @@ -51,11 +52,16 @@ else
  51. AC_CHECK_LIB([numa], [numa_available], [LIBNUMA="-lnuma"])
  52. fi
  53. -
  54. +if test -z `which clang`; then
  55. +CC=gcc
  56. +else
  57. +CC=clang
  58. +fi
  59. +
  60. HS_LIB=
  61. HS_INC=
  62. -AC_ARG_WITH(hyperscan, [ --with-hyperscan Enable nDPI build with Intel Hyperscan])
  63. +AC_ARG_WITH(hyperscan, [ --with-hyperscan Enable nDPI build with Intel Hyperscan])
  64. if test "${with_hyperscan+set}" = set; then
  65. BKP=$LIBS
  66. @@ -127,12 +133,13 @@ AC_ARG_ENABLE([debug-messages],
  67. AC_CHECK_LIB(pthread, pthread_setaffinity_np, AC_DEFINE_UNQUOTED(HAVE_PTHREAD_SETAFFINITY_NP, 1, [libc has pthread_setaffinity_np]))
  68. -AC_CONFIG_FILES([Makefile example/Makefile tests/Makefile libndpi.pc src/include/ndpi_define.h])
  69. +AC_CONFIG_FILES([Makefile example/Makefile tests/Makefile libndpi.pc src/include/ndpi_define.h src/lib/Makefile])
  70. AC_CONFIG_HEADERS(src/include/ndpi_config.h)
  71. AC_SUBST(GIT_RELEASE)
  72. AC_SUBST(NDPI_MAJOR)
  73. AC_SUBST(NDPI_MINOR)
  74. AC_SUBST(NDPI_PATCH)
  75. +AC_SUBST(NDPI_VERSION_SHORT)
  76. AC_SUBST(SVN_DATE)
  77. AC_SUBST(JSON_C_LIB)
  78. AC_SUBST(PCAP_INC)
  79. diff --git a/src/lib/Makefile.in b/src/lib/Makefile.in
  80. new file mode 100644
  81. index 0000000..ca29001
  82. --- /dev/null
  83. +++ b/src/lib/Makefile.in
  84. @@ -0,0 +1,54 @@
  85. +#
  86. +# Simple non-autotools dependent makefile
  87. +#
  88. +# ./autogen.sh
  89. +# cd src/lib
  90. +# make Makefile
  91. +#
  92. +
  93. +
  94. +#
  95. +# Installation directories
  96. +#
  97. +prefix = /usr
  98. +libdir = ${prefix}/lib
  99. +includedir = ${prefix}/include/ndpi
  100. +CC = @CC@
  101. +CFLAGS += -fPIC -DPIC -I../include -Ithird_party/include -DNDPI_LIB_COMPILATION -O2 # -g
  102. +RANLIB = ranlib
  103. +
  104. +OBJECTS = $(patsubst protocols/%.c, protocols/%.o, $(wildcard protocols/*.c)) $(patsubst third_party/src/%.c, third_party/src/%.o, $(wildcard third_party/src/*.c)) ndpi_main.o
  105. +HEADERS = $(wildcard ../include/*.h)
  106. +NDPI_LIB_STATIC = libndpi.a
  107. +NDPI_LIB_SHARED_BASE = libndpi.so
  108. +NDPI_LIB_SHARED = $(NDPI_LIB_SHARED_BASE).@NDPI_VERSION_SHORT@
  109. +NDPI_LIBS = $(NDPI_LIB_STATIC) $(NDPI_LIB_SHARED)
  110. +
  111. +ifeq ($(OS),Darwin)
  112. +CC=clang
  113. +endif
  114. +
  115. +all: $(NDPI_LIBS)
  116. +
  117. +ndpi_main.c: ndpi_content_match.c.inc
  118. +
  119. +$(NDPI_LIB_STATIC): $(OBJECTS)
  120. + ar rc $@ $(OBJECTS)
  121. + $(RANLIB) $@
  122. +
  123. +$(NDPI_LIB_SHARED): $(OBJECTS)
  124. + $(CC) -shared -fPIC -o $@ $(OBJECTS)
  125. + ln -Fs $(NDPI_LIB_SHARED) $(NDPI_LIB_SHARED_BASE)
  126. +
  127. +%.o: %.c $(HEADERS) Makefile
  128. + $(CC) $(CFLAGS) -c $< -o $@
  129. +
  130. +clean:
  131. + /bin/rm -f $(NDPI_LIB_STATIC) $(OBJECTS) *.o *.so *.lo
  132. +
  133. +install: $(NDPI_LIBS)
  134. + mkdir -p $(DESTDIR)$(libdir)
  135. + cp $(NDPI_LIBS) $(DESTDIR)$(libdir)/
  136. + ln -Fs $(DESTDIR)$(libdir)/$(NDPI_LIB_SHARED) $(DESTDIR)$(libdir)/$(NDPI_LIB_SHARED_BASE)
  137. + mkdir -p $(DESTDIR)$(includedir)
  138. + cp ../include/*.h $(DESTDIR)$(includedir)
  139. diff --git a/src/lib/ndpi_main.c b/src/lib/ndpi_main.c
  140. index b002126..5beb6b4 100644
  141. --- a/src/lib/ndpi_main.c
  142. +++ b/src/lib/ndpi_main.c
  143. @@ -43,9 +43,7 @@
  144. #include "ndpi_content_match.c.inc"
  145. #include "third_party/include/ndpi_patricia.h"
  146. -#include "third_party/src/ndpi_patricia.c"
  147. #include "third_party/include/hash.h"
  148. -#include "third_party/src/hash.c"
  149. #ifdef HAVE_HYPERSCAN
  150. #include <hs/hs.h>
  151. diff --git a/src/lib/protocols/ssl.c b/src/lib/protocols/ssl.c
  152. index b8c3697..59aedcb 100644
  153. --- a/src/lib/protocols/ssl.c
  154. +++ b/src/lib/protocols/ssl.c
  155. @@ -27,7 +27,7 @@
  156. #include "ndpi_api.h"
  157. -/* #define CERTIFICATE_DEBUG 1 */
  158. +// #define CERTIFICATE_DEBUG 1
  159. #define NDPI_MAX_SSL_REQUEST_SIZE 10000
  160. /* Skype.c */
  161. @@ -246,28 +246,43 @@ int getSSLcertificate(struct ndpi_detection_module_struct *ndpi_struct,
  162. u_int16_t compression_len;
  163. u_int16_t extensions_len;
  164. - compression_len = packet->payload[offset+1];
  165. - offset += compression_len + 3;
  166. + offset++;
  167. + compression_len = packet->payload[offset];
  168. + offset++;
  169. +
  170. +#ifdef CERTIFICATE_DEBUG
  171. + printf("SSL [compression_len: %u]\n", compression_len);
  172. +#endif
  173. +
  174. + // offset += compression_len + 3;
  175. + offset += compression_len;
  176. if(offset < total_len) {
  177. - extensions_len = packet->payload[offset];
  178. + extensions_len = ntohs(*((u_int16_t*)&packet->payload[offset]));
  179. + offset += 2;
  180. +
  181. +#ifdef CERTIFICATE_DEBUG
  182. + printf("SSL [extensions_len: %u]\n", extensions_len);
  183. +#endif
  184. - if((extensions_len+offset) < total_len) {
  185. + if((extensions_len+offset) <= total_len) {
  186. /* Move to the first extension
  187. Type is u_int to avoid possible overflow on extension_len addition */
  188. - u_int extension_offset = 1;
  189. + u_int extension_offset = 0;
  190. while(extension_offset < extensions_len) {
  191. u_int16_t extension_id, extension_len;
  192. - memcpy(&extension_id, &packet->payload[offset+extension_offset], 2);
  193. + extension_id = ntohs(*((u_int16_t*)&packet->payload[offset+extension_offset]));
  194. extension_offset += 2;
  195. - memcpy(&extension_len, &packet->payload[offset+extension_offset], 2);
  196. + extension_len = ntohs(*((u_int16_t*)&packet->payload[offset+extension_offset]));
  197. extension_offset += 2;
  198. - extension_id = ntohs(extension_id), extension_len = ntohs(extension_len);
  199. -
  200. +#ifdef CERTIFICATE_DEBUG
  201. + printf("SSL [extension_id: %u][extension_len: %u]\n", extension_id, extension_len);
  202. +#endif
  203. +
  204. if(extension_id == 0) {
  205. u_int begin = 0,len;
  206. char *server_name = (char*)&packet->payload[offset+extension_offset];
  207. @@ -316,6 +331,7 @@ int sslTryAndRetrieveServerCertificate(struct ndpi_detection_module_struct *ndpi
  208. if((packet->payload_packet_len > 9) && (packet->payload[0] == 0x16)) {
  209. char certificate[64];
  210. int rc;
  211. +
  212. certificate[0] = '\0';
  213. rc = getSSLcertificate(ndpi_struct, flow, certificate, sizeof(certificate));
  214. packet->ssl_certificate_num_checks++;
  215. diff --git a/src/lib/third_party/include/hash.h b/src/lib/third_party/include/hash.h
  216. index 4f53e5a..2251706 100644
  217. --- a/src/lib/third_party/include/hash.h
  218. +++ b/src/lib/third_party/include/hash.h
  219. @@ -25,5 +25,6 @@ extern int ht_hash( hashtable_t *hashtable, char *key );
  220. extern entry_t *ht_newpair( char *key, u_int16_t value );
  221. extern void ht_set( hashtable_t *hashtable, char *key, u_int16_t value );
  222. extern u_int16_t ht_get( hashtable_t *hashtable, char *key );
  223. +extern void ht_free( hashtable_t *hashtable );
  224. #endif /* _HASH_H_ */
  225. --
  226. 2.19.1