summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--CMakeLists.txt7
-rw-r--r--cmake/FindJansson.cmake59
-rw-r--r--src/server/net.c8
-rw-r--r--src/server/rpc.c18
-rw-r--r--src/server/rpc.h7
5 files changed, 94 insertions, 5 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index d734813..bd9fa93 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -31,9 +31,10 @@ ADD_DEFINITIONS(-DWITH_IPV6)
FIND_PACKAGE(Threads)
FIND_PACKAGE(Fuse)
-FIND_PACKAGE(PkgConfig REQUIRED)
+#FIND_PACKAGE(PkgConfig REQUIRED)
#FIND_PACKAGE(LibXml2 REQUIRED)
FIND_PACKAGE(ZLIB)
+FIND_PACKAGE(Jansson)
if(NOT FUSE_FOUND)
message( " *** No fuse dev libs found, won't build dnbd3-fuse" )
@@ -46,7 +47,7 @@ endif()
#PKG_CHECK_MODULES(GLIB glib-2.0 REQUIRED)
-INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR} ${CMAKE_BINARY_DIR} ${ZLIB_INCLUDE_DIR} ${FUSE_INCLUDE_DIR})
+INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR} ${CMAKE_BINARY_DIR} ${ZLIB_INCLUDE_DIR} ${FUSE_INCLUDE_DIR} ${JANSSON_INCLUDE_DIR})
################################################################################
# VERSION HEADER #
@@ -87,7 +88,7 @@ INSTALL(TARGETS dnbd3-client RUNTIME DESTINATION sbin)
if(THREADS_FOUND AND ZLIB_FOUND)
FILE(GLOB_RECURSE SERVER_SRCS src/server/*.c)
ADD_EXECUTABLE(dnbd3-server ${SERVER_SRCS})
- TARGET_LINK_LIBRARIES(dnbd3-server ${CMAKE_THREAD_LIBS_INIT} ${ZLIB_LIBRARIES})
+ TARGET_LINK_LIBRARIES(dnbd3-server ${CMAKE_THREAD_LIBS_INIT} ${ZLIB_LIBRARIES} ${JANSSON_LIBRARIES})
if(UNIX AND NOT APPLE)
target_link_libraries(dnbd3-server rt)
endif()
diff --git a/cmake/FindJansson.cmake b/cmake/FindJansson.cmake
new file mode 100644
index 0000000..3225923
--- /dev/null
+++ b/cmake/FindJansson.cmake
@@ -0,0 +1,59 @@
+# - Try to find Jansson
+# Once done this will define
+#
+# JANSSON_FOUND - system has Jansson
+# JANSSON_INCLUDE_DIRS - the Jansson include directory
+# JANSSON_LIBRARIES - Link these to use Jansson
+#
+# Copyright (c) 2011 Lee Hambley <lee.hambley@gmail.com>
+#
+# Redistribution and use is allowed according to the terms of the New
+# BSD license.
+# For details see the accompanying COPYING-CMAKE-SCRIPTS file.
+#
+
+if (JANSSON_LIBRARIES AND JANSSON_INCLUDE_DIRS)
+ # in cache already
+ set(JANSSON_FOUND TRUE)
+else (JANSSON_LIBRARIES AND JANSSON_INCLUDE_DIRS)
+ find_path(JANSSON_INCLUDE_DIR
+ NAMES
+ jansson.h
+ PATHS
+ /usr/include
+ /usr/local/include
+ /opt/local/include
+ /sw/include
+ )
+
+find_library(JANSSON_LIBRARY
+ NAMES
+ jansson
+ PATHS
+ /usr/lib
+ /usr/local/lib
+ /opt/local/lib
+ /sw/lib
+ )
+
+set(JANSSON_INCLUDE_DIRS
+ ${JANSSON_INCLUDE_DIR}
+ )
+
+if (JANSSON_LIBRARY)
+ set(JANSSON_LIBRARIES
+ ${JANSSON_LIBRARIES}
+ ${JANSSON_LIBRARY}
+ )
+endif (JANSSON_LIBRARY)
+
+ include(FindPackageHandleStandardArgs)
+ find_package_handle_standard_args(Jansson DEFAULT_MSG
+ JANSSON_LIBRARIES JANSSON_INCLUDE_DIRS)
+
+ # show the JANSSON_INCLUDE_DIRS and JANSSON_LIBRARIES variables only in the advanced view
+ mark_as_advanced(JANSSON_INCLUDE_DIRS JANSSON_LIBRARIES)
+
+endif (JANSSON_LIBRARIES AND JANSSON_INCLUDE_DIRS)
+
+
diff --git a/src/server/net.c b/src/server/net.c
index 9836043..2edb398 100644
--- a/src/server/net.c
+++ b/src/server/net.c
@@ -43,6 +43,8 @@
#include "../config.h"
#include "../types.h"
#include "locks.h"
+#include "rpc.h"
+
static uint64_t totalBytesSent = 0;
static pthread_spinlock_t statisticsSentLock;
@@ -66,8 +68,7 @@ static inline bool recv_request_header(int sock, dnbd3_request_t *request)
}
// Payload sanity check
if ( request->cmd != CMD_GET_BLOCK && request->size > MAX_PAYLOAD ) {
- logadd( LOG_WARNING, "Client tries to send a packet of type %d with %d bytes payload. Dropping client.", (int)request->cmd,
- (int)request->size );
+ logadd( LOG_WARNING, "Client tries to send a packet of type %d with %d bytes payload. Dropping client.", (int)request->cmd, (int)request->size );
return false;
}
return true;
@@ -187,6 +188,9 @@ void *net_client_handler(void *dnbd3_client)
}
}
}
+ } else if ( strncmp( (char*)&request, "GET ", 4 ) == 0 || strncmp( (char*)&request, "POST ", 5 ) == 0 ) {
+ rpc_sendStatsJson( client->sock );
+ logadd( LOG_INFO, "Sending statistics." );
}
if ( bOk ) {
diff --git a/src/server/rpc.c b/src/server/rpc.c
new file mode 100644
index 0000000..b2dc088
--- /dev/null
+++ b/src/server/rpc.c
@@ -0,0 +1,18 @@
+#include <unistd.h>
+#include <jansson.h>
+
+#include "rpc.h"
+
+void rpc_sendStatsJson(int sock)
+{
+ int receivedBytes = 0;
+ int sentBytes = 1;
+ json_t *statisticsJson = json_pack( "{sisi}", "receivedBytes", receivedBytes, "sentBytes", sentBytes );
+ char* jsonString = json_dumps(statisticsJson, 0);
+ char bla[500];
+ snprintf(bla, sizeof bla, "HTTP/1.1 200 OK\r\nConnection: Close\r\nContent-Length: %d\r\nContent-Type: application/json\r\n\r\n", (int)strlen(jsonString));
+ write( sock, bla, strlen(bla) );
+ int n = write( sock, jsonString, strlen(jsonString));
+ json_decref(statisticsJson);
+ free(jsonString);
+}
diff --git a/src/server/rpc.h b/src/server/rpc.h
new file mode 100644
index 0000000..78585cf
--- /dev/null
+++ b/src/server/rpc.h
@@ -0,0 +1,7 @@
+#ifndef _RPC_H_
+#define _RPC_H_
+
+
+void rpc_sendStatsJson(int sock);
+
+#endif