FS-9952: Big commit, first registered jsonrpc echo call successful, lots of cleanup remaining

This commit is contained in:
Shane Bryldt authored and Mike Jerris committed 2017-03-22 17:42:50 -04:00
1 parent fd49aebb1d
commit 8f569f715b
25 files changed
+778 -2813

No files matched your search

+4 -8
View File
@@ -10,13 +10,13 @@ lib_LTLIBRARIES = libks.la
libks_la_SOURCES = src/ks.c src/ks_string.c src/ks_json.c src/cJSON.c src/cJSON_Utils.c src/ks_thread.c src/ks_thread_pool.c src/ks_mutex.c src/ks_config.c
libks_la_SOURCES += src/ks_log.c src/ks_socket.c src/ks_buffer.c src/ks_pool.c src/simclist.c
libks_la_SOURCES += src/ks_time.c src/ks_printf.c src/ks_hash.c src/ks_q.c src/ks_dso.c # src/ks_dht.c
libks_la_SOURCES += src/ks_ssl.c src/kws.c src/ks_rng.c src/ks_rpcmessage.c src/ks_base64.c
libks_la_SOURCES += src/ks_ssl.c src/kws.c src/ks_rng.c src/ks_base64.c
libks_la_SOURCES += src/utp/utp_api.cpp src/utp/utp_callbacks.cpp src/utp/utp_hash.cpp src/utp/utp_internal.cpp
libks_la_SOURCES += src/utp/utp_packedsockaddr.cpp src/utp/utp_utils.cpp src/ks_bencode.c
libks_la_SOURCES += src/dht/ks_dht.c src/dht/ks_dht_datagram.c src/dht/ks_dht_endpoint.c src/dht/ks_dht_message.c src/dht/ks_dht_transaction.c
libks_la_SOURCES += src/dht/ks_dht_job.c src/dht/ks_dht_search.c src/dht/ks_dht_publish.c src/dht/ks_dht_distribute.c src/dht/ks_dht_storageitem.c
libks_la_SOURCES += src/dht/ks_dht_bucket.c
libks_la_SOURCES += crypt/aeskey.c crypt/aestab.c crypt/sha2.c crypt/twofish.c crypt/aes_modes.c crypt/aescrypt.c crypt/twofish_cfb.c
libks_la_SOURCES += crypt/aeskey.c crypt/aestab.c crypt/sha2.c crypt/twofish.c crypt/aes_modes.c crypt/aescrypt.c crypt/twofish_cfb.c
#aes.h aescpp.h brg_endian.h aesopt.h aestab.h brg_types.h sha2.h twofish.h
libks_la_SOURCES += src/ks_acl.c
@@ -29,14 +29,10 @@ library_include_HEADERS = src/include/ks_config.h src/include/ks.h src/include/k
library_include_HEADERS += src/include/ks_thread_pool.h src/include/ks_cJSON.h src/include/ks_cJSON_Utils.h
library_include_HEADERS += src/include/ks_pool.h src/include/simclist.h src/include/ks_time.h src/include/ks_q.h src/include/ks_socket.h
library_include_HEADERS += src/include/ks_dso.h src/include/ks_platform.h src/include/ks_types.h # src/include/ks_rng.h src/include/ks_dht.h
library_include_HEADERS += src/include/ks_printf.h src/include/ks_hash.h src/include/ks_ssl.h src/include/kws.h
library_include_HEADERS += src/include/ks_rpcmessage.h src/include/ks_base64.h
library_include_HEADERS += src/include/ks_printf.h src/include/ks_hash.h src/include/ks_ssl.h src/include/kws.h
library_include_HEADERS += src/include/ks_base64.h
library_include_HEADERS += src/utp/utp_internal.h src/utp/utp.h src/utp/utp_types.h src/utp/utp_callbacks.h src/utp/utp_templates.h
library_include_HEADERS += src/utp/utp_hash.h src/utp/utp_packedsockaddr.h src/utp/utp_utils.h src/include/ks_utp.h src/include/ks_acl.h
tests: libks.la
$(MAKE) -C test tests
+6 -7
View File
@@ -1,23 +1,23 @@
/*
* Copyright (c) 2007-2014, Anthony Minessale II
* All rights reserved.
*
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
*
* * Neither the name of the original author; nor the names of any contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
*
*
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
@@ -137,7 +137,6 @@ KS_DECLARE(void) ks_random_string(char *buf, uint16_t len, char *set);
#include "ks_bencode.h"
#include "ks_rng.h"
#include "ks_acl.h"
#include "ks_rpcmessage.h"
#include "ks_base64.h"
KS_END_EXTERN_C
-94
View File
@@ -1,94 +0,0 @@
/*
* Copyright (c) 2017, FreeSWITCH Solutions LLC
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of the original author; nor the names of any contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _KS_RPCMESSAGE_H_
#define _KS_RPCMESSAGE_H_
#include "ks.h"
KS_BEGIN_EXTERN_C
#define KS_RPCMESSAGE_NAMESPACE_LENGTH 16
#define KS_RPCMESSAGE_COMMAND_LENGTH 238
#define KS_RPCMESSAGE_FQCOMMAND_LENGTH (KS_RPCMESSAGE_NAMESPACE_LENGTH+KS_RPCMESSAGE_COMMAND_LENGTH+1)
#define KS_RPCMESSAGE_VERSION_LENGTH 9
typedef uint32_t ks_rpcmessageid_t;
KS_DECLARE(void) ks_rpcmessage_init(ks_pool_t *pool);
KS_DECLARE(void*) ks_json_pool_alloc(ks_size_t size);
KS_DECLARE(void) ks_json_pool_free(void *ptr);
KS_DECLARE(ks_rpcmessageid_t) ks_rpcmessage_create_request(char *namespace,
char *method,
cJSON **paramsP,
cJSON **requestP);
KS_DECLARE(ks_size_t) ks_rpc_create_buffer(char *namespace,
char *method,
cJSON **paramsP,
ks_buffer_t *buffer);
KS_DECLARE(ks_rpcmessageid_t) ks_rpcmessage_create_response(
const cJSON *request,
cJSON **resultP,
cJSON **responseP);
KS_DECLARE(ks_rpcmessageid_t) ks_rpcmessage_create_errorresponse(
const cJSON *request,
cJSON **errorP,
cJSON **responseP);
KS_DECLARE(ks_bool_t) ks_rpcmessage_isrequest(cJSON *msg);
KS_DECLARE(ks_bool_t) ks_rpcmessage_isrpc(cJSON *msg);
KS_END_EXTERN_C
#endif /* defined(_KS_RPCMESSAGE_H_) */
/* For Emacs:
* Local Variables:
* mode:c
* indent-tabs-mode:t
* tab-width:4
* c-basic-offset:4
* End:
* For VIM:
* vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
*/
-317
View File
@@ -1,317 +0,0 @@
/*
* Copyright (c) 2017 FreeSWITCH Solutions LLC
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of the original author; nor the names of any contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma GCC optimize ("O0")
#include <ks.h>
#include <ks_rpcmessage.h>
#include <ks_buffer.h>
struct
{
ks_mutex_t *id_mutex;
uint32_t message_id;
ks_pool_t *pool;
} handle = {NULL, 0, NULL};
const char PROTOCOL[] = "jsonrpc";
const char PROTOCOL_VERSION[] = "2.0";
const char ID[] = "id";
const char METHOD[] = "method";
const char PARAMS[] = "params";
const char ERROR[] = "error";
const char RESULT[] = "result";
KS_DECLARE(void*) ks_json_pool_alloc(ks_size_t size)
{
return ks_pool_alloc(handle.pool, size);
}
KS_DECLARE(void) ks_json_pool_free(void *ptr)
{
ks_pool_free(handle.pool, &ptr);
}
KS_DECLARE(void) ks_rpcmessage_init(ks_pool_t* pool)
{
if (!handle.id_mutex) {
ks_mutex_create(&handle.id_mutex, KS_MUTEX_FLAG_DEFAULT, pool);
handle.pool = pool;
cJSON_Hooks hooks;
hooks.malloc_fn = ks_json_pool_alloc;
hooks.free_fn = ks_json_pool_free;
cJSON_InitHooks(&hooks);
}
return;
}
static uint32_t ks_rpcmessage_next_id()
{
uint32_t message_id;
ks_mutex_lock(handle.id_mutex);
++handle.message_id;
if (!handle.message_id) {
++handle.message_id;
}
message_id = handle.message_id;
ks_mutex_unlock(handle.id_mutex);
return message_id;
}
static cJSON *ks_rpcmessage_new(uint32_t id)
{
cJSON *obj = cJSON_CreateObject();
cJSON_AddItemToObject(obj, PROTOCOL, cJSON_CreateString(PROTOCOL_VERSION));
if (id) {
cJSON_AddItemToObject(obj, ID, cJSON_CreateNumber(id));
}
return obj;
}
static cJSON *ks_rpcmessage_dup(cJSON *msgid)
{
cJSON *obj = cJSON_CreateObject();
cJSON_AddItemToObject(obj, PROTOCOL, cJSON_CreateString(PROTOCOL_VERSION));
if (msgid) {
cJSON_AddItemToObject(obj, ID, cJSON_Duplicate(msgid, 0));
}
return obj;
}
KS_DECLARE(ks_bool_t) ks_rpcmessage_isrequest(cJSON *msg)
{
cJSON *result = cJSON_GetObjectItem(msg, RESULT);
cJSON *error = cJSON_GetObjectItem(msg, ERROR);
if (result || error) {
return KS_FALSE;
}
return KS_TRUE;
}
KS_DECLARE(ks_bool_t) ks_rpcmessage_isrpc(cJSON *msg)
{
cJSON *rpc = cJSON_GetObjectItem(msg, PROTOCOL);
if (rpc) {
return KS_FALSE;
}
return KS_TRUE;
}
KS_DECLARE(ks_rpcmessageid_t) ks_rpcmessage_create_request(char *namespace,
char *command,
cJSON **paramsP,
cJSON **requestP)
{
cJSON *msg, *params = NULL;
*requestP = NULL;
ks_rpcmessageid_t msgid = ks_rpcmessage_next_id();
msg = ks_rpcmessage_new(msgid);
if (paramsP) {
if (*paramsP) { /* parameters have been passed */
params = *paramsP;
}
else {
params = cJSON_CreateObject();
*paramsP = params;
}
cJSON_AddItemToObject(msg, PARAMS, params);
}
char fqcommand[KS_RPCMESSAGE_FQCOMMAND_LENGTH];
memset(fqcommand, 0, sizeof(fqcommand));
sprintf(fqcommand, "%s.%s", namespace, command);
cJSON_AddItemToObject(msg, METHOD, cJSON_CreateString(fqcommand));
*requestP = msg;
return msgid;
}
KS_DECLARE(ks_size_t) ks_rpc_create_buffer(char *namespace,
char *method,
cJSON **params,
ks_buffer_t *buffer)
{
cJSON *message;
ks_rpcmessageid_t msgid = ks_rpcmessage_create_request(namespace, method, params, &message);
if (!msgid) {
return 0;
}
if ( (*params)->child == NULL) {
cJSON_AddNullToObject(*params, "bladenull");
}
const char* b = cJSON_PrintUnformatted(message);
ks_size_t size = strlen(b);
ks_buffer_write(buffer, b, size);
cJSON_Delete(message);
return size;
}
static ks_rpcmessageid_t ks_rpcmessage_get_messageid(const cJSON *msg, cJSON **cmsgidP)
{
ks_rpcmessageid_t msgid = 0;
cJSON *cmsgid = cJSON_GetObjectItem(msg, ID);
if (cmsgid->type == cJSON_Number) {
msgid = (ks_rpcmessageid_t) cmsgid->valueint;
}
*cmsgidP = cmsgid;
return msgid;
}
static ks_rpcmessageid_t ks_rpcmessage_new_response(
const cJSON *request,
cJSON **result,
cJSON **pmsg)
{
cJSON *respmsg = NULL;
cJSON *cmsgid = NULL;
cJSON *command = cJSON_GetObjectItem(request, METHOD);
ks_rpcmessageid_t msgid = ks_rpcmessage_get_messageid(request, &cmsgid );
if (!msgid || !command) {
return 0;
}
*pmsg = respmsg = ks_rpcmessage_dup(cmsgid);
cJSON_AddItemToObject(respmsg, METHOD, cJSON_Duplicate(command, 0));
if (result && *result) {
cJSON_AddItemToObject(respmsg, RESULT, *result);
}
return msgid;
}
KS_DECLARE(ks_rpcmessageid_t) ks_rpcmessage_create_response(
const cJSON *request,
cJSON **resultP,
cJSON **responseP)
{
ks_rpcmessageid_t msgid = ks_rpcmessage_new_response(request, resultP, responseP);
cJSON *respmsg = *responseP;
if (msgid) {
if (resultP && *resultP == NULL) {
cJSON *result = cJSON_CreateObject();
*resultP = result;
cJSON_AddItemToObject(respmsg, RESULT, result);
}
}
return msgid;
}
KS_DECLARE(ks_rpcmessageid_t) ks_rpcmessage_create_errorresponse(
const cJSON *request,
cJSON **errorP,
cJSON **responseP)
{
ks_rpcmessageid_t msgid = ks_rpcmessage_new_response(request, NULL, responseP);
cJSON *respmsg = *responseP;
if (msgid) {
if (errorP && *errorP == NULL) {
cJSON *error = cJSON_CreateObject();
*errorP = error;
cJSON_AddItemToObject(respmsg, ERROR, error);
}
else if (errorP && *errorP) {
cJSON_AddItemToObject(*responseP, ERROR, *errorP);
}
}
return msgid;
}
/* For Emacs:
* Local Variables:
* mode:c
* indent-tabs-mode:t
* tab-width:4
* c-basic-offset:4
* End:
* For VIM:
* vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
*/
+2 -8
View File
@@ -1,14 +1,9 @@
AM_CFLAGS += -I$(abs_top_srcdir)/src/include -g -ggdb -O0 $(openssl_CFLAGS)
TEST_LDADD = $(abs_top_builddir)/libks.la $(openssl_LIBS)
check_PROGRAMS =
check_PROGRAMS =
EXTRA_DIST = tap.h
check_PROGRAMS += testmessages
testmessages_SOURCES = testmessages.c tap.c
testmessages_CFLAGS = $(AM_CFLAGS)
testmessages_LDADD = $(TEST_LDADD)
check_PROGRAMS += testbuckets
testbuckets_SOURCES = testbuckets.c tap.c
testbuckets_CFLAGS = $(AM_CFLAGS)
@@ -96,7 +91,7 @@ nodeidgen_LDADD = $(TEST_LDADD)
#check_PROGRAMS += libtorrent_example
#libtorrent_example_SOURCES = libtorrent-example.c
#libtorrent_example_CFLAGS = $(AM_CFLAGS)
#libtorrent_example_CFLAGS = $(AM_CFLAGS)
#libtorrent_example_LDADD = $(abs_top_builddir)/libks.la $(abs_top_builddir)/test/libtorrent.so /usr/lib/x86_64-linux-gnu/libboost_system.a $(openssl_LIBS) -ledit -lpthread -ltorrent-rasterbar -lstdc++
TESTS=$(check_PROGRAMS)
@@ -108,4 +103,3 @@ $(abs_top_builddir)/test/libtorrent.so: $(abs_top_builddir)/test/libtorrent.o
$(abs_top_builddir)/test/libtorrent.o: $(abs_top_builddir)/test/libtorrent.cpp
g++ -c -fPIC -o $(abs_top_builddir)/test/libtorrent.o -I$(abs_top_builddir)/test/ $(abs_top_builddir)/test/libtorrent.cpp
-124
View File
@@ -1,124 +0,0 @@
#pragma GCC diagnostic ignored "-Wunused-but-set-variable"
#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic ignored "-Wunused-function"
#include "../src/include/ks_rpcmessage.h"
#pragma GCC optimize ("O0")
ks_pool_t *pool;
void test01()
{
printf("**** testrpcmessages - test01 start\n\n"); fflush(stdout);
cJSON* request1 = NULL;
cJSON* parms1 = NULL;
cJSON* response1 = NULL;
/*namespace, method, params, **request */
ks_rpcmessageid_t msgid = ks_rpcmessage_create_request("app1", "func1", &parms1, &request1);
if (msgid == 0) {
printf("message create failed %d\n", msgid);
}
cJSON_AddStringToObject(parms1, "hello", "cruel world");
char* data = cJSON_PrintUnformatted(request1);
printf("test01 request1: %d\n%s\n\n", msgid, data);
ks_json_pool_free(data);
/* convert to buffer */
cJSON* parms2 = NULL;
ks_buffer_t *buffer;
ks_buffer_create(&buffer, 256, 256, 1024);
ks_size_t n = ks_rpc_create_buffer("app2", "func2", &parms2, buffer);
ks_size_t size = ks_buffer_len(buffer);
char *b = (char *)ks_pool_alloc(pool, size+1);
ks_buffer_read(buffer, b, size);
printf("test01 request2: %zd %zd from ks_buffer\n%s\n\n\n", n, size, b);
/* create message 3 */
cJSON *parms3 = cJSON_CreateNumber(1);
cJSON *request3 = NULL;
msgid = ks_rpcmessage_create_request("app1", "badbunny", &parms3, &request3);
data = cJSON_PrintUnformatted(request3);
printf("\ntest01i request: %d\n%s\n\n", msgid, data);
cJSON *response3 = NULL;
ks_rpcmessage_create_response(request3, NULL, &response3);
data = cJSON_PrintUnformatted(response3);
printf("\ntest01 response3: %d\n%s\n\n", msgid, data);
ks_json_pool_free(data);
cJSON_Delete(request3);
cJSON_Delete(response3);
printf("**** testrpcmessages - test01 complete\n\n\n"); fflush(stdout);
}
void test02()
{
printf("**** testmessages - test02 start\n"); fflush(stdout);
printf("**** testmessages - test02 finished\n"); fflush(stdout);
return;
}
int main(int argc, char *argv[]) {
printf("testmessages - start\n");
int tests[100];
if (argc == 1) {
tests[0] = 1;
}
else {
for(int tix=1; tix<100 && tix<argc; ++tix) {
long i = strtol(argv[tix], NULL, 0);
tests[tix] = i;
}
}
ks_init();
ks_global_set_default_logger(7);
ks_status_t status;
status = ks_pool_open(&pool);
for (int tix=0; tix<argc; ++tix) {
if (tests[tix] == 1) {
ks_rpcmessage_init(pool);
test01();
continue;
}
if (tests[tix] == 2) {
ks_rpcmessage_init(pool);
test02();
continue;
}
}
return 0;
}