From e32021459d1ffe555475a562d5d2ceab527f5cb2 Mon Sep 17 00:00:00 2001 From: Ali Ince Date: Wed, 24 Oct 2018 22:23:04 +0100 Subject: [PATCH 01/11] Move struct declarations into private headers --- src/common/CMakeLists.txt | 8 - src/seabolt-cli/CMakeLists.txt | 7 +- src/seabolt-cli/src/main.c | 112 +++--- src/seabolt/src/CMakeLists.txt | 23 +- src/seabolt/src/address-private.h | 49 +++ src/seabolt/src/address-resolver-private.h | 31 ++ src/seabolt/src/address-resolver.c | 30 +- src/seabolt/src/address-resolver.h | 15 +- src/seabolt/src/address-set-private.h | 32 ++ src/seabolt/src/address-set.c | 42 +-- src/seabolt/src/address-set.h | 25 +- src/seabolt/src/address.c | 49 ++- src/seabolt/src/address.h | 45 +-- src/seabolt/src/auth.c | 2 +- src/seabolt/src/auth.h | 3 +- src/seabolt/src/bolt-private.h | 72 ++++ src/seabolt/src/bolt-public.h | 34 ++ src/seabolt/src/bolt.h | 10 +- src/seabolt/src/buffering.c | 52 +-- src/seabolt/src/buffering.h | 65 ++-- src/seabolt/src/config-impl.h | 65 ---- src/seabolt/src/config-private.h | 54 +++ src/seabolt/src/config.c | 321 ++++++++++++++++++ src/seabolt/src/config.h | 162 +++++++-- src/seabolt/src/connection-private.h | 87 +++++ .../src/{connections.c => connection.c} | 299 ++++++++-------- .../src/{connections.h => connection.h} | 204 +++-------- src/seabolt/src/connector-private.h | 32 ++ src/seabolt/src/connector.c | 112 ++---- src/seabolt/src/connector.h | 52 +-- src/seabolt/src/direct-pool.c | 101 +++--- src/seabolt/src/direct-pool.h | 2 +- src/seabolt/src/error.c | 2 +- src/seabolt/src/error.h | 2 +- src/seabolt/src/lifecycle.c | 4 +- src/seabolt/src/lifecycle.h | 2 +- src/seabolt/src/{logging.h => log-private.h} | 22 +- src/seabolt/src/{logging.c => log.c} | 56 ++- src/seabolt/src/log.h | 44 +++ src/seabolt/src/mem.c | 36 +- src/seabolt/src/mem.h | 21 +- src/seabolt/src/packstream.c | 7 +- src/seabolt/src/packstream.h | 2 +- src/seabolt/src/platform.c | 15 +- src/seabolt/src/platform.h | 4 +- src/seabolt/src/protocol.c | 5 +- src/seabolt/src/protocol.h | 6 +- src/seabolt/src/routing-pool.c | 49 +-- src/seabolt/src/routing-pool.h | 3 +- src/seabolt/src/routing-table.c | 23 +- src/seabolt/src/routing-table.h | 6 +- src/seabolt/src/stats.c | 38 +++ src/seabolt/src/{common-impl.h => stats.h} | 15 +- src/seabolt/src/status-private.h | 35 ++ src/seabolt/src/status.c | 55 +++ src/seabolt/src/status.h | 47 +++ .../utils => seabolt/src}/string-builder.c | 6 +- .../utils => seabolt/src}/string-builder.h | 8 +- src/seabolt/src/tls.c | 6 +- src/seabolt/src/tls.h | 4 +- src/seabolt/src/utils.c | 2 +- src/seabolt/src/utils.h | 2 +- src/seabolt/src/v1.c | 23 +- src/seabolt/src/v1.h | 6 +- src/seabolt/src/v3.c | 23 +- src/seabolt/src/v3.h | 2 +- src/seabolt/src/values-private.h | 98 ++++++ src/seabolt/src/values.c | 46 ++- src/seabolt/src/values.h | 80 +---- src/seabolt/tests/integration.hpp | 14 +- src/seabolt/tests/seabolt.cpp | 12 +- src/seabolt/tests/test-address-set.cpp | 71 ++-- src/seabolt/tests/test-addressing.cpp | 8 +- src/seabolt/tests/test-chunking-v1.cpp | 6 +- src/seabolt/tests/test-direct.cpp | 102 +++--- src/seabolt/tests/test-pooling.cpp | 28 +- src/seabolt/tests/test-values.cpp | 16 +- 77 files changed, 2078 insertions(+), 1181 deletions(-) create mode 100644 src/seabolt/src/address-private.h create mode 100644 src/seabolt/src/address-resolver-private.h create mode 100644 src/seabolt/src/address-set-private.h create mode 100644 src/seabolt/src/bolt-private.h create mode 100644 src/seabolt/src/bolt-public.h delete mode 100644 src/seabolt/src/config-impl.h create mode 100644 src/seabolt/src/config-private.h create mode 100644 src/seabolt/src/config.c create mode 100644 src/seabolt/src/connection-private.h rename src/seabolt/src/{connections.c => connection.c} (76%) rename src/seabolt/src/{connections.h => connection.h} (56%) create mode 100644 src/seabolt/src/connector-private.h rename src/seabolt/src/{logging.h => log-private.h} (80%) rename src/seabolt/src/{logging.c => log.c} (76%) create mode 100644 src/seabolt/src/log.h create mode 100644 src/seabolt/src/stats.c rename src/seabolt/src/{common-impl.h => stats.h} (70%) create mode 100644 src/seabolt/src/status-private.h create mode 100644 src/seabolt/src/status.c create mode 100644 src/seabolt/src/status.h rename src/{common/utils => seabolt/src}/string-builder.c (95%) rename src/{common/utils => seabolt/src}/string-builder.h (89%) create mode 100644 src/seabolt/src/values-private.h diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 3acb2572..b186a50a 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -3,11 +3,3 @@ add_library(catch2 INTERFACE) target_include_directories(catch2 INTERFACE ${CMAKE_CURRENT_LIST_DIR}/tests) - -add_library(string-builder - OBJECT - ${CMAKE_CURRENT_LIST_DIR}/utils/string-builder.c) - -target_include_directories(string-builder - PUBLIC - ${CMAKE_CURRENT_LIST_DIR}/utils) diff --git a/src/seabolt-cli/CMakeLists.txt b/src/seabolt-cli/CMakeLists.txt index 71d8d2b5..bd446250 100644 --- a/src/seabolt-cli/CMakeLists.txt +++ b/src/seabolt-cli/CMakeLists.txt @@ -2,13 +2,8 @@ add_executable(seabolt-cli "") target_sources(seabolt-cli PRIVATE - ${CMAKE_CURRENT_LIST_DIR}/src/main.c - $) + ${CMAKE_CURRENT_LIST_DIR}/src/main.c) target_link_libraries(seabolt-cli PRIVATE ${SEABOLT_SHARED}) - -target_include_directories(seabolt-cli - PRIVATE - $) diff --git a/src/seabolt-cli/src/main.c b/src/seabolt-cli/src/main.c index 566d2f34..7d194e6d 100644 --- a/src/seabolt-cli/src/main.c +++ b/src/seabolt-cli/src/main.c @@ -27,14 +27,13 @@ #include "auth.h" #include "connector.h" -#include "connections.h" +#include "connection.h" #include "lifecycle.h" #include "mem.h" #include "values.h" #include "platform.h" -#include "logging.h" +#include "log.h" #include "protocol.h" -#include "string-builder.h" #ifdef WIN32 @@ -78,7 +77,7 @@ enum Command { struct Application { struct BoltConnector* connector; struct BoltConnection* connection; - enum BoltAccessMode access_mode; + BoltAccessMode access_mode; struct { struct timespec connect_time; struct timespec init_time; @@ -117,18 +116,21 @@ void log_to_stderr(int state, const char* message) fprintf(stderr, "%s\n", message); } -struct BoltLog* create_logger() +struct BoltLog* create_logger(int enabled) { - struct BoltLog* log = BoltLog_create(); - log->state = 0; - log->debug_enabled = 0; - log->info_enabled = 0; - log->warning_enabled = 0; - log->error_enabled = 0; - log->debug_logger = log_to_stderr; - log->info_logger = log_to_stderr; - log->warning_logger = log_to_stderr; - log->error_logger = log_to_stderr; + struct BoltLog* log = BoltLog_create(0); + BoltLog_set_debug_func(log, enabled ? log_to_stderr : NULL); + BoltLog_set_warning_func(log, enabled ? log_to_stderr : NULL); + BoltLog_set_info_func(log, enabled ? log_to_stderr : NULL); + BoltLog_set_error_func(log, log_to_stderr); +// log->debug_enabled = enabled; +// log->info_enabled = enabled; +// log->warning_enabled = enabled; +// log->error_enabled = enabled; +// log->debug_logger = log_to_stderr; +// log->info_logger = log_to_stderr; +// log->warning_logger = log_to_stderr; +// log->error_logger = log_to_stderr; return log; } @@ -144,25 +146,6 @@ struct Application* app_create(int argc, char** argv) struct Application* app = BoltMem_allocate(sizeof(struct Application)); - struct BoltConfig config; - config.mode = (strcmp(BOLT_CONFIG_ROUTING, "1")==0 ? BOLT_ROUTING : BOLT_DIRECT); - config.transport = (strcmp(BOLT_CONFIG_SECURE, "1")==0) ? BOLT_SECURE_SOCKET : BOLT_SOCKET; - config.routing_context = NULL; - config.user_agent = "seabolt/1.0.0a"; - config.max_pool_size = 10; - config.log = create_logger(); - config.address_resolver = NULL; - config.trust = NULL; - config.sock_opts = NULL; - - struct BoltValue* auth_token = BoltAuth_basic(BOLT_CONFIG_USER, BOLT_CONFIG_PASSWORD, NULL); - - app->connector = BoltConnector_create(&BoltAddress_of((char*) BOLT_CONFIG_HOST, (char*) BOLT_CONFIG_PORT), - auth_token, &config); - - BoltValue_destroy(auth_token); - BoltLog_destroy(config.log); - app->access_mode = (strcmp(BOLT_CONFIG_ACCESS_MODE, "WRITE")==0 ? BOLT_ACCESS_MODE_WRITE : BOLT_ACCESS_MODE_READ); app->with_allocation_report = 0; app->with_header = 0; @@ -212,6 +195,25 @@ struct Application* app_create(int argc, char** argv) } } + BoltLog* log = create_logger(app->command==CMD_DEBUG); + BoltConfig* config = BoltConfig_create(); + BoltConfig_set_mode(config, (strcmp(BOLT_CONFIG_ROUTING, "1")==0) ? BOLT_MODE_ROUTING : BOLT_MODE_DIRECT); + BoltConfig_set_transport(config, + (strcmp(BOLT_CONFIG_SECURE, "1")==0) ? BOLT_TRANSPORT_ENCRYPTED : BOLT_TRANSPORT_PLAINTEXT); + BoltConfig_set_user_agent(config, "seabolt/" SEABOLT_VERSION); + BoltConfig_set_max_pool_size(config, 10); + BoltConfig_set_log(config, log); + + struct BoltValue* auth_token = BoltAuth_basic(BOLT_CONFIG_USER, BOLT_CONFIG_PASSWORD, NULL); + struct BoltAddress* address = BoltAddress_create((char*) BOLT_CONFIG_HOST, (char*) BOLT_CONFIG_PORT); + + app->connector = BoltConnector_create(address, auth_token, config); + + BoltValue_destroy(auth_token); + BoltAddress_destroy(address); + BoltLog_destroy(log); + BoltConfig_destroy(config); + return app; } @@ -242,11 +244,6 @@ int app_debug(struct Application* app, const char* cypher) BoltUtil_get_time(&t[1]); // Checkpoint 1 - right at the start - app->connector->config->log->debug_enabled = 1; - app->connector->config->log->info_enabled = 1; - app->connector->config->log->warning_enabled = 1; - app->connector->config->log->error_enabled = 1; - app_connect(app); BoltUtil_get_time(&t[2]); // Checkpoint 2 - after handshake and initialisation @@ -255,11 +252,11 @@ int app_debug(struct Application* app, const char* cypher) BoltConnection_load_begin_request(app->connection); BoltConnection_set_run_cypher(app->connection, cypher, strlen(cypher), 0); BoltConnection_load_run_request(app->connection); - bolt_request run = BoltConnection_last_request(app->connection); + BoltRequest run = BoltConnection_last_request(app->connection); BoltConnection_load_pull_request(app->connection, -1); - bolt_request pull = BoltConnection_last_request(app->connection); + BoltRequest pull = BoltConnection_last_request(app->connection); BoltConnection_load_commit_request(app->connection); - bolt_request commit = BoltConnection_last_request(app->connection); + BoltRequest commit = BoltConnection_last_request(app->connection); BoltConnection_send(app->connection); @@ -319,38 +316,39 @@ int app_run(struct Application* app, const char* cypher) BoltConnection_set_run_cypher(app->connection, cypher, strlen(cypher), 0); BoltConnection_load_run_request(app->connection); - bolt_request run = BoltConnection_last_request(app->connection); + BoltRequest run = BoltConnection_last_request(app->connection); BoltConnection_load_pull_request(app->connection, -1); - bolt_request pull = BoltConnection_last_request(app->connection); + BoltRequest pull = BoltConnection_last_request(app->connection); BoltConnection_send(app->connection); BoltConnection_fetch_summary(app->connection, run); + char string_buffer[4096]; if (app->with_header) { const struct BoltValue* fields = BoltConnection_field_names(app->connection); - for (int i = 0; isize; i++) { + for (int i = 0; i0) { putc('\t', stdout); } - struct StringBuilder* builder = StringBuilder_create(); - BoltValue_write(builder, BoltList_value(fields, i), app->connection->protocol->structure_name); - fprintf(stdout, "%s", StringBuilder_get_string(builder)); - StringBuilder_destroy(builder); + if (BoltValue_string(BoltList_value(fields, i), string_buffer, 4096, app->connection)>4096) { + string_buffer[4095] = 0; + } + fprintf(stdout, "%s", string_buffer); } putc('\n', stdout); } while (BoltConnection_fetch(app->connection, pull)) { const struct BoltValue* field_values = BoltConnection_field_values(app->connection); - for (int i = 0; isize; i++) { + for (int i = 0; i0) { putc('\t', stdout); } - struct StringBuilder* builder = StringBuilder_create(); - BoltValue_write(builder, value, app->connection->protocol->structure_name); - fprintf(stdout, "%s", StringBuilder_get_string(builder)); - StringBuilder_destroy(builder); + if (BoltValue_string(value, string_buffer, 4096, app->connection)>4096) { + string_buffer[4095] = 0; + } + fprintf(stdout, "%s", string_buffer); } putc('\n', stdout); } @@ -367,11 +365,11 @@ long run_fetch(const struct Application* app, const char* cypher) BoltConnection_load_begin_request(app->connection); BoltConnection_set_run_cypher(app->connection, cypher, strlen(cypher), 0); BoltConnection_load_run_request(app->connection); - bolt_request run = BoltConnection_last_request(app->connection); + BoltRequest run = BoltConnection_last_request(app->connection); BoltConnection_load_pull_request(app->connection, -1); - bolt_request pull = BoltConnection_last_request(app->connection); + BoltRequest pull = BoltConnection_last_request(app->connection); BoltConnection_load_commit_request(app->connection); - bolt_request commit = BoltConnection_last_request(app->connection); + BoltRequest commit = BoltConnection_last_request(app->connection); BoltConnection_send(app->connection); diff --git a/src/seabolt/src/CMakeLists.txt b/src/seabolt/src/CMakeLists.txt index cd5082e4..9ca2bcf9 100644 --- a/src/seabolt/src/CMakeLists.txt +++ b/src/seabolt/src/CMakeLists.txt @@ -4,18 +4,22 @@ list(APPEND private_source_files ${CMAKE_CURRENT_LIST_DIR}/address.c ${CMAKE_CURRENT_LIST_DIR}/auth.c ${CMAKE_CURRENT_LIST_DIR}/buffering.c - ${CMAKE_CURRENT_LIST_DIR}/connections.c + ${CMAKE_CURRENT_LIST_DIR}/config.c + ${CMAKE_CURRENT_LIST_DIR}/connection.c ${CMAKE_CURRENT_LIST_DIR}/connector.c ${CMAKE_CURRENT_LIST_DIR}/direct-pool.c ${CMAKE_CURRENT_LIST_DIR}/error.c ${CMAKE_CURRENT_LIST_DIR}/lifecycle.c - ${CMAKE_CURRENT_LIST_DIR}/logging.c + ${CMAKE_CURRENT_LIST_DIR}/log.c ${CMAKE_CURRENT_LIST_DIR}/mem.c ${CMAKE_CURRENT_LIST_DIR}/packstream.c ${CMAKE_CURRENT_LIST_DIR}/platform.c ${CMAKE_CURRENT_LIST_DIR}/protocol.c ${CMAKE_CURRENT_LIST_DIR}/routing-pool.c ${CMAKE_CURRENT_LIST_DIR}/routing-table.c + ${CMAKE_CURRENT_LIST_DIR}/stats.c + ${CMAKE_CURRENT_LIST_DIR}/status.c + ${CMAKE_CURRENT_LIST_DIR}/string-builder.c ${CMAKE_CURRENT_LIST_DIR}/tls.c ${CMAKE_CURRENT_LIST_DIR}/utils.c ${CMAKE_CURRENT_LIST_DIR}/v1.c @@ -29,23 +33,20 @@ list(APPEND public_header_files ${CMAKE_CURRENT_LIST_DIR}/address-set.h ${CMAKE_CURRENT_LIST_DIR}/address-resolver.h ${CMAKE_CURRENT_LIST_DIR}/auth.h + ${CMAKE_CURRENT_LIST_DIR}/bolt-public.h ${CMAKE_CURRENT_LIST_DIR}/config.h - ${CMAKE_CURRENT_LIST_DIR}/connections.h + ${CMAKE_CURRENT_LIST_DIR}/connection.h ${CMAKE_CURRENT_LIST_DIR}/connector.h ${CMAKE_CURRENT_LIST_DIR}/error.h ${CMAKE_CURRENT_LIST_DIR}/lifecycle.h - ${CMAKE_CURRENT_LIST_DIR}/logging.h - ${CMAKE_CURRENT_LIST_DIR}/mem.h + ${CMAKE_CURRENT_LIST_DIR}/log.h + ${CMAKE_CURRENT_LIST_DIR}/stats.h + ${CMAKE_CURRENT_LIST_DIR}/status.h ${CMAKE_CURRENT_LIST_DIR}/values.h) target_sources(${SEABOLT_SHARED} PRIVATE - ${private_source_files} - $) - -target_include_directories(${SEABOLT_SHARED} - PRIVATE - $) + ${private_source_files}) target_compile_definitions(${SEABOLT_SHARED} PUBLIC diff --git a/src/seabolt/src/address-private.h b/src/seabolt/src/address-private.h new file mode 100644 index 00000000..7cf47156 --- /dev/null +++ b/src/seabolt/src/address-private.h @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2002-2018 "Neo4j," + * Neo4j Sweden AB [http://neo4j.com] + * + * This file is part of Neo4j. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef SEABOLT_ADDRESS_PRIVATE_H +#define SEABOLT_ADDRESS_PRIVATE_H + +#include "address.h" + +struct BoltAddress { + /// Original host name or IP address string + const char* host; + /// Original service name or port number string + const char* port; + + /// Number of resolved IP addresses + int n_resolved_hosts; + /// Resolved IP address data + struct sockaddr_storage* resolved_hosts; + /// Resolved port number + uint16_t resolved_port; + + // Lock to protect DNS resolution process + void* lock; +}; + +#ifdef __cplusplus +#define BoltAddress_of(host, port) { (const char *)host, (const char *)port, 0, nullptr, 0, nullptr } +#else +#define BoltAddress_of(host, port) (BoltAddress) { (const char *)host, (const char *)port, 0, NULL, 0, NULL } +#endif + +BoltAddress* BoltAddress_create_from_string(const char* endpoint_str, int32_t endpoint_len); + +#endif //SEABOLT_ADDRESS_PRIVATE_H diff --git a/src/seabolt/src/address-resolver-private.h b/src/seabolt/src/address-resolver-private.h new file mode 100644 index 00000000..f779e1ec --- /dev/null +++ b/src/seabolt/src/address-resolver-private.h @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2002-2018 "Neo4j," + * Neo4j Sweden AB [http://neo4j.com] + * + * This file is part of Neo4j. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef SEABOLT_ADDRESS_RESOLVER_PRIVATE_H +#define SEABOLT_ADDRESS_RESOLVER_PRIVATE_H + +#include "address-resolver.h" + +struct BoltAddressResolver { + int state; + address_resolver_func resolver; +}; + +BoltAddressResolver* BoltAddressResolver_clone(BoltAddressResolver* resolver); + +#endif //SEABOLT_ADDRESS_RESOLVER_PRIVATE_H diff --git a/src/seabolt/src/address-resolver.c b/src/seabolt/src/address-resolver.c index b33501ae..512ba3d9 100644 --- a/src/seabolt/src/address-resolver.c +++ b/src/seabolt/src/address-resolver.c @@ -17,26 +17,34 @@ * limitations under the License. */ -#include "config-impl.h" -#include "address-resolver.h" +#include "bolt-private.h" +#include "address-resolver-private.h" #include "mem.h" -struct BoltAddressResolver* BoltAddressResolver_create() +BoltAddressResolver* BoltAddressResolver_create(int state, address_resolver_func resolver_func) { - struct BoltAddressResolver* resolver = (struct BoltAddressResolver*) BoltMem_allocate( - sizeof(struct BoltAddressResolver)); - resolver->state = 0; - resolver->resolver = NULL; + BoltAddressResolver* resolver = (struct BoltAddressResolver*) BoltMem_allocate(sizeof(BoltAddressResolver)); + resolver->state = state; + resolver->resolver = resolver_func; return resolver; } -void BoltAddressResolver_destroy(struct BoltAddressResolver* resolver) +BoltAddressResolver* BoltAddressResolver_clone(BoltAddressResolver* resolver) { - BoltMem_deallocate(resolver, sizeof(struct BoltAddressResolver)); + if (resolver==NULL) { + return NULL; + } + + BoltAddressResolver* clone = BoltAddressResolver_create(resolver->state, resolver->resolver); + return clone; +} + +void BoltAddressResolver_destroy(BoltAddressResolver* resolver) +{ + BoltMem_deallocate(resolver, sizeof(BoltAddressResolver)); } -void BoltAddressResolver_resolve(struct BoltAddressResolver* resolver, struct BoltAddress* address, - struct BoltAddressSet* resolved) +void BoltAddressResolver_resolve(BoltAddressResolver* resolver, BoltAddress* address, struct BoltAddressSet* resolved) { if (resolver!=NULL && resolver->resolver!=NULL) { resolver->resolver(resolver->state, address, resolved); diff --git a/src/seabolt/src/address-resolver.h b/src/seabolt/src/address-resolver.h index 95b26f1e..7ce7151b 100644 --- a/src/seabolt/src/address-resolver.h +++ b/src/seabolt/src/address-resolver.h @@ -19,22 +19,19 @@ #ifndef SEABOLT_ALL_SERVER_ADDRESS_RESOLVER_H #define SEABOLT_ALL_SERVER_ADDRESS_RESOLVER_H -#include "config.h" +#include "bolt-public.h" #include "address.h" #include "address-set.h" typedef void (* address_resolver_func)(int state, struct BoltAddress*, struct BoltAddressSet*); -struct BoltAddressResolver { - int state; - address_resolver_func resolver; -}; +typedef struct BoltAddressResolver BoltAddressResolver; -SEABOLT_EXPORT struct BoltAddressResolver* BoltAddressResolver_create(); +SEABOLT_EXPORT BoltAddressResolver* BoltAddressResolver_create(int state, address_resolver_func resolver_func); -SEABOLT_EXPORT void BoltAddressResolver_destroy(struct BoltAddressResolver* resolver); +SEABOLT_EXPORT void BoltAddressResolver_destroy(BoltAddressResolver* resolver); -void BoltAddressResolver_resolve(struct BoltAddressResolver* resolver, struct BoltAddress* address, - struct BoltAddressSet* resolved); +void BoltAddressResolver_resolve(BoltAddressResolver* resolver, BoltAddress* address, + BoltAddressSet* resolved); #endif //SEABOLT_ALL_SERVER_ADDRESS_RESOLVER_H diff --git a/src/seabolt/src/address-set-private.h b/src/seabolt/src/address-set-private.h new file mode 100644 index 00000000..42a0508e --- /dev/null +++ b/src/seabolt/src/address-set-private.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2002-2018 "Neo4j," + * Neo4j Sweden AB [http://neo4j.com] + * + * This file is part of Neo4j. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef SEABOLT_ADDRESS_SET_PRIVATE_H +#define SEABOLT_ADDRESS_SET_PRIVATE_H + +#include "address-set.h" + +struct BoltAddressSet { + int size; + struct BoltAddress** elements; +}; + +#define SIZE_OF_ADDRESS_SET sizeof(struct BoltAddressSet) +#define SIZE_OF_ADDRESS_SET_PTR sizeof(struct BoltAddressSet*) + +#endif //SEABOLT_ADDRESS_SET_PRIVATE_H diff --git a/src/seabolt/src/address-set.c b/src/seabolt/src/address-set.c index 8852815a..94a262b5 100644 --- a/src/seabolt/src/address-set.c +++ b/src/seabolt/src/address-set.c @@ -17,12 +17,12 @@ * limitations under the License. */ -#include - -#include "address-set.h" +#include "bolt-private.h" +#include "address-private.h" +#include "address-set-private.h" #include "mem.h" -struct BoltAddressSet* BoltAddressSet_create() +BoltAddressSet* BoltAddressSet_create() { struct BoltAddressSet* set = (struct BoltAddressSet*) BoltMem_allocate(SIZE_OF_ADDRESS_SET); set->size = 0; @@ -30,26 +30,26 @@ struct BoltAddressSet* BoltAddressSet_create() return set; } -void BoltAddressSet_destroy(struct BoltAddressSet* set) +void BoltAddressSet_destroy(BoltAddressSet* set) { for (int i = 0; isize; i++) { BoltAddress_destroy(set->elements[i]); } - BoltMem_deallocate(set->elements, set->size*SIZE_OF_ADDRESS_PTR); + BoltMem_deallocate(set->elements, set->size*sizeof(BoltAddress*)); BoltMem_deallocate(set, SIZE_OF_ADDRESS_SET); } -int BoltAddressSet_size(struct BoltAddressSet* set) +int BoltAddressSet_size(BoltAddressSet* set) { return set->size; } -int BoltAddressSet_index_of(struct BoltAddressSet* set, struct BoltAddress address) +int BoltAddressSet_index_of(BoltAddressSet* set, const BoltAddress* address) { for (int i = 0; isize; i++) { struct BoltAddress* current = set->elements[i]; - if (strcmp(address.host, current->host)==0 && strcmp(address.port, current->port)==0) { + if (strcmp(address->host, current->host)==0 && strcmp(address->port, current->port)==0) { return i; } } @@ -57,12 +57,12 @@ int BoltAddressSet_index_of(struct BoltAddressSet* set, struct BoltAddress addre return -1; } -int BoltAddressSet_add(struct BoltAddressSet* set, struct BoltAddress address) +int BoltAddressSet_add(BoltAddressSet* set, const BoltAddress* address) { if (BoltAddressSet_index_of(set, address)==-1) { - set->elements = (struct BoltAddress**) BoltMem_reallocate(set->elements, set->size*SIZE_OF_ADDRESS_PTR, - (set->size+1)*SIZE_OF_ADDRESS_PTR); - set->elements[set->size] = BoltAddress_create(address.host, address.port); + set->elements = (struct BoltAddress**) BoltMem_reallocate(set->elements, set->size*sizeof(BoltAddress*), + (set->size+1)*sizeof(BoltAddress*)); + set->elements[set->size] = BoltAddress_create(address->host, address->port); set->size++; return set->size-1; } @@ -70,12 +70,12 @@ int BoltAddressSet_add(struct BoltAddressSet* set, struct BoltAddress address) return -1; } -int BoltAddressSet_remove(struct BoltAddressSet* set, struct BoltAddress address) +int BoltAddressSet_remove(BoltAddressSet* set, const BoltAddress* address) { int index = BoltAddressSet_index_of(set, address); if (index>=0) { struct BoltAddress** old_elements = set->elements; - struct BoltAddress** new_elements = (struct BoltAddress**) BoltMem_allocate((set->size-1)*SIZE_OF_ADDRESS_PTR); + struct BoltAddress** new_elements = (struct BoltAddress**) BoltMem_allocate((set->size-1)*sizeof(BoltAddress*)); int new_elements_index = 0; for (int i = 0; isize; i++) { if (i==index) { @@ -86,7 +86,7 @@ int BoltAddressSet_remove(struct BoltAddressSet* set, struct BoltAddress address } BoltAddress_destroy(set->elements[index]); - BoltMem_deallocate(old_elements, set->size*SIZE_OF_ADDRESS_PTR); + BoltMem_deallocate(old_elements, set->size*sizeof(BoltAddress*)); set->elements = new_elements; set->size--; return index; @@ -94,23 +94,23 @@ int BoltAddressSet_remove(struct BoltAddressSet* set, struct BoltAddress address return -1; } -void BoltAddressSet_replace(struct BoltAddressSet* dest, struct BoltAddressSet* source) +void BoltAddressSet_replace(BoltAddressSet* dest, BoltAddressSet* source) { for (int i = 0; isize; i++) { BoltAddress_destroy(dest->elements[i]); } - dest->elements = (struct BoltAddress**) BoltMem_reallocate(dest->elements, dest->size*SIZE_OF_ADDRESS_PTR, - source->size*SIZE_OF_ADDRESS_PTR); + dest->elements = (struct BoltAddress**) BoltMem_reallocate(dest->elements, dest->size*sizeof(BoltAddress*), + source->size*sizeof(BoltAddress*)); for (int i = 0; isize; i++) { dest->elements[i] = BoltAddress_create(source->elements[i]->host, source->elements[i]->port); } dest->size = source->size; } -void BoltAddressSet_add_all(struct BoltAddressSet* destination, struct BoltAddressSet* source) +void BoltAddressSet_add_all(BoltAddressSet* destination, BoltAddressSet* source) { for (int i = 0; isize; i++) { - BoltAddressSet_add(destination, *source->elements[i]); + BoltAddressSet_add(destination, source->elements[i]); } } diff --git a/src/seabolt/src/address-set.h b/src/seabolt/src/address-set.h index af79ecde..b3d21289 100644 --- a/src/seabolt/src/address-set.h +++ b/src/seabolt/src/address-set.h @@ -19,30 +19,25 @@ #ifndef SEABOLT_ALL_ADDRESS_SET_H #define SEABOLT_ALL_ADDRESS_SET_H +#include "bolt-public.h" #include "address.h" -struct BoltAddressSet { - int size; - struct BoltAddress** elements; -}; +typedef struct BoltAddressSet BoltAddressSet; -#define SIZE_OF_ADDRESS_SET sizeof(struct BoltAddressSet) -#define SIZE_OF_ADDRESS_SET_PTR sizeof(struct BoltAddressSet*) +SEABOLT_EXPORT BoltAddressSet* BoltAddressSet_create(); -SEABOLT_EXPORT struct BoltAddressSet* BoltAddressSet_create(); +SEABOLT_EXPORT void BoltAddressSet_destroy(BoltAddressSet* set); -SEABOLT_EXPORT void BoltAddressSet_destroy(struct BoltAddressSet* set); +SEABOLT_EXPORT int BoltAddressSet_size(BoltAddressSet* set); -SEABOLT_EXPORT int BoltAddressSet_size(struct BoltAddressSet* set); +SEABOLT_EXPORT int BoltAddressSet_index_of(BoltAddressSet* set, const BoltAddress* address); -SEABOLT_EXPORT int BoltAddressSet_index_of(struct BoltAddressSet* set, struct BoltAddress address); +SEABOLT_EXPORT int BoltAddressSet_add(BoltAddressSet* set, const BoltAddress* address); -SEABOLT_EXPORT int BoltAddressSet_add(struct BoltAddressSet* set, struct BoltAddress address); +SEABOLT_EXPORT int BoltAddressSet_remove(BoltAddressSet* set, const BoltAddress* address); -SEABOLT_EXPORT int BoltAddressSet_remove(struct BoltAddressSet* set, struct BoltAddress address); +SEABOLT_EXPORT void BoltAddressSet_replace(BoltAddressSet* destination, BoltAddressSet* source); -SEABOLT_EXPORT void BoltAddressSet_replace(struct BoltAddressSet* destination, struct BoltAddressSet* source); - -SEABOLT_EXPORT void BoltAddressSet_add_all(struct BoltAddressSet* destination, struct BoltAddressSet* source); +SEABOLT_EXPORT void BoltAddressSet_add_all(BoltAddressSet* destination, BoltAddressSet* source); #endif //SEABOLT_ALL_ADDRESS_SET_H diff --git a/src/seabolt/src/address.c b/src/seabolt/src/address.c index 24c5affe..13e3deff 100644 --- a/src/seabolt/src/address.c +++ b/src/seabolt/src/address.c @@ -17,10 +17,11 @@ * limitations under the License. */ +#include -#include "config-impl.h" -#include "address.h" -#include "logging.h" +#include "bolt-private.h" +#include "address-private.h" +#include "log-private.h" #include "mem.h" #include "platform.h" @@ -29,9 +30,9 @@ #define SOCKADDR_STORAGE_SIZE sizeof(struct sockaddr_storage) -struct BoltAddress* BoltAddress_create(const char* host, const char* port) +BoltAddress* BoltAddress_create(const char* host, const char* port) { - struct BoltAddress* address = (struct BoltAddress*) BoltMem_allocate(sizeof(struct BoltAddress)); + BoltAddress* address = (BoltAddress*) BoltMem_allocate(sizeof(BoltAddress)); if (host==NULL || strlen(host)==0) { host = DEFAULT_BOLT_HOST; @@ -49,7 +50,17 @@ struct BoltAddress* BoltAddress_create(const char* host, const char* port) return address; } -struct BoltAddress* BoltAddress_create_from_string(const char* endpoint_str, int32_t endpoint_len) +const char* BoltAddress_host(BoltAddress* address) +{ + return address->host; +} + +const char* BoltAddress_port(BoltAddress* address) +{ + return address->port; +} + +BoltAddress* BoltAddress_create_from_string(const char* endpoint_str, int32_t endpoint_len) { if (endpoint_len<0) { endpoint_len = (int32_t) strlen(endpoint_str); @@ -69,12 +80,12 @@ struct BoltAddress* BoltAddress_create_from_string(const char* endpoint_str, int *port_str++ = '\0'; } - struct BoltAddress* result = BoltAddress_create(address_str, port_str); + BoltAddress* result = BoltAddress_create(address_str, port_str); BoltMem_deallocate(address_str, endpoint_len+1); return result; } -int BoltAddress_resolve(struct BoltAddress* address, struct BoltLog* log) +int BoltAddress_resolve(BoltAddress* address, int* n_resolved, BoltLog* log) { BoltUtil_mutex_lock(&address->lock); @@ -92,12 +103,12 @@ int BoltAddress_resolve(struct BoltAddress* address, struct BoltLog* log) struct addrinfo* ai; const int gai_status = getaddrinfo(address->host, address->port, &hints, &ai); if (gai_status==0) { - unsigned short n_resolved = 0; + unsigned short count = 0; for (struct addrinfo* ai_node = ai; ai_node!=NULL; ai_node = ai_node->ai_next) { switch (ai_node->ai_family) { case AF_INET: case AF_INET6: { - n_resolved += 1; + count += 1; break; } default: @@ -106,14 +117,14 @@ int BoltAddress_resolve(struct BoltAddress* address, struct BoltLog* log) } } if (address->resolved_hosts==NULL) { - address->resolved_hosts = (struct sockaddr_storage*) BoltMem_allocate(n_resolved*SOCKADDR_STORAGE_SIZE); + address->resolved_hosts = (struct sockaddr_storage*) BoltMem_allocate(count*SOCKADDR_STORAGE_SIZE); } else { address->resolved_hosts = (struct sockaddr_storage*) BoltMem_reallocate(address->resolved_hosts, address->n_resolved_hosts*SOCKADDR_STORAGE_SIZE, - n_resolved*SOCKADDR_STORAGE_SIZE); + count*SOCKADDR_STORAGE_SIZE); } - address->n_resolved_hosts = n_resolved; + address->n_resolved_hosts = count; size_t p = 0; for (struct addrinfo* ai_node = ai; ai_node!=NULL; ai_node = ai_node->ai_next) { switch (ai_node->ai_family) { @@ -149,19 +160,23 @@ int BoltAddress_resolve(struct BoltAddress* address, struct BoltLog* log) address->resolved_port = ntohs(resolved_port); } + if (gai_status==0 && n_resolved!=NULL) { + *n_resolved = address->n_resolved_hosts; + } + BoltUtil_mutex_unlock(&address->lock); return gai_status; } -int BoltAddress_copy_resolved_host(struct BoltAddress* address, size_t index, char* buffer, +int BoltAddress_copy_resolved_host(BoltAddress* address, size_t index, char* buffer, int32_t buffer_size) { struct sockaddr_storage* resolved_host_storage = &address->resolved_hosts[index]; const struct sockaddr* resolved_host = (const struct sockaddr*) resolved_host_storage; const socklen_t resolved_host_size = resolved_host_storage->ss_family==AF_INET ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6); - const int status = getnameinfo(resolved_host, resolved_host_size, buffer, (socklen_t)buffer_size, + const int status = getnameinfo(resolved_host, resolved_host_size, buffer, (socklen_t) buffer_size, NULL, 0, NI_NUMERICHOST); switch (status) { case 0: @@ -171,7 +186,7 @@ int BoltAddress_copy_resolved_host(struct BoltAddress* address, size_t index, ch } } -void BoltAddress_destroy(struct BoltAddress* address) +void BoltAddress_destroy(BoltAddress* address) { if (address->resolved_hosts!=NULL) { address->resolved_hosts = (struct sockaddr_storage*) BoltMem_deallocate(address->resolved_hosts, @@ -182,5 +197,5 @@ void BoltAddress_destroy(struct BoltAddress* address) BoltUtil_mutex_destroy(&address->lock); BoltMem_deallocate((char*) address->host, strlen(address->host)+1); BoltMem_deallocate((char*) address->port, strlen(address->port)+1); - BoltMem_deallocate(address, sizeof(struct BoltAddress)); + BoltMem_deallocate(address, sizeof(BoltAddress)); } diff --git a/src/seabolt/src/address.h b/src/seabolt/src/address.h index 4525c7b6..9f9a8589 100644 --- a/src/seabolt/src/address.h +++ b/src/seabolt/src/address.h @@ -20,39 +20,14 @@ #ifndef SEABOLT_ADDRESSING_H #define SEABOLT_ADDRESSING_H -#include "config.h" +#include "bolt-public.h" /** * The address of a Bolt server. This can carry both the original host * and port details, as supplied by the application, as well as one or * more resolved IP addresses and port number. */ -struct BoltAddress { - /// Original host name or IP address string - const char* host; - /// Original service name or port number string - const char* port; - - /// Number of resolved IP addresses - int n_resolved_hosts; - /// Resolved IP address data - struct sockaddr_storage* resolved_hosts; - /// Resolved port number - uint16_t resolved_port; - - // Lock to protect DNS resolution process - void* lock; -}; - -#define SIZE_OF_ADDRESS sizeof(struct BoltAddress) -#define SIZE_OF_ADDRESS_PTR sizeof(struct BoltAddress *) - -#ifdef __cplusplus -#define BoltAddress_of(host, port) { (const char *)host, (const char *)port, 0, nullptr, 0, nullptr } -#else -#define BoltAddress_of(host, port) (struct BoltAddress) { (const char *)host, (const char *)port, 0, NULL, 0, NULL } -#endif - +typedef struct BoltAddress BoltAddress; /** * Create a new address structure for a given host and port. No name @@ -62,21 +37,23 @@ struct BoltAddress { * * @param host host name for this address, e.g. "www.example.com" * @param port port name or numeric string, e.g. "7687" or "http" - * @return pointer to a new BoltAddress structure + * @return pointer to a new BoltAddress */ SEABOLT_EXPORT struct BoltAddress* BoltAddress_create(const char* host, const char* port); -SEABOLT_EXPORT struct BoltAddress* BoltAddress_create_from_string(const char* endpoint_str, int32_t endpoint_len); +SEABOLT_EXPORT const char* BoltAddress_host(BoltAddress* address); +SEABOLT_EXPORT const char* BoltAddress_port(BoltAddress* address); /** * Resolve the original host and port into one or more IP addresses and * a port number. This can be carried out more than once on the same - * address any newly-resolved addresses will replace any previously stored. + * address. Any newly-resolved addresses will replace any previously stored. * - * @param address pointer to a BoltAddress structure + * @param address pointer to a BoltAddress + * @param n_resolved pointer to an int value where number of resolved addresses will be saved * @return status of the internal getaddrinfo call */ -SEABOLT_EXPORT int BoltAddress_resolve(struct BoltAddress* address, struct BoltLog* log); +SEABOLT_EXPORT int BoltAddress_resolve(BoltAddress* address, int* n_resolved, struct BoltLog* log); /** * Copy the textual representation of a resolved host IP address into a buffer. @@ -92,13 +69,13 @@ SEABOLT_EXPORT int BoltAddress_resolve(struct BoltAddress* address, struct BoltL * @return address family (AF_INET or AF_INET6) or -1 on error */ SEABOLT_EXPORT int -BoltAddress_copy_resolved_host(struct BoltAddress* address, size_t index, char* buffer, int32_t buffer_size); +BoltAddress_copy_resolved_host(BoltAddress* address, size_t index, char* buffer, int32_t buffer_size); /** * Destroy an address structure and deallocate any associated memory. * * @param address pointer to a BoltAddress structure */ -SEABOLT_EXPORT void BoltAddress_destroy(struct BoltAddress* address); +SEABOLT_EXPORT void BoltAddress_destroy(BoltAddress* address); #endif // SEABOLT_ADDRESSING_H diff --git a/src/seabolt/src/auth.c b/src/seabolt/src/auth.c index df6bf257..951760e7 100644 --- a/src/seabolt/src/auth.c +++ b/src/seabolt/src/auth.c @@ -22,7 +22,7 @@ #include "auth.h" -struct BoltValue* BoltAuth_basic(const char* username, const char* password, const char* realm) +BoltValue* BoltAuth_basic(const char* username, const char* password, const char* realm) { struct BoltValue* auth_token = BoltValue_create(); BoltValue_format_as_Dictionary(auth_token, realm==NULL ? 3 : 4); diff --git a/src/seabolt/src/auth.h b/src/seabolt/src/auth.h index db4a6c62..d03fd601 100644 --- a/src/seabolt/src/auth.h +++ b/src/seabolt/src/auth.h @@ -21,9 +21,8 @@ #ifndef SEABOLT_AUTH #define SEABOLT_AUTH -#include "config.h" #include "values.h" -SEABOLT_EXPORT struct BoltValue* BoltAuth_basic(const char* username, const char* password, const char* realm); +SEABOLT_EXPORT BoltValue* BoltAuth_basic(const char* username, const char* password, const char* realm); #endif // SEABOLT_AUTH diff --git a/src/seabolt/src/bolt-private.h b/src/seabolt/src/bolt-private.h new file mode 100644 index 00000000..8eee242e --- /dev/null +++ b/src/seabolt/src/bolt-private.h @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2002-2018 "Neo4j," + * Neo4j Sweden AB [http://neo4j.com] + * + * This file is part of Neo4j. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef SEABOLT_BOLT_PRIVATE_H +#define SEABOLT_BOLT_PRIVATE_H + +#include +#include +#include +#include + +#if USE_POSIXSOCK + +#include +#include +#include +#include +#include +#include + +#endif // USE_POSIXSOCK + +#if USE_WINSOCK + +#include +#include +#include + +#endif // USE_WINSOCK + +#if USE_WINSSPI + +#endif // USE_WINSSPI + +#if USE_OPENSSL + +#include +#include +#include +#include + +#endif // USE_OPENSSL + +#if USE_OPENSSL + +extern int SSL_CTX_TRUST_INDEX; +extern int SSL_CTX_LOG_INDEX; +extern int SSL_CTX_ID_INDEX; + +#endif // USE_OPENSSL + +#include "error.h" + +#define SIZE_OF_C_STRING(str) (sizeof(char)*(strlen(str)+1)) +#define UNUSED(x) (void)(x) + +#endif //SEABOLT_BOLT_PRIVATE_H diff --git a/src/seabolt/src/bolt-public.h b/src/seabolt/src/bolt-public.h new file mode 100644 index 00000000..8d2e1158 --- /dev/null +++ b/src/seabolt/src/bolt-public.h @@ -0,0 +1,34 @@ +/* +* Copyright (c) 2002-2018 "Neo4j," +* Neo4j Sweden AB [http://neo4j.com] +* +* This file is part of Neo4j. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +#ifndef SEABOLT_BOLT_PUBLIC +#define SEABOLT_BOLT_PUBLIC + +#include +#include +#include +#include +#include + +#include "bolt-version.h" +#include "bolt-exports.h" + +typedef struct BoltLog BoltLog; +typedef struct BoltConnection BoltConnection; + +#endif // SEABOLT_BOLT_PUBLIC diff --git a/src/seabolt/src/bolt.h b/src/seabolt/src/bolt.h index ca4d7bdd..4052ca91 100644 --- a/src/seabolt/src/bolt.h +++ b/src/seabolt/src/bolt.h @@ -24,17 +24,19 @@ extern "C" { #endif -#include "config.h" +#include "bolt-public.h" #include "address.h" #include "address-resolver.h" #include "auth.h" +#include "config.h" #include "connector.h" -#include "connections.h" +#include "connection.h" #include "error.h" #include "lifecycle.h" -#include "logging.h" -#include "mem.h" +#include "log.h" +#include "stats.h" +#include "status.h" #include "values.h" #ifdef __cplusplus diff --git a/src/seabolt/src/buffering.c b/src/seabolt/src/buffering.c index 8498790b..c8f18745 100644 --- a/src/seabolt/src/buffering.c +++ b/src/seabolt/src/buffering.c @@ -24,9 +24,9 @@ #include "buffering.h" #include "mem.h" -struct BoltBuffer* BoltBuffer_create(int size) +BoltBuffer* BoltBuffer_create(int size) { - struct BoltBuffer* buffer = BoltMem_allocate(sizeof(struct BoltBuffer)); + BoltBuffer* buffer = BoltMem_allocate(sizeof(BoltBuffer)); buffer->size = size; buffer->data = BoltMem_allocate((size_t) (buffer->size)); buffer->extent = 0; @@ -34,13 +34,13 @@ struct BoltBuffer* BoltBuffer_create(int size) return buffer; } -void BoltBuffer_destroy(struct BoltBuffer* buffer) +void BoltBuffer_destroy(BoltBuffer* buffer) { buffer->data = BoltMem_deallocate(buffer->data, (size_t) (buffer->size)); - BoltMem_deallocate(buffer, sizeof(struct BoltBuffer)); + BoltMem_deallocate(buffer, sizeof(BoltBuffer)); } -void BoltBuffer_compact(struct BoltBuffer* buffer) +void BoltBuffer_compact(BoltBuffer* buffer) { if (buffer->cursor>0) { int available = buffer->extent-buffer->cursor; @@ -52,13 +52,13 @@ void BoltBuffer_compact(struct BoltBuffer* buffer) } } -int BoltBuffer_loadable(struct BoltBuffer* buffer) +int BoltBuffer_loadable(BoltBuffer* buffer) { int available = buffer->size-buffer->extent; return available>INT_MAX ? INT_MAX : available; } -char* BoltBuffer_load_pointer(struct BoltBuffer* buffer, int size) +char* BoltBuffer_load_pointer(BoltBuffer* buffer, int size) { int available = BoltBuffer_loadable(buffer); if (size>available) { @@ -71,60 +71,60 @@ char* BoltBuffer_load_pointer(struct BoltBuffer* buffer, int size) return &buffer->data[extent]; } -void BoltBuffer_load(struct BoltBuffer* buffer, const char* data, int size) +void BoltBuffer_load(BoltBuffer* buffer, const char* data, int size) { char* target = BoltBuffer_load_pointer(buffer, size); memcpy(target, data, size>=0 ? (size_t) (size) : 0); } -void BoltBuffer_load_i8(struct BoltBuffer* buffer, int8_t x) +void BoltBuffer_load_i8(BoltBuffer* buffer, int8_t x) { char* target = BoltBuffer_load_pointer(buffer, sizeof(x)); target[0] = (char) (x); } -void BoltBuffer_load_u8(struct BoltBuffer* buffer, uint8_t x) +void BoltBuffer_load_u8(BoltBuffer* buffer, uint8_t x) { char* target = BoltBuffer_load_pointer(buffer, sizeof(x)); target[0] = (char) (x); } -void BoltBuffer_load_u16be(struct BoltBuffer* buffer, uint16_t x) +void BoltBuffer_load_u16be(BoltBuffer* buffer, uint16_t x) { char* target = BoltBuffer_load_pointer(buffer, sizeof(x)); memcpy_be(&target[0], &x, sizeof(x)); } -void BoltBuffer_load_i16be(struct BoltBuffer* buffer, int16_t x) +void BoltBuffer_load_i16be(BoltBuffer* buffer, int16_t x) { char* target = BoltBuffer_load_pointer(buffer, sizeof(x)); memcpy_be(&target[0], &x, sizeof(x)); } -void BoltBuffer_load_i32be(struct BoltBuffer* buffer, int32_t x) +void BoltBuffer_load_i32be(BoltBuffer* buffer, int32_t x) { char* target = BoltBuffer_load_pointer(buffer, sizeof(x)); memcpy_be(&target[0], &x, sizeof(x)); } -void BoltBuffer_load_i64be(struct BoltBuffer* buffer, int64_t x) +void BoltBuffer_load_i64be(BoltBuffer* buffer, int64_t x) { char* target = BoltBuffer_load_pointer(buffer, sizeof(x)); memcpy_be(&target[0], &x, sizeof(x)); } -void BoltBuffer_load_f64be(struct BoltBuffer* buffer, double x) +void BoltBuffer_load_f64be(BoltBuffer* buffer, double x) { char* target = BoltBuffer_load_pointer(buffer, (int) ((sizeof(x)))); memcpy_be(&target[0], &x, sizeof(x)); } -int BoltBuffer_unloadable(struct BoltBuffer* buffer) +int BoltBuffer_unloadable(BoltBuffer* buffer) { return buffer->extent-buffer->cursor; } -char* BoltBuffer_unload_pointer(struct BoltBuffer* buffer, int size) +char* BoltBuffer_unload_pointer(BoltBuffer* buffer, int size) { int available = BoltBuffer_unloadable(buffer); if (size>available) return NULL; @@ -137,7 +137,7 @@ char* BoltBuffer_unload_pointer(struct BoltBuffer* buffer, int size) return &buffer->data[cursor]; } -int BoltBuffer_unload(struct BoltBuffer* buffer, char* data, int size) +int BoltBuffer_unload(BoltBuffer* buffer, char* data, int size) { int available = BoltBuffer_unloadable(buffer); if (size>available) return -1; @@ -149,21 +149,21 @@ int BoltBuffer_unload(struct BoltBuffer* buffer, char* data, int size) return size; } -int BoltBuffer_peek_u8(struct BoltBuffer* buffer, uint8_t* x) +int BoltBuffer_peek_u8(BoltBuffer* buffer, uint8_t* x) { if (BoltBuffer_unloadable(buffer)<1) return -1; *x = (uint8_t) (buffer->data[buffer->cursor]); return 0; } -int BoltBuffer_unload_u8(struct BoltBuffer* buffer, uint8_t* x) +int BoltBuffer_unload_u8(BoltBuffer* buffer, uint8_t* x) { if (BoltBuffer_unloadable(buffer)<1) return -1; *x = (uint8_t) (buffer->data[buffer->cursor++]); return 0; } -int BoltBuffer_unload_u16be(struct BoltBuffer* buffer, uint16_t* x) +int BoltBuffer_unload_u16be(BoltBuffer* buffer, uint16_t* x) { int size_of_x = (int) sizeof(*x); if (BoltBuffer_unloadable(buffer) -#include - +#include "bolt-public.h" /** * General purpose data buffer. */ -struct BoltBuffer -{ +typedef struct BoltBuffer { int size; int extent; int cursor; - char * data; -}; - + char* data; +} BoltBuffer; /** * Create a buffer. @@ -49,21 +42,21 @@ struct BoltBuffer * @param size * @return */ -struct BoltBuffer * BoltBuffer_create(int size); +BoltBuffer* BoltBuffer_create(int size); /** * Destroy a buffer. * * @param buffer */ -void BoltBuffer_destroy(struct BoltBuffer * buffer); +void BoltBuffer_destroy(BoltBuffer* buffer); /** * Compact a buffer by removing unused space. * * @param buffer */ -void BoltBuffer_compact(struct BoltBuffer * buffer); +void BoltBuffer_compact(BoltBuffer* buffer); /** * Return the amount of loadable space in a buffer, in bytes. @@ -71,7 +64,7 @@ void BoltBuffer_compact(struct BoltBuffer * buffer); * @param buffer * @return */ -int BoltBuffer_loadable(struct BoltBuffer * buffer); +int BoltBuffer_loadable(BoltBuffer* buffer); /** * Allocate space in a buffer for loading data and return a pointer to that space. @@ -80,7 +73,7 @@ int BoltBuffer_loadable(struct BoltBuffer * buffer); * @param size * @return */ -char * BoltBuffer_load_pointer(struct BoltBuffer * buffer, int size); +char* BoltBuffer_load_pointer(BoltBuffer* buffer, int size); /** * Load data into a buffer. @@ -89,7 +82,7 @@ char * BoltBuffer_load_pointer(struct BoltBuffer * buffer, int size); * @param data * @param size */ -void BoltBuffer_load(struct BoltBuffer * buffer, const char * data, int size); +void BoltBuffer_load(BoltBuffer* buffer, const char* data, int size); /** * Load an unsigned 8-bit integer into a buffer. @@ -97,7 +90,7 @@ void BoltBuffer_load(struct BoltBuffer * buffer, const char * data, int size); * @param buffer * @param x */ -void BoltBuffer_load_u8(struct BoltBuffer * buffer, uint8_t x); +void BoltBuffer_load_u8(BoltBuffer* buffer, uint8_t x); /** * Load an unsigned 16-bit integer (big-endian) into a buffer. @@ -105,7 +98,7 @@ void BoltBuffer_load_u8(struct BoltBuffer * buffer, uint8_t x); * @param buffer * @param x */ -void BoltBuffer_load_u16be(struct BoltBuffer * buffer, uint16_t x); +void BoltBuffer_load_u16be(BoltBuffer* buffer, uint16_t x); /** * Load a signed 8-bit integer into a buffer. @@ -113,7 +106,7 @@ void BoltBuffer_load_u16be(struct BoltBuffer * buffer, uint16_t x); * @param buffer * @param x */ -void BoltBuffer_load_i8(struct BoltBuffer * buffer, int8_t x); +void BoltBuffer_load_i8(BoltBuffer* buffer, int8_t x); /** * Load a signed 16-bit integer (big-endian) into a buffer. @@ -121,7 +114,7 @@ void BoltBuffer_load_i8(struct BoltBuffer * buffer, int8_t x); * @param buffer * @param x */ -void BoltBuffer_load_i16be(struct BoltBuffer * buffer, int16_t x); +void BoltBuffer_load_i16be(BoltBuffer* buffer, int16_t x); /** * Load a signed 32-bit integer (big-endian) into a buffer. @@ -129,7 +122,7 @@ void BoltBuffer_load_i16be(struct BoltBuffer * buffer, int16_t x); * @param buffer * @param x */ -void BoltBuffer_load_i32be(struct BoltBuffer * buffer, int32_t x); +void BoltBuffer_load_i32be(BoltBuffer* buffer, int32_t x); /** * Load a signed 64-bit integer (big-endian) into a buffer. @@ -137,7 +130,7 @@ void BoltBuffer_load_i32be(struct BoltBuffer * buffer, int32_t x); * @param buffer * @param x */ -void BoltBuffer_load_i64be(struct BoltBuffer * buffer, int64_t x); +void BoltBuffer_load_i64be(BoltBuffer* buffer, int64_t x); /** * Load a double precision floating point number (big-endian) into a buffer. @@ -145,7 +138,7 @@ void BoltBuffer_load_i64be(struct BoltBuffer * buffer, int64_t x); * @param buffer * @param x */ -void BoltBuffer_load_f64be(struct BoltBuffer * buffer, double x); +void BoltBuffer_load_f64be(BoltBuffer* buffer, double x); /** * Return the amount of unloadable data in a buffer, in bytes. @@ -153,7 +146,7 @@ void BoltBuffer_load_f64be(struct BoltBuffer * buffer, double x); * @param buffer * @return */ -int BoltBuffer_unloadable(struct BoltBuffer * buffer); +int BoltBuffer_unloadable(BoltBuffer* buffer); /** * Mark data in a buffer for unloading and return a pointer to that data. @@ -162,7 +155,7 @@ int BoltBuffer_unloadable(struct BoltBuffer * buffer); * @param size * @return */ -char * BoltBuffer_unload_pointer(struct BoltBuffer * buffer, int size); +char* BoltBuffer_unload_pointer(BoltBuffer* buffer, int size); /** * Unload data from a buffer. @@ -172,7 +165,7 @@ char * BoltBuffer_unload_pointer(struct BoltBuffer * buffer, int size); * @param size * @return */ -int BoltBuffer_unload(struct BoltBuffer * buffer, char * data, int size); +int BoltBuffer_unload(BoltBuffer* buffer, char* data, int size); /** * Return the next unloadable byte in a buffer as an unsigned 8-bit integer. @@ -181,7 +174,7 @@ int BoltBuffer_unload(struct BoltBuffer * buffer, char * data, int size); * @param x * @return */ -int BoltBuffer_peek_u8(struct BoltBuffer * buffer, uint8_t * x); +int BoltBuffer_peek_u8(BoltBuffer* buffer, uint8_t* x); /** * Unload an unsigned 8-bit integer from a buffer. @@ -190,8 +183,7 @@ int BoltBuffer_peek_u8(struct BoltBuffer * buffer, uint8_t * x); * @param x * @return */ -int BoltBuffer_unload_u8(struct BoltBuffer * buffer, uint8_t * x); - +int BoltBuffer_unload_u8(BoltBuffer* buffer, uint8_t* x); /** * Unload an unsigned 16-bit integer (big endian) from a buffer. @@ -200,7 +192,7 @@ int BoltBuffer_unload_u8(struct BoltBuffer * buffer, uint8_t * x); * @param x * @return */ -int BoltBuffer_unload_u16be(struct BoltBuffer * buffer, uint16_t * x); +int BoltBuffer_unload_u16be(BoltBuffer* buffer, uint16_t* x); /** * Unload a signed 8-bit integer from a buffer. @@ -209,7 +201,7 @@ int BoltBuffer_unload_u16be(struct BoltBuffer * buffer, uint16_t * x); * @param x * @return */ -int BoltBuffer_unload_i8(struct BoltBuffer * buffer, int8_t * x); +int BoltBuffer_unload_i8(BoltBuffer* buffer, int8_t* x); /** * Unload a signed 16-bit integer (big endian) from a buffer. @@ -218,7 +210,7 @@ int BoltBuffer_unload_i8(struct BoltBuffer * buffer, int8_t * x); * @param x * @return */ -int BoltBuffer_unload_i16be(struct BoltBuffer * buffer, int16_t * x); +int BoltBuffer_unload_i16be(BoltBuffer* buffer, int16_t* x); /** * Unload a signed 32-bit integer (big endian) from a buffer. @@ -227,7 +219,7 @@ int BoltBuffer_unload_i16be(struct BoltBuffer * buffer, int16_t * x); * @param x * @return */ -int BoltBuffer_unload_i32be(struct BoltBuffer * buffer, int32_t * x); +int BoltBuffer_unload_i32be(BoltBuffer* buffer, int32_t* x); /** * Unload a signed 64-bit integer (big endian) from a buffer. @@ -236,7 +228,7 @@ int BoltBuffer_unload_i32be(struct BoltBuffer * buffer, int32_t * x); * @param x * @return */ -int BoltBuffer_unload_i64be(struct BoltBuffer * buffer, int64_t * x); +int BoltBuffer_unload_i64be(BoltBuffer* buffer, int64_t* x); /** * Unload a double precision floating point number (big endian) from a buffer. @@ -245,7 +237,6 @@ int BoltBuffer_unload_i64be(struct BoltBuffer * buffer, int64_t * x); * @param x * @return */ -int BoltBuffer_unload_f64be(struct BoltBuffer * buffer, double * x); - +int BoltBuffer_unload_f64be(BoltBuffer* buffer, double* x); #endif // SEABOLT_BUFFERING diff --git a/src/seabolt/src/config-impl.h b/src/seabolt/src/config-impl.h deleted file mode 100644 index 1cea8484..00000000 --- a/src/seabolt/src/config-impl.h +++ /dev/null @@ -1,65 +0,0 @@ -/* -* Copyright (c) 2002-2018 "Neo4j," -* Neo4j Sweden AB [http://neo4j.com] -* -* This file is part of Neo4j. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ -#ifndef SEABOLT_CONFIG_IMPL -#define SEABOLT_CONFIG_IMPL - -#include "config.h" -#include "common-impl.h" -#include "error.h" - -#include -#include - -#if USE_POSIXSOCK - -#include -#include -#include -#include -#include -#include - -#endif // USE_POSIXSOCK - -#if USE_WINSOCK - -#include -#include -#include - -#endif // USE_WINSOCK - -#if USE_WINSSPI - -#endif // USE_WINSSPI - -#if USE_OPENSSL - -#include -#include -#include -#include - -extern int SSL_CTX_TRUST_INDEX; -extern int SSL_CTX_LOG_INDEX; -extern int SSL_CTX_ID_INDEX; - -#endif // USE_OPENSSL - -#endif // SEABOLT_CONFIG_IMPL diff --git a/src/seabolt/src/config-private.h b/src/seabolt/src/config-private.h new file mode 100644 index 00000000..e276e47e --- /dev/null +++ b/src/seabolt/src/config-private.h @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2002-2018 "Neo4j," + * Neo4j Sweden AB [http://neo4j.com] + * + * This file is part of Neo4j. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef SEABOLT_CONFIG_PRIVATE_H +#define SEABOLT_CONFIG_PRIVATE_H + +#include "config.h" + +struct BoltTrust { + char* certs; + int32_t certs_len; + int skip_verify; + int skip_verify_hostname; +}; + +struct BoltSocketOptions { + int connect_timeout; + int recv_timeout; + int send_timeout; + int keep_alive; +}; + +struct BoltConfig { + BoltMode mode; + BoltTransport transport; + struct BoltTrust* trust; + char* user_agent; + struct BoltValue* routing_context; + struct BoltAddressResolver* address_resolver; + struct BoltLog* log; + int max_pool_size; + int max_connection_life_time; + int max_connection_acquisition_time; + struct BoltSocketOptions* socket_options; +}; + +BoltConfig* BoltConfig_clone(BoltConfig* config); + +#endif //SEABOLT_CONFIG_PRIVATE_H diff --git a/src/seabolt/src/config.c b/src/seabolt/src/config.c new file mode 100644 index 00000000..59a8907c --- /dev/null +++ b/src/seabolt/src/config.c @@ -0,0 +1,321 @@ +/* + * Copyright (c) 2002-2018 "Neo4j," + * Neo4j Sweden AB [http://neo4j.com] + * + * This file is part of Neo4j. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "bolt-private.h" +#include "config-private.h" +#include "address-resolver-private.h" +#include "log-private.h" +#include "mem.h" + +BoltSocketOptions* BoltSocketOptions_create() +{ + BoltSocketOptions* socket_options = BoltMem_allocate(sizeof(BoltSocketOptions)); + socket_options->connect_timeout = 5000; + socket_options->recv_timeout = 0; + socket_options->send_timeout = 0; + socket_options->keep_alive = 1; + return socket_options; +} + +BoltSocketOptions* BoltSocketOptions_clone(BoltSocketOptions* socket_options) +{ + if (socket_options==NULL) { + return NULL; + } + + BoltSocketOptions* clone = BoltSocketOptions_create(); + clone->connect_timeout = socket_options->connect_timeout; + clone->recv_timeout = socket_options->recv_timeout; + clone->send_timeout = socket_options->send_timeout; + clone->keep_alive = socket_options->keep_alive; + return clone; +} + +void BoltSocketOptions_destroy(BoltSocketOptions* socket_options) +{ + BoltMem_deallocate(socket_options, sizeof(BoltSocketOptions)); +} + +int BoltSocketOptions_get_connect_timeout(BoltSocketOptions* socket_options) +{ + return socket_options->connect_timeout; +} + +int BoltSocketOptions_set_connect_timeout(BoltSocketOptions* socket_options, int connect_timeout) +{ + socket_options->connect_timeout = connect_timeout; + return BOLT_SUCCESS; +} + +int BoltSocketOptions_get_keep_alive(BoltSocketOptions* socket_options) +{ + return socket_options->keep_alive; +} + +int BoltSocketOptions_set_keep_alive(BoltSocketOptions* socket_options, int keep_alive) +{ + socket_options->keep_alive = keep_alive; + return BOLT_SUCCESS; +} + +BoltTrust* BoltTrust_create() +{ + BoltTrust* trust = BoltMem_allocate(sizeof(BoltTrust)); + trust->certs = NULL; + trust->certs_len = 0; + trust->skip_verify = 1; + trust->skip_verify_hostname = 1; + return trust; +} + +BoltTrust* BoltTrust_clone(BoltTrust* source) +{ + if (source==NULL) { + return NULL; + } + + BoltTrust* clone = BoltTrust_create(); + BoltTrust_set_certs(clone, source->certs, source->certs_len); + BoltTrust_set_skip_verify(clone, source->skip_verify); + BoltTrust_set_skip_verify_hostname(clone, source->skip_verify_hostname); + return clone; +} + +void BoltTrust_destroy(BoltTrust* trust) +{ + if (trust->certs!=NULL) { + BoltMem_deallocate(trust->certs, trust->certs_len); + } + BoltMem_deallocate(trust, sizeof(BoltTrust)); +} + +const char* BoltTrust_get_certs(BoltTrust* trust, size_t* size) +{ + *size = trust->certs_len; + return trust->certs; +} + +int BoltTrust_set_certs(BoltTrust* trust, const char* certs_pem, size_t size) +{ + trust->certs = BoltMem_duplicate(certs_pem, size); + trust->certs_len = size; + return BOLT_SUCCESS; +} + +int BoltTrust_get_skip_verify(BoltTrust* trust) +{ + return trust->skip_verify; +} + +int BoltTrust_set_skip_verify(BoltTrust* trust, int skip_verify) +{ + trust->skip_verify = skip_verify; + return BOLT_SUCCESS; +} + +int BoltTrust_get_skip_verify_hostname(BoltTrust* trust) +{ + return trust->skip_verify_hostname; +} + +int BoltTrust_set_skip_verify_hostname(BoltTrust* trust, int skip_verify_hostname) +{ + trust->skip_verify_hostname = skip_verify_hostname; + return BOLT_SUCCESS; +} + +BoltConfig* BoltConfig_create() +{ + BoltConfig* config = BoltMem_allocate(sizeof(BoltConfig)); + config->mode = BOLT_MODE_DIRECT; + config->transport = BOLT_TRANSPORT_ENCRYPTED; + config->trust = NULL; + config->user_agent = NULL; + config->routing_context = NULL; + config->address_resolver = NULL; + config->log = NULL; + config->max_pool_size = 100; + config->max_connection_life_time = 0; + config->max_connection_acquisition_time = 0; + config->socket_options = NULL; + return config; +} + +BoltConfig* BoltConfig_clone(BoltConfig* config) +{ + BoltConfig* clone = BoltConfig_create(); + if (config!=NULL) { + BoltConfig_set_mode(clone, config->mode); + BoltConfig_set_transport(clone, config->transport); + BoltConfig_set_trust(clone, config->trust); + BoltConfig_set_user_agent(clone, config->user_agent); + BoltConfig_set_routing_context(clone, config->routing_context); + BoltConfig_set_address_resolver(clone, config->address_resolver); + BoltConfig_set_log(clone, config->log); + BoltConfig_set_max_pool_size(clone, config->max_pool_size); + BoltConfig_set_max_connection_life_time(clone, config->max_connection_life_time); + BoltConfig_set_max_connection_acquisition_time(clone, config->max_connection_acquisition_time); + BoltConfig_set_socket_options(clone, config->socket_options); + } + return clone; +} + +void BoltConfig_destroy(BoltConfig* config) +{ + if (config->trust!=NULL) { + BoltTrust_destroy(config->trust); + } + if (config->user_agent!=NULL) { + BoltMem_deallocate(config->user_agent, SIZE_OF_C_STRING(config->user_agent)); + } + if (config->routing_context!=NULL) { + BoltValue_destroy(config->routing_context); + } + if (config->address_resolver!=NULL) { + BoltAddressResolver_destroy(config->address_resolver); + } + if (config->log!=NULL) { + BoltLog_destroy(config->log); + } + if (config->socket_options!=NULL) { + BoltSocketOptions_destroy(config->socket_options); + } + BoltMem_deallocate(config, sizeof(BoltConfig)); +} + +BoltMode BoltConfig_get_mode(BoltConfig* config) +{ + return config->mode; +} + +int BoltConfig_set_mode(BoltConfig* config, BoltMode mode) +{ + config->mode = mode; + return BOLT_SUCCESS; +} + +BoltTransport BoltConfig_get_transport(BoltConfig* config) +{ + return config->transport; +} + +int BoltConfig_set_transport(BoltConfig* config, BoltTransport transport) +{ + config->transport = transport; + return BOLT_SUCCESS; +} + +BoltTrust* BoltConfig_get_trust(BoltConfig* config) +{ + return config->trust; +} + +int BoltConfig_set_trust(BoltConfig* config, BoltTrust* trust) +{ + config->trust = BoltTrust_clone(trust); + return BOLT_SUCCESS; +} + +const char* BoltConfig_get_user_agent(BoltConfig* config) +{ + return config->user_agent; +} + +int BoltConfig_set_user_agent(BoltConfig* config, const char* user_agent) +{ + config->user_agent = BoltMem_duplicate(user_agent, SIZE_OF_C_STRING(user_agent)); + return BOLT_SUCCESS; +} + +BoltValue* BoltConfig_get_routing_context(BoltConfig* config) +{ + return config->routing_context; +} + +int BoltConfig_set_routing_context(BoltConfig* config, BoltValue* routing_context) +{ + config->routing_context = BoltValue_duplicate(routing_context); + return BOLT_SUCCESS; +} + +BoltAddressResolver* BoltConfig_get_address_resolver(BoltConfig* config) +{ + return config->address_resolver; +} + +int BoltConfig_set_address_resolver(BoltConfig* config, BoltAddressResolver* address_resolver) +{ + config->address_resolver = BoltAddressResolver_clone(address_resolver); + return BOLT_SUCCESS; +} + +BoltLog* BoltConfig_get_log(BoltConfig* config) +{ + return config->log; +} + +int BoltConfig_set_log(BoltConfig* config, BoltLog* log) +{ + config->log = BoltLog_clone(log); + return BOLT_SUCCESS; +} + +int BoltConfig_get_max_pool_size(BoltConfig* config) +{ + return config->max_pool_size; +} + +int BoltConfig_set_max_pool_size(BoltConfig* config, int max_pool_size) +{ + config->max_pool_size = max_pool_size; + return BOLT_SUCCESS; +} + +int BoltConfig_get_max_connection_life_time(BoltConfig* config) +{ + return config->max_connection_life_time; +} + +int BoltConfig_set_max_connection_life_time(BoltConfig* config, int max_connection_life_time) +{ + config->max_connection_life_time = max_connection_life_time; + return BOLT_SUCCESS; +} + +int BoltConfig_get_max_connection_acquisition_time(BoltConfig* config) +{ + return config->max_connection_acquisition_time; +} + +int BoltConfig_set_max_connection_acquisition_time(BoltConfig* config, int max_connection_acquisition_time) +{ + config->max_connection_acquisition_time = max_connection_acquisition_time; + return BOLT_SUCCESS; +} + +BoltSocketOptions* BoltConfig_get_socket_options(BoltConfig* config) +{ + return config->socket_options; +} + +int BoltConfig_set_socket_options(BoltConfig* config, BoltSocketOptions* socket_options) +{ + config->socket_options = BoltSocketOptions_clone(socket_options); + return BOLT_SUCCESS; +} diff --git a/src/seabolt/src/config.h b/src/seabolt/src/config.h index 603d07d3..2f61cc9e 100644 --- a/src/seabolt/src/config.h +++ b/src/seabolt/src/config.h @@ -1,38 +1,126 @@ /* -* Copyright (c) 2002-2018 "Neo4j," -* Neo4j Sweden AB [http://neo4j.com] -* -* This file is part of Neo4j. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ -#ifndef SEABOLT_CONFIG -#define SEABOLT_CONFIG -#include -#include - -#include "bolt-version.h" -#include "bolt-exports.h" - -#if USE_POSIXSOCK -#include -#endif - -#ifdef _WIN32 -typedef unsigned short in_port_t; -typedef int socklen_t; -#endif // _WIN32 - -struct BoltLog; - -#endif // SEABOLT_CONFIG + * Copyright (c) 2002-2018 "Neo4j," + * Neo4j Sweden AB [http://neo4j.com] + * + * This file is part of Neo4j. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef SEABOLT_CONFIG_H +#define SEABOLT_CONFIG_H + +#include "bolt-public.h" +#include "address-resolver.h" +#include "values.h" + +typedef int BoltMode; +#define BOLT_MODE_DIRECT 0 +#define BOLT_MODE_ROUTING 1 + +typedef int BoltTransport; +#define BOLT_TRANSPORT_PLAINTEXT 0 +#define BOLT_TRANSPORT_ENCRYPTED 1 + +/** + * Socket options + */ +typedef struct BoltSocketOptions BoltSocketOptions; + +SEABOLT_EXPORT BoltSocketOptions* BoltSocketOptions_create(); + +SEABOLT_EXPORT void BoltSocketOptions_destroy(BoltSocketOptions* socket_options); + +SEABOLT_EXPORT int BoltSocketOptions_get_connect_timeout(BoltSocketOptions* socket_options); + +SEABOLT_EXPORT int BoltSocketOptions_set_connect_timeout(BoltSocketOptions* socket_options, int connect_timeout); + +SEABOLT_EXPORT int BoltSocketOptions_get_keep_alive(BoltSocketOptions* socket_options); + +SEABOLT_EXPORT int BoltSocketOptions_set_keep_alive(BoltSocketOptions* socket_options, int keep_alive); + +/** + * Trust options + */ +typedef struct BoltTrust BoltTrust; + +SEABOLT_EXPORT BoltTrust* BoltTrust_create(); + +SEABOLT_EXPORT void BoltTrust_destroy(BoltTrust* trust); + +SEABOLT_EXPORT const char* BoltTrust_get_certs(BoltTrust* trust, size_t* size); + +SEABOLT_EXPORT int BoltTrust_set_certs(BoltTrust* trust, const char* certs_pem, size_t size); + +SEABOLT_EXPORT int BoltTrust_get_skip_verify(BoltTrust* trust); + +SEABOLT_EXPORT int BoltTrust_set_skip_verify(BoltTrust* trust, int skip_verify); + +SEABOLT_EXPORT int BoltTrust_get_skip_verify_hostname(BoltTrust* trust); + +SEABOLT_EXPORT int BoltTrust_set_skip_verify_hostname(BoltTrust* trust, int skip_verify_hostname); + +/** + * Configuration options + */ +typedef struct BoltConfig BoltConfig; + +SEABOLT_EXPORT BoltConfig* BoltConfig_create(); + +SEABOLT_EXPORT void BoltConfig_destroy(BoltConfig* config); + +SEABOLT_EXPORT BoltMode BoltConfig_get_mode(BoltConfig* config); + +SEABOLT_EXPORT int BoltConfig_set_mode(BoltConfig* config, BoltMode mode); + +SEABOLT_EXPORT BoltTransport BoltConfig_get_transport(BoltConfig* config); + +SEABOLT_EXPORT int BoltConfig_set_transport(BoltConfig* config, BoltTransport transport); + +SEABOLT_EXPORT BoltTrust* BoltConfig_get_trust(BoltConfig* config); + +SEABOLT_EXPORT int BoltConfig_set_trust(BoltConfig* config, BoltTrust* trust); + +SEABOLT_EXPORT const char* BoltConfig_get_user_agent(BoltConfig* config); + +SEABOLT_EXPORT int BoltConfig_set_user_agent(BoltConfig* config, const char* user_agent); + +SEABOLT_EXPORT BoltValue* BoltConfig_get_routing_context(BoltConfig* config); + +SEABOLT_EXPORT int BoltConfig_set_routing_context(BoltConfig* config, BoltValue* routing_context); + +SEABOLT_EXPORT BoltAddressResolver* BoltConfig_get_address_resolver(BoltConfig* config); + +SEABOLT_EXPORT int BoltConfig_set_address_resolver(BoltConfig* config, BoltAddressResolver* address_resolver); + +SEABOLT_EXPORT BoltLog* BoltConfig_get_log(BoltConfig* config); + +SEABOLT_EXPORT int BoltConfig_set_log(BoltConfig* config, BoltLog* log); + +SEABOLT_EXPORT int BoltConfig_get_max_pool_size(BoltConfig* config); + +SEABOLT_EXPORT int BoltConfig_set_max_pool_size(BoltConfig* config, int max_pool_size); + +SEABOLT_EXPORT int BoltConfig_get_max_connection_life_time(BoltConfig* config); + +SEABOLT_EXPORT int BoltConfig_set_max_connection_life_time(BoltConfig* config, int max_connection_life_time); + +SEABOLT_EXPORT int BoltConfig_get_max_connection_acquisition_time(BoltConfig* config); + +SEABOLT_EXPORT int +BoltConfig_set_max_connection_acquisition_time(BoltConfig* config, int max_connection_acquisition_time); + +SEABOLT_EXPORT BoltSocketOptions* BoltConfig_get_socket_options(BoltConfig* config); + +SEABOLT_EXPORT int BoltConfig_set_socket_options(BoltConfig* config, BoltSocketOptions* socket_options); + +#endif //SEABOLT_CONFIG_H diff --git a/src/seabolt/src/connection-private.h b/src/seabolt/src/connection-private.h new file mode 100644 index 00000000..3f667fe9 --- /dev/null +++ b/src/seabolt/src/connection-private.h @@ -0,0 +1,87 @@ +/* + * Copyright (c) 2002-2018 "Neo4j," + * Neo4j Sweden AB [http://neo4j.com] + * + * This file is part of Neo4j. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef SEABOLT_CONNECTION_PRIVATE_H +#define SEABOLT_CONNECTION_PRIVATE_H + +#include "connection.h" +#include "status-private.h" + +typedef void (* error_action_func)(struct BoltConnection*, void*); + +typedef struct BoltConnectionMetrics BoltConnectionMetrics; + +/** + * Record of connection usage statistics. + */ +struct BoltConnectionMetrics { + struct timespec time_opened; + struct timespec time_closed; + unsigned long long bytes_sent; + unsigned long long bytes_received; +}; + +struct BoltConnection { + /// The agent currently responsible for using this connection + const void* agent; + + /// Transport type for this connection + BoltTransport transport; + + /// Socket options + const BoltSocketOptions* sock_opts; + + const BoltAddress* address; + const BoltAddress* local_address; + const BoltAddress* resolved_address; + char* id; + + const BoltLog* log; + + int owns_ssl_context; + /// The security context (secure connections only) + struct ssl_ctx_st* ssl_context; + /// A secure socket wrapper (secure connections only) + struct ssl_st* ssl; + /// The raw socket that backs this connection + int socket; + + /// The protocol version used for this connection + int32_t protocol_version; + /// State required by the protocol + struct BoltProtocol* protocol; + + // These buffers contain data exactly as it is transmitted or + // received. Therefore for Bolt v1, chunk headers are included + // in these buffers + + /// Transmit buffer + struct BoltBuffer* tx_buffer; + /// Receive buffer + struct BoltBuffer* rx_buffer; + + /// Connection metrics + BoltConnectionMetrics* metrics; + /// Current status of the connection + BoltStatus* status; + + error_action_func on_error_cb; + void* on_error_cb_state; +}; + +#endif //SEABOLT_CONNECTION_PRIVATE_H diff --git a/src/seabolt/src/connections.c b/src/seabolt/src/connection.c similarity index 76% rename from src/seabolt/src/connections.c rename to src/seabolt/src/connection.c index d90d4e72..17bd5898 100644 --- a/src/seabolt/src/connections.c +++ b/src/seabolt/src/connection.c @@ -17,19 +17,18 @@ * limitations under the License. */ -#include - -#include "config-impl.h" -#include "buffering.h" -#include "connections.h" -#include "logging.h" +#include "bolt-private.h" +#include "address-private.h" +#include "config-private.h" +#include "connection-private.h" +#include "log-private.h" #include "mem.h" -#include "platform.h" -#include "tls.h" #include "protocol.h" +#include "tls.h" #include "v1.h" #include "v2.h" #include "v3.h" +#include "platform.h" #define INITIAL_TX_BUFFER_SIZE 8192 #define INITIAL_RX_BUFFER_SIZE 8192 @@ -61,11 +60,11 @@ if (status_try != BOLT_SUCCESS) { \ if (status_try == -1) { \ int last_error = _last_error_code(); \ - _set_status_with_ctx(connection, BOLT_DEFUNCT, _transform_error(last_error), error_ctx_fmt, file, line, last_error); \ + _set_status_with_ctx(connection, BOLT_CONNECTION_STATE_DEFUNCT, _transform_error(last_error), error_ctx_fmt, file, line, last_error); \ } else if (status_try == BOLT_STATUS_SET) { \ return -1; \ } else { \ - _set_status_with_ctx(connection, BOLT_DEFUNCT, status_try, error_ctx_fmt, file, line, -1); \ + _set_status_with_ctx(connection, BOLT_CONNECTION_STATE_DEFUNCT, status_try, error_ctx_fmt, file, line, -1); \ } \ return status_try; \ } \ @@ -149,7 +148,7 @@ int _last_error_code() #endif } -int _last_error_code_ssl(struct BoltConnection* connection, int ssl_ret, int* ssl_error_code, int* last_error) +int _last_error_code_ssl(BoltConnection* connection, int ssl_ret, int* ssl_error_code, int* last_error) { // On windows, SSL_get_error resets WSAGetLastError so we're left without an error code after // asking error code - so we're saving it here in case. @@ -172,65 +171,66 @@ int _last_error_code_ssl(struct BoltConnection* connection, int ssl_ret, int* ss } } -void _status_changed(struct BoltConnection* connection) +void _status_changed(BoltConnection* connection) { char* status_text = NULL; - switch (connection->status) { - case BOLT_DISCONNECTED: + switch (connection->status->state) { + case BOLT_CONNECTION_STATE_DISCONNECTED: status_text = ""; break; - case BOLT_CONNECTED: + case BOLT_CONNECTION_STATE_CONNECTED: status_text = ""; break; - case BOLT_READY: + case BOLT_CONNECTION_STATE_READY: status_text = ""; break; - case BOLT_FAILED: + case BOLT_CONNECTION_STATE_FAILED: status_text = ""; break; - case BOLT_DEFUNCT: + case BOLT_CONNECTION_STATE_DEFUNCT: status_text = ""; break; } - if (connection->error_ctx[0]!=0) { + if (connection->status->error_ctx[0]!=0) { BoltLog_info(connection->log, "[%s]: %s [%s]", BoltConnection_id(connection), status_text, - connection->error_ctx); + connection->status->error_ctx); } else { BoltLog_info(connection->log, "[%s]: %s", BoltConnection_id(connection), status_text); } - if (connection->status==BOLT_DEFUNCT || connection->status==BOLT_FAILED) { + if (connection->status->state==BOLT_CONNECTION_STATE_DEFUNCT + || connection->status->state==BOLT_CONNECTION_STATE_FAILED) { if (connection->on_error_cb!=NULL) { (*connection->on_error_cb)(connection, connection->on_error_cb_state); } } } -void _set_status(struct BoltConnection* connection, enum BoltConnectionStatus status, int error) +void _set_status(BoltConnection* connection, BoltConnectionState state, int error) { - enum BoltConnectionStatus old_status = connection->status; - connection->status = status; - connection->error = error; - connection->error_ctx[0] = '\0'; + BoltConnectionState old_status = connection->status->state; + connection->status->state = state; + connection->status->error = error; + connection->status->error_ctx[0] = '\0'; - if (status!=old_status) { + if (state!=old_status) { _status_changed(connection); } } -void _set_status_with_ctx(struct BoltConnection* connection, enum BoltConnectionStatus status, int error, +void _set_status_with_ctx(BoltConnection* connection, BoltConnectionState status, int error, const char* error_ctx_format, ...) { - enum BoltConnectionStatus old_status = connection->status; - connection->status = status; - connection->error = error; - connection->error_ctx[0] = '\0'; + BoltConnectionState old_status = connection->status->state; + connection->status->state = status; + connection->status->error = error; + connection->status->error_ctx[0] = '\0'; if (error_ctx_format!=NULL) { va_list args; va_start(args, error_ctx_format); - vsnprintf(connection->error_ctx, ERROR_CTX_SIZE, error_ctx_format, args); + vsnprintf(connection->status->error_ctx, ERROR_CTX_SIZE, error_ctx_format, args); va_end(args); } @@ -239,7 +239,7 @@ void _set_status_with_ctx(struct BoltConnection* connection, enum BoltConnection } } -int _socket_configure(struct BoltConnection* connection) +int _socket_configure(BoltConnection* connection) { const int YES = 1, NO = 0; @@ -248,7 +248,7 @@ int _socket_configure(struct BoltConnection* connection) "_socket_configure(%s:%d), set tcp_nodelay error code: %d", __FILE__, __LINE__); // Set keep-alive accordingly, default: TRUE - if (connection->sock_opts==NULL || connection->sock_opts->keepalive) { + if (connection->sock_opts==NULL || connection->sock_opts->keep_alive) { TRY(SETSOCKETOPT(connection->socket, SOL_SOCKET, SO_KEEPALIVE, &YES, sizeof(YES)), "_socket_configure(%s:%d), set so_keepalive error code: %d", __FILE__, __LINE__); } @@ -294,7 +294,7 @@ int _socket_configure(struct BoltConnection* connection) return BOLT_SUCCESS; } -int _socket_set_mode(struct BoltConnection* connection, int blocking) +int _socket_set_mode(BoltConnection* connection, int blocking) { int status = 0; @@ -315,9 +315,9 @@ int _socket_set_mode(struct BoltConnection* connection, int blocking) return status; } -int _open(struct BoltConnection* connection, enum BoltTransport transport, const struct sockaddr_storage* address) +int _open(BoltConnection* connection, BoltTransport transport, const struct sockaddr_storage* address) { - memset(&connection->metrics, 0, sizeof(connection->metrics)); + memset(connection->metrics, 0, sizeof(BoltConnectionMetrics)); connection->transport = transport; switch (address->ss_family) { case AF_INET: @@ -333,13 +333,14 @@ int _open(struct BoltConnection* connection, enum BoltTransport transport, const default: BoltLog_error(connection->log, "[%s]: Unsupported address family %d", BoltConnection_id(connection), address->ss_family); - _set_status_with_ctx(connection, BOLT_DEFUNCT, BOLT_UNSUPPORTED, "_open(%s:%d)", __FILE__, __LINE__); + _set_status_with_ctx(connection, BOLT_CONNECTION_STATE_DEFUNCT, BOLT_UNSUPPORTED, "_open(%s:%d)", __FILE__, + __LINE__); return BOLT_STATUS_SET; } connection->socket = (int) SOCKET(address->ss_family, SOCK_STREAM, IPPROTO_TCP); if (connection->socket==-1) { int last_error_code = _last_error_code(); - _set_status_with_ctx(connection, BOLT_DEFUNCT, _transform_error(last_error_code), + _set_status_with_ctx(connection, BOLT_CONNECTION_STATE_DEFUNCT, _transform_error(last_error_code), "_open(%s:%d), socket error code: %d", __FILE__, __LINE__, last_error_code); return BOLT_STATUS_SET; } @@ -363,7 +364,7 @@ int _open(struct BoltConnection* connection, enum BoltTransport transport, const } #endif default: - _set_status_with_ctx(connection, BOLT_DEFUNCT, _transform_error(error_code), + _set_status_with_ctx(connection, BOLT_CONNECTION_STATE_DEFUNCT, _transform_error(error_code), "_open(%s:%d), connect error code: %d", __FILE__, __LINE__, error_code); return BOLT_STATUS_SET; } @@ -384,7 +385,8 @@ int _open(struct BoltConnection* connection, enum BoltTransport transport, const case 0: { //timeout expired BoltLog_info(connection->log, "[%s]: Connect timed out", BoltConnection_id(connection)); - _set_status_with_ctx(connection, BOLT_DEFUNCT, BOLT_TIMED_OUT, "_open(%s:%d)", __FILE__, __LINE__); + _set_status_with_ctx(connection, BOLT_CONNECTION_STATE_DEFUNCT, BOLT_TIMED_OUT, "_open(%s:%d)", + __FILE__, __LINE__); return BOLT_STATUS_SET; } case 1: { @@ -394,7 +396,7 @@ int _open(struct BoltConnection* connection, enum BoltTransport transport, const TRY(GETSOCKETOPT(connection->socket, SOL_SOCKET, SO_ERROR, &so_error, &so_error_len), "_open(%s:%d), getsocketopt error code: %d", __FILE__, __LINE__); if (so_error!=0) { - _set_status_with_ctx(connection, BOLT_DEFUNCT, _transform_error(so_error), + _set_status_with_ctx(connection, BOLT_CONNECTION_STATE_DEFUNCT, _transform_error(so_error), "_open(%s:%d), socket error code: %d", __FILE__, __LINE__, so_error); return BOLT_STATUS_SET; } @@ -403,7 +405,7 @@ int _open(struct BoltConnection* connection, enum BoltTransport transport, const } default: { int last_error = _last_error_code(); - _set_status_with_ctx(connection, BOLT_DEFUNCT, _transform_error(last_error), + _set_status_with_ctx(connection, BOLT_CONNECTION_STATE_DEFUNCT, _transform_error(last_error), "_open(%s:%d), select error code: %d", __FILE__, __LINE__, last_error); return BOLT_STATUS_SET; } @@ -419,13 +421,13 @@ int _open(struct BoltConnection* connection, enum BoltTransport transport, const } TRY(_socket_configure(connection), "_open(%s:%d), _socket_configure error code: %d", __FILE__, __LINE__); - BoltUtil_get_time(&connection->metrics.time_opened); + BoltUtil_get_time(&connection->metrics->time_opened); connection->tx_buffer = BoltBuffer_create(INITIAL_TX_BUFFER_SIZE); connection->rx_buffer = BoltBuffer_create(INITIAL_RX_BUFFER_SIZE); return BOLT_SUCCESS; } -int _secure(struct BoltConnection* connection, struct BoltTrust* trust) +int _secure(BoltConnection* connection, struct BoltTrust* trust) { int status = 1; @@ -443,7 +445,7 @@ int _secure(struct BoltConnection* connection, struct BoltTrust* trust) status = SSL_set_fd(connection->ssl, connection->socket); if (!status) { - _set_status_with_ctx(connection, BOLT_DEFUNCT, BOLT_TLS_ERROR, + _set_status_with_ctx(connection, BOLT_CONNECTION_STATE_DEFUNCT, BOLT_TLS_ERROR, "_secure(%s:%d), SSL_set_fd returned: %d", __FILE__, __LINE__, status); } @@ -454,7 +456,7 @@ int _secure(struct BoltConnection* connection, struct BoltTrust* trust) status = SSL_set_tlsext_host_name(connection->ssl, connection->address->host); if (!status) { - _set_status_with_ctx(connection, BOLT_DEFUNCT, BOLT_TLS_ERROR, + _set_status_with_ctx(connection, BOLT_CONNECTION_STATE_DEFUNCT, BOLT_TLS_ERROR, "_secure(%s:%d), SSL_set_tlsext_host_name returned: %d", __FILE__, __LINE__, status); } @@ -466,7 +468,8 @@ int _secure(struct BoltConnection* connection, struct BoltTrust* trust) return 0; } - _set_status_with_ctx(connection, BOLT_DEFUNCT, BOLT_TLS_ERROR, "_secure(%s:%d), SSL_connect returned: %d", + _set_status_with_ctx(connection, BOLT_CONNECTION_STATE_DEFUNCT, BOLT_TLS_ERROR, + "_secure(%s:%d), SSL_connect returned: %d", __FILE__, __LINE__, status); } @@ -481,7 +484,7 @@ int _secure(struct BoltConnection* connection, struct BoltTrust* trust) return -1; } -void _close(struct BoltConnection* connection) +void _close(BoltConnection* connection) { BoltLog_info(connection->log, "[%s]: Closing connection", BoltConnection_id(connection)); @@ -506,12 +509,12 @@ void _close(struct BoltConnection* connection) } switch (connection->transport) { - case BOLT_SOCKET: { + case BOLT_TRANSPORT_PLAINTEXT: { SHUTDOWN(connection->socket, SHUT_RDWR); CLOSE(connection->socket); break; } - case BOLT_SECURE_SOCKET: { + case BOLT_TRANSPORT_ENCRYPTED: { if (connection->ssl!=NULL) { SSL_shutdown(connection->ssl); SSL_free(connection->ssl); @@ -527,12 +530,12 @@ void _close(struct BoltConnection* connection) break; } } - BoltUtil_get_time(&connection->metrics.time_closed); + BoltUtil_get_time(&connection->metrics->time_closed); connection->socket = 0; - _set_status(connection, BOLT_DISCONNECTED, BOLT_SUCCESS); + _set_status(connection, BOLT_CONNECTION_STATE_DISCONNECTED, BOLT_SUCCESS); } -int _send(struct BoltConnection* connection, const char* data, int size) +int _send(BoltConnection* connection, const char* data, int size) { if (size==0) { return BOLT_SUCCESS; @@ -542,38 +545,38 @@ int _send(struct BoltConnection* connection, const char* data, int size) while (total_senttransport) { - case BOLT_SOCKET: { + case BOLT_TRANSPORT_PLAINTEXT: { sent = TRANSMIT(connection->socket, data+total_sent, remaining, 0); break; } - case BOLT_SECURE_SOCKET: { + case BOLT_TRANSPORT_ENCRYPTED: { sent = TRANSMIT_S(connection->ssl, data+total_sent, remaining, 0); break; } } if (sent>=0) { - connection->metrics.bytes_sent += sent; + connection->metrics->bytes_sent += sent; total_sent += sent; remaining -= sent; } else { switch (connection->transport) { - case BOLT_SOCKET: { + case BOLT_TRANSPORT_PLAINTEXT: { int last_error = _last_error_code(); - _set_status_with_ctx(connection, BOLT_DEFUNCT, _transform_error(last_error), + _set_status_with_ctx(connection, BOLT_CONNECTION_STATE_DEFUNCT, _transform_error(last_error), "_send(%s:%d), send error code: %d", __FILE__, __LINE__, last_error); BoltLog_error(connection->log, "[%s]: Socket error %d on transmit", BoltConnection_id(connection), - connection->error); + connection->status->error); break; } - case BOLT_SECURE_SOCKET: { + case BOLT_TRANSPORT_ENCRYPTED: { int last_error = 0, ssl_error = 0; int last_error_transformed = _last_error_code_ssl(connection, sent, &ssl_error, &last_error); - _set_status_with_ctx(connection, BOLT_DEFUNCT, last_error_transformed, + _set_status_with_ctx(connection, BOLT_CONNECTION_STATE_DEFUNCT, last_error_transformed, "_send(%s:%d), send_s error code: %d, underlying error code: %d", __FILE__, __LINE__, ssl_error, last_error); BoltLog_error(connection->log, "[%s]: SSL error %d on transmit", BoltConnection_id(connection), - connection->error); + connection->status->error); break; } } @@ -593,7 +596,7 @@ int _send(struct BoltConnection* connection, const char* data, int size) * @param max_size * @return */ -int _receive(struct BoltConnection* connection, char* buffer, int min_size, int max_size, int* received) +int _receive(BoltConnection* connection, char* buffer, int min_size, int max_size, int* received) { if (min_size==0) { return BOLT_SUCCESS; @@ -603,42 +606,42 @@ int _receive(struct BoltConnection* connection, char* buffer, int min_size, int while (total_receivedtransport) { - case BOLT_SOCKET: + case BOLT_TRANSPORT_PLAINTEXT: single_received = RECEIVE(connection->socket, buffer+total_received, max_remaining, 0); break; - case BOLT_SECURE_SOCKET: + case BOLT_TRANSPORT_ENCRYPTED: single_received = RECEIVE_S(connection->ssl, buffer+total_received, max_remaining, 0); break; } if (single_received>0) { - connection->metrics.bytes_received += single_received; + connection->metrics->bytes_received += single_received; total_received += single_received; max_remaining -= single_received; } else if (single_received==0) { BoltLog_info(connection->log, "[%s]: Detected end of transmission", BoltConnection_id(connection)); - _set_status_with_ctx(connection, BOLT_DISCONNECTED, BOLT_END_OF_TRANSMISSION, + _set_status_with_ctx(connection, BOLT_CONNECTION_STATE_DISCONNECTED, BOLT_END_OF_TRANSMISSION, "_receive(%s:%d), receive returned 0", __FILE__, __LINE__); return BOLT_STATUS_SET; } else { switch (connection->transport) { - case BOLT_SOCKET: { + case BOLT_TRANSPORT_PLAINTEXT: { int last_error = _last_error_code(); - _set_status_with_ctx(connection, BOLT_DEFUNCT, _transform_error(last_error), + _set_status_with_ctx(connection, BOLT_CONNECTION_STATE_DEFUNCT, _transform_error(last_error), "_receive(%s:%d), receive error code: %d", __FILE__, __LINE__, last_error); BoltLog_error(connection->log, "[%s]: Socket error %d on receive", BoltConnection_id(connection), - connection->error); + connection->status->error); break; } - case BOLT_SECURE_SOCKET: { + case BOLT_TRANSPORT_ENCRYPTED: { int last_error = 0, ssl_error = 0; int last_error_transformed = _last_error_code_ssl(connection, single_received, &ssl_error, &last_error); - _set_status_with_ctx(connection, BOLT_DEFUNCT, last_error_transformed, + _set_status_with_ctx(connection, BOLT_CONNECTION_STATE_DEFUNCT, last_error_transformed, "_receive(%s:%d), receive_s error code: %d, underlying error code: %d", __FILE__, __LINE__, ssl_error, last_error); BoltLog_error(connection->log, "[%s]: SSL error %d on receive", BoltConnection_id(connection), - connection->error); + connection->status->error); break; } } @@ -657,7 +660,7 @@ int _receive(struct BoltConnection* connection, char* buffer, int min_size, int return BOLT_SUCCESS; } -int handshake_b(struct BoltConnection* connection, int32_t _1, int32_t _2, int32_t _3, int32_t _4) +int handshake_b(BoltConnection* connection, int32_t _1, int32_t _2, int32_t _3, int32_t _4) { BoltLog_info(connection->log, "[%s]: Performing handshake", BoltConnection_id(connection)); char handshake[20]; @@ -689,30 +692,37 @@ int handshake_b(struct BoltConnection* connection, int32_t _1, int32_t _2, int32 } } -struct BoltConnection* BoltConnection_create() +BoltConnection* BoltConnection_create() { - const size_t size = sizeof(struct BoltConnection); - struct BoltConnection* connection = BoltMem_allocate(size); + const size_t size = sizeof(BoltConnection); + BoltConnection* connection = BoltMem_allocate(size); memset(connection, 0, size); + connection->status = BoltStatus_create(ERROR_CTX_SIZE); + connection->metrics = BoltMem_allocate(sizeof(BoltConnectionMetrics)); return connection; } -void BoltConnection_destroy(struct BoltConnection* connection) +void BoltConnection_destroy(BoltConnection* connection) { - BoltMem_deallocate(connection, sizeof(struct BoltConnection)); + if (connection->status!=NULL) { + BoltStatus_destroy(connection->status); + } + if (connection->metrics!=NULL) { + BoltMem_deallocate(connection->metrics, sizeof(BoltConnectionMetrics)); + } + BoltMem_deallocate(connection, sizeof(BoltConnection)); } int -BoltConnection_open(struct BoltConnection* connection, enum BoltTransport transport, struct BoltAddress* address, +BoltConnection_open(BoltConnection* connection, BoltTransport transport, struct BoltAddress* address, struct BoltTrust* trust, struct BoltLog* log, struct BoltSocketOptions* sock_opts) { - if (connection->status!=BOLT_DISCONNECTED) { + if (connection->status!=BOLT_CONNECTION_STATE_DISCONNECTED) { BoltConnection_close(connection); } // Id buffer composed of local&remote Endpoints connection->id = BoltMem_allocate(MAX_ID_LEN); snprintf(connection->id, MAX_ID_LEN, "conn-%" PRId64, BoltUtil_increment(&id_seq)); - connection->error_ctx = (char*) BoltMem_allocate(ERROR_CTX_SIZE); connection->log = log; // Store connection info connection->address = BoltAddress_create(address->host, address->port); @@ -721,14 +731,14 @@ BoltConnection_open(struct BoltConnection* connection, enum BoltTransport transp for (int i = 0; in_resolved_hosts; i++) { const int opened = _open(connection, transport, &address->resolved_hosts[i]); if (opened==BOLT_SUCCESS) { - if (connection->transport==BOLT_SECURE_SOCKET) { + if (connection->transport==BOLT_TRANSPORT_ENCRYPTED) { const int secured = _secure(connection, trust); if (secured==0) { TRY(handshake_b(connection, 3, 2, 1, 0), "BoltConnection_open(%s:%d), handshake_b error code: %d", __FILE__, __LINE__); } else { - return connection->error; + return connection->status->error; } } else { @@ -757,26 +767,24 @@ BoltConnection_open(struct BoltConnection* connection, enum BoltTransport transp BoltLog_info(connection->log, "[%s]: Local endpoint is %s:%s", BoltConnection_id(connection), local_host, local_port); - _set_status(connection, BOLT_CONNECTED, BOLT_SUCCESS); + _set_status(connection, BOLT_CONNECTION_STATE_CONNECTED, BOLT_SUCCESS); break; } } - if (connection->status==BOLT_DISCONNECTED) { - _set_status_with_ctx(connection, BOLT_DEFUNCT, BOLT_NO_VALID_ADDRESS, "BoltConnection_open(%s:%d)", __FILE__, + if (connection->status->state==BOLT_CONNECTION_STATE_DISCONNECTED) { + _set_status_with_ctx(connection, BOLT_CONNECTION_STATE_DEFUNCT, BOLT_NO_VALID_ADDRESS, + "BoltConnection_open(%s:%d)", __FILE__, __LINE__); return -1; } - return connection->status==BOLT_READY ? BOLT_SUCCESS : connection->error; + return connection->status->state==BOLT_CONNECTION_STATE_READY ? BOLT_SUCCESS : connection->status->error; } -void BoltConnection_close(struct BoltConnection* connection) +void BoltConnection_close(BoltConnection* connection) { - if (connection->status!=BOLT_DISCONNECTED) { + if (connection->status->state!=BOLT_CONNECTION_STATE_DISCONNECTED) { _close(connection); } - if (connection->error_ctx!=NULL) { - BoltMem_deallocate(connection->error_ctx, ERROR_CTX_SIZE); - } if (connection->rx_buffer!=NULL) { BoltBuffer_destroy(connection->rx_buffer); connection->rx_buffer = NULL; @@ -803,7 +811,7 @@ void BoltConnection_close(struct BoltConnection* connection) } } -int BoltConnection_send(struct BoltConnection* connection) +int BoltConnection_send(BoltConnection* connection) { int size = BoltBuffer_unloadable(connection->tx_buffer); TRY(_send(connection, BoltBuffer_unload_pointer(connection->tx_buffer, size), size), @@ -812,7 +820,7 @@ int BoltConnection_send(struct BoltConnection* connection) return 0; } -int BoltConnection_receive(struct BoltConnection* connection, char* buffer, int size) +int BoltConnection_receive(BoltConnection* connection, char* buffer, int size) { if (size==0) return 0; int available = BoltBuffer_unloadable(connection->rx_buffer); @@ -837,29 +845,29 @@ int BoltConnection_receive(struct BoltConnection* connection, char* buffer, int return BOLT_SUCCESS; } -int BoltConnection_fetch(struct BoltConnection* connection, bolt_request request) +int BoltConnection_fetch(BoltConnection* connection, BoltRequest request) { const int fetched = connection->protocol->fetch(connection, request); if (fetched==FETCH_SUMMARY) { if (connection->protocol->is_success_summary(connection)) { - _set_status(connection, BOLT_READY, BOLT_SUCCESS); + _set_status(connection, BOLT_CONNECTION_STATE_READY, BOLT_SUCCESS); } else if (connection->protocol->is_ignored_summary(connection)) { // we may need to update status based on an earlier reported FAILURE // which our consumer did not care its result if (connection->protocol->failure(connection)!=NULL) { - _set_status_with_ctx(connection, BOLT_FAILED, BOLT_SERVER_FAILURE, + _set_status_with_ctx(connection, BOLT_CONNECTION_STATE_FAILED, BOLT_SERVER_FAILURE, "BoltConnection_fetch(%s:%d), failure upon ignored message", __FILE__, __LINE__); } } else if (connection->protocol->is_failure_summary(connection)) { - _set_status_with_ctx(connection, BOLT_FAILED, BOLT_SERVER_FAILURE, + _set_status_with_ctx(connection, BOLT_CONNECTION_STATE_FAILED, BOLT_SERVER_FAILURE, "BoltConnection_fetch(%s:%d), failure message", __FILE__, __LINE__); } else { BoltLog_error(connection->log, "[%s]: Protocol violation (received summary code %d)", BoltConnection_id(connection), connection->protocol->last_data_type(connection)); - _set_status_with_ctx(connection, BOLT_DEFUNCT, BOLT_PROTOCOL_VIOLATION, + _set_status_with_ctx(connection, BOLT_CONNECTION_STATE_DEFUNCT, BOLT_PROTOCOL_VIOLATION, "BoltConnection_fetch(%s:%d), received summary code: %d", __FILE__, __LINE__, connection->protocol->last_data_type(connection)); return FETCH_ERROR; @@ -868,7 +876,7 @@ int BoltConnection_fetch(struct BoltConnection* connection, bolt_request request return fetched; } -int BoltConnection_fetch_summary(struct BoltConnection* connection, bolt_request request) +int BoltConnection_fetch_summary(BoltConnection* connection, BoltRequest request) { int records = 0; int data; @@ -883,23 +891,23 @@ int BoltConnection_fetch_summary(struct BoltConnection* connection, bolt_request return records; } -struct BoltValue* BoltConnection_field_values(struct BoltConnection* connection) +struct BoltValue* BoltConnection_field_values(BoltConnection* connection) { return connection->protocol->field_values(connection); } -int BoltConnection_summary_success(struct BoltConnection* connection) +int BoltConnection_summary_success(BoltConnection* connection) { return connection->protocol->is_success_summary(connection); } -int BoltConnection_summary_failure(struct BoltConnection* connection) +int BoltConnection_summary_failure(BoltConnection* connection) { return connection->protocol->is_failure_summary(connection); } int -BoltConnection_init(struct BoltConnection* connection, const char* user_agent, const struct BoltValue* auth_token) +BoltConnection_init(BoltConnection* connection, const char* user_agent, const struct BoltValue* auth_token) { BoltLog_info(connection->log, "[%s]: Initialising connection", BoltConnection_id(connection)); switch (connection->protocol_version) { @@ -909,56 +917,57 @@ BoltConnection_init(struct BoltConnection* connection, const char* user_agent, c int code = connection->protocol->init(connection, user_agent, auth_token); switch (code) { case BOLT_V1_SUCCESS: - _set_status(connection, BOLT_READY, BOLT_SUCCESS); + _set_status(connection, BOLT_CONNECTION_STATE_READY, BOLT_SUCCESS); return 0; case BOLT_V1_FAILURE: - _set_status_with_ctx(connection, BOLT_DEFUNCT, BOLT_PERMISSION_DENIED, + _set_status_with_ctx(connection, BOLT_CONNECTION_STATE_DEFUNCT, BOLT_PERMISSION_DENIED, "BoltConnection_init(%s:%d), failure message", __FILE__, __LINE__); return -1; default: BoltLog_error(connection->log, "[%s]: Protocol violation (received summary code %d)", BoltConnection_id(connection), code); - _set_status_with_ctx(connection, BOLT_DEFUNCT, BOLT_PROTOCOL_VIOLATION, + _set_status_with_ctx(connection, BOLT_CONNECTION_STATE_DEFUNCT, BOLT_PROTOCOL_VIOLATION, "BoltConnection_init(%s:%d), received summary code: %d", __FILE__, __LINE__, code); return -1; } } default: - _set_status_with_ctx(connection, BOLT_DEFUNCT, BOLT_PROTOCOL_UNSUPPORTED, "BoltConnection_init(%s:%d)", + _set_status_with_ctx(connection, BOLT_CONNECTION_STATE_DEFUNCT, BOLT_PROTOCOL_UNSUPPORTED, + "BoltConnection_init(%s:%d)", __FILE__, __LINE__); return -1; } } -int BoltConnection_clear_begin(struct BoltConnection* connection) +int BoltConnection_clear_begin(BoltConnection* connection) { TRY(connection->protocol->clear_begin_tx(connection), "BoltConnection_clear_begin(%s:%d), error code: %d", __FILE__, __LINE__); return BOLT_SUCCESS; } -int BoltConnection_set_begin_bookmarks(struct BoltConnection* connection, struct BoltValue* bookmark_list) +int BoltConnection_set_begin_bookmarks(BoltConnection* connection, struct BoltValue* bookmark_list) { TRY(connection->protocol->set_begin_tx_bookmark(connection, bookmark_list), "BoltConnection_set_begin_bookmarks(%s:%d), error code: %d", __FILE__, __LINE__); return BOLT_SUCCESS; } -int BoltConnection_set_begin_tx_timeout(struct BoltConnection* connection, int64_t timeout) +int BoltConnection_set_begin_tx_timeout(BoltConnection* connection, int64_t timeout) { TRY(connection->protocol->set_begin_tx_timeout(connection, timeout), "BoltConnection_set_begin_tx_timeout(%s:%d), error code: %d", __FILE__, __LINE__); return BOLT_SUCCESS; } -int BoltConnection_set_begin_tx_metadata(struct BoltConnection* connection, struct BoltValue* metadata) +int BoltConnection_set_begin_tx_metadata(BoltConnection* connection, struct BoltValue* metadata) { TRY(connection->protocol->set_begin_tx_metadata(connection, metadata), "BoltConnection_set_begin_tx_metadata(%s:%d), error code: %d", __FILE__, __LINE__); return BOLT_SUCCESS; } -int BoltConnection_load_begin_request(struct BoltConnection* connection) +int BoltConnection_load_begin_request(BoltConnection* connection) { TRY(connection->protocol->load_begin_tx(connection), "BoltConnection_load_begin_request(%s:%d), error code: %d", __FILE__, @@ -966,7 +975,7 @@ int BoltConnection_load_begin_request(struct BoltConnection* connection) return BOLT_SUCCESS; } -int BoltConnection_load_commit_request(struct BoltConnection* connection) +int BoltConnection_load_commit_request(BoltConnection* connection) { TRY(connection->protocol->load_commit_tx(connection), "BoltConnection_load_commit_request(%s:%d), error code: %d", __FILE__, @@ -974,7 +983,7 @@ int BoltConnection_load_commit_request(struct BoltConnection* connection) return BOLT_SUCCESS; } -int BoltConnection_load_rollback_request(struct BoltConnection* connection) +int BoltConnection_load_rollback_request(BoltConnection* connection) { TRY(connection->protocol->load_rollback_tx(connection), "BoltConnection_load_rollback_request(%s:%d), error code: %d", @@ -982,7 +991,7 @@ int BoltConnection_load_rollback_request(struct BoltConnection* connection) return BOLT_SUCCESS; } -int BoltConnection_clear_run(struct BoltConnection* connection) +int BoltConnection_clear_run(BoltConnection* connection) { TRY(connection->protocol->clear_run(connection), "BoltConnection_clear_run(%s:%d), error code: %d", __FILE__, __LINE__); @@ -990,7 +999,7 @@ int BoltConnection_clear_run(struct BoltConnection* connection) } int -BoltConnection_set_run_cypher(struct BoltConnection* connection, const char* cypher, const size_t cypher_size, +BoltConnection_set_run_cypher(BoltConnection* connection, const char* cypher, const size_t cypher_size, int32_t n_parameter) { TRY(connection->protocol->set_run_cypher(connection, cypher, cypher_size, n_parameter), @@ -999,72 +1008,72 @@ BoltConnection_set_run_cypher(struct BoltConnection* connection, const char* cyp } struct BoltValue* -BoltConnection_set_run_cypher_parameter(struct BoltConnection* connection, int32_t index, const char* name, +BoltConnection_set_run_cypher_parameter(BoltConnection* connection, int32_t index, const char* name, size_t name_size) { return connection->protocol->set_run_cypher_parameter(connection, index, name, name_size); } -int BoltConnection_set_run_bookmarks(struct BoltConnection* connection, struct BoltValue* bookmark_list) +int BoltConnection_set_run_bookmarks(BoltConnection* connection, struct BoltValue* bookmark_list) { TRY(connection->protocol->set_run_bookmark(connection, bookmark_list), "BoltConnection_set_run_bookmarks(%s:%d), error code: %d", __FILE__, __LINE__); return BOLT_SUCCESS; } -int BoltConnection_set_run_tx_timeout(struct BoltConnection* connection, int64_t timeout) +int BoltConnection_set_run_tx_timeout(BoltConnection* connection, int64_t timeout) { TRY(connection->protocol->set_run_tx_timeout(connection, timeout), "BoltConnection_set_run_tx_timeout(%s:%d), error code: %d", __FILE__, __LINE__); return BOLT_SUCCESS; } -int BoltConnection_set_run_tx_metadata(struct BoltConnection* connection, struct BoltValue* metadata) +int BoltConnection_set_run_tx_metadata(BoltConnection* connection, struct BoltValue* metadata) { TRY(connection->protocol->set_run_tx_metadata(connection, metadata), "BoltConnection_set_run_tx_metadata(%s:%d), error code: %d", __FILE__, __LINE__); return BOLT_SUCCESS; } -int BoltConnection_load_run_request(struct BoltConnection* connection) +int BoltConnection_load_run_request(BoltConnection* connection) { TRY(connection->protocol->load_run(connection), "BoltConnection_load_run_request(%s:%d), error code: %d", __FILE__, __LINE__); return BOLT_SUCCESS; } -int BoltConnection_load_discard_request(struct BoltConnection* connection, int32_t n) +int BoltConnection_load_discard_request(BoltConnection* connection, int32_t n) { TRY(connection->protocol->load_discard(connection, n), "BoltConnection_load_discard_request(%s:%d), error code: %d", __FILE__, __LINE__); return BOLT_SUCCESS; } -int BoltConnection_load_pull_request(struct BoltConnection* connection, int32_t n) +int BoltConnection_load_pull_request(BoltConnection* connection, int32_t n) { TRY(connection->protocol->load_pull(connection, n), "BoltConnection_load_pull_request(%s:%d), error code: %d", __FILE__, __LINE__); return BOLT_SUCCESS; } -int BoltConnection_load_reset_request(struct BoltConnection* connection) +int BoltConnection_load_reset_request(BoltConnection* connection) { TRY(connection->protocol->load_reset(connection), "BoltConnection_load_reset_request(%s:%d), error code: %d", __FILE__, __LINE__); return BOLT_SUCCESS; } -bolt_request BoltConnection_last_request(struct BoltConnection* connection) +BoltRequest BoltConnection_last_request(BoltConnection* connection) { return connection->protocol->last_request(connection); } -char* BoltConnection_server(struct BoltConnection* connection) +char* BoltConnection_server(BoltConnection* connection) { return connection->protocol->server(connection); } -char* BoltConnection_id(struct BoltConnection* connection) +char* BoltConnection_id(BoltConnection* connection) { if (connection->protocol!=NULL && connection->protocol->id!=NULL) { return connection->protocol->id(connection); @@ -1073,22 +1082,42 @@ char* BoltConnection_id(struct BoltConnection* connection) return connection->id; } -char* BoltConnection_last_bookmark(struct BoltConnection* connection) +const BoltAddress* BoltConnection_address(BoltConnection* connection) +{ + return connection->address; +} + +const BoltAddress* BoltConnection_remote_endpoint(BoltConnection* connection) +{ + return connection->resolved_address; +} + +const BoltAddress* BoltConnection_local_endpoint(BoltConnection* connection) +{ + return connection->local_address; +} + +char* BoltConnection_last_bookmark(BoltConnection* connection) { return connection->protocol->last_bookmark(connection); } -struct BoltValue* BoltConnection_field_names(struct BoltConnection* connection) +struct BoltValue* BoltConnection_field_names(BoltConnection* connection) { return connection->protocol->field_names(connection); } -struct BoltValue* BoltConnection_metadata(struct BoltConnection* connection) +struct BoltValue* BoltConnection_metadata(BoltConnection* connection) { return connection->protocol->metadata(connection); } -struct BoltValue* BoltConnection_failure(struct BoltConnection* connection) +struct BoltValue* BoltConnection_failure(BoltConnection* connection) { return connection->protocol->failure(connection); } + +BoltStatus* BoltConnection_status(BoltConnection* connection) +{ + return connection->status; +} diff --git a/src/seabolt/src/connections.h b/src/seabolt/src/connection.h similarity index 56% rename from src/seabolt/src/connections.h rename to src/seabolt/src/connection.h index 7cd92be3..48bab36f 100644 --- a/src/seabolt/src/connections.h +++ b/src/seabolt/src/connection.h @@ -21,140 +21,33 @@ * @file */ -#ifndef SEABOLT_CONNECT -#define SEABOLT_CONNECT - -#include -#include -#include +#ifndef SEABOLT_CONNECTION +#define SEABOLT_CONNECTION +#include "bolt-public.h" #include "address.h" #include "config.h" -#include "values.h" - -typedef uint64_t bolt_request; - -/** - * - */ -enum BoltTransport { - BOLT_SOCKET = 0, - BOLT_SECURE_SOCKET = 1, -}; - -/** - * - */ -enum BoltConnectionStatus { - BOLT_DISCONNECTED = 0, // not connected - BOLT_CONNECTED = 1, // connected but not authenticated - BOLT_READY = 2, // connected and authenticated - BOLT_FAILED = 3, // recoverable failure - BOLT_DEFUNCT = 4, // unrecoverable failure -}; - -struct BoltConnection; - -struct BoltProtocol; +#include "status.h" -struct BoltTrust; - -typedef void (* error_action_func)(struct BoltConnection*, void*); - -/** - * Record of connection usage statistics. - */ -struct BoltConnectionMetrics { - struct timespec time_opened; - struct timespec time_closed; - unsigned long long bytes_sent; - unsigned long long bytes_received; -}; - -/** - * Socket options - */ -struct BoltSocketOptions { - int connect_timeout; - int recv_timeout; - int send_timeout; - int keepalive; -}; +typedef uint64_t BoltRequest; /** * A Bolt client-server connection instance. * */ -struct BoltConnection { - /// The agent currently responsible for using this connection - const void* agent; - - /// Transport type for this connection - enum BoltTransport transport; - - /// Socket options - const struct BoltSocketOptions* sock_opts; - - const struct BoltAddress* address; - const struct BoltAddress* local_address; - const struct BoltAddress* resolved_address; - char* id; - - const struct BoltLog* log; - - int owns_ssl_context; - /// The security context (secure connections only) - struct ssl_ctx_st* ssl_context; - /// A secure socket wrapper (secure connections only) - struct ssl_st* ssl; - /// The raw socket that backs this connection - int socket; - - /// The protocol version used for this connection - int32_t protocol_version; - /// State required by the protocol - struct BoltProtocol* protocol; - - // These buffers contain data exactly as it is transmitted or - // received. Therefore for Bolt v1, chunk headers are included - // in these buffers - - /// Transmit buffer - struct BoltBuffer* tx_buffer; - /// Receive buffer - struct BoltBuffer* rx_buffer; - - /// Connection metrics - struct BoltConnectionMetrics metrics; - /// Current status of the connection - enum BoltConnectionStatus status; - /// Current connection error code - int error; - /// Additional context info about error - char* error_ctx; - - error_action_func on_error_cb; - void* on_error_cb_state; -}; - -struct BoltTrust { - char* certs; - int32_t certs_len; - int skip_verify; - int skip_verify_hostname; -}; +typedef struct BoltConnection BoltConnection; /** * Create a new connection. * * @return */ -SEABOLT_EXPORT struct BoltConnection* BoltConnection_create(); +SEABOLT_EXPORT BoltConnection* BoltConnection_create(); /** * Destroy a connection. */ -SEABOLT_EXPORT void BoltConnection_destroy(struct BoltConnection* connection); +SEABOLT_EXPORT void BoltConnection_destroy(BoltConnection* connection); /** * Open a connection to a Bolt server. @@ -192,15 +85,15 @@ SEABOLT_EXPORT void BoltConnection_destroy(struct BoltConnection* connection); * @param address descriptor of the remote Bolt server address * @return 0 if the connection was opened successfully, -1 otherwise */ -SEABOLT_EXPORT int BoltConnection_open(struct BoltConnection* connection, enum BoltTransport transport, - struct BoltAddress* address, struct BoltTrust* trust, struct BoltLog* log, struct BoltSocketOptions* sock_opts); +SEABOLT_EXPORT int BoltConnection_open(BoltConnection* connection, BoltTransport transport, + BoltAddress* address, struct BoltTrust* trust, struct BoltLog* log, struct BoltSocketOptions* sock_opts); /** * Close a connection. * * @param connection */ -SEABOLT_EXPORT void BoltConnection_close(struct BoltConnection* connection); +SEABOLT_EXPORT void BoltConnection_close(BoltConnection* connection); /** * Initialise the connection and authenticate using the basic @@ -212,7 +105,7 @@ SEABOLT_EXPORT void BoltConnection_close(struct BoltConnection* connection); * @return */ SEABOLT_EXPORT int -BoltConnection_init(struct BoltConnection* connection, const char* user_agent, const struct BoltValue* auth_token); +BoltConnection_init(BoltConnection* connection, const char* user_agent, const BoltValue* auth_token); /** * Send all queued requests. @@ -220,7 +113,7 @@ BoltConnection_init(struct BoltConnection* connection, const char* user_agent, c * @param connection * @return the latest request ID */ -SEABOLT_EXPORT int BoltConnection_send(struct BoltConnection* connection); +SEABOLT_EXPORT int BoltConnection_send(BoltConnection* connection); /** * Take an exact amount of data from the receive buffer, deferring to @@ -231,7 +124,7 @@ SEABOLT_EXPORT int BoltConnection_send(struct BoltConnection* connection); * @param size * @return */ -int BoltConnection_receive(struct BoltConnection* connection, char* buffer, int size); +int BoltConnection_receive(BoltConnection* connection, char* buffer, int size); /** * Fetch the next value from the result stream for a given request. @@ -254,7 +147,7 @@ int BoltConnection_receive(struct BoltConnection* connection, char* buffer, int * -1 if an error occurs * */ -SEABOLT_EXPORT int BoltConnection_fetch(struct BoltConnection* connection, bolt_request request); +SEABOLT_EXPORT int BoltConnection_fetch(BoltConnection* connection, BoltRequest request); /** * Fetch values from the result stream for a given request, up to and @@ -274,7 +167,7 @@ SEABOLT_EXPORT int BoltConnection_fetch(struct BoltConnection* connection, bolt_ * @return >=0 the number of records discarded from this result * -1 if an error occurs */ -SEABOLT_EXPORT int BoltConnection_fetch_summary(struct BoltConnection* connection, bolt_request request); +SEABOLT_EXPORT int BoltConnection_fetch_summary(BoltConnection* connection, BoltRequest request); /** * Load a transaction BEGIN request into the request queue. @@ -282,14 +175,13 @@ SEABOLT_EXPORT int BoltConnection_fetch_summary(struct BoltConnection* connectio * @param connection * @return */ -SEABOLT_EXPORT int BoltConnection_clear_begin(struct BoltConnection* connection); +SEABOLT_EXPORT int BoltConnection_clear_begin(BoltConnection* connection); -SEABOLT_EXPORT int -BoltConnection_set_begin_bookmarks(struct BoltConnection* connection, struct BoltValue* bookmark_list); +SEABOLT_EXPORT int BoltConnection_set_begin_bookmarks(BoltConnection* connection, BoltValue* bookmark_list); -SEABOLT_EXPORT int BoltConnection_set_begin_tx_timeout(struct BoltConnection* connection, int64_t timeout); +SEABOLT_EXPORT int BoltConnection_set_begin_tx_timeout(BoltConnection* connection, int64_t timeout); -SEABOLT_EXPORT int BoltConnection_set_begin_tx_metadata(struct BoltConnection* connection, struct BoltValue* metadata); +SEABOLT_EXPORT int BoltConnection_set_begin_tx_metadata(BoltConnection* connection, BoltValue* metadata); /** * Load a transaction BEGIN request into the request queue. @@ -297,7 +189,7 @@ SEABOLT_EXPORT int BoltConnection_set_begin_tx_metadata(struct BoltConnection* c * @param connection * @return */ -SEABOLT_EXPORT int BoltConnection_load_begin_request(struct BoltConnection* connection); +SEABOLT_EXPORT int BoltConnection_load_begin_request(BoltConnection* connection); /** * Load a transaction COMMIT request into the request queue. @@ -305,7 +197,7 @@ SEABOLT_EXPORT int BoltConnection_load_begin_request(struct BoltConnection* conn * @param connection * @return */ -SEABOLT_EXPORT int BoltConnection_load_commit_request(struct BoltConnection* connection); +SEABOLT_EXPORT int BoltConnection_load_commit_request(BoltConnection* connection); /** * Load a transaction ROLLBACK request into the request queue. @@ -313,7 +205,7 @@ SEABOLT_EXPORT int BoltConnection_load_commit_request(struct BoltConnection* con * @param connection * @return */ -SEABOLT_EXPORT int BoltConnection_load_rollback_request(struct BoltConnection* connection); +SEABOLT_EXPORT int BoltConnection_load_rollback_request(BoltConnection* connection); /** * Load a RUN request into the request queue. @@ -321,23 +213,23 @@ SEABOLT_EXPORT int BoltConnection_load_rollback_request(struct BoltConnection* c * @param connection * @return */ -SEABOLT_EXPORT int BoltConnection_clear_run(struct BoltConnection* connection); +SEABOLT_EXPORT int BoltConnection_clear_run(BoltConnection* connection); -SEABOLT_EXPORT int BoltConnection_set_run_bookmarks(struct BoltConnection* connection, struct BoltValue* bookmark_list); +SEABOLT_EXPORT int BoltConnection_set_run_bookmarks(BoltConnection* connection, BoltValue* bookmark_list); -SEABOLT_EXPORT int BoltConnection_set_run_tx_timeout(struct BoltConnection* connection, int64_t timeout); +SEABOLT_EXPORT int BoltConnection_set_run_tx_timeout(BoltConnection* connection, int64_t timeout); -SEABOLT_EXPORT int BoltConnection_set_run_tx_metadata(struct BoltConnection* connection, struct BoltValue* metadata); +SEABOLT_EXPORT int BoltConnection_set_run_tx_metadata(BoltConnection* connection, BoltValue* metadata); SEABOLT_EXPORT int -BoltConnection_set_run_cypher(struct BoltConnection* connection, const char* cypher, const size_t cypher_size, +BoltConnection_set_run_cypher(BoltConnection* connection, const char* cypher, const size_t cypher_size, int32_t n_parameter); -SEABOLT_EXPORT struct BoltValue* -BoltConnection_set_run_cypher_parameter(struct BoltConnection* connection, int32_t index, const char* name, +SEABOLT_EXPORT BoltValue* +BoltConnection_set_run_cypher_parameter(BoltConnection* connection, int32_t index, const char* name, size_t name_size); -SEABOLT_EXPORT int BoltConnection_load_run_request(struct BoltConnection* connection); +SEABOLT_EXPORT int BoltConnection_load_run_request(BoltConnection* connection); /** * Load a DISCARD_ALL request into the request queue. @@ -346,7 +238,7 @@ SEABOLT_EXPORT int BoltConnection_load_run_request(struct BoltConnection* connec * @param n should always be -1 * @return */ -SEABOLT_EXPORT int BoltConnection_load_discard_request(struct BoltConnection* connection, int32_t n); +SEABOLT_EXPORT int BoltConnection_load_discard_request(BoltConnection* connection, int32_t n); /** * Load a PULL_ALL request into the request queue. @@ -355,7 +247,7 @@ SEABOLT_EXPORT int BoltConnection_load_discard_request(struct BoltConnection* co * @param n should always be -1 * @return */ -SEABOLT_EXPORT int BoltConnection_load_pull_request(struct BoltConnection* connection, int32_t n); +SEABOLT_EXPORT int BoltConnection_load_pull_request(BoltConnection* connection, int32_t n); /** * Load a RESET request into the request queue. @@ -367,7 +259,7 @@ SEABOLT_EXPORT int BoltConnection_load_pull_request(struct BoltConnection* conne * @param connection * @return */ -SEABOLT_EXPORT int BoltConnection_load_reset_request(struct BoltConnection* connection); +SEABOLT_EXPORT int BoltConnection_load_reset_request(BoltConnection* connection); /** * Obtain a handle to the last request sent to the server. This handle @@ -376,11 +268,17 @@ SEABOLT_EXPORT int BoltConnection_load_reset_request(struct BoltConnection* conn * @param connection * @return */ -SEABOLT_EXPORT bolt_request BoltConnection_last_request(struct BoltConnection* connection); +SEABOLT_EXPORT BoltRequest BoltConnection_last_request(BoltConnection* connection); + +SEABOLT_EXPORT char* BoltConnection_server(BoltConnection* connection); -SEABOLT_EXPORT char* BoltConnection_server(struct BoltConnection* connection); +SEABOLT_EXPORT char* BoltConnection_id(BoltConnection* connection); -SEABOLT_EXPORT char* BoltConnection_id(struct BoltConnection* connection); +SEABOLT_EXPORT const BoltAddress* BoltConnection_address(BoltConnection* connection); + +SEABOLT_EXPORT const BoltAddress* BoltConnection_remote_endpoint(BoltConnection* connection); + +SEABOLT_EXPORT const BoltAddress* BoltConnection_local_endpoint(BoltConnection* connection); /** * Obtain the latest bookmark sent by the server. This may return null if @@ -392,7 +290,7 @@ SEABOLT_EXPORT char* BoltConnection_id(struct BoltConnection* connection); * @param connection * @return */ -SEABOLT_EXPORT char* BoltConnection_last_bookmark(struct BoltConnection* connection); +SEABOLT_EXPORT char* BoltConnection_last_bookmark(BoltConnection* connection); /** @@ -400,7 +298,7 @@ SEABOLT_EXPORT char* BoltConnection_last_bookmark(struct BoltConnection* connect * @param connection * @return */ -SEABOLT_EXPORT int BoltConnection_summary_success(struct BoltConnection* connection); +SEABOLT_EXPORT int BoltConnection_summary_success(BoltConnection* connection); /** * Obtain the details of the latest server generated FAILURE message @@ -408,7 +306,7 @@ SEABOLT_EXPORT int BoltConnection_summary_success(struct BoltConnection* connect * @param connection * @return */ -SEABOLT_EXPORT struct BoltValue* BoltConnection_failure(struct BoltConnection* connection); +SEABOLT_EXPORT BoltValue* BoltConnection_failure(BoltConnection* connection); /** * Return the fields available in the current result. @@ -416,7 +314,7 @@ SEABOLT_EXPORT struct BoltValue* BoltConnection_failure(struct BoltConnection* c * @param connection * @return */ -SEABOLT_EXPORT struct BoltValue* BoltConnection_field_names(struct BoltConnection* connection); +SEABOLT_EXPORT BoltValue* BoltConnection_field_names(BoltConnection* connection); /** * Obtain a value from the current record. @@ -425,7 +323,7 @@ SEABOLT_EXPORT struct BoltValue* BoltConnection_field_names(struct BoltConnectio * @param field * @return pointer to a `BoltValue` data structure formatted as a BOLT_LIST */ -SEABOLT_EXPORT struct BoltValue* BoltConnection_field_values(struct BoltConnection* connection); +SEABOLT_EXPORT BoltValue* BoltConnection_field_values(BoltConnection* connection); /** * Returns the metadata sent by the server. @@ -433,6 +331,8 @@ SEABOLT_EXPORT struct BoltValue* BoltConnection_field_values(struct BoltConnecti * @param connection * @return */ -SEABOLT_EXPORT struct BoltValue* BoltConnection_metadata(struct BoltConnection* connection); +SEABOLT_EXPORT BoltValue* BoltConnection_metadata(BoltConnection* connection); + +SEABOLT_EXPORT BoltStatus* BoltConnection_status(BoltConnection* connection); -#endif // SEABOLT_CONNECT +#endif // SEABOLT_CONNECTION diff --git a/src/seabolt/src/connector-private.h b/src/seabolt/src/connector-private.h new file mode 100644 index 00000000..1537cd3a --- /dev/null +++ b/src/seabolt/src/connector-private.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2002-2018 "Neo4j," + * Neo4j Sweden AB [http://neo4j.com] + * + * This file is part of Neo4j. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef SEABOLT_CONNECTOR_PRIVATE_H +#define SEABOLT_CONNECTOR_PRIVATE_H + +#include "connector.h" + +struct BoltConnector { + const struct BoltAddress* address; + const struct BoltValue* auth_token; + const struct BoltConfig* config; + + void* pool_state; +}; + +#endif //SEABOLT_CONNECTOR_PRIVATE_H diff --git a/src/seabolt/src/connector.c b/src/seabolt/src/connector.c index e25c3048..732cd138 100644 --- a/src/seabolt/src/connector.c +++ b/src/seabolt/src/connector.c @@ -17,89 +17,39 @@ * limitations under the License. */ -#include "config-impl.h" -#include "connector.h" -#include "logging.h" -#include "mem.h" -#include "tls.h" +#include "bolt-private.h" + +#include "address-private.h" +#include "config-private.h" +#include "connector-private.h" #include "direct-pool.h" +#include "mem.h" #include "routing-pool.h" -#define SIZE_OF_CONNECTOR sizeof(struct BoltConnector) -#define SIZE_OF_CONFIG sizeof(struct BoltConfig) -#define SIZE_OF_TRUST sizeof(struct BoltTrust) -#define SIZE_OF_LOG sizeof(struct BoltLog) -#define SIZE_OF_SOCK_OPTS sizeof(struct BoltSocketOptions) -#define SIZE_OF_ADDRESS_RESOLVER sizeof(struct BoltAddressResolver) - -struct BoltConfig* BoltConfig_clone(struct BoltConfig* config) +BoltConfig* BoltConnector_apply_defaults(BoltConfig* config) { - struct BoltConfig* clone = (struct BoltConfig*) BoltMem_allocate(SIZE_OF_CONFIG); - clone->mode = config->mode; - clone->transport = config->transport; - clone->trust = (struct BoltTrust*) BoltMem_allocate(SIZE_OF_TRUST); - if (config->trust!=NULL) { - clone->trust->certs = (char*) BoltMem_duplicate(config->trust->certs, config->trust->certs_len); - clone->trust->certs_len = config->trust->certs_len; - clone->trust->skip_verify = config->trust->skip_verify; - clone->trust->skip_verify_hostname = config->trust->skip_verify_hostname; - } - else { - // by default we trust any certificate presented but perform hostname verification - clone->trust->certs = NULL; - clone->trust->certs_len = 0; - clone->trust->skip_verify = 1; - clone->trust->skip_verify_hostname = 0; + if (config->trust==NULL) { + config->trust = BoltTrust_create(); } - clone->user_agent = (char*) BoltMem_duplicate(config->user_agent, SIZE_OF_C_STRING(config->user_agent)); - clone->routing_context = BoltValue_duplicate(config->routing_context); - clone->max_pool_size = config->max_pool_size; - clone->max_connection_lifetime = config->max_connection_lifetime; - clone->max_connection_acquire_time = config->max_connection_acquire_time; - clone->sock_opts = (struct BoltSocketOptions*) BoltMem_allocate(SIZE_OF_SOCK_OPTS); - if (config->sock_opts!=NULL) { - clone->sock_opts->connect_timeout = config->sock_opts->connect_timeout; - clone->sock_opts->recv_timeout = config->sock_opts->recv_timeout; - clone->sock_opts->send_timeout = config->sock_opts->send_timeout; - clone->sock_opts->keepalive = config->sock_opts->keepalive; + if (config->socket_options==NULL) { + config->socket_options = BoltSocketOptions_create(); } - else { - clone->sock_opts->connect_timeout = 5000; - clone->sock_opts->recv_timeout = 0; - clone->sock_opts->send_timeout = 0; - clone->sock_opts->keepalive = 1; - } - clone->log = (struct BoltLog*) BoltMem_duplicate(config->log, SIZE_OF_LOG); - clone->address_resolver = (struct BoltAddressResolver*) BoltMem_duplicate(config->address_resolver, - SIZE_OF_ADDRESS_RESOLVER); - return clone; -} - -void BoltConfig_destroy(struct BoltConfig* config) -{ - BoltMem_deallocate(config->trust->certs, config->trust->certs_len); - BoltMem_deallocate(config->trust, SIZE_OF_TRUST); - BoltMem_deallocate(config->sock_opts, SIZE_OF_SOCK_OPTS); - BoltMem_deallocate(config->user_agent, SIZE_OF_C_STRING(config->user_agent)); - BoltValue_destroy(config->routing_context); - BoltLog_destroy(config->log); - BoltAddressResolver_destroy(config->address_resolver); - BoltMem_deallocate(config, SIZE_OF_CONFIG); + return config; } -struct BoltConnector* -BoltConnector_create(struct BoltAddress* address, struct BoltValue* auth_token, struct BoltConfig* config) +BoltConnector* +BoltConnector_create(BoltAddress* address, BoltValue* auth_token, struct BoltConfig* config) { - struct BoltConnector* connector = (struct BoltConnector*) BoltMem_allocate(SIZE_OF_CONNECTOR); + BoltConnector* connector = (BoltConnector*) BoltMem_allocate(sizeof(BoltConnector)); connector->address = BoltAddress_create(address->host, address->port); connector->auth_token = BoltValue_duplicate(auth_token); - connector->config = BoltConfig_clone(config); + connector->config = BoltConnector_apply_defaults(BoltConfig_clone(config)); switch (connector->config->mode) { - case BOLT_DIRECT: + case BOLT_MODE_DIRECT: connector->pool_state = BoltDirectPool_create(address, connector->auth_token, connector->config); break; - case BOLT_ROUTING: + case BOLT_MODE_ROUTING: connector->pool_state = BoltRoutingPool_create(address, connector->auth_token, connector->config); break; default: @@ -110,45 +60,45 @@ BoltConnector_create(struct BoltAddress* address, struct BoltValue* auth_token, return connector; } -void BoltConnector_destroy(struct BoltConnector* connector) +void BoltConnector_destroy(BoltConnector* connector) { switch (connector->config->mode) { - case BOLT_DIRECT: + case BOLT_MODE_DIRECT: BoltDirectPool_destroy((struct BoltDirectPool*) connector->pool_state); break; - case BOLT_ROUTING: + case BOLT_MODE_ROUTING: BoltRoutingPool_destroy((struct BoltRoutingPool*) connector->pool_state); break; } BoltConfig_destroy((struct BoltConfig*) connector->config); - BoltAddress_destroy((struct BoltAddress*) connector->address); - BoltValue_destroy((struct BoltValue*) connector->auth_token); - BoltMem_deallocate(connector, SIZE_OF_CONNECTOR); + BoltAddress_destroy((BoltAddress*) connector->address); + BoltValue_destroy((BoltValue*) connector->auth_token); + BoltMem_deallocate(connector, sizeof(BoltConnector)); } -struct BoltConnectionResult BoltConnector_acquire(struct BoltConnector* connector, enum BoltAccessMode mode) +struct BoltConnectionResult BoltConnector_acquire(BoltConnector* connector, BoltAccessMode mode) { switch (connector->config->mode) { - case BOLT_DIRECT: + case BOLT_MODE_DIRECT: return BoltDirectPool_acquire((struct BoltDirectPool*) connector->pool_state); - case BOLT_ROUTING: + case BOLT_MODE_ROUTING: return BoltRoutingPool_acquire((struct BoltRoutingPool*) connector->pool_state, mode); } struct BoltConnectionResult result = { - NULL, BOLT_DISCONNECTED, BOLT_UNSUPPORTED, NULL + NULL, BOLT_CONNECTION_STATE_DISCONNECTED, BOLT_UNSUPPORTED, NULL }; return result; } -void BoltConnector_release(struct BoltConnector* connector, struct BoltConnection* connection) +void BoltConnector_release(BoltConnector* connector, BoltConnection* connection) { switch (connector->config->mode) { - case BOLT_DIRECT: + case BOLT_MODE_DIRECT: BoltDirectPool_release((struct BoltDirectPool*) connector->pool_state, connection); break; - case BOLT_ROUTING: + case BOLT_MODE_ROUTING: BoltRoutingPool_release((struct BoltRoutingPool*) connector->pool_state, connection); break; } diff --git a/src/seabolt/src/connector.h b/src/seabolt/src/connector.h index 73370e60..708cec9e 100644 --- a/src/seabolt/src/connector.h +++ b/src/seabolt/src/connector.h @@ -19,55 +19,31 @@ #ifndef SEABOLT_ALL_CONNECTOR_H #define SEABOLT_ALL_CONNECTOR_H -#include "logging.h" -#include "connections.h" +#include "bolt-public.h" +#include "log.h" +#include "connection.h" #include "address-resolver.h" -enum BoltConnectorMode { - BOLT_DIRECT = 0, - BOLT_ROUTING = 1 -}; - -enum BoltAccessMode { - BOLT_ACCESS_MODE_READ = 1, - BOLT_ACCESS_MODE_WRITE = 2 -}; - -struct BoltConfig { - enum BoltConnectorMode mode; - enum BoltTransport transport; - struct BoltTrust* trust; - char* user_agent; - struct BoltValue* routing_context; - struct BoltAddressResolver* address_resolver; - struct BoltLog* log; - int max_pool_size; - int max_connection_lifetime; - int max_connection_acquire_time; - struct BoltSocketOptions* sock_opts; -}; - -struct BoltConnector { - const struct BoltAddress* address; - const struct BoltValue* auth_token; - const struct BoltConfig* config; - void* pool_state; -}; +typedef int BoltAccessMode; +#define BOLT_ACCESS_MODE_WRITE 0 +#define BOLT_ACCESS_MODE_READ 1 struct BoltConnectionResult { - struct BoltConnection* connection; - enum BoltConnectionStatus connection_status; + BoltConnection* connection; + BoltConnectionState connection_status; int connection_error; char* connection_error_ctx; }; -SEABOLT_EXPORT struct BoltConnector* +typedef struct BoltConnector BoltConnector; + +SEABOLT_EXPORT BoltConnector* BoltConnector_create(struct BoltAddress* address, struct BoltValue* auth_token, struct BoltConfig* config); -SEABOLT_EXPORT void BoltConnector_destroy(struct BoltConnector* connector); +SEABOLT_EXPORT void BoltConnector_destroy(BoltConnector* connector); -SEABOLT_EXPORT struct BoltConnectionResult BoltConnector_acquire(struct BoltConnector* connector, enum BoltAccessMode mode); +SEABOLT_EXPORT struct BoltConnectionResult BoltConnector_acquire(BoltConnector* connector, BoltAccessMode mode); -SEABOLT_EXPORT void BoltConnector_release(struct BoltConnector* connector, struct BoltConnection* connection); +SEABOLT_EXPORT void BoltConnector_release(BoltConnector* connector, struct BoltConnection* connection); #endif //SEABOLT_ALL_CONNECTOR_H diff --git a/src/seabolt/src/direct-pool.c b/src/seabolt/src/direct-pool.c index 1dce42e7..439ce751 100644 --- a/src/seabolt/src/direct-pool.c +++ b/src/seabolt/src/direct-pool.c @@ -17,14 +17,16 @@ * limitations under the License. */ -#include "config-impl.h" -#include "connections.h" -#include "logging.h" +#include "bolt-private.h" +#include "address-private.h" +#include "config-private.h" +#include "connection-private.h" +#include "direct-pool.h" +#include "log-private.h" #include "mem.h" +#include "protocol.h" #include "tls.h" #include "utils.h" -#include "direct-pool.h" -#include "protocol.h" #define MAX_ID_LEN 16 @@ -32,13 +34,13 @@ static int64_t pool_seq = 0; void close_pool_entry(struct BoltDirectPool* pool, int index) { - struct BoltConnection* connection = &pool->connections[index]; - if (connection->status!=BOLT_DISCONNECTED) { - if (connection->metrics.time_opened.tv_sec!=0 || connection->metrics.time_opened.tv_nsec!=0) { + struct BoltConnection* connection = pool->connections[index]; + if (connection->status->state!=BOLT_CONNECTION_STATE_DISCONNECTED) { + if (connection->metrics->time_opened.tv_sec!=0 || connection->metrics->time_opened.tv_nsec!=0) { struct timespec now; struct timespec diff; BoltUtil_get_time(&now); - BoltUtil_diff_time(&diff, &now, &connection->metrics.time_opened); + BoltUtil_diff_time(&diff, &now, &connection->metrics->time_opened); BoltLog_info(pool->config->log, "[%s]: Connection alive for %lds %09ldns", BoltConnection_id(connection), (long) (diff.tv_sec), diff.tv_nsec); } @@ -50,18 +52,19 @@ void close_pool_entry(struct BoltDirectPool* pool, int index) int find_unused_connection(struct BoltDirectPool* pool) { for (int i = 0; isize; i++) { - struct BoltConnection* connection = &pool->connections[i]; + struct BoltConnection* connection = pool->connections[i]; if (connection->agent==NULL) { // check for max lifetime and close existing connection if required - if (connection->status!=BOLT_DISCONNECTED && connection->status!=BOLT_DEFUNCT) { - if (pool->config->max_connection_lifetime>0) { + if (connection->status->state!=BOLT_CONNECTION_STATE_DISCONNECTED + && connection->status->state!=BOLT_CONNECTION_STATE_DEFUNCT) { + if (pool->config->max_connection_life_time>0) { int64_t - now = BoltUtil_get_time_ms(); + now = BoltUtil_get_time_ms(); int64_t - created = BoltUtil_get_time_ms_from(&connection->metrics.time_opened); + created = BoltUtil_get_time_ms_from(&connection->metrics->time_opened); - if (now-created>pool->config->max_connection_lifetime) { + if (now-created>pool->config->max_connection_life_time) { BoltLog_info(pool->config->log, "[%s]: Connection reached its maximum lifetime, force closing.", BoltConnection_id(connection)); @@ -79,7 +82,7 @@ int find_unused_connection(struct BoltDirectPool* pool) int find_connection(struct BoltDirectPool* pool, struct BoltConnection* connection) { for (int i = 0; isize; i++) { - struct BoltConnection* candidate = &pool->connections[i]; + struct BoltConnection* candidate = pool->connections[i]; if (candidate==connection) { return i; } @@ -89,7 +92,7 @@ int find_connection(struct BoltDirectPool* pool, struct BoltConnection* connecti int init(struct BoltDirectPool* pool, int index) { - struct BoltConnection* connection = &pool->connections[index]; + struct BoltConnection* connection = pool->connections[index]; switch (BoltConnection_init(connection, pool->config->user_agent, pool->auth_token)) { case 0: return BOLT_SUCCESS; @@ -100,11 +103,10 @@ int init(struct BoltDirectPool* pool, int index) int reset(struct BoltDirectPool* pool, int index) { - struct BoltConnection* connection = &pool->connections[index]; + struct BoltConnection* connection = pool->connections[index]; switch (BoltConnection_load_reset_request(connection)) { case 0: { - bolt_request - request_id = BoltConnection_last_request(connection); + BoltRequest request_id = BoltConnection_last_request(connection); if (BoltConnection_send(connection)<0) { return -1; } @@ -129,15 +131,15 @@ int open_init(struct BoltDirectPool* pool, int index) // Host name resolution is carried out every time a connection // is opened. Given that connections are pooled and reused, // this is not a huge overhead. - switch (BoltAddress_resolve(pool->address, pool->config->log)) { + switch (BoltAddress_resolve(pool->address, NULL, pool->config->log)) { case 0: break; default: return BOLT_ADDRESS_NOT_RESOLVED; // Could not resolve address } - struct BoltConnection* connection = &pool->connections[index]; + struct BoltConnection* connection = pool->connections[index]; switch (BoltConnection_open(connection, pool->config->transport, pool->address, pool->config->trust, - pool->config->log, pool->config->sock_opts)) { + pool->config->log, pool->config->socket_options)) { case 0: return init(pool, index); default: @@ -178,15 +180,17 @@ struct BoltDirectPool* BoltDirectPool_create(const struct BoltAddress* address, pool->address = BoltAddress_create(address->host, address->port); pool->auth_token = auth_token; pool->size = config->max_pool_size; - pool->connections = (struct BoltConnection*) BoltMem_allocate(config->max_pool_size*sizeof(struct BoltConnection)); - memset(pool->connections, 0, config->max_pool_size*sizeof(struct BoltConnection)); - if (config->transport==BOLT_SECURE_SOCKET) { + pool->connections = (struct BoltConnection**) BoltMem_allocate(config->max_pool_size*sizeof(BoltConnection*)); + for (int i = 0; imax_pool_size; i++) { + pool->connections[i] = BoltConnection_create(); + } + if (config->transport==BOLT_TRANSPORT_ENCRYPTED) { pool->ssl_context = create_ssl_ctx(config->trust, address->host, config->log, id); // assign ssl_context to all connections for (int i = 0; imax_pool_size; i++) { - pool->connections[0].ssl_context = pool->ssl_context; - pool->connections[0].owns_ssl_context = 0; + pool->connections[0]->ssl_context = pool->ssl_context; + pool->connections[0]->owns_ssl_context = 0; } } else { @@ -201,8 +205,9 @@ void BoltDirectPool_destroy(struct BoltDirectPool* pool) pool->address->port); for (int index = 0; indexsize; index++) { close_pool_entry(pool, index); + BoltConnection_destroy(pool->connections[index]); } - BoltMem_deallocate(pool->connections, pool->size*sizeof(struct BoltConnection)); + BoltMem_deallocate(pool->connections, pool->size*sizeof(BoltConnection*)); if (pool->ssl_context!=NULL) { free_ssl_context(pool->ssl_context); } @@ -219,7 +224,7 @@ struct BoltConnectionResult BoltDirectPool_acquire(struct BoltDirectPool* pool) int pool_error = BOLT_SUCCESS; int64_t - started_at = BoltUtil_get_time_ms(); + started_at = BoltUtil_get_time_ms(); BoltLog_info(pool->config->log, "[%s]: Acquiring connection from the pool towards %s:%s", pool->id, pool->address->host, pool->address->port); @@ -229,25 +234,25 @@ struct BoltConnectionResult BoltDirectPool_acquire(struct BoltDirectPool* pool) index = find_unused_connection(pool); pool_error = index>=0 ? BOLT_SUCCESS : BOLT_POOL_FULL; if (index>=0) { - switch (pool->connections[index].status) { - case BOLT_DISCONNECTED: - case BOLT_DEFUNCT: + switch (pool->connections[index]->status->state) { + case BOLT_CONNECTION_STATE_DISCONNECTED: + case BOLT_CONNECTION_STATE_DEFUNCT: // if the connection is DISCONNECTED or DEFUNCT then try // to open and initialise it before handing it out. pool_error = open_init(pool, index); break; - case BOLT_CONNECTED: + case BOLT_CONNECTION_STATE_CONNECTED: // If CONNECTED, the connection will need to be initialised. // This state should rarely, if ever, be encountered here. pool_error = init(pool, index); break; - case BOLT_FAILED: + case BOLT_CONNECTION_STATE_FAILED: // If FAILED, attempt to RESET the connection, reopening // from scratch if that fails. This state should rarely, // if ever, be encountered here. pool_error = reset_or_open_init(pool, index); break; - case BOLT_READY: + case BOLT_CONNECTION_STATE_READY: // If the connection is already in the READY state then // do nothing and assume that the connection hasn't been // timed out by some piece of network housekeeping @@ -257,24 +262,24 @@ struct BoltConnectionResult BoltDirectPool_acquire(struct BoltDirectPool* pool) } } - handle.connection_status = BOLT_DISCONNECTED; + handle.connection_status = BOLT_CONNECTION_STATE_DISCONNECTED; handle.connection_error = BOLT_SUCCESS; handle.connection_error_ctx = NULL; handle.connection = NULL; switch (pool_error) { case BOLT_SUCCESS: - handle.connection = &pool->connections[index]; + handle.connection = pool->connections[index]; handle.connection->agent = "USED"; - handle.connection_status = handle.connection->status; + handle.connection_status = handle.connection->status->state; break; case BOLT_CONNECTION_HAS_MORE_INFO: - handle.connection_status = pool->connections[index].status; - handle.connection_error = pool->connections[index].error; - handle.connection_error_ctx = pool->connections[index].error_ctx; + handle.connection_status = pool->connections[index]->status->state; + handle.connection_error = pool->connections[index]->status->error; + handle.connection_error_ctx = pool->connections[index]->status->error_ctx; break; default: - handle.connection_status = BOLT_DISCONNECTED; + handle.connection_status = BOLT_CONNECTION_STATE_DISCONNECTED; handle.connection_error = pool_error; handle.connection_error_ctx = NULL; break; @@ -282,16 +287,16 @@ struct BoltConnectionResult BoltDirectPool_acquire(struct BoltDirectPool* pool) BoltUtil_mutex_unlock(&pool->mutex); - if (pool->config->max_connection_acquire_time==0) { + if (pool->config->max_connection_acquisition_time==0) { // fail fast, we report the failure asap break; } // Retry acquire operation until we get a live connection or timeout if (handle.connection_error==BOLT_POOL_FULL) { - if (pool->config->max_connection_acquire_time>0 - && BoltUtil_get_time_ms()-started_at>pool->config->max_connection_acquire_time) { - handle.connection_status = BOLT_DISCONNECTED; + if (pool->config->max_connection_acquisition_time>0 + && BoltUtil_get_time_ms()-started_at>pool->config->max_connection_acquisition_time) { + handle.connection_status = BOLT_CONNECTION_STATE_DISCONNECTED; handle.connection_error = BOLT_POOL_ACQUISITION_TIMED_OUT; handle.connection_error_ctx = NULL; @@ -337,7 +342,7 @@ int BoltDirectPool_connections_in_use(struct BoltDirectPool* pool) { int count = 0; for (int i = 0; isize; i++) { - if (pool->connections[i].agent!=NULL) { + if (pool->connections[i]->agent!=NULL) { count++; } } diff --git a/src/seabolt/src/direct-pool.h b/src/seabolt/src/direct-pool.h index 036e1284..02e191bc 100644 --- a/src/seabolt/src/direct-pool.h +++ b/src/seabolt/src/direct-pool.h @@ -38,7 +38,7 @@ struct BoltDirectPool { const struct BoltConfig* config; struct ssl_ctx_st *ssl_context; int size; - struct BoltConnection* connections; + BoltConnection** connections; }; #define SIZE_OF_DIRECT_POOL sizeof(struct BoltDirectPool) diff --git a/src/seabolt/src/error.c b/src/seabolt/src/error.c index 4bd555df..1f1eb5f4 100644 --- a/src/seabolt/src/error.c +++ b/src/seabolt/src/error.c @@ -17,7 +17,7 @@ * limitations under the License */ -#include "config-impl.h" +#include "bolt-private.h" const char* BoltError_get_string(int code) { diff --git a/src/seabolt/src/error.h b/src/seabolt/src/error.h index 425bf832..c0d57d73 100644 --- a/src/seabolt/src/error.h +++ b/src/seabolt/src/error.h @@ -19,7 +19,7 @@ #ifndef SEABOLT_ALL_ERROR_H #define SEABOLT_ALL_ERROR_H -#include "config.h" +#include "bolt-public.h" #define BOLT_SUCCESS 0 #define BOLT_UNKNOWN_ERROR 1 diff --git a/src/seabolt/src/lifecycle.c b/src/seabolt/src/lifecycle.c index dcac4692..64a9bf56 100644 --- a/src/seabolt/src/lifecycle.c +++ b/src/seabolt/src/lifecycle.c @@ -17,9 +17,7 @@ * limitations under the License. */ -#include "config-impl.h" -#include "lifecycle.h" -#include "logging.h" +#include "bolt-private.h" int SSL_CTX_TRUST_INDEX = -1; int SSL_CTX_LOG_INDEX = -1; diff --git a/src/seabolt/src/lifecycle.h b/src/seabolt/src/lifecycle.h index b1d43829..98c16009 100644 --- a/src/seabolt/src/lifecycle.h +++ b/src/seabolt/src/lifecycle.h @@ -23,7 +23,7 @@ #include -#include "config.h" +#include "bolt-public.h" SEABOLT_EXPORT void Bolt_startup(); diff --git a/src/seabolt/src/logging.h b/src/seabolt/src/log-private.h similarity index 80% rename from src/seabolt/src/logging.h rename to src/seabolt/src/log-private.h index c96e571a..4f4b903c 100644 --- a/src/seabolt/src/logging.h +++ b/src/seabolt/src/log-private.h @@ -16,18 +16,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#ifndef SEABOLT_LOG_PRIVATE_H +#define SEABOLT_LOG_PRIVATE_H - -#ifndef SEABOLT_LOGGING -#define SEABOLT_LOGGING - -#include - -#include "config.h" -#include "connections.h" -#include "values.h" - -typedef void (* log_func)(int state, const char* message); +#include "log.h" struct BoltLog { int state; @@ -43,9 +35,7 @@ struct BoltLog { log_func debug_logger; }; -SEABOLT_EXPORT struct BoltLog* BoltLog_create(); - -SEABOLT_EXPORT void BoltLog_destroy(struct BoltLog* log); +BoltLog* BoltLog_clone(BoltLog *log); void BoltLog_error(const struct BoltLog* log, const char* format, ...); @@ -59,7 +49,7 @@ void BoltLog_value(const struct BoltLog* log, const char* format, const char* id, struct BoltValue* value, name_resolver_func struct_name_resolver); -void BoltLog_message(const struct BoltLog* log, const char* id, const char* peer, bolt_request request_id, int16_t code, +void BoltLog_message(const struct BoltLog* log, const char* id, const char* peer, BoltRequest request_id, int16_t code, struct BoltValue* fields, name_resolver_func struct_name_resolver, name_resolver_func message_name_resolver); -#endif // SEABOLT_LOGGING +#endif //SEABOLT_LOG_PRIVATE_H diff --git a/src/seabolt/src/logging.c b/src/seabolt/src/log.c similarity index 76% rename from src/seabolt/src/logging.c rename to src/seabolt/src/log.c index 3c01c181..58f4e85b 100644 --- a/src/seabolt/src/logging.c +++ b/src/seabolt/src/log.c @@ -18,18 +18,16 @@ */ -#include -#include - -#include "logging.h" +#include "bolt-private.h" +#include "log-private.h" #include "mem.h" -#include "v1.h" #include "string-builder.h" +#include "values-private.h" -struct BoltLog* BoltLog_create() +struct BoltLog* BoltLog_create(int state) { struct BoltLog* log = (struct BoltLog*) BoltMem_allocate(sizeof(struct BoltLog)); - log->state = 0; + log->state = state; log->debug_logger = NULL; log->info_logger = NULL; log->warning_logger = NULL; @@ -41,11 +39,53 @@ struct BoltLog* BoltLog_create() return log; } +BoltLog* BoltLog_clone(BoltLog* log) +{ + if (log==NULL) { + return NULL; + } + + BoltLog* clone = BoltLog_create(log->state); + clone->debug_logger = log->debug_logger; + clone->debug_enabled = log->debug_enabled; + clone->info_logger = log->info_logger; + clone->info_enabled = log->info_enabled; + clone->warning_logger = log->warning_logger; + clone->warning_enabled = log->warning_enabled; + clone->error_logger = log->error_logger; + clone->error_enabled = log->error_enabled; + return clone; +} + void BoltLog_destroy(struct BoltLog* log) { BoltMem_deallocate(log, sizeof(struct BoltLog)); } +void BoltLog_set_error_func(BoltLog* log, log_func func) +{ + log->error_enabled = func!=NULL; + log->error_logger = func; +} + +void BoltLog_set_warning_func(BoltLog* log, log_func func) +{ + log->warning_enabled = func!=NULL; + log->warning_logger = func; +} + +void BoltLog_set_info_func(BoltLog* log, log_func func) +{ + log->info_enabled = func!=NULL; + log->info_logger = func; +} + +void BoltLog_set_debug_func(BoltLog* log, log_func func) +{ + log->debug_enabled = func!=NULL; + log->debug_logger = func; +} + void _perform_log_call(log_func func, int state, const char* format, va_list args) { size_t @@ -123,7 +163,7 @@ BoltLog_value(const struct BoltLog* log, const char* format, const char* id, str } } -void BoltLog_message(const struct BoltLog* log, const char* id, const char* peer, bolt_request request_id, int16_t code, +void BoltLog_message(const struct BoltLog* log, const char* id, const char* peer, BoltRequest request_id, int16_t code, struct BoltValue* fields, name_resolver_func struct_name_resolver, name_resolver_func message_name_resolver) { if (log!=NULL && log->debug_enabled) { diff --git a/src/seabolt/src/log.h b/src/seabolt/src/log.h new file mode 100644 index 00000000..4075b0c8 --- /dev/null +++ b/src/seabolt/src/log.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2002-2018 "Neo4j," + * Neo4j Sweden AB [http://neo4j.com] + * + * This file is part of Neo4j. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +#ifndef SEABOLT_LOG +#define SEABOLT_LOG + +#include "bolt-public.h" +#include "connection.h" +#include "values.h" + +typedef void (* log_func)(int state, const char* message); + +typedef struct BoltLog BoltLog; + +SEABOLT_EXPORT struct BoltLog* BoltLog_create(int state); + +SEABOLT_EXPORT void BoltLog_destroy(struct BoltLog* log); + +SEABOLT_EXPORT void BoltLog_set_error_func(BoltLog* log, log_func func); + +SEABOLT_EXPORT void BoltLog_set_warning_func(BoltLog* log, log_func func); + +SEABOLT_EXPORT void BoltLog_set_info_func(BoltLog* log, log_func func); + +SEABOLT_EXPORT void BoltLog_set_debug_func(BoltLog* log, log_func func); + +#endif // SEABOLT_LOG diff --git a/src/seabolt/src/mem.c b/src/seabolt/src/mem.c index 0145f033..d4c8f9fa 100644 --- a/src/seabolt/src/mem.c +++ b/src/seabolt/src/mem.c @@ -17,13 +17,10 @@ * limitations under the License. */ +#include "bolt-private.h" -#include -#include -#include - -#include "logging.h" #include "mem.h" +#include "platform.h" void* BoltMem_reverse_copy(void* dest, const void* src, size_t n) { @@ -35,25 +32,28 @@ void* BoltMem_reverse_copy(void* dest, const void* src, size_t n) return dest; } -static size_t __allocation = 0; -static size_t __peak_allocation = 0; +static int64_t __allocation = 0; +static int64_t __peak_allocation = 0; static int64_t __allocation_events = 0; void* BoltMem_allocate(size_t new_size) { void* p = malloc(new_size); - __allocation += new_size; - if (__allocation>__peak_allocation) __peak_allocation = __allocation; - __allocation_events += 1; + int64_t new_allocation = BoltUtil_add(&__allocation, new_size); + int64_t peak_allocation = BoltUtil_add(&__peak_allocation, 0); + if (new_allocation>peak_allocation) BoltUtil_add(&__peak_allocation, new_allocation-peak_allocation); + BoltUtil_increment(&__allocation_events); return p; } void* BoltMem_reallocate(void* ptr, size_t old_size, size_t new_size) { void* p = realloc(ptr, new_size); - __allocation = __allocation-old_size+new_size; - if (__allocation>__peak_allocation) __peak_allocation = __allocation; - __allocation_events += 1; + + int64_t new_allocation = BoltUtil_add(&__allocation, -old_size+new_size); + int64_t peak_allocation = BoltUtil_add(&__peak_allocation, 0); + if (__allocation>__peak_allocation) BoltUtil_add(&__peak_allocation, new_allocation-peak_allocation); + BoltUtil_increment(&__allocation_events); return p; } @@ -64,8 +64,8 @@ void* BoltMem_deallocate(void* ptr, size_t old_size) } free(ptr); - __allocation -= old_size; - __allocation_events += 1; + BoltUtil_add(&__allocation, -old_size); + BoltUtil_increment(&__allocation_events); return NULL; } @@ -105,7 +105,7 @@ void* BoltMem_adjust(void* ptr, size_t old_size, size_t new_size) void* BoltMem_duplicate(const void* ptr, size_t ptr_size) { - if (ptr == NULL) { + if (ptr==NULL) { return NULL; } void* p = BoltMem_allocate(ptr_size); @@ -113,12 +113,12 @@ void* BoltMem_duplicate(const void* ptr, size_t ptr_size) return p; } -size_t BoltMem_current_allocation() +int64_t BoltMem_current_allocation() { return __allocation; } -size_t BoltMem_peak_allocation() +int64_t BoltMem_peak_allocation() { return __peak_allocation; } diff --git a/src/seabolt/src/mem.h b/src/seabolt/src/mem.h index 1862cd67..0f079bad 100644 --- a/src/seabolt/src/mem.h +++ b/src/seabolt/src/mem.h @@ -23,10 +23,10 @@ #include -#include "config.h" +#include "bolt-public.h" -SEABOLT_EXPORT void* BoltMem_reverse_copy(void * dest, const void * src, size_t n); +void* BoltMem_reverse_copy(void * dest, const void * src, size_t n); #if IS_BIG_ENDIAN #define memcpy_be(target, src, n) memcpy(target, src, n) @@ -41,7 +41,7 @@ SEABOLT_EXPORT void* BoltMem_reverse_copy(void * dest, const void * src, size_t * @param new_size * @return */ -SEABOLT_EXPORT void* BoltMem_allocate(size_t new_size); +void* BoltMem_allocate(size_t new_size); /** * Reallocate memory. @@ -51,7 +51,7 @@ SEABOLT_EXPORT void* BoltMem_allocate(size_t new_size); * @param new_size * @return */ -SEABOLT_EXPORT void* BoltMem_reallocate(void* ptr, size_t old_size, size_t new_size); +void* BoltMem_reallocate(void* ptr, size_t old_size, size_t new_size); /** * Deallocate memory. @@ -60,7 +60,7 @@ SEABOLT_EXPORT void* BoltMem_reallocate(void* ptr, size_t old_size, size_t new_s * @param old_size * @return */ -SEABOLT_EXPORT void* BoltMem_deallocate(void* ptr, size_t old_size); +void* BoltMem_deallocate(void* ptr, size_t old_size); /** * Allocate, reallocate or free memory for data storage. @@ -72,7 +72,7 @@ SEABOLT_EXPORT void* BoltMem_deallocate(void* ptr, size_t old_size); * @param old_size the number of bytes already allocated * @param new_size the new number of bytes required */ -SEABOLT_EXPORT void* BoltMem_adjust(void* ptr, size_t old_size, size_t new_size); +void* BoltMem_adjust(void* ptr, size_t old_size, size_t new_size); /** * Duplicates the memory region specified by ptr and ptr_size. @@ -81,27 +81,26 @@ SEABOLT_EXPORT void* BoltMem_adjust(void* ptr, size_t old_size, size_t new_size) * @param ptr_size * @return */ -SEABOLT_EXPORT void* BoltMem_duplicate(const void *ptr, size_t ptr_size); +void* BoltMem_duplicate(const void *ptr, size_t ptr_size); /** * Retrieve the amount of memory currently allocated. * * @return */ -SEABOLT_EXPORT size_t BoltMem_current_allocation(); +int64_t BoltMem_current_allocation(); /** * * @return */ -SEABOLT_EXPORT size_t BoltMem_peak_allocation(); +int64_t BoltMem_peak_allocation(); /** * Get an activity count for memory (de/re/-)allocation. * * @return */ -SEABOLT_EXPORT int64_t BoltMem_allocation_events(); - +int64_t BoltMem_allocation_events(); #endif // SEABOLT_MEM diff --git a/src/seabolt/src/packstream.c b/src/seabolt/src/packstream.c index cf7671cd..622ba1b2 100644 --- a/src/seabolt/src/packstream.c +++ b/src/seabolt/src/packstream.c @@ -17,11 +17,10 @@ * limitations under the License. */ -#include "config-impl.h" -#include "buffering.h" -#include "connections.h" -#include "logging.h" +#include "bolt-private.h" +#include "log-private.h" #include "packstream.h" +#include "values-private.h" #define TRY(code) { int status_try = (code); if (status_try != BOLT_SUCCESS) { return status_try; } } diff --git a/src/seabolt/src/packstream.h b/src/seabolt/src/packstream.h index d8f6c482..5ac63e72 100644 --- a/src/seabolt/src/packstream.h +++ b/src/seabolt/src/packstream.h @@ -22,7 +22,7 @@ #include #include "buffering.h" -#include "logging.h" +#include "log.h" #include "values.h" enum PackStreamType { diff --git a/src/seabolt/src/platform.c b/src/seabolt/src/platform.c index 2f72e566..d0a5107d 100644 --- a/src/seabolt/src/platform.c +++ b/src/seabolt/src/platform.c @@ -17,9 +17,9 @@ * limitations under the License. */ -#include "config-impl.h" -#include "mem.h" +#include "bolt-private.h" #include "platform.h" +#include "mem.h" #ifdef __APPLE__ #include @@ -91,6 +91,17 @@ int64_t BoltUtil_decrement(volatile int64_t* ref) #endif } +int64_t BoltUtil_add(volatile int64_t* ref, int64_t by) +{ +#if defined(__APPLE__) + return OSAtomicAdd64(by, ref); +#elif defined(_WIN32) + return _InterlockedAdd64(ref, by); +#else + return __sync_add_and_fetch(ref, by); +#endif +} + void BoltUtil_sleep(int milliseconds) { #ifdef _WIN32 diff --git a/src/seabolt/src/platform.h b/src/seabolt/src/platform.h index 9d90c49b..9ad266bd 100644 --- a/src/seabolt/src/platform.h +++ b/src/seabolt/src/platform.h @@ -20,7 +20,7 @@ #ifndef SEABOLT_UTILS_H #define SEABOLT_UTILS_H -#include "config.h" +#include "bolt-public.h" #include typedef void * mutex_t; @@ -37,6 +37,8 @@ int64_t BoltUtil_increment(volatile int64_t* ref); int64_t BoltUtil_decrement(volatile int64_t* ref); +int64_t BoltUtil_add(volatile int64_t* ref, int64_t by); + void BoltUtil_sleep(int milliseconds); int BoltUtil_mutex_create(mutex_t* mutex); diff --git a/src/seabolt/src/protocol.c b/src/seabolt/src/protocol.c index 6bfbe617..85eb469f 100644 --- a/src/seabolt/src/protocol.c +++ b/src/seabolt/src/protocol.c @@ -17,11 +17,10 @@ * limitations under the License. */ -#include "config-impl.h" -#include "buffering.h" +#include "bolt-private.h" #include "mem.h" -#include "packstream.h" #include "protocol.h" +#include "values-private.h" #define BOLT_MAX_CHUNK_SIZE 65535 diff --git a/src/seabolt/src/protocol.h b/src/seabolt/src/protocol.h index 71966e87..6de3fa1d 100644 --- a/src/seabolt/src/protocol.h +++ b/src/seabolt/src/protocol.h @@ -20,7 +20,7 @@ #ifndef SEABOLT_ALL_PROTOCOL_H #define SEABOLT_ALL_PROTOCOL_H -#include "config.h" +#include "bolt-public.h" #include "packstream.h" #define FETCH_ERROR -1 @@ -79,9 +79,9 @@ typedef int (* load_pull_func)(struct BoltConnection*, int32_t); typedef int (* load_reset_func)(struct BoltConnection*); -typedef bolt_request (* last_request_func)(struct BoltConnection*); +typedef BoltRequest (* last_request_func)(struct BoltConnection*); -typedef int (* fetch_func)(struct BoltConnection*, bolt_request); +typedef int (* fetch_func)(struct BoltConnection*, BoltRequest); struct BoltProtocol { void* proto_state; diff --git a/src/seabolt/src/routing-pool.c b/src/seabolt/src/routing-pool.c index b3d8fe85..9f62c565 100644 --- a/src/seabolt/src/routing-pool.c +++ b/src/seabolt/src/routing-pool.c @@ -16,17 +16,18 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include -#include -#include "config-impl.h" -#include "address-set.h" -#include "connections.h" -#include "mem.h" +#include "bolt-private.h" +#include "address-private.h" +#include "address-set-private.h" +#include "config-private.h" +#include "connection-private.h" #include "direct-pool.h" -#include "routing-table.h" - +#include "log-private.h" +#include "mem.h" #include "routing-pool.h" +#include "routing-table.h" +#include "values-private.h" #define WRITE_LOCK_TIMEOUT 50 @@ -34,7 +35,7 @@ int BoltRoutingPool_ensure_server(struct BoltRoutingPool* pool, const struct Bol { int index = -1; while (index<0) { - index = BoltAddressSet_index_of(pool->servers, *server); + index = BoltAddressSet_index_of(pool->servers, server); // Release read lock BoltUtil_rwlock_rdunlock(&pool->rwlock); @@ -42,10 +43,10 @@ int BoltRoutingPool_ensure_server(struct BoltRoutingPool* pool, const struct Bol if (BoltUtil_rwlock_timedwrlock(&pool->rwlock, WRITE_LOCK_TIMEOUT)) { // Check once more if any other thread added this server in the mean-time - index = BoltAddressSet_index_of(pool->servers, *server); + index = BoltAddressSet_index_of(pool->servers, server); if (index<0) { // Add and return the index (which is always the end of the set - there's no ordering in place) - index = BoltAddressSet_add(pool->servers, *server); + index = BoltAddressSet_add(pool->servers, server); // Expand the direct pools and create a new one for this server pool->server_pools = (struct BoltDirectPool**) BoltMem_reallocate(pool->server_pools, @@ -72,12 +73,12 @@ int BoltRoutingPool_update_routing_table_from(struct BoltRoutingPool* pool, stru struct BoltConnection* connection = BoltConnection_create(); // Resolve the address - int status = BoltAddress_resolve(server, pool->config->log); + int status = BoltAddress_resolve(server, NULL, pool->config->log); // Open a new connection if (status==BOLT_SUCCESS) { status = BoltConnection_open(connection, pool->config->transport, server, pool->config->trust, - pool->config->log, pool->config->sock_opts); + pool->config->log, pool->config->socket_options); } // Initialize @@ -106,7 +107,7 @@ int BoltRoutingPool_update_routing_table_from(struct BoltRoutingPool* pool, stru } // Send pending messages - bolt_request pull_all = 0; + BoltRequest pull_all = 0; if (status==BOLT_SUCCESS) { pull_all = BoltConnection_last_request(connection); @@ -161,7 +162,7 @@ int BoltRoutingPool_update_routing_table(struct BoltRoutingPool* pool) BoltAddressResolver_resolve(pool->config->address_resolver, pool->address, initial_routers); // if nothing got added to the initial router addresses, add the connector hostname and port if (initial_routers->size==0) { - BoltAddressSet_add(initial_routers, *pool->address); + BoltAddressSet_add(initial_routers, pool->address); } // Create a set of servers to update the routing table from @@ -205,7 +206,7 @@ void BoltRoutingPool_cleanup(struct BoltRoutingPool* pool) int cleanup_count = 0; int* cleanup_marker = (int*) BoltMem_allocate(old_size*sizeof(int)); for (int i = 0; ielements[i])<0 + cleanup_marker[i] = BoltAddressSet_index_of(active_servers, old_servers->elements[i])<0 && BoltDirectPool_connections_in_use(old_server_pools[i])==0; cleanup_count = cleanup_count+cleanup_marker[i]; } @@ -222,7 +223,7 @@ void BoltRoutingPool_cleanup(struct BoltRoutingPool* pool) continue; } - int index = BoltAddressSet_add(new_servers, *old_servers->elements[i]); + int index = BoltAddressSet_add(new_servers, old_servers->elements[i]); new_server_pools[index] = old_server_pools[i]; } @@ -243,7 +244,7 @@ void BoltRoutingPool_cleanup(struct BoltRoutingPool* pool) BoltAddressSet_destroy(active_servers); } -int BoltRoutingPool_ensure_routing_table(struct BoltRoutingPool* pool, enum BoltAccessMode mode) +int BoltRoutingPool_ensure_routing_table(struct BoltRoutingPool* pool, BoltAccessMode mode) { int status = BOLT_SUCCESS; @@ -340,7 +341,7 @@ void BoltRoutingPool_forget_server(struct BoltRoutingPool* pool, const struct Bo } } - RoutingTable_forget_server(pool->routing_table, *server); + RoutingTable_forget_server(pool->routing_table, server); BoltRoutingPool_cleanup(pool); // Unlock @@ -356,7 +357,7 @@ void BoltRoutingPool_forget_writer(struct BoltRoutingPool* pool, const struct Bo } } - RoutingTable_forget_writer(pool->routing_table, *server); + RoutingTable_forget_writer(pool->routing_table, server); BoltRoutingPool_cleanup(pool); // Unlock @@ -423,7 +424,7 @@ void BoltRoutingPool_handle_connection_error_by_failure(struct BoltRoutingPool* void BoltRoutingPool_handle_connection_error(struct BoltRoutingPool* pool, struct BoltConnection* connection) { - switch (connection->error) { + switch (connection->status->error) { case BOLT_SUCCESS: break; case BOLT_SERVER_FAILURE: @@ -431,7 +432,7 @@ void BoltRoutingPool_handle_connection_error(struct BoltRoutingPool* pool, struc BoltConnection_failure(connection)); break; default: - BoltRoutingPool_handle_connection_error_by_code(pool, connection->address, connection->error); + BoltRoutingPool_handle_connection_error_by_code(pool, connection->address, connection->status->error); break; } } @@ -478,7 +479,7 @@ void BoltRoutingPool_destroy(struct BoltRoutingPool* pool) } struct BoltConnectionResult -BoltRoutingPool_acquire(struct BoltRoutingPool* pool, enum BoltAccessMode mode) +BoltRoutingPool_acquire(struct BoltRoutingPool* pool, BoltAccessMode mode) { struct BoltAddress* server = NULL; @@ -502,7 +503,7 @@ BoltRoutingPool_acquire(struct BoltRoutingPool* pool, enum BoltAccessMode mode) } } - struct BoltConnectionResult result = {NULL, BOLT_DISCONNECTED, BOLT_SUCCESS, NULL}; + struct BoltConnectionResult result = {NULL, BOLT_CONNECTION_STATE_DISCONNECTED, BOLT_SUCCESS, NULL}; if (status==BOLT_SUCCESS) { result = BoltDirectPool_acquire(pool->server_pools[server_pool_index]); if (result.connection!=NULL) { diff --git a/src/seabolt/src/routing-pool.h b/src/seabolt/src/routing-pool.h index b339eac0..cfbc5fa0 100644 --- a/src/seabolt/src/routing-pool.h +++ b/src/seabolt/src/routing-pool.h @@ -22,6 +22,7 @@ #include "connector.h" #include "address.h" #include "values.h" +#include "platform.h" struct BoltRoutingPool { struct BoltAddress* address; @@ -47,7 +48,7 @@ BoltRoutingPool_create(struct BoltAddress* address, const struct BoltValue* auth void BoltRoutingPool_destroy(struct BoltRoutingPool* pool); struct BoltConnectionResult -BoltRoutingPool_acquire(struct BoltRoutingPool* pool, enum BoltAccessMode mode); +BoltRoutingPool_acquire(struct BoltRoutingPool* pool, BoltAccessMode mode); int BoltRoutingPool_release(struct BoltRoutingPool* pool, struct BoltConnection* connection); diff --git a/src/seabolt/src/routing-table.c b/src/seabolt/src/routing-table.c index 8994d1cb..d7bb8b25 100644 --- a/src/seabolt/src/routing-table.c +++ b/src/seabolt/src/routing-table.c @@ -17,14 +17,13 @@ * limitations under the License. */ -#include -#include - -#include "config-impl.h" -#include "address-set.h" +#include "bolt-private.h" +#include "address-private.h" +#include "address-set-private.h" #include "mem.h" -#include "routing-table.h" #include "platform.h" +#include "routing-table.h" +#include "values-private.h" #define READ_ROLE "READ" #define WRITE_ROLE "WRITE" @@ -119,13 +118,13 @@ int RoutingTable_update(struct RoutingTable* table, struct BoltValue* response) address_value->size); if (strcmp(role, READ_ROLE)==0) { - BoltAddressSet_add(readers, *address); + BoltAddressSet_add(readers, address); } else if (strcmp(role, WRITE_ROLE)==0) { - BoltAddressSet_add(writers, *address); + BoltAddressSet_add(writers, address); } else if (strcmp(role, ROUTE_ROLE)==0) { - BoltAddressSet_add(routers, *address); + BoltAddressSet_add(routers, address); } else { status = BOLT_ROUTING_UNEXPECTED_DISCOVERY_RESPONSE; @@ -151,21 +150,21 @@ int RoutingTable_update(struct RoutingTable* table, struct BoltValue* response) return status; } -int RoutingTable_is_expired(struct RoutingTable* state, enum BoltAccessMode mode) +int RoutingTable_is_expired(struct RoutingTable* state, BoltAccessMode mode) { return state->routers->size==0 || (mode==BOLT_ACCESS_MODE_READ ? state->readers->size==0 : state->writers->size==0) || state->expires<=BoltUtil_get_time_ms(); } -void RoutingTable_forget_server(struct RoutingTable* state, struct BoltAddress address) +void RoutingTable_forget_server(struct RoutingTable* state, const struct BoltAddress* address) { BoltAddressSet_remove(state->routers, address); BoltAddressSet_remove(state->readers, address); BoltAddressSet_remove(state->writers, address); } -void RoutingTable_forget_writer(struct RoutingTable* state, struct BoltAddress address) +void RoutingTable_forget_writer(struct RoutingTable* state, const struct BoltAddress* address) { BoltAddressSet_remove(state->writers, address); } diff --git a/src/seabolt/src/routing-table.h b/src/seabolt/src/routing-table.h index 3865e16b..c6d26d5f 100644 --- a/src/seabolt/src/routing-table.h +++ b/src/seabolt/src/routing-table.h @@ -39,10 +39,10 @@ void RoutingTable_destroy(struct RoutingTable* state); int RoutingTable_update(struct RoutingTable* state, struct BoltValue* response); -int RoutingTable_is_expired(struct RoutingTable* state, enum BoltAccessMode mode); +int RoutingTable_is_expired(struct RoutingTable* state, BoltAccessMode mode); -void RoutingTable_forget_server(struct RoutingTable* state, struct BoltAddress address); +void RoutingTable_forget_server(struct RoutingTable* state, const struct BoltAddress* address); -void RoutingTable_forget_writer(struct RoutingTable* state, struct BoltAddress address); +void RoutingTable_forget_writer(struct RoutingTable* state, const struct BoltAddress* address); #endif //SEABOLT_ALL_ROUTING_TABLE_H diff --git a/src/seabolt/src/stats.c b/src/seabolt/src/stats.c new file mode 100644 index 00000000..6171cf45 --- /dev/null +++ b/src/seabolt/src/stats.c @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2002-2018 "Neo4j," + * Neo4j Sweden AB [http://neo4j.com] + * + * This file is part of Neo4j. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "bolt-private.h" +#include "stats.h" +#include "mem.h" + +size_t BoltStat_memory_allocation_current() +{ + return BoltMem_current_allocation(); +} + +size_t BoltStat_memory_allocation_peak() +{ + return BoltMem_peak_allocation(); +} + +int64_t BoltStat_memory_allocation_events() +{ + return BoltMem_allocation_events(); +} + diff --git a/src/seabolt/src/common-impl.h b/src/seabolt/src/stats.h similarity index 70% rename from src/seabolt/src/common-impl.h rename to src/seabolt/src/stats.h index 7f8dab76..465d3266 100644 --- a/src/seabolt/src/common-impl.h +++ b/src/seabolt/src/stats.h @@ -16,10 +16,15 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#ifndef SEABOLT_ALL_COMMON_IMPL_H -#define SEABOLT_ALL_COMMON_IMPL_H +#ifndef SEABOLT_STATS_H +#define SEABOLT_STATS_H -#define SIZE_OF_C_STRING(str) (sizeof(char)*(strlen(str)+1)) -#define UNUSED(x) (void)(x) +#include "bolt-public.h" -#endif //SEABOLT_ALL_COMMON_IMPL_H +SEABOLT_EXPORT size_t BoltStat_memory_allocation_current(); + +SEABOLT_EXPORT size_t BoltStat_memory_allocation_peak(); + +SEABOLT_EXPORT int64_t BoltStat_memory_allocation_events(); + +#endif //SEABOLT_STATS_H diff --git a/src/seabolt/src/status-private.h b/src/seabolt/src/status-private.h new file mode 100644 index 00000000..e474045b --- /dev/null +++ b/src/seabolt/src/status-private.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2002-2018 "Neo4j," + * Neo4j Sweden AB [http://neo4j.com] + * + * This file is part of Neo4j. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef SEABOLT_STATUS_PRIVATE_H +#define SEABOLT_STATUS_PRIVATE_H + +#include "status.h" + +struct BoltStatus { + BoltConnectionState state; + int error; + char* error_ctx; + int error_ctx_size; +}; + +BoltStatus* BoltStatus_create(size_t context_size); + +void BoltStatus_destroy(BoltStatus* status); + +#endif //SEABOLT_STATUS_PRIVATE_H diff --git a/src/seabolt/src/status.c b/src/seabolt/src/status.c new file mode 100644 index 00000000..56d252b1 --- /dev/null +++ b/src/seabolt/src/status.c @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2002-2018 "Neo4j," + * Neo4j Sweden AB [http://neo4j.com] + * + * This file is part of Neo4j. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "bolt-private.h" +#include "status-private.h" +#include "mem.h" + +BoltStatus* BoltStatus_create(size_t context_size) +{ + BoltStatus* status = BoltMem_allocate(sizeof(BoltStatus)); + status->state = BOLT_CONNECTION_STATE_DISCONNECTED; + status->error = BOLT_SUCCESS; + status->error_ctx = BoltMem_allocate(context_size); + status->error_ctx_size = context_size; + return status; +} + +void BoltStatus_destroy(BoltStatus* status) +{ + if (status->error_ctx!=NULL) { + BoltMem_deallocate(status->error_ctx, status->error_ctx_size); + } + BoltMem_deallocate(status, sizeof(BoltStatus)); +} + +BoltConnectionState BoltStatus_get_state(BoltStatus* status) +{ + return status->state; +} + +int BoltStatus_get_error(BoltStatus* status) +{ + return status->error; +} + +const char* BoltStatus_get_error_context(BoltStatus* status) +{ + return status->error_ctx; +} diff --git a/src/seabolt/src/status.h b/src/seabolt/src/status.h new file mode 100644 index 00000000..5c121b94 --- /dev/null +++ b/src/seabolt/src/status.h @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2002-2018 "Neo4j," + * Neo4j Sweden AB [http://neo4j.com] + * + * This file is part of Neo4j. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef SEABOLT_STATUS_H +#define SEABOLT_STATUS_H + +#include "bolt-public.h" + +typedef int BoltConnectionState; +// not connected +#define BOLT_CONNECTION_STATE_DISCONNECTED 0 +// connected but not authenticated +#define BOLT_CONNECTION_STATE_CONNECTED 1 +// connected and authenticated +#define BOLT_CONNECTION_STATE_READY 2 +// recoverable failure +#define BOLT_CONNECTION_STATE_FAILED 3 +// unrecoverable failure +#define BOLT_CONNECTION_STATE_DEFUNCT 4 + +/** + * Status information about connection including details about errors + */ +typedef struct BoltStatus BoltStatus; + +SEABOLT_EXPORT BoltConnectionState BoltStatus_get_state(BoltStatus* status); + +SEABOLT_EXPORT int BoltStatus_get_error(BoltStatus* status); + +SEABOLT_EXPORT const char* BoltStatus_get_error_context(BoltStatus* status); + +#endif //SEABOLT_STATUS_H diff --git a/src/common/utils/string-builder.c b/src/seabolt/src/string-builder.c similarity index 95% rename from src/common/utils/string-builder.c rename to src/seabolt/src/string-builder.c index 8e9ccb8f..20422289 100644 --- a/src/common/utils/string-builder.c +++ b/src/seabolt/src/string-builder.c @@ -39,7 +39,7 @@ void StringBuilder_destroy(struct StringBuilder* builder) free(builder); } -void StringBuilder_ensure_buffer(struct StringBuilder* builder, int64_t size_to_add) +void StringBuilder_ensure_buffer(struct StringBuilder* builder, int32_t size_to_add) { if (builder->buffer_size-builder->buffer_pos>size_to_add) { return; @@ -55,7 +55,7 @@ void StringBuilder_append(struct StringBuilder* builder, const char* string) StringBuilder_append_n(builder, string, strlen(string)); } -void StringBuilder_append_n(struct StringBuilder* builder, const char* string, const int64_t len) +void StringBuilder_append_n(struct StringBuilder* builder, const char* string, const int32_t len) { StringBuilder_ensure_buffer(builder, len+1); strncpy(builder->buffer+builder->buffer_pos, string, len); @@ -90,7 +90,7 @@ char* StringBuilder_get_string(struct StringBuilder* builder) return builder->buffer; } -int64_t StringBuilder_get_length(struct StringBuilder* builder) +int32_t StringBuilder_get_length(struct StringBuilder* builder) { return builder->buffer_pos; } diff --git a/src/common/utils/string-builder.h b/src/seabolt/src/string-builder.h similarity index 89% rename from src/common/utils/string-builder.h rename to src/seabolt/src/string-builder.h index fd1a870c..1a7905c4 100644 --- a/src/common/utils/string-builder.h +++ b/src/seabolt/src/string-builder.h @@ -24,8 +24,8 @@ struct StringBuilder { char* buffer; - int64_t buffer_size; - int64_t buffer_pos; + int32_t buffer_size; + int32_t buffer_pos; }; struct StringBuilder* StringBuilder_create(); @@ -34,12 +34,12 @@ void StringBuilder_destroy(struct StringBuilder* builder); void StringBuilder_append(struct StringBuilder* builder, const char* string); -void StringBuilder_append_n(struct StringBuilder* builder, const char* string, const int64_t len); +void StringBuilder_append_n(struct StringBuilder* builder, const char* string, const int32_t len); void StringBuilder_append_f(struct StringBuilder* builder, const char* format, ...); char* StringBuilder_get_string(struct StringBuilder* builder); -int64_t StringBuilder_get_length(struct StringBuilder* builder); +int32_t StringBuilder_get_length(struct StringBuilder* builder); #endif //SEABOLT_STRING_BUILDER_H diff --git a/src/seabolt/src/tls.c b/src/seabolt/src/tls.c index 8fb0ad9a..8a0e615a 100644 --- a/src/seabolt/src/tls.c +++ b/src/seabolt/src/tls.c @@ -17,9 +17,9 @@ * limitations under the License. */ -#include "connections.h" -#include "logging.h" -#include "tls.h" +#include "bolt-private.h" +#include "config-private.h" +#include "log-private.h" int verify_callback(int preverify_ok, X509_STORE_CTX* ctx) { diff --git a/src/seabolt/src/tls.h b/src/seabolt/src/tls.h index 004567e7..41d6e819 100644 --- a/src/seabolt/src/tls.h +++ b/src/seabolt/src/tls.h @@ -19,8 +19,8 @@ #ifndef SEABOLT_ALL_TLS_H #define SEABOLT_ALL_TLS_H -#include "config-impl.h" -#include "logging.h" +#include "bolt-private.h" +#include "log.h" SEABOLT_EXPORT struct ssl_ctx_st* create_ssl_ctx(struct BoltTrust* trust, const char* hostname, const struct BoltLog* log, const char* id); diff --git a/src/seabolt/src/utils.c b/src/seabolt/src/utils.c index a19a9e26..56270790 100644 --- a/src/seabolt/src/utils.c +++ b/src/seabolt/src/utils.c @@ -19,7 +19,7 @@ #include -#include "config-impl.h" +#include "bolt-private.h" #include "mem.h" #include "utils.h" diff --git a/src/seabolt/src/utils.h b/src/seabolt/src/utils.h index 4ef36c19..c22c7063 100644 --- a/src/seabolt/src/utils.h +++ b/src/seabolt/src/utils.h @@ -20,7 +20,7 @@ #ifndef SEABOLT_ALL_UTILS_H #define SEABOLT_ALL_UTILS_H -#include "config.h" +#include "bolt-public.h" #include void BoltUtil_diff_time(struct timespec* t, struct timespec* t0, struct timespec* t1); diff --git a/src/seabolt/src/v1.c b/src/seabolt/src/v1.c index 9c9ddd4b..f8aec5ba 100644 --- a/src/seabolt/src/v1.c +++ b/src/seabolt/src/v1.c @@ -17,18 +17,13 @@ * limitations under the License. */ -#include -#include -#include -#include - -#include "config-impl.h" -#include "buffering.h" -#include "connections.h" -#include "logging.h" +#include "bolt-private.h" +#include "connection-private.h" +#include "log-private.h" #include "mem.h" -#include "packstream.h" +#include "protocol.h" #include "v1.h" +#include "values-private.h" #define BOOKMARKS_KEY "bookmarks" #define BOOKMARKS_KEY_SIZE 9 @@ -214,7 +209,7 @@ int BoltProtocolV1_init(struct BoltConnection* connection, const char* user_agen init->fields, connection->protocol->structure_name, connection->protocol->message_name); TRY(BoltProtocolV1_compile_INIT(init, user_agent, auth_token, 0)); TRY(BoltProtocolV1_load_message(connection, init, 1)); - bolt_request init_request = BoltConnection_last_request(connection); + BoltRequest init_request = BoltConnection_last_request(connection); BoltMessage_destroy(init); TRY(BoltConnection_send(connection)); TRY(BoltConnection_fetch_summary(connection, init_request)); @@ -425,7 +420,7 @@ char* BoltProtocolV1_server(struct BoltConnection* connection) return state->server; } -bolt_request BoltProtocolV1_last_request(struct BoltConnection* connection) +BoltRequest BoltProtocolV1_last_request(struct BoltConnection* connection) { struct BoltProtocolV1State* state = BoltProtocolV1_state(connection); return state->next_request_id-1; @@ -687,10 +682,10 @@ void BoltProtocolV1_extract_metadata(struct BoltConnection* connection, struct B } } -int BoltProtocolV1_fetch(struct BoltConnection* connection, bolt_request request_id) +int BoltProtocolV1_fetch(struct BoltConnection* connection, BoltRequest request_id) { struct BoltProtocolV1State* state = BoltProtocolV1_state(connection); - bolt_request response_id; + BoltRequest response_id; do { char header[2]; int status = BoltConnection_receive(connection, &header[0], 2); diff --git a/src/seabolt/src/v1.h b/src/seabolt/src/v1.h index 31b22f76..a049296d 100644 --- a/src/seabolt/src/v1.h +++ b/src/seabolt/src/v1.h @@ -26,7 +26,7 @@ #include -#include "connections.h" +#include "connection.h" #include "protocol.h" #define BOLT_V1_INIT 0x01 @@ -62,8 +62,8 @@ struct BoltProtocolV1State { /// The last bookmark received from the server char* last_bookmark; - bolt_request next_request_id; - bolt_request response_counter; + BoltRequest next_request_id; + BoltRequest response_counter; unsigned long long record_counter; struct BoltMessage* run_request; diff --git a/src/seabolt/src/v3.c b/src/seabolt/src/v3.c index f9cb2b0f..827bbb4d 100644 --- a/src/seabolt/src/v3.c +++ b/src/seabolt/src/v3.c @@ -17,14 +17,13 @@ * limitations under the License. */ -#include - -#include "config-impl.h" -#include "buffering.h" -#include "logging.h" +#include "bolt-private.h" +#include "connection-private.h" +#include "log-private.h" #include "mem.h" #include "protocol.h" #include "v3.h" +#include "values-private.h" #define MASK "********" #define MASK_SIZE 8 @@ -80,8 +79,8 @@ struct BoltProtocolV3State { /// A connection identifier assigned by the server char* connection_id; - bolt_request next_request_id; - bolt_request response_counter; + BoltRequest next_request_id; + BoltRequest response_counter; unsigned long long record_counter; struct BoltMessage* run_request; @@ -401,7 +400,7 @@ int BoltProtocolV3_hello(struct BoltConnection* connection, const char* user_age hello->fields, connection->protocol->structure_name, connection->protocol->message_name); TRY(BoltProtocolV3_compile_HELLO(hello, user_agent, auth_token, 0)); TRY(BoltProtocolV3_load_message(connection, hello, 1)); - bolt_request hello_request = BoltConnection_last_request(connection); + BoltRequest hello_request = BoltConnection_last_request(connection); BoltMessage_destroy(hello); TRY(BoltConnection_send(connection)); TRY(BoltConnection_fetch_summary(connection, hello_request)); @@ -744,7 +743,7 @@ char* BoltProtocolV3_id(struct BoltConnection* connection) return state->connection_id; } -bolt_request BoltProtocolV3_last_request(struct BoltConnection* connection) +BoltRequest BoltProtocolV3_last_request(struct BoltConnection* connection) { struct BoltProtocolV3State* state = BoltProtocolV3_state(connection); return state->next_request_id-1; @@ -884,7 +883,7 @@ void BoltProtocolV3_extract_metadata(struct BoltConnection* connection, struct B switch (BoltValue_type(value)) { case BOLT_STRING: { char new_connection_id[MAX_CONNECTION_ID_SIZE]; - strncpy(new_connection_id, BoltString_get(value), (size_t) value->size); + strncpy(new_connection_id, BoltString_get(value), (size_t) value->size+1); char* old_connection_id = BoltConnection_id(connection); snprintf(state->connection_id, MAX_CONNECTION_ID_SIZE, "%s, %s", old_connection_id, new_connection_id); @@ -957,10 +956,10 @@ void BoltProtocolV3_extract_metadata(struct BoltConnection* connection, struct B } } -int BoltProtocolV3_fetch(struct BoltConnection* connection, bolt_request request_id) +int BoltProtocolV3_fetch(struct BoltConnection* connection, BoltRequest request_id) { struct BoltProtocolV3State* state = BoltProtocolV3_state(connection); - bolt_request response_id; + BoltRequest response_id; do { char header[2]; int status = BoltConnection_receive(connection, &header[0], 2); diff --git a/src/seabolt/src/v3.h b/src/seabolt/src/v3.h index c7da1ae4..f32273a1 100644 --- a/src/seabolt/src/v3.h +++ b/src/seabolt/src/v3.h @@ -19,7 +19,7 @@ #ifndef SEABOLT_ALL_V3_H #define SEABOLT_ALL_V3_H -#include "connections.h" +#include "connection.h" #define BOLT_V3_HELLO 0x01 #define BOLT_V3_GOODBYE 0x02 diff --git a/src/seabolt/src/values-private.h b/src/seabolt/src/values-private.h new file mode 100644 index 00000000..afd7e336 --- /dev/null +++ b/src/seabolt/src/values-private.h @@ -0,0 +1,98 @@ +/* + * Copyright (c) 2002-2018 "Neo4j," + * Neo4j Sweden AB [http://neo4j.com] + * + * This file is part of Neo4j. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef SEABOLT_VALUES_PRIVATE_H +#define SEABOLT_VALUES_PRIVATE_H + +#include "values.h" +#include "string-builder.h" + +static const char HEX_DIGITS[] = {'0', '1', '2', '3', '4', '5', '6', '7', + '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; + +#define hex5(mem, offset) HEX_DIGITS[((mem)[offset] >> 20) & 0x0F] + +#define hex4(mem, offset) HEX_DIGITS[((mem)[offset] >> 16) & 0x0F] + +#define hex3(mem, offset) HEX_DIGITS[((mem)[offset] >> 12) & 0x0F] + +#define hex2(mem, offset) HEX_DIGITS[((mem)[offset] >> 8) & 0x0F] + +#define hex1(mem, offset) HEX_DIGITS[((mem)[offset] >> 4) & 0x0F] + +#define hex0(mem, offset) HEX_DIGITS[(mem)[offset] & 0x0F] + +#define sizeof_n(type, n) (size_t)((n) >= 0 ? sizeof(type) * (n) : 0) + +#define to_bit(x) (char)((x) == 0 ? 0 : 1); + +/** + * For holding extended values that exceed the size of a single BoltValue. + */ +union BoltExtendedValue { + void* as_ptr; + char* as_char; + struct BoltValue* as_value; +}; + +struct BoltValue { + /// Type of this value, as defined in BoltType. + int16_t type; + + /// Subtype tag, for use with Structure values. + int16_t subtype; + + /// Logical size of this value. + /// For portability between platforms, the logical + /// size of a value (e.g. string length or list size) + /// cannot exceed 2^31, therefore an int32_t is safe + /// here. + int32_t size; + + /// Physical size of this value, in bytes. + uint64_t data_size; + + /// Data content of the value, or a pointer to + /// extended content. + union { + char as_char[16]; + uint32_t as_uint32[4]; + int8_t as_int8[16]; + int16_t as_int16[8]; + int32_t as_int32[4]; + int64_t as_int64[2]; + double as_double[2]; + union BoltExtendedValue extended; + } data; + +}; + +int BoltString_equals(struct BoltValue* value, const char* data, const size_t data_size); + +/** + * Write a textual representation of a BoltValue to a FILE. + * + * @param value + * @param file + * @param protocol_version + * @return + */ +int +BoltValue_write(struct StringBuilder* builder, const struct BoltValue* value, name_resolver_func struct_name_resolver); + +#endif //SEABOLT_VALUES_PRIVATE_H diff --git a/src/seabolt/src/values.c b/src/seabolt/src/values.c index bce205d8..522fb293 100644 --- a/src/seabolt/src/values.c +++ b/src/seabolt/src/values.c @@ -18,14 +18,11 @@ */ -#include -#include -#include - +#include "bolt-private.h" +#include "values-private.h" +#include "connection-private.h" #include "mem.h" -#include "values.h" -#include "v1.h" -#include "string-builder.h" +#include "protocol.h" #define STRING_QUOTE '"' #define REPLACEMENT_CHARACTER "\xFF\xFD" @@ -94,7 +91,7 @@ void _resize(struct BoltValue* value, int32_t size, int multiplier) void _set_type(struct BoltValue* value, enum BoltType type, int16_t subtype, int32_t size) { - assert(((int)type)<0x80); + assert(((int) type)<0x80); value->type = (char) (type); value->subtype = subtype; value->size = size; @@ -279,11 +276,37 @@ void BoltValue_copy(struct BoltValue* dest, const struct BoltValue* src) } } +int32_t BoltValue_size(const struct BoltValue* value) +{ + return value->size; +} + enum BoltType BoltValue_type(const struct BoltValue* value) { return (enum BoltType) (value->type); } +int32_t BoltValue_string(const struct BoltValue* value, char* dest, int32_t length, struct BoltConnection* connection) +{ + name_resolver_func name_resolver = NULL; + struct StringBuilder* builder = StringBuilder_create(); + if (connection!=NULL && connection->protocol!=NULL) { + name_resolver = connection->protocol->structure_name; + } + + int32_t result = BoltValue_write(builder, value, name_resolver); + if (result==BOLT_SUCCESS) { + int32_t builder_length = StringBuilder_get_length(builder); + int32_t copy_length = builder_length>length ? length : builder_length; + strncpy(dest, StringBuilder_get_string(builder), copy_length); + result = builder_length; + } + + StringBuilder_destroy(builder); + + return result; +} + void BoltValue_format_as_Null(struct BoltValue* value) { if (BoltValue_type(value)!=BOLT_NULL) { @@ -525,7 +548,8 @@ struct BoltValue* BoltStructure_value(const struct BoltValue* value, int32_t ind return &value->data.extended.as_value[index]; } -int BoltValue_write(struct StringBuilder* builder, struct BoltValue* value, name_resolver_func struct_name_resolver) +int +BoltValue_write(struct StringBuilder* builder, const struct BoltValue* value, name_resolver_func struct_name_resolver) { switch (BoltValue_type(value)) { case BOLT_NULL: { @@ -547,7 +571,7 @@ int BoltValue_write(struct StringBuilder* builder, struct BoltValue* value, name case BOLT_STRING: { char* data = BoltString_get(value); if (value->size>=0) { - StringBuilder_append_n(builder, data, (size_t) (value->size)); + StringBuilder_append_n(builder, data, value->size); return 0; } else { @@ -561,7 +585,7 @@ int BoltValue_write(struct StringBuilder* builder, struct BoltValue* value, name struct BoltValue* key = BoltDictionary_key(value, i); if (key!=NULL) { if (comma) StringBuilder_append(builder, ", "); - StringBuilder_append_n(builder, BoltString_get(key), (size_t) key->size); + StringBuilder_append_n(builder, BoltString_get(key), key->size); StringBuilder_append(builder, ": "); BoltValue_write(builder, BoltDictionary_value(value, i), struct_name_resolver); comma = 1; diff --git a/src/seabolt/src/values.h b/src/seabolt/src/values.h index 5749df8e..7dcb775b 100644 --- a/src/seabolt/src/values.h +++ b/src/seabolt/src/values.h @@ -28,34 +28,12 @@ #include #include -#include "config.h" +#include "bolt-public.h" #if CHAR_BIT!=8 #error "Cannot compile if `char` is not 8-bit" #endif -static const char HEX_DIGITS[] = {'0', '1', '2', '3', '4', '5', '6', '7', - '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; - -#define hex5(mem, offset) HEX_DIGITS[((mem)[offset] >> 20) & 0x0F] - -#define hex4(mem, offset) HEX_DIGITS[((mem)[offset] >> 16) & 0x0F] - -#define hex3(mem, offset) HEX_DIGITS[((mem)[offset] >> 12) & 0x0F] - -#define hex2(mem, offset) HEX_DIGITS[((mem)[offset] >> 8) & 0x0F] - -#define hex1(mem, offset) HEX_DIGITS[((mem)[offset] >> 4) & 0x0F] - -#define hex0(mem, offset) HEX_DIGITS[(mem)[offset] & 0x0F] - -#define sizeof_n(type, n) (size_t)((n) >= 0 ? sizeof(type) * (n) : 0) - -#define to_bit(x) (char)((x) == 0 ? 0 : 1); - -struct BoltValue; -struct StringBuilder; - /** * Enumeration of the types available in the Bolt type system. */ @@ -71,15 +49,6 @@ enum BoltType { BOLT_STRUCTURE = 8, }; -/** - * For holding extended values that exceed the size of a single BoltValue. - */ -union BoltExtendedValue { - void* as_ptr; - char* as_char; - struct BoltValue* as_value; -}; - /** * A BoltValue consists of a 128-bit header followed by a 128-byte data block. For * values that require more space than 128 bits, external memory is allocated and @@ -94,37 +63,7 @@ union BoltExtendedValue { * +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+ * ``` */ -struct BoltValue { - /// Type of this value, as defined in BoltType. - int16_t type; - - /// Subtype tag, for use with Structure values. - int16_t subtype; - - /// Logical size of this value. - /// For portability between platforms, the logical - /// size of a value (e.g. string length or list size) - /// cannot exceed 2^31, therefore an int32_t is safe - /// here. - int32_t size; - - /// Physical size of this value, in bytes. - uint64_t data_size; - - /// Data content of the value, or a pointer to - /// extended content. - union { - char as_char[16]; - uint32_t as_uint32[4]; - int8_t as_int8[16]; - int16_t as_int16[8]; - int32_t as_int32[4]; - int64_t as_int64[2]; - double as_double[2]; - union BoltExtendedValue extended; - } data; - -}; +typedef struct BoltValue BoltValue; typedef const char* (* name_resolver_func)(int16_t code); @@ -160,24 +99,22 @@ SEABOLT_EXPORT struct BoltValue* BoltValue_duplicate(const struct BoltValue* val SEABOLT_EXPORT void BoltValue_copy(struct BoltValue* dest, const struct BoltValue* src); /** - * Return the type of a BoltValue. + * Return the size of a BoltValue. * * @param value * @return */ -SEABOLT_EXPORT enum BoltType BoltValue_type(const struct BoltValue* value); +SEABOLT_EXPORT int32_t BoltValue_size(const struct BoltValue* value); /** - * Write a textual representation of a BoltValue to a FILE. + * Return the type of a BoltValue. * * @param value - * @param file - * @param protocol_version * @return */ -SEABOLT_EXPORT int -BoltValue_write(struct StringBuilder* builder, struct BoltValue* value, name_resolver_func struct_name_resolver); +SEABOLT_EXPORT enum BoltType BoltValue_type(const struct BoltValue* value); +SEABOLT_EXPORT int32_t BoltValue_string(const struct BoltValue* value, char *dest, int32_t length, struct BoltConnection* connection); /** * Set a BoltValue instance to null. * @@ -201,8 +138,6 @@ SEABOLT_EXPORT void BoltValue_format_as_String(struct BoltValue* value, const ch SEABOLT_EXPORT char* BoltString_get(const struct BoltValue* value); -SEABOLT_EXPORT int BoltString_equals(struct BoltValue* value, const char* data, const size_t data_size); - SEABOLT_EXPORT void BoltValue_format_as_Dictionary(struct BoltValue* value, int32_t length); SEABOLT_EXPORT struct BoltValue* BoltDictionary_key(const struct BoltValue* value, int32_t index); @@ -220,7 +155,6 @@ SEABOLT_EXPORT struct BoltValue* BoltDictionary_value(const struct BoltValue* va SEABOLT_EXPORT struct BoltValue* BoltDictionary_value_by_key(const struct BoltValue* value, const char* key, size_t key_size); - /** * Format a BoltValue instance to hold a list. * diff --git a/src/seabolt/tests/integration.hpp b/src/seabolt/tests/integration.hpp index 99e989a9..67b28e72 100644 --- a/src/seabolt/tests/integration.hpp +++ b/src/seabolt/tests/integration.hpp @@ -25,6 +25,14 @@ #include #include "bolt.h" +extern "C" +{ +#include "address-private.h" +#include "config-private.h" +#include "connection-private.h" +#include "values-private.h" +} + #define SETTING(name, default_value) ((char*)((getenv(name) == nullptr) ? (default_value) : getenv(name))) #define BOLT_IPV4_HOST SETTING("BOLT_IPV4_HOST", "127.0.0.1") @@ -34,17 +42,15 @@ #define BOLT_PASSWORD SETTING("BOLT_PASSWORD", "password") #define BOLT_USER_AGENT SETTING("BOLT_USER_AGENT", "seabolt/1.0.0a") -#define VERBOSE() BoltLog_set_file(stderr) - static struct BoltAddress BOLT_IPV6_ADDRESS{(char*) BOLT_IPV6_HOST, (char*) BOLT_PORT, 0, NULL, 0, NULL}; static struct BoltAddress BOLT_IPV4_ADDRESS{(char*) BOLT_IPV4_HOST, (char*) BOLT_PORT, 0, NULL, 0, NULL}; struct BoltAddress* bolt_get_address(const char* host, const char* port); -struct BoltConnection* bolt_open_b(enum BoltTransport transport, const char* host, const char* port); +struct BoltConnection* bolt_open_b(BoltTransport transport, const char* host, const char* port); -struct BoltConnection* bolt_open_init_b(enum BoltTransport transport, const char* host, const char* port, +struct BoltConnection* bolt_open_init_b(BoltTransport transport, const char* host, const char* port, const char* user_agent, const struct BoltValue* auth_token); struct BoltConnection* bolt_open_init_default(); diff --git a/src/seabolt/tests/seabolt.cpp b/src/seabolt/tests/seabolt.cpp index ff9a4a59..e6bec9b8 100644 --- a/src/seabolt/tests/seabolt.cpp +++ b/src/seabolt/tests/seabolt.cpp @@ -27,35 +27,35 @@ struct BoltAddress* bolt_get_address(const char* host, const char* port) { struct BoltAddress* address = BoltAddress_create(host, port); - int status = BoltAddress_resolve(address, NULL); + int status = BoltAddress_resolve(address, NULL, NULL); REQUIRE(status==0); return address; } -struct BoltConnection* bolt_open_b(enum BoltTransport transport, const char* host, const char* port) +struct BoltConnection* bolt_open_b(BoltTransport transport, const char* host, const char* port) { struct BoltTrust trust{nullptr, 0, 1, 1}; struct BoltAddress* address = bolt_get_address(host, port); struct BoltConnection* connection = BoltConnection_create(); BoltConnection_open(connection, transport, address, &trust, NULL, NULL); BoltAddress_destroy(address); - REQUIRE(connection->status==BOLT_CONNECTED); + REQUIRE(connection->status->state==BOLT_CONNECTION_STATE_CONNECTED); return connection; } -struct BoltConnection* bolt_open_init_b(enum BoltTransport transport, const char* host, const char* port, +struct BoltConnection* bolt_open_init_b(BoltTransport transport, const char* host, const char* port, const char* user_agent, const struct BoltValue* auth_token) { struct BoltConnection* connection = bolt_open_b(transport, host, port); BoltConnection_init(connection, user_agent, auth_token); - REQUIRE(connection->status==BOLT_READY); + REQUIRE(connection->status->state==BOLT_CONNECTION_STATE_READY); return connection; } struct BoltConnection* bolt_open_init_default() { struct BoltValue* auth_token = BoltAuth_basic(BOLT_USER, BOLT_PASSWORD, NULL); - struct BoltConnection* connection = bolt_open_init_b(BOLT_SECURE_SOCKET, BOLT_IPV6_HOST, BOLT_PORT, BOLT_USER_AGENT, auth_token); + struct BoltConnection* connection = bolt_open_init_b(BOLT_TRANSPORT_ENCRYPTED, BOLT_IPV6_HOST, BOLT_PORT, BOLT_USER_AGENT, auth_token); BoltValue_destroy(auth_token); return connection; } diff --git a/src/seabolt/tests/test-address-set.cpp b/src/seabolt/tests/test-address-set.cpp index f18454c5..ffb8c35f 100644 --- a/src/seabolt/tests/test-address-set.cpp +++ b/src/seabolt/tests/test-address-set.cpp @@ -19,9 +19,15 @@ #include "catch.hpp" #include "bolt.h" +#include "address-set-private.h" SCENARIO("BoltAddressSet") { + struct BoltAddress* localhost7687 = BoltAddress_create("localhost", "7687"); + struct BoltAddress* localhost7688 = BoltAddress_create("localhost", "7688"); + struct BoltAddress* localhost7689 = BoltAddress_create("localhost", "7689"); + struct BoltAddress* localhost7690 = BoltAddress_create("localhost", "7690"); + WHEN("constructed") { struct BoltAddressSet* set = BoltAddressSet_create(); @@ -42,47 +48,47 @@ SCENARIO("BoltAddressSet") struct BoltAddressSet* set = BoltAddressSet_create(); WHEN("BoltAddress[localhost,7687] is added") { - BoltAddressSet_add(set, BoltAddress_of("localhost", "7687")); + BoltAddressSet_add(set, localhost7687); THEN("it should have size = 1") { REQUIRE(BoltAddressSet_size(set)==1); } THEN("it should report index of = 0") { - REQUIRE(BoltAddressSet_index_of(set, BoltAddress_of("localhost", "7687"))==0); + REQUIRE(BoltAddressSet_index_of(set, localhost7687)==0); } } WHEN("BoltAddress[localhost,7687] is added again") { - BoltAddressSet_add(set, BoltAddress_of("localhost", "7687")); + BoltAddressSet_add(set, localhost7687); THEN("it should have size = 1") { REQUIRE(BoltAddressSet_size(set)==1); } THEN("it should report index of = 0") { - REQUIRE(BoltAddressSet_index_of(set, BoltAddress_of("localhost", "7687"))==0); + REQUIRE(BoltAddressSet_index_of(set, localhost7687)==0); } } WHEN("BoltAddress[localhost,7687] and BoltAddress[localhost,7688] is added") { - BoltAddressSet_add(set, BoltAddress_of("localhost", "7687")); - BoltAddressSet_add(set, BoltAddress_of("localhost", "7688")); + BoltAddressSet_add(set, localhost7687); + BoltAddressSet_add(set, localhost7688); THEN("it should have size = 2") { REQUIRE(BoltAddressSet_size(set)==2); } THEN("it should report index of BoltAddress[localhost,7687] = 0") { - REQUIRE(BoltAddressSet_index_of(set, BoltAddress_of("localhost", "7687"))==0); + REQUIRE(BoltAddressSet_index_of(set, localhost7687)==0); } THEN("it should report index of BoltAddress[localhost,7688] = 1") { - REQUIRE(BoltAddressSet_index_of(set, BoltAddress_of("localhost", "7688"))==1); + REQUIRE(BoltAddressSet_index_of(set, localhost7688)==1); } THEN("it should report index of BoltAddress[localhost,7689] = -1") { - REQUIRE(BoltAddressSet_index_of(set, BoltAddress_of("localhost", "7689"))==-1); + REQUIRE(BoltAddressSet_index_of(set, localhost7689)==-1); } } @@ -92,30 +98,30 @@ SCENARIO("BoltAddressSet") GIVEN("A BoltAddressSet with 3 addresses") { struct BoltAddressSet* set = BoltAddressSet_create(); - BoltAddressSet_add(set, BoltAddress_of("localhost", "7687")); - BoltAddressSet_add(set, BoltAddress_of("localhost", "7688")); - BoltAddressSet_add(set, BoltAddress_of("localhost", "7689")); + BoltAddressSet_add(set, localhost7687); + BoltAddressSet_add(set, localhost7688); + BoltAddressSet_add(set, localhost7689); WHEN("index of BoltAddress[localhost,7689] is queried") { THEN("it should report = 2") { - REQUIRE(BoltAddressSet_index_of(set, BoltAddress_of("localhost", "7689"))==2); + REQUIRE(BoltAddressSet_index_of(set, localhost7689)==2); } } WHEN("index of BoltAddress[localhost,7690] is queried") { THEN("it should report = -1") { - REQUIRE(BoltAddressSet_index_of(set, BoltAddress_of("localhost", "7690"))==-1); + REQUIRE(BoltAddressSet_index_of(set, localhost7690)==-1); } } WHEN("BoltAddress[localhost,7689] is added again") { THEN("it should return -1") { - REQUIRE(BoltAddressSet_add(set, BoltAddress_of("localhost", "7689"))==-1); + REQUIRE(BoltAddressSet_add(set, localhost7689)==-1); } } WHEN("BoltAddress[localhost,7689] is removed") { - REQUIRE(BoltAddressSet_remove(set, BoltAddress_of("localhost", "7689"))==2); + REQUIRE(BoltAddressSet_remove(set, localhost7689)==2); THEN("it should have size = 2") { REQUIRE(BoltAddressSet_size(set)==2); @@ -123,7 +129,7 @@ SCENARIO("BoltAddressSet") AND_WHEN("BoltAddress[localhost,7689] is removed again") { THEN("it should return -1") { - REQUIRE(BoltAddressSet_remove(set, BoltAddress_of("localhost", "7689"))==-1); + REQUIRE(BoltAddressSet_remove(set, localhost7689)==-1); } THEN("it should have size = 2") { @@ -137,11 +143,11 @@ SCENARIO("BoltAddressSet") GIVEN("Two different BoltAddressSets") { struct BoltAddressSet* set1 = BoltAddressSet_create(); - REQUIRE(BoltAddressSet_add(set1, BoltAddress_of("localhost", "7687"))==0); - REQUIRE(BoltAddressSet_add(set1, BoltAddress_of("localhost", "7688"))==1); + REQUIRE(BoltAddressSet_add(set1, localhost7687)==0); + REQUIRE(BoltAddressSet_add(set1, localhost7688)==1); struct BoltAddressSet* set2 = BoltAddressSet_create(); - REQUIRE(BoltAddressSet_add(set2, BoltAddress_of("localhost", "7689"))==0); + REQUIRE(BoltAddressSet_add(set2, localhost7689)==0); WHEN("set1 is updated from set2") { BoltAddressSet_replace(set1, set2); @@ -151,27 +157,27 @@ SCENARIO("BoltAddressSet") } THEN("set1 should contain BoltAddress[localhost,7689]") { - REQUIRE(BoltAddressSet_index_of(set1, BoltAddress_of("localhost", "7689"))==0); + REQUIRE(BoltAddressSet_index_of(set1, localhost7689)==0); } THEN("set1 should not contain BoltAddress[localhost,7687]") { - REQUIRE(BoltAddressSet_index_of(set1, BoltAddress_of("localhost", "7687"))==-1); + REQUIRE(BoltAddressSet_index_of(set1, localhost7687)==-1); } THEN("set1 should not contain BoltAddress[localhost,7688]") { - REQUIRE(BoltAddressSet_index_of(set1, BoltAddress_of("localhost", "7688"))==-1); + REQUIRE(BoltAddressSet_index_of(set1, localhost7688)==-1); } } } GIVEN("Two different BoltAddressSets") { struct BoltAddressSet* set1 = BoltAddressSet_create(); - REQUIRE(BoltAddressSet_add(set1, BoltAddress_of("localhost", "7689"))==0); + REQUIRE(BoltAddressSet_add(set1, localhost7689)==0); struct BoltAddressSet* set2 = BoltAddressSet_create(); - REQUIRE(BoltAddressSet_add(set2, BoltAddress_of("localhost", "7687"))==0); - REQUIRE(BoltAddressSet_add(set2, BoltAddress_of("localhost", "7688"))==1); - REQUIRE(BoltAddressSet_add(set2, BoltAddress_of("localhost", "7689"))==2); + REQUIRE(BoltAddressSet_add(set2, localhost7687)==0); + REQUIRE(BoltAddressSet_add(set2, localhost7688)==1); + REQUIRE(BoltAddressSet_add(set2, localhost7689)==2); WHEN("set2 is added to set1") { BoltAddressSet_add_all(set1, set2); @@ -181,16 +187,21 @@ SCENARIO("BoltAddressSet") } THEN("set1 should contain BoltAddress[localhost,7689]") { - REQUIRE(BoltAddressSet_index_of(set1, BoltAddress_of("localhost", "7689"))==0); + REQUIRE(BoltAddressSet_index_of(set1, localhost7689)==0); } THEN("set1 should not contain BoltAddress[localhost,7687]") { - REQUIRE(BoltAddressSet_index_of(set1, BoltAddress_of("localhost", "7687"))>=0); + REQUIRE(BoltAddressSet_index_of(set1, localhost7687)>=0); } THEN("set1 should not contain BoltAddress[localhost,7688]") { - REQUIRE(BoltAddressSet_index_of(set1, BoltAddress_of("localhost", "7688"))>=0); + REQUIRE(BoltAddressSet_index_of(set1, localhost7688)>=0); } } } + + BoltAddress_destroy(localhost7687); + BoltAddress_destroy(localhost7688); + BoltAddress_destroy(localhost7689); + BoltAddress_destroy(localhost7690); } \ No newline at end of file diff --git a/src/seabolt/tests/test-addressing.cpp b/src/seabolt/tests/test-addressing.cpp index 7ff5e04b..b3ece5db 100644 --- a/src/seabolt/tests/test-addressing.cpp +++ b/src/seabolt/tests/test-addressing.cpp @@ -23,6 +23,8 @@ #if USE_WINSOCK #include +#else +#include #endif SCENARIO("Test address construction", "") @@ -98,7 +100,7 @@ SCENARIO("Test address resolution (IPv4)", "[dns]") REQUIRE(address->n_resolved_hosts==0); REQUIRE(address->resolved_port==0); for (int i = 0; i<2; i++) { - BoltAddress_resolve(address, nullptr); + BoltAddress_resolve(address, nullptr, nullptr); REQUIRE(address->n_resolved_hosts==1); char host_string[40]; int af = BoltAddress_copy_resolved_host(address, 0, &host_string[0], sizeof(host_string)); @@ -119,7 +121,7 @@ SCENARIO("Test address resolution (IPv6)", "[dns]") REQUIRE(address->n_resolved_hosts==0); REQUIRE(address->resolved_port==0); for (int i = 0; i<2; i++) { - int status = BoltAddress_resolve(address, nullptr); + int status = BoltAddress_resolve(address, nullptr, nullptr); if (status==0) { REQUIRE(address->n_resolved_hosts==1); char host_string[40]; @@ -142,7 +144,7 @@ SCENARIO("Test address resolution (IPv4 and IPv6)", "[dns]") REQUIRE(address->n_resolved_hosts==0); REQUIRE(address->resolved_port==0); for (int i = 0; i<2; i++) { - int status = BoltAddress_resolve(address, nullptr); + int status = BoltAddress_resolve(address, nullptr, nullptr); if (status==0) { for (int j = 0; jn_resolved_hosts; j++) { char host_string[40]; diff --git a/src/seabolt/tests/test-chunking-v1.cpp b/src/seabolt/tests/test-chunking-v1.cpp index 192b7574..ced687f0 100644 --- a/src/seabolt/tests/test-chunking-v1.cpp +++ b/src/seabolt/tests/test-chunking-v1.cpp @@ -27,18 +27,18 @@ using Catch::Matchers::Equals; #define REQUIRE_BOLT_BOOLEAN(value, x) { REQUIRE(BoltValue_type(value) == BOLT_BOOLEAN); REQUIRE(BoltBoolean_get(value) == (x)); } #define REQUIRE_BOLT_INTEGER(value, x) { REQUIRE(BoltValue_type(value) == BOLT_INTEGER); REQUIRE(BoltInteger_get(value) == (x)); } #define REQUIRE_BOLT_FLOAT(value, x) { REQUIRE(BoltValue_type(value) == BOLT_FLOAT); REQUIRE( BoltFloat_get(value) == (x)); } -#define REQUIRE_BOLT_STRING(value, x, size_) { REQUIRE(BoltValue_type(value) == BOLT_STRING); REQUIRE(strncmp(BoltString_get(value), x, size_) == 0); REQUIRE((value)->size == (int32_t)(size_)); } +#define REQUIRE_BOLT_STRING(value, x, size_) { REQUIRE(BoltValue_type(value) == BOLT_STRING); REQUIRE(strncmp(BoltString_get(value), x, size_) == 0); REQUIRE(BoltValue_size(value) == (int32_t)(size_)); } #define REQUIRE_BOLT_DICTIONARY(value, size_) { REQUIRE(BoltValue_type(value) == BOLT_DICTIONARY); REQUIRE((value)->size == (int32_t)(size_)); } #define REQUIRE_BOLT_LIST(value, size_) { REQUIRE(BoltValue_type(value) == BOLT_LIST); REQUIRE((value)->size == (int32_t)(size_)); } #define REQUIRE_BOLT_BYTES(value, size_) { REQUIRE(BoltValue_type(value) == BOLT_BYTES); REQUIRE((value)->size == (int32_t)(size_)); } -#define REQUIRE_BOLT_STRUCTURE(value, code, size_) { REQUIRE(BoltValue_type(value) == BOLT_STRUCTURE); REQUIRE(BoltStructure_code(value) == (code)); REQUIRE((value)->size == (int32_t)(size_)); } +#define REQUIRE_BOLT_STRUCTURE(value, code, size_) { REQUIRE(BoltValue_type(value) == BOLT_STRUCTURE); REQUIRE(BoltStructure_code(value) == (code)); REQUIRE(BoltValue_size(value) == (int32_t)(size_)); } #define REQUIRE_BOLT_SUCCESS(connection) { REQUIRE(BoltConnection_summary_success(connection) == 1); } #define RUN_PULL_SEND(connection, result)\ BoltConnection_load_run_request(connection);\ BoltConnection_load_pull_request(connection, -1);\ BoltConnection_send(connection);\ - bolt_request (result) = BoltConnection_last_request(connection); + BoltRequest (result) = BoltConnection_last_request(connection); SCENARIO("Test chunking", "[integration][ipv6][secure]") { diff --git a/src/seabolt/tests/test-direct.cpp b/src/seabolt/tests/test-direct.cpp index 722a240d..b0cc24ec 100644 --- a/src/seabolt/tests/test-direct.cpp +++ b/src/seabolt/tests/test-direct.cpp @@ -28,9 +28,9 @@ SCENARIO("Test basic secure connection (IPv4)", "[integration][ipv4][secure]") WHEN("a secure connection is opened") { struct BoltTrust trust{nullptr, 0, 1, 1}; struct BoltConnection* connection = BoltConnection_create(); - BoltConnection_open(connection, BOLT_SECURE_SOCKET, address, &trust, nullptr, nullptr); + BoltConnection_open(connection, BOLT_TRANSPORT_ENCRYPTED, address, &trust, nullptr, nullptr); THEN("the connection should be connected") { - REQUIRE(connection->status==BOLT_CONNECTED); + REQUIRE(connection->status->state==BOLT_CONNECTION_STATE_CONNECTED); } BoltConnection_close(connection); BoltConnection_destroy(connection); @@ -46,9 +46,9 @@ SCENARIO("Test basic secure connection (IPv6)", "[integration][ipv6][secure]") WHEN("a secure connection is opened") { struct BoltTrust trust{nullptr, 0, 1, 1}; struct BoltConnection* connection = BoltConnection_create(); - BoltConnection_open(connection, BOLT_SECURE_SOCKET, address, &trust, nullptr, nullptr); + BoltConnection_open(connection, BOLT_TRANSPORT_ENCRYPTED, address, &trust, nullptr, nullptr); THEN("the connection should be connected") { - REQUIRE(connection->status==BOLT_CONNECTED); + REQUIRE(connection->status->state==BOLT_CONNECTION_STATE_CONNECTED); } BoltConnection_close(connection); BoltConnection_destroy(connection); @@ -64,9 +64,9 @@ SCENARIO("Test basic insecure connection (IPv4)", "[integration][ipv4][insecure] WHEN("an insecure connection is opened") { struct BoltTrust trust{nullptr, 0, 1, 1}; struct BoltConnection* connection = BoltConnection_create(); - BoltConnection_open(connection, BOLT_SOCKET, address, &trust, nullptr, nullptr); + BoltConnection_open(connection, BOLT_TRANSPORT_PLAINTEXT, address, &trust, nullptr, nullptr); THEN("the connection should be connected") { - REQUIRE(connection->status==BOLT_CONNECTED); + REQUIRE(connection->status->state==BOLT_CONNECTION_STATE_CONNECTED); } BoltConnection_close(connection); BoltConnection_destroy(connection); @@ -81,9 +81,9 @@ SCENARIO("Test basic insecure connection (IPv6)", "[integration][ipv6][insecure] struct BoltAddress* address = bolt_get_address(BOLT_IPV6_HOST, BOLT_PORT); WHEN("an insecure connection is opened") { struct BoltConnection* connection = BoltConnection_create(); - BoltConnection_open(connection, BOLT_SOCKET, address, nullptr, nullptr, nullptr); + BoltConnection_open(connection, BOLT_TRANSPORT_PLAINTEXT, address, nullptr, nullptr, nullptr); THEN("the connection should be connected") { - REQUIRE(connection->status==BOLT_CONNECTED); + REQUIRE(connection->status->state==BOLT_CONNECTION_STATE_CONNECTED); } BoltConnection_close(connection); BoltConnection_destroy(connection); @@ -98,9 +98,9 @@ SCENARIO("Test secure connection to dead port", "[integration][ipv6][secure]") struct BoltAddress* address = bolt_get_address(BOLT_IPV6_HOST, "9999"); WHEN("a secure connection attempt is made") { struct BoltConnection* connection = BoltConnection_create(); - BoltConnection_open(connection, BOLT_SECURE_SOCKET, address, nullptr, nullptr, nullptr); + BoltConnection_open(connection, BOLT_TRANSPORT_ENCRYPTED, address, nullptr, nullptr, nullptr); THEN("a DEFUNCT connection should be returned") { - REQUIRE(connection->status==BOLT_DEFUNCT); + REQUIRE(connection->status->state==BOLT_CONNECTION_STATE_DEFUNCT); } BoltConnection_close(connection); BoltConnection_destroy(connection); @@ -115,9 +115,9 @@ SCENARIO("Test insecure connection to dead port", "[integration][ipv6][insecure] struct BoltAddress* address = bolt_get_address(BOLT_IPV6_HOST, "9999"); WHEN("an insecure connection attempt is made") { struct BoltConnection* connection = BoltConnection_create(); - BoltConnection_open(connection, BOLT_SOCKET, address, nullptr, nullptr, nullptr); + BoltConnection_open(connection, BOLT_TRANSPORT_PLAINTEXT, address, nullptr, nullptr, nullptr); THEN("a DEFUNCT connection should be returned") { - REQUIRE(connection->status==BOLT_DEFUNCT); + REQUIRE(connection->status->state==BOLT_CONNECTION_STATE_DEFUNCT); } BoltConnection_close(connection); BoltConnection_destroy(connection); @@ -133,17 +133,17 @@ SCENARIO("Test connection reuse after graceful shutdown", "[integration][ipv6][s WHEN("a secure connection is opened") { struct BoltTrust trust{nullptr, 0, 1, 1}; struct BoltConnection* connection = BoltConnection_create(); - BoltConnection_open(connection, BOLT_SECURE_SOCKET, address, &trust, nullptr, nullptr); + BoltConnection_open(connection, BOLT_TRANSPORT_ENCRYPTED, address, &trust, nullptr, nullptr); THEN("the connection should be connected") { - REQUIRE(connection->status==BOLT_CONNECTED); + REQUIRE(connection->status->state==BOLT_CONNECTION_STATE_CONNECTED); } BoltConnection_close(connection); THEN("the connection should be disconnected") { - REQUIRE(connection->status==BOLT_DISCONNECTED); + REQUIRE(connection->status->state==BOLT_CONNECTION_STATE_DISCONNECTED); } - BoltConnection_open(connection, BOLT_SECURE_SOCKET, address, &trust, nullptr, nullptr); + BoltConnection_open(connection, BOLT_TRANSPORT_ENCRYPTED, address, &trust, nullptr, nullptr); THEN("the connection should be connected") { - REQUIRE(connection->status==BOLT_CONNECTED); + REQUIRE(connection->status->state==BOLT_CONNECTION_STATE_CONNECTED); } BoltConnection_close(connection); BoltConnection_destroy(connection); @@ -159,14 +159,14 @@ SCENARIO("Test connection reuse after graceless shutdown", "[integration][ipv6][ WHEN("a secure connection is opened") { struct BoltTrust trust{nullptr, 0, 1, 1}; struct BoltConnection* connection = BoltConnection_create(); - BoltConnection_open(connection, BOLT_SECURE_SOCKET, address, &trust, nullptr, nullptr); + BoltConnection_open(connection, BOLT_TRANSPORT_ENCRYPTED, address, &trust, nullptr, nullptr); THEN("the connection should be connected") { - REQUIRE(connection->status==BOLT_CONNECTED); + REQUIRE(connection->status->state==BOLT_CONNECTION_STATE_CONNECTED); } - connection->status = BOLT_DEFUNCT; - BoltConnection_open(connection, BOLT_SECURE_SOCKET, address, &trust, nullptr, nullptr); + connection->status->state = BOLT_CONNECTION_STATE_DEFUNCT; + BoltConnection_open(connection, BOLT_TRANSPORT_ENCRYPTED, address, &trust, nullptr, nullptr); THEN("the connection should be connected") { - REQUIRE(connection->status==BOLT_CONNECTED); + REQUIRE(connection->status->state==BOLT_CONNECTION_STATE_CONNECTED); } BoltConnection_close(connection); BoltConnection_destroy(connection); @@ -179,14 +179,14 @@ SCENARIO("Test init with valid credentials", "[integration][ipv6][secure]") { GIVEN("an open connection") { const auto auth_token = BoltAuth_basic(BOLT_USER, BOLT_PASSWORD, NULL); - struct BoltConnection* connection = bolt_open_b(BOLT_SECURE_SOCKET, BOLT_IPV6_HOST, BOLT_PORT); + struct BoltConnection* connection = bolt_open_b(BOLT_TRANSPORT_ENCRYPTED, BOLT_IPV6_HOST, BOLT_PORT); WHEN("successfully initialised") { int rv = BoltConnection_init(connection, BOLT_USER_AGENT, auth_token); THEN("return value should be 0") { REQUIRE(rv==0); } THEN("status should change to READY") { - REQUIRE(connection->status==BOLT_READY); + REQUIRE(connection->status->state==BOLT_CONNECTION_STATE_READY); } } BoltConnection_close(connection); @@ -198,7 +198,7 @@ SCENARIO("Test init with valid credentials", "[integration][ipv6][secure]") SCENARIO("Test init with invalid credentials", "[integration][ipv6][secure]") { GIVEN("an open connection") { - struct BoltConnection* connection = bolt_open_b(BOLT_SECURE_SOCKET, BOLT_IPV6_HOST, BOLT_PORT); + struct BoltConnection* connection = bolt_open_b(BOLT_TRANSPORT_ENCRYPTED, BOLT_IPV6_HOST, BOLT_PORT); WHEN("unsuccessfully initialised") { REQUIRE(strcmp(BOLT_PASSWORD, "X")!=0); @@ -209,7 +209,7 @@ SCENARIO("Test init with invalid credentials", "[integration][ipv6][secure]") REQUIRE(rv!=0); } THEN("status should change to DEFUNCT") { - REQUIRE(connection->status==BOLT_DEFUNCT); + REQUIRE(connection->status->state==BOLT_CONNECTION_STATE_DEFUNCT); } BoltValue_destroy(auth_token); @@ -227,9 +227,9 @@ SCENARIO("Test execution of simple Cypher statement", "[integration][ipv6][secur const char* cypher = "RETURN 1"; BoltConnection_set_run_cypher(connection, cypher, strlen(cypher), 0); BoltConnection_load_run_request(connection); - bolt_request run = BoltConnection_last_request(connection); + BoltRequest run = BoltConnection_last_request(connection); BoltConnection_load_pull_request(connection, -1); - bolt_request pull = BoltConnection_last_request(connection); + BoltRequest pull = BoltConnection_last_request(connection); BoltConnection_send(connection); int records = BoltConnection_fetch_summary(connection, run); REQUIRE(records==0); @@ -248,24 +248,24 @@ SCENARIO("Test field names returned from Cypher execution", "[integration][ipv6] const char* cypher = "RETURN 1 AS first, true AS second, 3.14 AS third"; BoltConnection_set_run_cypher(connection, cypher, strlen(cypher), 0); BoltConnection_load_run_request(connection); - bolt_request run = BoltConnection_last_request(connection); + BoltRequest run = BoltConnection_last_request(connection); BoltConnection_load_pull_request(connection, -1); BoltConnection_send(connection); - bolt_request last = BoltConnection_last_request(connection); + BoltRequest last = BoltConnection_last_request(connection); BoltConnection_fetch_summary(connection, run); const struct BoltValue* fields = BoltConnection_field_names(connection); - REQUIRE(fields->size==3); + REQUIRE(BoltValue_size(fields)==3); struct BoltValue* field_name_value = BoltList_value(fields, 0); const char* field_name = BoltString_get(field_name_value); - int field_name_size = field_name_value->size; + int field_name_size = BoltValue_size(field_name_value); REQUIRE(strncmp(field_name, "first", field_name_size)==0); field_name_value = BoltList_value(fields, 1); field_name = BoltString_get(field_name_value); - field_name_size = field_name_value->size; + field_name_size = BoltValue_size(field_name_value); REQUIRE(strncmp(field_name, "second", field_name_size)==0); field_name_value = BoltList_value(fields, 2); field_name = BoltString_get(field_name_value); - field_name_size = field_name_value->size; + field_name_size = BoltValue_size(field_name_value); REQUIRE(strncmp(field_name, "third", field_name_size)==0); REQUIRE(field_name_size==5); BoltConnection_fetch_summary(connection, last); @@ -284,9 +284,9 @@ SCENARIO("Test parameterised Cypher statements", "[integration][ipv6][secure]") BoltValue* x = BoltConnection_set_run_cypher_parameter(connection, 0, "x", 1); BoltValue_format_as_Integer(x, 42); BoltConnection_load_run_request(connection); - bolt_request run = BoltConnection_last_request(connection); + BoltRequest run = BoltConnection_last_request(connection); BoltConnection_load_pull_request(connection, -1); - bolt_request pull = BoltConnection_last_request(connection); + BoltRequest pull = BoltConnection_last_request(connection); BoltConnection_send(connection); int records = BoltConnection_fetch_summary(connection, run); REQUIRE(records==0); @@ -334,20 +334,20 @@ SCENARIO("Test transactions", "[integration][ipv6][secure]") struct BoltConnection* connection = bolt_open_init_default(); WHEN("successfully executed Cypher") { BoltConnection_load_begin_request(connection); - bolt_request begin = BoltConnection_last_request(connection); + BoltRequest begin = BoltConnection_last_request(connection); const char* cypher = "RETURN 1"; BoltConnection_set_run_cypher(connection, cypher, strlen(cypher), 0); BoltConnection_load_run_request(connection); - bolt_request run = BoltConnection_last_request(connection); + BoltRequest run = BoltConnection_last_request(connection); BoltConnection_load_pull_request(connection, -1); - bolt_request pull = BoltConnection_last_request(connection); + BoltRequest pull = BoltConnection_last_request(connection); BoltConnection_load_commit_request(connection); - bolt_request commit = BoltConnection_last_request(connection); + BoltRequest commit = BoltConnection_last_request(connection); BoltConnection_send(connection); - bolt_request last = BoltConnection_last_request(connection); + BoltRequest last = BoltConnection_last_request(connection); REQUIRE(last==commit); int records = BoltConnection_fetch_summary(connection, begin); @@ -394,8 +394,8 @@ SCENARIO("Test FAILURE", "[integration][ipv6][secure]") const auto records = BoltConnection_fetch_summary(connection, run); REQUIRE(records==0); REQUIRE(BoltConnection_summary_success(connection)==0); - REQUIRE(connection->status==BOLT_FAILED); - REQUIRE(connection->error==BOLT_SERVER_FAILURE); + REQUIRE(connection->status->state==BOLT_CONNECTION_STATE_FAILED); + REQUIRE(connection->status->error==BOLT_SERVER_FAILURE); auto failure_data = BoltConnection_failure(connection); REQUIRE(failure_data!=nullptr); @@ -415,8 +415,8 @@ SCENARIO("Test FAILURE", "[integration][ipv6][secure]") REQUIRE(records==0); REQUIRE(BoltConnection_summary_success(connection)==0); - REQUIRE(connection->status==BOLT_FAILED); - REQUIRE(connection->error==BOLT_SERVER_FAILURE); + REQUIRE(connection->status->state==BOLT_CONNECTION_STATE_FAILED); + REQUIRE(connection->status->error==BOLT_SERVER_FAILURE); struct BoltValue* failure_data = BoltConnection_failure(connection); REQUIRE(failure_data!=nullptr); @@ -442,8 +442,8 @@ SCENARIO("Test FAILURE", "[integration][ipv6][secure]") const auto records = BoltConnection_fetch_summary(connection, run1); REQUIRE(records==0); REQUIRE(BoltConnection_summary_success(connection)==0); - REQUIRE(connection->status==BOLT_FAILED); - REQUIRE(connection->error==BOLT_SERVER_FAILURE); + REQUIRE(connection->status->state==BOLT_CONNECTION_STATE_FAILED); + REQUIRE(connection->status->error==BOLT_SERVER_FAILURE); auto failure_data = BoltConnection_failure(connection); REQUIRE(failure_data!=nullptr); @@ -462,8 +462,8 @@ SCENARIO("Test FAILURE", "[integration][ipv6][secure]") const auto records = BoltConnection_fetch_summary(connection, run); REQUIRE(records==0); REQUIRE(BoltConnection_summary_success(connection)==0); - REQUIRE(connection->status==BOLT_FAILED); - REQUIRE(connection->error==BOLT_SERVER_FAILURE); + REQUIRE(connection->status->state==BOLT_CONNECTION_STATE_FAILED); + REQUIRE(connection->status->error==BOLT_SERVER_FAILURE); auto status = BoltConnection_load_reset_request(connection); @@ -475,8 +475,8 @@ SCENARIO("Test FAILURE", "[integration][ipv6][secure]") const auto records_1 = BoltConnection_fetch_summary(connection, ack_failure); REQUIRE(records_1==0); REQUIRE(BoltConnection_summary_success(connection)==1); - REQUIRE(connection->status==BOLT_READY); - REQUIRE(connection->error==BOLT_SUCCESS); + REQUIRE(connection->status->state==BOLT_CONNECTION_STATE_READY); + REQUIRE(connection->status->error==BOLT_SUCCESS); } } BoltConnection_close(connection); diff --git a/src/seabolt/tests/test-pooling.cpp b/src/seabolt/tests/test-pooling.cpp index 417d6460..89423b59 100644 --- a/src/seabolt/tests/test-pooling.cpp +++ b/src/seabolt/tests/test-pooling.cpp @@ -26,17 +26,17 @@ SCENARIO("Test using a pooled connection", "[integration][ipv6][secure][pooling] GIVEN("a new connection pool") { const auto auth_token = BoltAuth_basic(BOLT_USER, BOLT_PASSWORD, NULL); struct BoltTrust trust{nullptr, 0, 1, 1}; - struct BoltConfig config{BOLT_DIRECT, BOLT_SECURE_SOCKET, &trust, BOLT_USER_AGENT, nullptr, nullptr, nullptr, + struct BoltConfig config{BOLT_MODE_DIRECT, BOLT_TRANSPORT_ENCRYPTED, &trust, BOLT_USER_AGENT, nullptr, nullptr, nullptr, 10, 0, 0, NULL}; struct BoltConnector* connector = BoltConnector_create(&BOLT_IPV6_ADDRESS, auth_token, &config); WHEN("a connection is acquired") { struct BoltConnectionResult result = BoltConnector_acquire(connector, BOLT_ACCESS_MODE_READ); THEN("the connection should be connected") { - REQUIRE(result.connection_status==BOLT_READY); + REQUIRE(result.connection_status==BOLT_CONNECTION_STATE_READY); REQUIRE(result.connection_error==BOLT_SUCCESS); REQUIRE(result.connection_error_ctx==nullptr); REQUIRE(result.connection!=nullptr); - REQUIRE(result.connection->status==BOLT_READY); + REQUIRE(result.connection->status->state==BOLT_CONNECTION_STATE_READY); } BoltConnector_release(connector, result.connection); } @@ -50,7 +50,7 @@ SCENARIO("Test reusing a pooled connection", "[integration][ipv6][secure][poolin GIVEN("a new connection pool with one entry") { const auto auth_token = BoltAuth_basic(BOLT_USER, BOLT_PASSWORD, NULL); struct BoltTrust trust{nullptr, 0, 1, 1}; - struct BoltConfig config{BOLT_DIRECT, BOLT_SECURE_SOCKET, &trust, BOLT_USER_AGENT, nullptr, nullptr, nullptr, + struct BoltConfig config{BOLT_MODE_DIRECT, BOLT_TRANSPORT_ENCRYPTED, &trust, BOLT_USER_AGENT, nullptr, nullptr, nullptr, 1, 0, 0, NULL}; struct BoltConnector* connector = BoltConnector_create(&BOLT_IPV6_ADDRESS, auth_token, &config); WHEN("a connection is acquired, released and acquired again") { @@ -58,11 +58,11 @@ SCENARIO("Test reusing a pooled connection", "[integration][ipv6][secure][poolin BoltConnector_release(connector, result1.connection); struct BoltConnectionResult result2 = BoltConnector_acquire(connector, BOLT_ACCESS_MODE_READ); THEN("the connection should be connected") { - REQUIRE(result2.connection_status==BOLT_READY); + REQUIRE(result2.connection_status==BOLT_CONNECTION_STATE_READY); REQUIRE(result2.connection_error==BOLT_SUCCESS); REQUIRE(result2.connection_error_ctx==nullptr); REQUIRE(result2.connection!=nullptr); - REQUIRE(result2.connection->status==BOLT_READY); + REQUIRE(result2.connection->status->state==BOLT_CONNECTION_STATE_READY); } AND_THEN("the same connection should have been reused") { REQUIRE(result1.connection==result2.connection); @@ -79,13 +79,13 @@ SCENARIO("Test reusing a pooled connection that was abandoned", "[integration][i GIVEN("a new connection pool with one entry") { const auto auth_token = BoltAuth_basic(BOLT_USER, BOLT_PASSWORD, NULL); struct BoltTrust trust{nullptr, 0, 1, 1}; - struct BoltConfig config{BOLT_DIRECT, BOLT_SECURE_SOCKET, &trust, BOLT_USER_AGENT, nullptr, nullptr, + struct BoltConfig config{BOLT_MODE_DIRECT, BOLT_TRANSPORT_ENCRYPTED, &trust, BOLT_USER_AGENT, nullptr, nullptr, nullptr, 1, 0, 0, NULL}; struct BoltConnector* connector = BoltConnector_create(&BOLT_IPV6_ADDRESS, auth_token, &config); WHEN("a connection is acquired, released and acquired again") { struct BoltConnectionResult result1 = BoltConnector_acquire(connector, BOLT_ACCESS_MODE_READ); THEN("handle should include a valid connection") { - REQUIRE(result1.connection_status==BOLT_READY); + REQUIRE(result1.connection_status==BOLT_CONNECTION_STATE_READY); REQUIRE(result1.connection_error==BOLT_SUCCESS); REQUIRE(result1.connection_error_ctx==nullptr); REQUIRE(result1.connection!=nullptr); @@ -97,13 +97,13 @@ SCENARIO("Test reusing a pooled connection that was abandoned", "[integration][i BoltConnector_release(connector, result1.connection); struct BoltConnectionResult result2 = BoltConnector_acquire(connector, BOLT_ACCESS_MODE_READ); THEN("handle should include a valid connection") { - REQUIRE(result2.connection_status==BOLT_READY); + REQUIRE(result2.connection_status==BOLT_CONNECTION_STATE_READY); REQUIRE(result2.connection_error==BOLT_SUCCESS); REQUIRE(result2.connection_error_ctx==nullptr); REQUIRE(result2.connection!=nullptr); } THEN("the connection should be connected") { - REQUIRE(result2.connection->status==BOLT_READY); + REQUIRE(result2.connection->status->state==BOLT_CONNECTION_STATE_READY); } AND_THEN("the same connection should have been reused") { REQUIRE(result1.connection==result2.connection); @@ -120,21 +120,21 @@ SCENARIO("Test running out of connections", "[integration][ipv6][secure][pooling GIVEN("a new connection pool with one entry") { const auto auth_token = BoltAuth_basic(BOLT_USER, BOLT_PASSWORD, NULL); struct BoltTrust trust{nullptr, 0, 1, 1}; - struct BoltConfig config{BOLT_DIRECT, BOLT_SECURE_SOCKET, &trust, BOLT_USER_AGENT, nullptr, nullptr, nullptr, + struct BoltConfig config{BOLT_MODE_DIRECT, BOLT_TRANSPORT_ENCRYPTED, &trust, BOLT_USER_AGENT, nullptr, nullptr, nullptr, 1, 0, 0, NULL}; struct BoltConnector* connector = BoltConnector_create(&BOLT_IPV6_ADDRESS, auth_token, &config); WHEN("two connections are acquired in turn") { struct BoltConnectionResult result1 = BoltConnector_acquire(connector, BOLT_ACCESS_MODE_READ); struct BoltConnectionResult result2 = BoltConnector_acquire(connector, BOLT_ACCESS_MODE_READ); THEN("the first connection should be connected") { - REQUIRE(result1.connection_status==BOLT_READY); + REQUIRE(result1.connection_status==BOLT_CONNECTION_STATE_READY); REQUIRE(result1.connection_error==BOLT_SUCCESS); REQUIRE(result1.connection_error_ctx==nullptr); REQUIRE(result1.connection!=nullptr); - REQUIRE(result1.connection->status==BOLT_READY); + REQUIRE(result1.connection->status->state==BOLT_CONNECTION_STATE_READY); } AND_THEN("the second connection should be invalid") { - REQUIRE(result2.connection_status==BOLT_DISCONNECTED); + REQUIRE(result2.connection_status==BOLT_CONNECTION_STATE_DISCONNECTED); REQUIRE(result2.connection_error==BOLT_POOL_FULL); REQUIRE(result2.connection_error_ctx==nullptr); REQUIRE(result2.connection==nullptr); diff --git a/src/seabolt/tests/test-values.cpp b/src/seabolt/tests/test-values.cpp index 5b317bfe..2c90d7e0 100644 --- a/src/seabolt/tests/test-values.cpp +++ b/src/seabolt/tests/test-values.cpp @@ -27,18 +27,18 @@ using Catch::Matchers::Equals; #define REQUIRE_BOLT_BOOLEAN(value, x) { REQUIRE(BoltValue_type(value) == BOLT_BOOLEAN); REQUIRE(BoltBoolean_get(value) == (x)); } #define REQUIRE_BOLT_INTEGER(value, x) { REQUIRE(BoltValue_type(value) == BOLT_INTEGER); REQUIRE(BoltInteger_get(value) == (x)); } #define REQUIRE_BOLT_FLOAT(value, x) { REQUIRE(BoltValue_type(value) == BOLT_FLOAT); REQUIRE( BoltFloat_get(value) == (x)); } -#define REQUIRE_BOLT_STRING(value, x, size_) { REQUIRE(BoltValue_type(value) == BOLT_STRING); REQUIRE(strncmp(BoltString_get(value), x, size_) == 0); REQUIRE((value)->size == (size_)); } -#define REQUIRE_BOLT_DICTIONARY(value, size_) { REQUIRE(BoltValue_type(value) == BOLT_DICTIONARY); REQUIRE((value)->size == (size_)); } -#define REQUIRE_BOLT_LIST(value, size_) { REQUIRE(BoltValue_type(value) == BOLT_LIST); REQUIRE((value)->size == (size_)); } -#define REQUIRE_BOLT_BYTES(value, size_) { REQUIRE(BoltValue_type(value) == BOLT_BYTES); REQUIRE((value)->size == (size_)); } -#define REQUIRE_BOLT_STRUCTURE(value, code, size_) { REQUIRE(BoltValue_type(value) == BOLT_STRUCTURE); REQUIRE(BoltStructure_code(value) == (code)); REQUIRE((value)->size == (size_)); } +#define REQUIRE_BOLT_STRING(value, x, size_) { REQUIRE(BoltValue_type(value) == BOLT_STRING); REQUIRE(strncmp(BoltString_get(value), x, size_) == 0); REQUIRE(BoltValue_size(value) == (size_)); } +#define REQUIRE_BOLT_DICTIONARY(value, size_) { REQUIRE(BoltValue_type(value) == BOLT_DICTIONARY); REQUIRE(BoltValue_size(value) == (size_)); } +#define REQUIRE_BOLT_LIST(value, size_) { REQUIRE(BoltValue_type(value) == BOLT_LIST); REQUIRE(BoltValue_size(value) == (size_)); } +#define REQUIRE_BOLT_BYTES(value, size_) { REQUIRE(BoltValue_type(value) == BOLT_BYTES); REQUIRE(BoltValue_size(value) == (size_)); } +#define REQUIRE_BOLT_STRUCTURE(value, code, size_) { REQUIRE(BoltValue_type(value) == BOLT_STRUCTURE); REQUIRE(BoltStructure_code(value) == (code)); REQUIRE(BoltValue_size(value) == (size_)); } #define REQUIRE_BOLT_SUCCESS(connection) { REQUIRE(BoltConnection_summary_success(connection) == 1); } #define RUN_PULL_SEND(connection, result)\ BoltConnection_load_run_request(connection);\ BoltConnection_load_pull_request(connection, -1);\ BoltConnection_send(connection);\ - bolt_request (result) = BoltConnection_last_request(connection); + BoltRequest (result) = BoltConnection_last_request(connection); SCENARIO("Test null parameter", "[integration][ipv6][secure]") { @@ -435,10 +435,10 @@ SCENARIO("Test structure in result", "[integration][ipv6][secure]") BoltConnection_set_run_cypher(connection, cypher, strlen(cypher), 0); BoltConnection_load_run_request(connection); BoltConnection_load_pull_request(connection, -1); - bolt_request result = BoltConnection_last_request(connection); + BoltRequest result = BoltConnection_last_request(connection); BoltConnection_load_rollback_request(connection); BoltConnection_send(connection); - bolt_request last = BoltConnection_last_request(connection); + BoltRequest last = BoltConnection_last_request(connection); while (BoltConnection_fetch(connection, result)) { const struct BoltValue* field_values = BoltConnection_field_values(connection); struct BoltValue* node = BoltList_value(field_values, 0); From c418b24c7ac35c0e7c28b3fa296a14418673242e Mon Sep 17 00:00:00 2001 From: Ali Ince Date: Thu, 25 Oct 2018 13:21:25 +0100 Subject: [PATCH 02/11] Fix windows errors and restructure code to align with installation --- CMakeLists.txt | 2 +- src/seabolt-cli/src/main.c | 23 ++--- src/seabolt/src/CMakeLists.txt | 92 +++++++++---------- src/seabolt/src/{ => bolt}/address-private.h | 0 .../src/{ => bolt}/address-resolver-private.h | 0 src/seabolt/src/{ => bolt}/address-resolver.c | 0 src/seabolt/src/{ => bolt}/address-resolver.h | 0 .../src/{ => bolt}/address-set-private.h | 0 src/seabolt/src/{ => bolt}/address-set.c | 0 src/seabolt/src/{ => bolt}/address-set.h | 0 src/seabolt/src/{ => bolt}/address.c | 2 +- src/seabolt/src/{ => bolt}/address.h | 0 src/seabolt/src/{ => bolt}/auth.c | 0 src/seabolt/src/{ => bolt}/auth.h | 0 src/seabolt/src/{ => bolt}/bolt-private.h | 0 src/seabolt/src/{ => bolt}/bolt-public.h | 1 + src/seabolt/src/{ => bolt}/bolt.h | 0 src/seabolt/src/{ => bolt}/buffering.c | 0 src/seabolt/src/{ => bolt}/buffering.h | 0 src/seabolt/src/{ => bolt}/config-private.h | 2 +- src/seabolt/src/{ => bolt}/config.c | 6 +- src/seabolt/src/{ => bolt}/config.h | 2 +- .../src/{ => bolt}/connection-private.h | 0 src/seabolt/src/{ => bolt}/connection.c | 0 src/seabolt/src/{ => bolt}/connection.h | 0 .../src/{ => bolt}/connector-private.h | 0 src/seabolt/src/{ => bolt}/connector.c | 0 src/seabolt/src/{ => bolt}/connector.h | 0 src/seabolt/src/{ => bolt}/direct-pool.c | 0 src/seabolt/src/{ => bolt}/direct-pool.h | 0 src/seabolt/src/{ => bolt}/error.c | 0 src/seabolt/src/{ => bolt}/error.h | 0 src/seabolt/src/{ => bolt}/lifecycle.c | 1 + src/seabolt/src/{ => bolt}/lifecycle.h | 5 - src/seabolt/src/{ => bolt}/log-private.h | 0 src/seabolt/src/{ => bolt}/log.c | 0 src/seabolt/src/{ => bolt}/log.h | 0 src/seabolt/src/{ => bolt}/mem.c | 22 ++--- src/seabolt/src/{ => bolt}/mem.h | 12 +-- src/seabolt/src/{ => bolt}/packstream.c | 0 src/seabolt/src/{ => bolt}/packstream.h | 0 src/seabolt/src/{ => bolt}/platform.c | 2 +- src/seabolt/src/{ => bolt}/platform.h | 0 src/seabolt/src/{ => bolt}/protocol.c | 0 src/seabolt/src/{ => bolt}/protocol.h | 0 src/seabolt/src/{ => bolt}/routing-pool.c | 0 src/seabolt/src/{ => bolt}/routing-pool.h | 0 src/seabolt/src/{ => bolt}/routing-table.c | 0 src/seabolt/src/{ => bolt}/routing-table.h | 0 src/seabolt/src/{ => bolt}/stats.c | 0 src/seabolt/src/{ => bolt}/stats.h | 0 src/seabolt/src/{ => bolt}/status-private.h | 2 +- src/seabolt/src/{ => bolt}/status.c | 0 src/seabolt/src/{ => bolt}/status.h | 0 src/seabolt/src/{ => bolt}/string-builder.c | 15 ++- src/seabolt/src/{ => bolt}/string-builder.h | 8 +- src/seabolt/src/{ => bolt}/tls.c | 0 src/seabolt/src/{ => bolt}/tls.h | 60 ++++++------ src/seabolt/src/{ => bolt}/utils.c | 0 src/seabolt/src/{ => bolt}/utils.h | 0 src/seabolt/src/{ => bolt}/v1.c | 0 src/seabolt/src/{ => bolt}/v1.h | 0 src/seabolt/src/{ => bolt}/v2.c | 0 src/seabolt/src/{ => bolt}/v2.h | 0 src/seabolt/src/{ => bolt}/v3.c | 0 src/seabolt/src/{ => bolt}/v3.h | 0 src/seabolt/src/{ => bolt}/values-private.h | 0 src/seabolt/src/{ => bolt}/values.c | 0 src/seabolt/src/{ => bolt}/values.h | 0 src/seabolt/tests/integration.hpp | 11 ++- src/seabolt/tests/main.cpp | 5 +- src/seabolt/tests/test-address-set.cpp | 3 +- src/seabolt/tests/test-warden.cpp | 6 +- 73 files changed, 132 insertions(+), 150 deletions(-) rename src/seabolt/src/{ => bolt}/address-private.h (100%) rename src/seabolt/src/{ => bolt}/address-resolver-private.h (100%) rename src/seabolt/src/{ => bolt}/address-resolver.c (100%) rename src/seabolt/src/{ => bolt}/address-resolver.h (100%) rename src/seabolt/src/{ => bolt}/address-set-private.h (100%) rename src/seabolt/src/{ => bolt}/address-set.c (100%) rename src/seabolt/src/{ => bolt}/address-set.h (100%) rename src/seabolt/src/{ => bolt}/address.c (98%) rename src/seabolt/src/{ => bolt}/address.h (100%) rename src/seabolt/src/{ => bolt}/auth.c (100%) rename src/seabolt/src/{ => bolt}/auth.h (100%) rename src/seabolt/src/{ => bolt}/bolt-private.h (100%) rename src/seabolt/src/{ => bolt}/bolt-public.h (98%) rename src/seabolt/src/{ => bolt}/bolt.h (100%) rename src/seabolt/src/{ => bolt}/buffering.c (100%) rename src/seabolt/src/{ => bolt}/buffering.h (100%) rename src/seabolt/src/{ => bolt}/config-private.h (98%) rename src/seabolt/src/{ => bolt}/config.c (98%) rename src/seabolt/src/{ => bolt}/config.h (99%) rename src/seabolt/src/{ => bolt}/connection-private.h (100%) rename src/seabolt/src/{ => bolt}/connection.c (100%) rename src/seabolt/src/{ => bolt}/connection.h (100%) rename src/seabolt/src/{ => bolt}/connector-private.h (100%) rename src/seabolt/src/{ => bolt}/connector.c (100%) rename src/seabolt/src/{ => bolt}/connector.h (100%) rename src/seabolt/src/{ => bolt}/direct-pool.c (100%) rename src/seabolt/src/{ => bolt}/direct-pool.h (100%) rename src/seabolt/src/{ => bolt}/error.c (100%) rename src/seabolt/src/{ => bolt}/error.h (100%) rename src/seabolt/src/{ => bolt}/lifecycle.c (98%) rename src/seabolt/src/{ => bolt}/lifecycle.h (97%) rename src/seabolt/src/{ => bolt}/log-private.h (100%) rename src/seabolt/src/{ => bolt}/log.c (100%) rename src/seabolt/src/{ => bolt}/log.h (100%) rename src/seabolt/src/{ => bolt}/mem.c (83%) rename src/seabolt/src/{ => bolt}/mem.h (83%) rename src/seabolt/src/{ => bolt}/packstream.c (100%) rename src/seabolt/src/{ => bolt}/packstream.h (100%) rename src/seabolt/src/{ => bolt}/platform.c (99%) rename src/seabolt/src/{ => bolt}/platform.h (100%) rename src/seabolt/src/{ => bolt}/protocol.c (100%) rename src/seabolt/src/{ => bolt}/protocol.h (100%) rename src/seabolt/src/{ => bolt}/routing-pool.c (100%) rename src/seabolt/src/{ => bolt}/routing-pool.h (100%) rename src/seabolt/src/{ => bolt}/routing-table.c (100%) rename src/seabolt/src/{ => bolt}/routing-table.h (100%) rename src/seabolt/src/{ => bolt}/stats.c (100%) rename src/seabolt/src/{ => bolt}/stats.h (100%) rename src/seabolt/src/{ => bolt}/status-private.h (97%) rename src/seabolt/src/{ => bolt}/status.c (100%) rename src/seabolt/src/{ => bolt}/status.h (100%) rename src/seabolt/src/{ => bolt}/string-builder.c (88%) rename src/seabolt/src/{ => bolt}/string-builder.h (89%) rename src/seabolt/src/{ => bolt}/tls.c (100%) rename src/seabolt/src/{ => bolt}/tls.h (97%) rename src/seabolt/src/{ => bolt}/utils.c (100%) rename src/seabolt/src/{ => bolt}/utils.h (100%) rename src/seabolt/src/{ => bolt}/v1.c (100%) rename src/seabolt/src/{ => bolt}/v1.h (100%) rename src/seabolt/src/{ => bolt}/v2.c (100%) rename src/seabolt/src/{ => bolt}/v2.h (100%) rename src/seabolt/src/{ => bolt}/v3.c (100%) rename src/seabolt/src/{ => bolt}/v3.h (100%) rename src/seabolt/src/{ => bolt}/values-private.h (100%) rename src/seabolt/src/{ => bolt}/values.c (100%) rename src/seabolt/src/{ => bolt}/values.h (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7d1a595e..50ecea83 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -122,7 +122,7 @@ install( DESTINATION ${INSTALL_LIBDIR} COMPONENT lib PUBLIC_HEADER - DESTINATION ${INSTALL_INCLUDEDIR}/bolt + DESTINATION ${INSTALL_INCLUDEDIR} COMPONENT dev) install( diff --git a/src/seabolt-cli/src/main.c b/src/seabolt-cli/src/main.c index 7d194e6d..0abd2449 100644 --- a/src/seabolt-cli/src/main.c +++ b/src/seabolt-cli/src/main.c @@ -25,15 +25,8 @@ #include -#include "auth.h" -#include "connector.h" -#include "connection.h" -#include "lifecycle.h" -#include "mem.h" -#include "values.h" -#include "platform.h" -#include "log.h" -#include "protocol.h" +#include "bolt/bolt.h" +#include "bolt/platform.h" #ifdef WIN32 @@ -144,7 +137,7 @@ struct Application* app_create(int argc, char** argv) const char* BOLT_CONFIG_USER = getenv_or_default("BOLT_USER", "neo4j"); const char* BOLT_CONFIG_PASSWORD = getenv("BOLT_PASSWORD"); - struct Application* app = BoltMem_allocate(sizeof(struct Application)); + struct Application* app = malloc(sizeof(struct Application)); app->access_mode = (strcmp(BOLT_CONFIG_ACCESS_MODE, "WRITE")==0 ? BOLT_ACCESS_MODE_WRITE : BOLT_ACCESS_MODE_READ); app->with_allocation_report = 0; @@ -220,7 +213,7 @@ struct Application* app_create(int argc, char** argv) void app_destroy(struct Application* app) { BoltConnector_destroy(app->connector); - BoltMem_deallocate(app, sizeof(struct Application)); + free(app); } void app_connect(struct Application* app) @@ -455,13 +448,13 @@ int main(int argc, char* argv[]) if (with_allocation_report) { fprintf(stderr, "=====================================\n"); - fprintf(stderr, "current allocation : %" PRIu64 " bytes\n", (int64_t) BoltMem_current_allocation()); - fprintf(stderr, "peak allocation : %" PRId64 " bytes\n", (int64_t) BoltMem_peak_allocation()); - fprintf(stderr, "allocation events : %" PRId64 "\n", BoltMem_allocation_events()); + fprintf(stderr, "current allocation : %" PRIu64 " bytes\n", (int64_t) BoltStat_memory_allocation_current()); + fprintf(stderr, "peak allocation : %" PRId64 " bytes\n", (int64_t) BoltStat_memory_allocation_peak()); + fprintf(stderr, "allocation events : %" PRId64 "\n", BoltStat_memory_allocation_events()); fprintf(stderr, "=====================================\n"); } - if (BoltMem_current_allocation()==0) { + if (BoltStat_memory_allocation_current()==0) { return EXIT_SUCCESS; } else { diff --git a/src/seabolt/src/CMakeLists.txt b/src/seabolt/src/CMakeLists.txt index 9ca2bcf9..fcb29cac 100644 --- a/src/seabolt/src/CMakeLists.txt +++ b/src/seabolt/src/CMakeLists.txt @@ -1,48 +1,48 @@ list(APPEND private_source_files - ${CMAKE_CURRENT_LIST_DIR}/address-resolver.c - ${CMAKE_CURRENT_LIST_DIR}/address-set.c - ${CMAKE_CURRENT_LIST_DIR}/address.c - ${CMAKE_CURRENT_LIST_DIR}/auth.c - ${CMAKE_CURRENT_LIST_DIR}/buffering.c - ${CMAKE_CURRENT_LIST_DIR}/config.c - ${CMAKE_CURRENT_LIST_DIR}/connection.c - ${CMAKE_CURRENT_LIST_DIR}/connector.c - ${CMAKE_CURRENT_LIST_DIR}/direct-pool.c - ${CMAKE_CURRENT_LIST_DIR}/error.c - ${CMAKE_CURRENT_LIST_DIR}/lifecycle.c - ${CMAKE_CURRENT_LIST_DIR}/log.c - ${CMAKE_CURRENT_LIST_DIR}/mem.c - ${CMAKE_CURRENT_LIST_DIR}/packstream.c - ${CMAKE_CURRENT_LIST_DIR}/platform.c - ${CMAKE_CURRENT_LIST_DIR}/protocol.c - ${CMAKE_CURRENT_LIST_DIR}/routing-pool.c - ${CMAKE_CURRENT_LIST_DIR}/routing-table.c - ${CMAKE_CURRENT_LIST_DIR}/stats.c - ${CMAKE_CURRENT_LIST_DIR}/status.c - ${CMAKE_CURRENT_LIST_DIR}/string-builder.c - ${CMAKE_CURRENT_LIST_DIR}/tls.c - ${CMAKE_CURRENT_LIST_DIR}/utils.c - ${CMAKE_CURRENT_LIST_DIR}/v1.c - ${CMAKE_CURRENT_LIST_DIR}/v2.c - ${CMAKE_CURRENT_LIST_DIR}/v3.c - ${CMAKE_CURRENT_LIST_DIR}/values.c) + ${CMAKE_CURRENT_LIST_DIR}/bolt/address-resolver.c + ${CMAKE_CURRENT_LIST_DIR}/bolt/address-set.c + ${CMAKE_CURRENT_LIST_DIR}/bolt/address.c + ${CMAKE_CURRENT_LIST_DIR}/bolt/auth.c + ${CMAKE_CURRENT_LIST_DIR}/bolt/buffering.c + ${CMAKE_CURRENT_LIST_DIR}/bolt/config.c + ${CMAKE_CURRENT_LIST_DIR}/bolt/connection.c + ${CMAKE_CURRENT_LIST_DIR}/bolt/connector.c + ${CMAKE_CURRENT_LIST_DIR}/bolt/direct-pool.c + ${CMAKE_CURRENT_LIST_DIR}/bolt/error.c + ${CMAKE_CURRENT_LIST_DIR}/bolt/lifecycle.c + ${CMAKE_CURRENT_LIST_DIR}/bolt/log.c + ${CMAKE_CURRENT_LIST_DIR}/bolt/mem.c + ${CMAKE_CURRENT_LIST_DIR}/bolt/packstream.c + ${CMAKE_CURRENT_LIST_DIR}/bolt/platform.c + ${CMAKE_CURRENT_LIST_DIR}/bolt/protocol.c + ${CMAKE_CURRENT_LIST_DIR}/bolt/routing-pool.c + ${CMAKE_CURRENT_LIST_DIR}/bolt/routing-table.c + ${CMAKE_CURRENT_LIST_DIR}/bolt/stats.c + ${CMAKE_CURRENT_LIST_DIR}/bolt/status.c + ${CMAKE_CURRENT_LIST_DIR}/bolt/string-builder.c + ${CMAKE_CURRENT_LIST_DIR}/bolt/tls.c + ${CMAKE_CURRENT_LIST_DIR}/bolt/utils.c + ${CMAKE_CURRENT_LIST_DIR}/bolt/v1.c + ${CMAKE_CURRENT_LIST_DIR}/bolt/v2.c + ${CMAKE_CURRENT_LIST_DIR}/bolt/v3.c + ${CMAKE_CURRENT_LIST_DIR}/bolt/values.c) list(APPEND public_header_files ${CMAKE_CURRENT_LIST_DIR}/bolt.h - ${CMAKE_CURRENT_LIST_DIR}/address.h - ${CMAKE_CURRENT_LIST_DIR}/address-set.h - ${CMAKE_CURRENT_LIST_DIR}/address-resolver.h - ${CMAKE_CURRENT_LIST_DIR}/auth.h - ${CMAKE_CURRENT_LIST_DIR}/bolt-public.h - ${CMAKE_CURRENT_LIST_DIR}/config.h - ${CMAKE_CURRENT_LIST_DIR}/connection.h - ${CMAKE_CURRENT_LIST_DIR}/connector.h - ${CMAKE_CURRENT_LIST_DIR}/error.h - ${CMAKE_CURRENT_LIST_DIR}/lifecycle.h - ${CMAKE_CURRENT_LIST_DIR}/log.h - ${CMAKE_CURRENT_LIST_DIR}/stats.h - ${CMAKE_CURRENT_LIST_DIR}/status.h - ${CMAKE_CURRENT_LIST_DIR}/values.h) + ${CMAKE_CURRENT_LIST_DIR}/bolt/address.h + ${CMAKE_CURRENT_LIST_DIR}/bolt/address-set.h + ${CMAKE_CURRENT_LIST_DIR}/bolt/address-resolver.h + ${CMAKE_CURRENT_LIST_DIR}/bolt/auth.h + ${CMAKE_CURRENT_LIST_DIR}/bolt/bolt-public.h + ${CMAKE_CURRENT_LIST_DIR}/bolt/config.h + ${CMAKE_CURRENT_LIST_DIR}/bolt/connection.h + ${CMAKE_CURRENT_LIST_DIR}/bolt/connector.h + ${CMAKE_CURRENT_LIST_DIR}/bolt/error.h + ${CMAKE_CURRENT_LIST_DIR}/bolt/lifecycle.h + ${CMAKE_CURRENT_LIST_DIR}/bolt/log.h + ${CMAKE_CURRENT_LIST_DIR}/bolt/stats.h + ${CMAKE_CURRENT_LIST_DIR}/bolt/status.h + ${CMAKE_CURRENT_LIST_DIR}/bolt/values.h) target_sources(${SEABOLT_SHARED} PRIVATE @@ -91,7 +91,7 @@ if (WITH_TLS_SUPPORT AND WITH_TLS_OPENSSL) endif () if (CMAKE_C_COMPILER_ID MATCHES AppleClang) - set_source_files_properties(${CMAKE_CURRENT_LIST_DIR}/platform.c + set_source_files_properties(${CMAKE_CURRENT_LIST_DIR}/bolt/platform.c PROPERTIES COMPILE_FLAGS -Wno-deprecated-declarations) endif () @@ -115,19 +115,19 @@ message(STATUS "Git hash is ${PROJECT_VERSION_HASH}") # generate file version.h based on version.h.in configure_file( ${CMAKE_CURRENT_LIST_DIR}/version.h.in - ${CMAKE_BINARY_DIR}/${INSTALL_INCLUDEDIR}/bolt-version.h + ${CMAKE_BINARY_DIR}/${INSTALL_INCLUDEDIR}/bolt/bolt-version.h @ONLY ) include(GenerateExportHeader) generate_export_header(${SEABOLT_SHARED} BASE_NAME seabolt - EXPORT_FILE_NAME "${CMAKE_BINARY_DIR}/${INSTALL_INCLUDEDIR}/bolt-exports.h" + EXPORT_FILE_NAME "${CMAKE_BINARY_DIR}/${INSTALL_INCLUDEDIR}/bolt/bolt-exports.h" DEFINE_NO_DEPRECATED) target_include_directories(${SEABOLT_SHARED} PUBLIC $ - $ + $ $) set_target_properties(${SEABOLT_SHARED} @@ -137,5 +137,5 @@ set_target_properties(${SEABOLT_SHARED} VISIBILITY_INLINES_HIDDEN 1 SOVERSION 1 OUTPUT_NAME "${SEABOLT_NAME}" - PUBLIC_HEADER "${public_header_files};${CMAKE_BINARY_DIR}/${INSTALL_INCLUDEDIR}/bolt-exports.h;${CMAKE_BINARY_DIR}/${INSTALL_INCLUDEDIR}/bolt-version.h" + PUBLIC_HEADER "${public_header_files};${CMAKE_BINARY_DIR}/${INSTALL_INCLUDEDIR}/bolt/bolt-exports.h;${CMAKE_BINARY_DIR}/${INSTALL_INCLUDEDIR}/bolt/bolt-version.h" WINDOWS_EXPORT_ALL_SYMBOLS OFF) diff --git a/src/seabolt/src/address-private.h b/src/seabolt/src/bolt/address-private.h similarity index 100% rename from src/seabolt/src/address-private.h rename to src/seabolt/src/bolt/address-private.h diff --git a/src/seabolt/src/address-resolver-private.h b/src/seabolt/src/bolt/address-resolver-private.h similarity index 100% rename from src/seabolt/src/address-resolver-private.h rename to src/seabolt/src/bolt/address-resolver-private.h diff --git a/src/seabolt/src/address-resolver.c b/src/seabolt/src/bolt/address-resolver.c similarity index 100% rename from src/seabolt/src/address-resolver.c rename to src/seabolt/src/bolt/address-resolver.c diff --git a/src/seabolt/src/address-resolver.h b/src/seabolt/src/bolt/address-resolver.h similarity index 100% rename from src/seabolt/src/address-resolver.h rename to src/seabolt/src/bolt/address-resolver.h diff --git a/src/seabolt/src/address-set-private.h b/src/seabolt/src/bolt/address-set-private.h similarity index 100% rename from src/seabolt/src/address-set-private.h rename to src/seabolt/src/bolt/address-set-private.h diff --git a/src/seabolt/src/address-set.c b/src/seabolt/src/bolt/address-set.c similarity index 100% rename from src/seabolt/src/address-set.c rename to src/seabolt/src/bolt/address-set.c diff --git a/src/seabolt/src/address-set.h b/src/seabolt/src/bolt/address-set.h similarity index 100% rename from src/seabolt/src/address-set.h rename to src/seabolt/src/bolt/address-set.h diff --git a/src/seabolt/src/address.c b/src/seabolt/src/bolt/address.c similarity index 98% rename from src/seabolt/src/address.c rename to src/seabolt/src/bolt/address.c index 13e3deff..e3d5194d 100644 --- a/src/seabolt/src/address.c +++ b/src/seabolt/src/bolt/address.c @@ -154,7 +154,7 @@ int BoltAddress_resolve(BoltAddress* address, int* n_resolved, BoltLog* log) if (address->n_resolved_hosts>0) { struct sockaddr_storage* resolved = &address->resolved_hosts[0]; - const in_port_t resolved_port = resolved->ss_family==AF_INET ? + const uint16_t resolved_port = resolved->ss_family==AF_INET ? ((struct sockaddr_in*) (resolved))->sin_port : ((struct sockaddr_in6*) (resolved))->sin6_port; address->resolved_port = ntohs(resolved_port); diff --git a/src/seabolt/src/address.h b/src/seabolt/src/bolt/address.h similarity index 100% rename from src/seabolt/src/address.h rename to src/seabolt/src/bolt/address.h diff --git a/src/seabolt/src/auth.c b/src/seabolt/src/bolt/auth.c similarity index 100% rename from src/seabolt/src/auth.c rename to src/seabolt/src/bolt/auth.c diff --git a/src/seabolt/src/auth.h b/src/seabolt/src/bolt/auth.h similarity index 100% rename from src/seabolt/src/auth.h rename to src/seabolt/src/bolt/auth.h diff --git a/src/seabolt/src/bolt-private.h b/src/seabolt/src/bolt/bolt-private.h similarity index 100% rename from src/seabolt/src/bolt-private.h rename to src/seabolt/src/bolt/bolt-private.h diff --git a/src/seabolt/src/bolt-public.h b/src/seabolt/src/bolt/bolt-public.h similarity index 98% rename from src/seabolt/src/bolt-public.h rename to src/seabolt/src/bolt/bolt-public.h index 8d2e1158..15560775 100644 --- a/src/seabolt/src/bolt-public.h +++ b/src/seabolt/src/bolt/bolt-public.h @@ -24,6 +24,7 @@ #include #include #include +#include #include "bolt-version.h" #include "bolt-exports.h" diff --git a/src/seabolt/src/bolt.h b/src/seabolt/src/bolt/bolt.h similarity index 100% rename from src/seabolt/src/bolt.h rename to src/seabolt/src/bolt/bolt.h diff --git a/src/seabolt/src/buffering.c b/src/seabolt/src/bolt/buffering.c similarity index 100% rename from src/seabolt/src/buffering.c rename to src/seabolt/src/bolt/buffering.c diff --git a/src/seabolt/src/buffering.h b/src/seabolt/src/bolt/buffering.h similarity index 100% rename from src/seabolt/src/buffering.h rename to src/seabolt/src/bolt/buffering.h diff --git a/src/seabolt/src/config-private.h b/src/seabolt/src/bolt/config-private.h similarity index 98% rename from src/seabolt/src/config-private.h rename to src/seabolt/src/bolt/config-private.h index e276e47e..d740b706 100644 --- a/src/seabolt/src/config-private.h +++ b/src/seabolt/src/bolt/config-private.h @@ -23,7 +23,7 @@ struct BoltTrust { char* certs; - int32_t certs_len; + int certs_len; int skip_verify; int skip_verify_hostname; }; diff --git a/src/seabolt/src/config.c b/src/seabolt/src/bolt/config.c similarity index 98% rename from src/seabolt/src/config.c rename to src/seabolt/src/bolt/config.c index 59a8907c..253db5d2 100644 --- a/src/seabolt/src/config.c +++ b/src/seabolt/src/bolt/config.c @@ -111,10 +111,10 @@ const char* BoltTrust_get_certs(BoltTrust* trust, size_t* size) return trust->certs; } -int BoltTrust_set_certs(BoltTrust* trust, const char* certs_pem, size_t size) +int BoltTrust_set_certs(BoltTrust* trust, const char* certs_pem, int certs_pem_size) { - trust->certs = BoltMem_duplicate(certs_pem, size); - trust->certs_len = size; + trust->certs = BoltMem_duplicate(certs_pem, certs_pem_size); + trust->certs_len = certs_pem_size; return BOLT_SUCCESS; } diff --git a/src/seabolt/src/config.h b/src/seabolt/src/bolt/config.h similarity index 99% rename from src/seabolt/src/config.h rename to src/seabolt/src/bolt/config.h index 2f61cc9e..1aef3a6d 100644 --- a/src/seabolt/src/config.h +++ b/src/seabolt/src/bolt/config.h @@ -59,7 +59,7 @@ SEABOLT_EXPORT void BoltTrust_destroy(BoltTrust* trust); SEABOLT_EXPORT const char* BoltTrust_get_certs(BoltTrust* trust, size_t* size); -SEABOLT_EXPORT int BoltTrust_set_certs(BoltTrust* trust, const char* certs_pem, size_t size); +SEABOLT_EXPORT int BoltTrust_set_certs(BoltTrust* trust, const char* certs_pem, int certs_pem_size); SEABOLT_EXPORT int BoltTrust_get_skip_verify(BoltTrust* trust); diff --git a/src/seabolt/src/connection-private.h b/src/seabolt/src/bolt/connection-private.h similarity index 100% rename from src/seabolt/src/connection-private.h rename to src/seabolt/src/bolt/connection-private.h diff --git a/src/seabolt/src/connection.c b/src/seabolt/src/bolt/connection.c similarity index 100% rename from src/seabolt/src/connection.c rename to src/seabolt/src/bolt/connection.c diff --git a/src/seabolt/src/connection.h b/src/seabolt/src/bolt/connection.h similarity index 100% rename from src/seabolt/src/connection.h rename to src/seabolt/src/bolt/connection.h diff --git a/src/seabolt/src/connector-private.h b/src/seabolt/src/bolt/connector-private.h similarity index 100% rename from src/seabolt/src/connector-private.h rename to src/seabolt/src/bolt/connector-private.h diff --git a/src/seabolt/src/connector.c b/src/seabolt/src/bolt/connector.c similarity index 100% rename from src/seabolt/src/connector.c rename to src/seabolt/src/bolt/connector.c diff --git a/src/seabolt/src/connector.h b/src/seabolt/src/bolt/connector.h similarity index 100% rename from src/seabolt/src/connector.h rename to src/seabolt/src/bolt/connector.h diff --git a/src/seabolt/src/direct-pool.c b/src/seabolt/src/bolt/direct-pool.c similarity index 100% rename from src/seabolt/src/direct-pool.c rename to src/seabolt/src/bolt/direct-pool.c diff --git a/src/seabolt/src/direct-pool.h b/src/seabolt/src/bolt/direct-pool.h similarity index 100% rename from src/seabolt/src/direct-pool.h rename to src/seabolt/src/bolt/direct-pool.h diff --git a/src/seabolt/src/error.c b/src/seabolt/src/bolt/error.c similarity index 100% rename from src/seabolt/src/error.c rename to src/seabolt/src/bolt/error.c diff --git a/src/seabolt/src/error.h b/src/seabolt/src/bolt/error.h similarity index 100% rename from src/seabolt/src/error.h rename to src/seabolt/src/bolt/error.h diff --git a/src/seabolt/src/lifecycle.c b/src/seabolt/src/bolt/lifecycle.c similarity index 98% rename from src/seabolt/src/lifecycle.c rename to src/seabolt/src/bolt/lifecycle.c index 64a9bf56..2a2e6345 100644 --- a/src/seabolt/src/lifecycle.c +++ b/src/seabolt/src/bolt/lifecycle.c @@ -18,6 +18,7 @@ */ #include "bolt-private.h" +#include "lifecycle.h" int SSL_CTX_TRUST_INDEX = -1; int SSL_CTX_LOG_INDEX = -1; diff --git a/src/seabolt/src/lifecycle.h b/src/seabolt/src/bolt/lifecycle.h similarity index 97% rename from src/seabolt/src/lifecycle.h rename to src/seabolt/src/bolt/lifecycle.h index 98c16009..c556a47b 100644 --- a/src/seabolt/src/lifecycle.h +++ b/src/seabolt/src/bolt/lifecycle.h @@ -20,15 +20,10 @@ #ifndef SEABOLT_BOLT #define SEABOLT_BOLT - -#include - #include "bolt-public.h" - SEABOLT_EXPORT void Bolt_startup(); SEABOLT_EXPORT void Bolt_shutdown(); - #endif // SEABOLT_BOLT diff --git a/src/seabolt/src/log-private.h b/src/seabolt/src/bolt/log-private.h similarity index 100% rename from src/seabolt/src/log-private.h rename to src/seabolt/src/bolt/log-private.h diff --git a/src/seabolt/src/log.c b/src/seabolt/src/bolt/log.c similarity index 100% rename from src/seabolt/src/log.c rename to src/seabolt/src/bolt/log.c diff --git a/src/seabolt/src/log.h b/src/seabolt/src/bolt/log.h similarity index 100% rename from src/seabolt/src/log.h rename to src/seabolt/src/bolt/log.h diff --git a/src/seabolt/src/mem.c b/src/seabolt/src/bolt/mem.c similarity index 83% rename from src/seabolt/src/mem.c rename to src/seabolt/src/bolt/mem.c index d4c8f9fa..fdabea3e 100644 --- a/src/seabolt/src/mem.c +++ b/src/seabolt/src/bolt/mem.c @@ -22,11 +22,11 @@ #include "mem.h" #include "platform.h" -void* BoltMem_reverse_copy(void* dest, const void* src, size_t n) +void* BoltMem_reverse_copy(void* dest, const void* src, int64_t n) { char* dest_c = (char*) (dest); const char* src_c = (const char*) (src); - for (size_t i = 0; ipeak_allocation) BoltUtil_add(&__peak_allocation, new_allocation-peak_allocation); BoltUtil_increment(&__allocation_events); return p; } -void* BoltMem_reallocate(void* ptr, size_t old_size, size_t new_size) +void* BoltMem_reallocate(void* ptr, int64_t old_size, int64_t new_size) { void* p = realloc(ptr, new_size); - int64_t new_allocation = BoltUtil_add(&__allocation, -old_size+new_size); - int64_t peak_allocation = BoltUtil_add(&__peak_allocation, 0); + size_t new_allocation = BoltUtil_add(&__allocation, -old_size+new_size); + size_t peak_allocation = BoltUtil_add(&__peak_allocation, 0); if (__allocation>__peak_allocation) BoltUtil_add(&__peak_allocation, new_allocation-peak_allocation); BoltUtil_increment(&__allocation_events); return p; } -void* BoltMem_deallocate(void* ptr, size_t old_size) +void* BoltMem_deallocate(void* ptr, int64_t old_size) { if (ptr==NULL) { return NULL; @@ -69,7 +69,7 @@ void* BoltMem_deallocate(void* ptr, size_t old_size) return NULL; } -void* BoltMem_adjust(void* ptr, size_t old_size, size_t new_size) +void* BoltMem_adjust(void* ptr, int64_t old_size, int64_t new_size) { if (new_size==old_size) { // In this case, the physical data storage requirement @@ -103,7 +103,7 @@ void* BoltMem_adjust(void* ptr, size_t old_size, size_t new_size) return BoltMem_reallocate(ptr, old_size, new_size); } -void* BoltMem_duplicate(const void* ptr, size_t ptr_size) +void* BoltMem_duplicate(const void* ptr, int64_t ptr_size) { if (ptr==NULL) { return NULL; diff --git a/src/seabolt/src/mem.h b/src/seabolt/src/bolt/mem.h similarity index 83% rename from src/seabolt/src/mem.h rename to src/seabolt/src/bolt/mem.h index 0f079bad..bd8ae53b 100644 --- a/src/seabolt/src/mem.h +++ b/src/seabolt/src/bolt/mem.h @@ -26,7 +26,7 @@ #include "bolt-public.h" -void* BoltMem_reverse_copy(void * dest, const void * src, size_t n); +void* BoltMem_reverse_copy(void * dest, const void * src, int64_t n); #if IS_BIG_ENDIAN #define memcpy_be(target, src, n) memcpy(target, src, n) @@ -41,7 +41,7 @@ void* BoltMem_reverse_copy(void * dest, const void * src, size_t n); * @param new_size * @return */ -void* BoltMem_allocate(size_t new_size); +void* BoltMem_allocate(int64_t new_size); /** * Reallocate memory. @@ -51,7 +51,7 @@ void* BoltMem_allocate(size_t new_size); * @param new_size * @return */ -void* BoltMem_reallocate(void* ptr, size_t old_size, size_t new_size); +void* BoltMem_reallocate(void* ptr, int64_t old_size, int64_t new_size); /** * Deallocate memory. @@ -60,7 +60,7 @@ void* BoltMem_reallocate(void* ptr, size_t old_size, size_t new_size); * @param old_size * @return */ -void* BoltMem_deallocate(void* ptr, size_t old_size); +void* BoltMem_deallocate(void* ptr, int64_t old_size); /** * Allocate, reallocate or free memory for data storage. @@ -72,7 +72,7 @@ void* BoltMem_deallocate(void* ptr, size_t old_size); * @param old_size the number of bytes already allocated * @param new_size the new number of bytes required */ -void* BoltMem_adjust(void* ptr, size_t old_size, size_t new_size); +void* BoltMem_adjust(void* ptr, int64_t old_size, int64_t new_size); /** * Duplicates the memory region specified by ptr and ptr_size. @@ -81,7 +81,7 @@ void* BoltMem_adjust(void* ptr, size_t old_size, size_t new_size); * @param ptr_size * @return */ -void* BoltMem_duplicate(const void *ptr, size_t ptr_size); +void* BoltMem_duplicate(const void *ptr, int64_t ptr_size); /** * Retrieve the amount of memory currently allocated. diff --git a/src/seabolt/src/packstream.c b/src/seabolt/src/bolt/packstream.c similarity index 100% rename from src/seabolt/src/packstream.c rename to src/seabolt/src/bolt/packstream.c diff --git a/src/seabolt/src/packstream.h b/src/seabolt/src/bolt/packstream.h similarity index 100% rename from src/seabolt/src/packstream.h rename to src/seabolt/src/bolt/packstream.h diff --git a/src/seabolt/src/platform.c b/src/seabolt/src/bolt/platform.c similarity index 99% rename from src/seabolt/src/platform.c rename to src/seabolt/src/bolt/platform.c index d0a5107d..dfdb03b7 100644 --- a/src/seabolt/src/platform.c +++ b/src/seabolt/src/bolt/platform.c @@ -96,7 +96,7 @@ int64_t BoltUtil_add(volatile int64_t* ref, int64_t by) #if defined(__APPLE__) return OSAtomicAdd64(by, ref); #elif defined(_WIN32) - return _InterlockedAdd64(ref, by); + return _InterlockedExchangeAdd64(ref, by); #else return __sync_add_and_fetch(ref, by); #endif diff --git a/src/seabolt/src/platform.h b/src/seabolt/src/bolt/platform.h similarity index 100% rename from src/seabolt/src/platform.h rename to src/seabolt/src/bolt/platform.h diff --git a/src/seabolt/src/protocol.c b/src/seabolt/src/bolt/protocol.c similarity index 100% rename from src/seabolt/src/protocol.c rename to src/seabolt/src/bolt/protocol.c diff --git a/src/seabolt/src/protocol.h b/src/seabolt/src/bolt/protocol.h similarity index 100% rename from src/seabolt/src/protocol.h rename to src/seabolt/src/bolt/protocol.h diff --git a/src/seabolt/src/routing-pool.c b/src/seabolt/src/bolt/routing-pool.c similarity index 100% rename from src/seabolt/src/routing-pool.c rename to src/seabolt/src/bolt/routing-pool.c diff --git a/src/seabolt/src/routing-pool.h b/src/seabolt/src/bolt/routing-pool.h similarity index 100% rename from src/seabolt/src/routing-pool.h rename to src/seabolt/src/bolt/routing-pool.h diff --git a/src/seabolt/src/routing-table.c b/src/seabolt/src/bolt/routing-table.c similarity index 100% rename from src/seabolt/src/routing-table.c rename to src/seabolt/src/bolt/routing-table.c diff --git a/src/seabolt/src/routing-table.h b/src/seabolt/src/bolt/routing-table.h similarity index 100% rename from src/seabolt/src/routing-table.h rename to src/seabolt/src/bolt/routing-table.h diff --git a/src/seabolt/src/stats.c b/src/seabolt/src/bolt/stats.c similarity index 100% rename from src/seabolt/src/stats.c rename to src/seabolt/src/bolt/stats.c diff --git a/src/seabolt/src/stats.h b/src/seabolt/src/bolt/stats.h similarity index 100% rename from src/seabolt/src/stats.h rename to src/seabolt/src/bolt/stats.h diff --git a/src/seabolt/src/status-private.h b/src/seabolt/src/bolt/status-private.h similarity index 97% rename from src/seabolt/src/status-private.h rename to src/seabolt/src/bolt/status-private.h index e474045b..4302ba67 100644 --- a/src/seabolt/src/status-private.h +++ b/src/seabolt/src/bolt/status-private.h @@ -25,7 +25,7 @@ struct BoltStatus { BoltConnectionState state; int error; char* error_ctx; - int error_ctx_size; + size_t error_ctx_size; }; BoltStatus* BoltStatus_create(size_t context_size); diff --git a/src/seabolt/src/status.c b/src/seabolt/src/bolt/status.c similarity index 100% rename from src/seabolt/src/status.c rename to src/seabolt/src/bolt/status.c diff --git a/src/seabolt/src/status.h b/src/seabolt/src/bolt/status.h similarity index 100% rename from src/seabolt/src/status.h rename to src/seabolt/src/bolt/status.h diff --git a/src/seabolt/src/string-builder.c b/src/seabolt/src/bolt/string-builder.c similarity index 88% rename from src/seabolt/src/string-builder.c rename to src/seabolt/src/bolt/string-builder.c index 20422289..c43913f8 100644 --- a/src/seabolt/src/string-builder.c +++ b/src/seabolt/src/bolt/string-builder.c @@ -17,11 +17,8 @@ * limitations under the License. */ +#include "bolt-private.h" #include "string-builder.h" -#include -#include -#include -#include struct StringBuilder* StringBuilder_create() { @@ -39,23 +36,23 @@ void StringBuilder_destroy(struct StringBuilder* builder) free(builder); } -void StringBuilder_ensure_buffer(struct StringBuilder* builder, int32_t size_to_add) +void StringBuilder_ensure_buffer(struct StringBuilder* builder, int size_to_add) { if (builder->buffer_size-builder->buffer_pos>size_to_add) { return; } - size_t new_size = builder->buffer_pos+size_to_add; + int new_size = builder->buffer_pos+size_to_add; builder->buffer = (char*) realloc(builder->buffer, new_size); builder->buffer_size = new_size; } void StringBuilder_append(struct StringBuilder* builder, const char* string) { - StringBuilder_append_n(builder, string, strlen(string)); + StringBuilder_append_n(builder, string, (int)SIZE_OF_C_STRING(string)); } -void StringBuilder_append_n(struct StringBuilder* builder, const char* string, const int32_t len) +void StringBuilder_append_n(struct StringBuilder* builder, const char* string, const int len) { StringBuilder_ensure_buffer(builder, len+1); strncpy(builder->buffer+builder->buffer_pos, string, len); @@ -90,7 +87,7 @@ char* StringBuilder_get_string(struct StringBuilder* builder) return builder->buffer; } -int32_t StringBuilder_get_length(struct StringBuilder* builder) +int StringBuilder_get_length(struct StringBuilder* builder) { return builder->buffer_pos; } diff --git a/src/seabolt/src/string-builder.h b/src/seabolt/src/bolt/string-builder.h similarity index 89% rename from src/seabolt/src/string-builder.h rename to src/seabolt/src/bolt/string-builder.h index 1a7905c4..4ddd2787 100644 --- a/src/seabolt/src/string-builder.h +++ b/src/seabolt/src/bolt/string-builder.h @@ -24,8 +24,8 @@ struct StringBuilder { char* buffer; - int32_t buffer_size; - int32_t buffer_pos; + int buffer_size; + int buffer_pos; }; struct StringBuilder* StringBuilder_create(); @@ -34,12 +34,12 @@ void StringBuilder_destroy(struct StringBuilder* builder); void StringBuilder_append(struct StringBuilder* builder, const char* string); -void StringBuilder_append_n(struct StringBuilder* builder, const char* string, const int32_t len); +void StringBuilder_append_n(struct StringBuilder* builder, const char* string, const int len); void StringBuilder_append_f(struct StringBuilder* builder, const char* format, ...); char* StringBuilder_get_string(struct StringBuilder* builder); -int32_t StringBuilder_get_length(struct StringBuilder* builder); +int StringBuilder_get_length(struct StringBuilder* builder); #endif //SEABOLT_STRING_BUILDER_H diff --git a/src/seabolt/src/tls.c b/src/seabolt/src/bolt/tls.c similarity index 100% rename from src/seabolt/src/tls.c rename to src/seabolt/src/bolt/tls.c diff --git a/src/seabolt/src/tls.h b/src/seabolt/src/bolt/tls.h similarity index 97% rename from src/seabolt/src/tls.h rename to src/seabolt/src/bolt/tls.h index 41d6e819..bab57623 100644 --- a/src/seabolt/src/tls.h +++ b/src/seabolt/src/bolt/tls.h @@ -1,30 +1,30 @@ -/* - * Copyright (c) 2002-2018 "Neo4j," - * Neo4j Sweden AB [http://neo4j.com] - * - * This file is part of Neo4j. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#ifndef SEABOLT_ALL_TLS_H -#define SEABOLT_ALL_TLS_H - -#include "bolt-private.h" -#include "log.h" - -SEABOLT_EXPORT struct ssl_ctx_st* -create_ssl_ctx(struct BoltTrust* trust, const char* hostname, const struct BoltLog* log, const char* id); - -SEABOLT_EXPORT void free_ssl_context(struct ssl_ctx_st* ctx); - -#endif //SEABOLT_ALL_TLS_H +/* + * Copyright (c) 2002-2018 "Neo4j," + * Neo4j Sweden AB [http://neo4j.com] + * + * This file is part of Neo4j. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef SEABOLT_ALL_TLS_H +#define SEABOLT_ALL_TLS_H + +#include "bolt-private.h" +#include "log.h" + +SEABOLT_EXPORT struct ssl_ctx_st* +create_ssl_ctx(struct BoltTrust* trust, const char* hostname, const struct BoltLog* log, const char* id); + +SEABOLT_EXPORT void free_ssl_context(struct ssl_ctx_st* ctx); + +#endif //SEABOLT_ALL_TLS_H diff --git a/src/seabolt/src/utils.c b/src/seabolt/src/bolt/utils.c similarity index 100% rename from src/seabolt/src/utils.c rename to src/seabolt/src/bolt/utils.c diff --git a/src/seabolt/src/utils.h b/src/seabolt/src/bolt/utils.h similarity index 100% rename from src/seabolt/src/utils.h rename to src/seabolt/src/bolt/utils.h diff --git a/src/seabolt/src/v1.c b/src/seabolt/src/bolt/v1.c similarity index 100% rename from src/seabolt/src/v1.c rename to src/seabolt/src/bolt/v1.c diff --git a/src/seabolt/src/v1.h b/src/seabolt/src/bolt/v1.h similarity index 100% rename from src/seabolt/src/v1.h rename to src/seabolt/src/bolt/v1.h diff --git a/src/seabolt/src/v2.c b/src/seabolt/src/bolt/v2.c similarity index 100% rename from src/seabolt/src/v2.c rename to src/seabolt/src/bolt/v2.c diff --git a/src/seabolt/src/v2.h b/src/seabolt/src/bolt/v2.h similarity index 100% rename from src/seabolt/src/v2.h rename to src/seabolt/src/bolt/v2.h diff --git a/src/seabolt/src/v3.c b/src/seabolt/src/bolt/v3.c similarity index 100% rename from src/seabolt/src/v3.c rename to src/seabolt/src/bolt/v3.c diff --git a/src/seabolt/src/v3.h b/src/seabolt/src/bolt/v3.h similarity index 100% rename from src/seabolt/src/v3.h rename to src/seabolt/src/bolt/v3.h diff --git a/src/seabolt/src/values-private.h b/src/seabolt/src/bolt/values-private.h similarity index 100% rename from src/seabolt/src/values-private.h rename to src/seabolt/src/bolt/values-private.h diff --git a/src/seabolt/src/values.c b/src/seabolt/src/bolt/values.c similarity index 100% rename from src/seabolt/src/values.c rename to src/seabolt/src/bolt/values.c diff --git a/src/seabolt/src/values.h b/src/seabolt/src/bolt/values.h similarity index 100% rename from src/seabolt/src/values.h rename to src/seabolt/src/bolt/values.h diff --git a/src/seabolt/tests/integration.hpp b/src/seabolt/tests/integration.hpp index 67b28e72..03fad42a 100644 --- a/src/seabolt/tests/integration.hpp +++ b/src/seabolt/tests/integration.hpp @@ -23,14 +23,15 @@ #include #include -#include "bolt.h" +#include "bolt/bolt.h" extern "C" { -#include "address-private.h" -#include "config-private.h" -#include "connection-private.h" -#include "values-private.h" +#include "bolt/address-private.h" +#include "bolt/address-set-private.h" +#include "bolt/config-private.h" +#include "bolt/connection-private.h" +#include "bolt/values-private.h" } #define SETTING(name, default_value) ((char*)((getenv(name) == nullptr) ? (default_value) : getenv(name))) diff --git a/src/seabolt/tests/main.cpp b/src/seabolt/tests/main.cpp index 16363252..f0cf2786 100644 --- a/src/seabolt/tests/main.cpp +++ b/src/seabolt/tests/main.cpp @@ -21,10 +21,7 @@ #define CATCH_CONFIG_RUNNER // This tells Catch to provide a main() #include "catch.hpp" - -extern "C" { -#include "lifecycle.h" -} +#include "integration.hpp" int main(int argc, char* argv[]) { diff --git a/src/seabolt/tests/test-address-set.cpp b/src/seabolt/tests/test-address-set.cpp index ffb8c35f..fbb1aaf7 100644 --- a/src/seabolt/tests/test-address-set.cpp +++ b/src/seabolt/tests/test-address-set.cpp @@ -18,8 +18,7 @@ */ #include "catch.hpp" -#include "bolt.h" -#include "address-set-private.h" +#include "integration.hpp" SCENARIO("BoltAddressSet") { diff --git a/src/seabolt/tests/test-warden.cpp b/src/seabolt/tests/test-warden.cpp index 2f4dfd30..74653775 100644 --- a/src/seabolt/tests/test-warden.cpp +++ b/src/seabolt/tests/test-warden.cpp @@ -18,12 +18,10 @@ */ #include "catch.hpp" +#include "integration.hpp" extern "C" { -#include -#include -#include -#include "mem.h" +#include "bolt/mem.h" } SCENARIO("Test BoltMem_reverse_copy against memcpy") From 3dce7ca09113f926bc8b0c4f489c64a6171556a1 Mon Sep 17 00:00:00 2001 From: Ali Ince Date: Thu, 25 Oct 2018 13:33:43 +0100 Subject: [PATCH 03/11] Fixed a couple of typos in CMakeFiles --- CMakeLists.txt | 2 +- src/seabolt/src/CMakeLists.txt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 50ecea83..7d1a595e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -122,7 +122,7 @@ install( DESTINATION ${INSTALL_LIBDIR} COMPONENT lib PUBLIC_HEADER - DESTINATION ${INSTALL_INCLUDEDIR} + DESTINATION ${INSTALL_INCLUDEDIR}/bolt COMPONENT dev) install( diff --git a/src/seabolt/src/CMakeLists.txt b/src/seabolt/src/CMakeLists.txt index fcb29cac..4e94b28f 100644 --- a/src/seabolt/src/CMakeLists.txt +++ b/src/seabolt/src/CMakeLists.txt @@ -28,7 +28,7 @@ list(APPEND private_source_files ${CMAKE_CURRENT_LIST_DIR}/bolt/values.c) list(APPEND public_header_files - ${CMAKE_CURRENT_LIST_DIR}/bolt.h + ${CMAKE_CURRENT_LIST_DIR}/bolt/bolt.h ${CMAKE_CURRENT_LIST_DIR}/bolt/address.h ${CMAKE_CURRENT_LIST_DIR}/bolt/address-set.h ${CMAKE_CURRENT_LIST_DIR}/bolt/address-resolver.h From af30392e4a3052b33f7c2ea076f078d15b37954d Mon Sep 17 00:00:00 2001 From: Ali Ince Date: Thu, 25 Oct 2018 13:53:34 +0100 Subject: [PATCH 04/11] Introduce static target and link cli and tests against it --- CMakeLists.txt | 1 + cmake/Internals.cmake | 5 ++++ src/seabolt-cli/CMakeLists.txt | 2 +- src/seabolt/CMakeLists.txt | 1 + src/seabolt/src/CMakeLists.txt | 46 ++++++++++++++++++++++++++++++++ src/seabolt/tests/CMakeLists.txt | 2 +- 6 files changed, 55 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7d1a595e..8735ea16 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -110,6 +110,7 @@ include(InstallRequiredSystemLibraries) install( TARGETS ${SEABOLT_SHARED} + ${SEABOLT_STATIC} EXPORT ${SEABOLT_NAME}Targets ARCHIVE diff --git a/cmake/Internals.cmake b/cmake/Internals.cmake index 3d1dbfeb..f15ee0a5 100644 --- a/cmake/Internals.cmake +++ b/cmake/Internals.cmake @@ -87,6 +87,11 @@ endmacro() macro(set_names) set(SEABOLT_NAME "seabolt${PROJECT_VERSION_MAJOR}${PROJECT_VERSION_MINOR}" CACHE STRING "Seabolt version suffixed name") + if (CMAKE_SYSTEM_NAME STREQUAL "Windows") + set(SEABOLT_STATIC_NAME ${SEABOLT_NAME}-static) + else () + set(SEABOLT_STATIC_NAME ${SEABOLT_NAME}) + endif () set(SEABOLT_SHARED "seabolt-shared") set(SEABOLT_STATIC "seabolt-static") set(SEABOLT_TEST "seabolt-test") diff --git a/src/seabolt-cli/CMakeLists.txt b/src/seabolt-cli/CMakeLists.txt index bd446250..1a2419fb 100644 --- a/src/seabolt-cli/CMakeLists.txt +++ b/src/seabolt-cli/CMakeLists.txt @@ -6,4 +6,4 @@ target_sources(seabolt-cli target_link_libraries(seabolt-cli PRIVATE - ${SEABOLT_SHARED}) + ${SEABOLT_STATIC}) diff --git a/src/seabolt/CMakeLists.txt b/src/seabolt/CMakeLists.txt index b7ebf26b..70c2584e 100644 --- a/src/seabolt/CMakeLists.txt +++ b/src/seabolt/CMakeLists.txt @@ -1,4 +1,5 @@ add_library(${SEABOLT_SHARED} SHARED "") +add_library(${SEABOLT_STATIC} STATIC "") add_executable(${SEABOLT_TEST} "") include(${CMAKE_CURRENT_LIST_DIR}/src/CMakeLists.txt) diff --git a/src/seabolt/src/CMakeLists.txt b/src/seabolt/src/CMakeLists.txt index 4e94b28f..5140d338 100644 --- a/src/seabolt/src/CMakeLists.txt +++ b/src/seabolt/src/CMakeLists.txt @@ -48,6 +48,10 @@ target_sources(${SEABOLT_SHARED} PRIVATE ${private_source_files}) +target_sources(${SEABOLT_STATIC} + PRIVATE + ${private_source_files}) + target_compile_definitions(${SEABOLT_SHARED} PUBLIC IS_BIG_ENDIAN=$ @@ -58,12 +62,26 @@ target_compile_definitions(${SEABOLT_SHARED} INTERFACE $) +target_compile_definitions(${SEABOLT_STATIC} + PUBLIC + IS_BIG_ENDIAN=$ + USE_WINSOCK=$ + USE_POSIXSOCK=$ + USE_WINSSPI=$ + USE_OPENSSL=$ + INTERFACE + $) + if (ON_POSIX) set(CMAKE_THREAD_PREFER_PTHREAD ON) find_package(Threads REQUIRED) target_link_libraries(${SEABOLT_SHARED} PUBLIC pthread) + + target_link_libraries(${SEABOLT_STATIC} + PUBLIC + pthread) endif () if (ON_WINDOWS) @@ -79,6 +97,12 @@ if (WITH_TLS_SUPPORT AND WITH_TLS_OPENSSL) PUBLIC ${OPENSSL_SHARED_LIBRARIES} ${CMAKE_DL_LIBS}) + + target_link_libraries(${SEABOLT_STATIC} + PUBLIC + ${OPENSSL_STATIC_LIBRARIES} + ${CMAKE_DL_LIBS}) + if (ON_WINDOWS) target_link_libraries(${SEABOLT_SHARED} PUBLIC @@ -88,6 +112,10 @@ if (WITH_TLS_SUPPORT AND WITH_TLS_OPENSSL) target_include_directories(${SEABOLT_SHARED} PRIVATE ${OPENSSL_SHARED_INCLUDE_DIR}) + + target_include_directories(${SEABOLT_STATIC} + PRIVATE + ${OPENSSL_SHARED_INCLUDE_DIR}) endif () if (CMAKE_C_COMPILER_ID MATCHES AppleClang) @@ -124,12 +152,22 @@ generate_export_header(${SEABOLT_SHARED} BASE_NAME seabolt EXPORT_FILE_NAME "${CMAKE_BINARY_DIR}/${INSTALL_INCLUDEDIR}/bolt/bolt-exports.h" DEFINE_NO_DEPRECATED) +set_target_properties(${SEABOLT_STATIC} + PROPERTIES + COMPILE_FLAGS -DLIBSHARED_AND_STATIC_STATIC_DEFINE) + target_include_directories(${SEABOLT_SHARED} PUBLIC $ $ $) +target_include_directories(${SEABOLT_STATIC} + PUBLIC + $ + $ + $) + set_target_properties(${SEABOLT_SHARED} PROPERTIES POSITION_INDEPENDENT_CODE 1 @@ -139,3 +177,11 @@ set_target_properties(${SEABOLT_SHARED} OUTPUT_NAME "${SEABOLT_NAME}" PUBLIC_HEADER "${public_header_files};${CMAKE_BINARY_DIR}/${INSTALL_INCLUDEDIR}/bolt/bolt-exports.h;${CMAKE_BINARY_DIR}/${INSTALL_INCLUDEDIR}/bolt/bolt-version.h" WINDOWS_EXPORT_ALL_SYMBOLS OFF) + +set_target_properties(${SEABOLT_STATIC} + PROPERTIES + POSITION_INDEPENDENT_CODE 1 + CXX_VISIBILITY_PRESET hidden + VISIBILITY_INLINES_HIDDEN 1 + OUTPUT_NAME "${SEABOLT_STATIC_NAME}" + PUBLIC_HEADER "${public_header_files};${CMAKE_BINARY_DIR}/${INSTALL_INCLUDEDIR}/bolt/bolt-exports.h;${CMAKE_BINARY_DIR}/${INSTALL_INCLUDEDIR}/bolt/bolt-version.h") diff --git a/src/seabolt/tests/CMakeLists.txt b/src/seabolt/tests/CMakeLists.txt index d7def347..73dbde48 100644 --- a/src/seabolt/tests/CMakeLists.txt +++ b/src/seabolt/tests/CMakeLists.txt @@ -16,7 +16,7 @@ target_include_directories(seabolt-test target_link_libraries(seabolt-test PRIVATE - ${SEABOLT_SHARED} + ${SEABOLT_STATIC} catch2) include(ParseAndAddCatchTests) From 03c494725e7a3e7d809fed55c42dc8b3b42cab07 Mon Sep 17 00:00:00 2001 From: Ali Ince Date: Thu, 25 Oct 2018 14:37:46 +0100 Subject: [PATCH 05/11] Fix static library discovery problems for openssl on windows --- cmake/Internals.cmake | 28 +++++++++++++++++++++------- src/seabolt/src/CMakeLists.txt | 16 +++++++++++----- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/cmake/Internals.cmake b/cmake/Internals.cmake index f15ee0a5..91d6de09 100644 --- a/cmake/Internals.cmake +++ b/cmake/Internals.cmake @@ -22,23 +22,36 @@ macro(fix_compiler_settings) endif () endmacro() -macro(find_openssl_both) +macro(clear_openssl_cached_vars) unset(OPENSSL_FOUND CACHE) unset(OPENSSL_INCLUDE_DIR CACHE) unset(OPENSSL_SSL_LIBRARY CACHE) unset(OPENSSL_CRYPTO_LIBRARY CACHE) unset(OPENSSL_LIBRARIES CACHE) unset(OPENSSL_VERSION CACHE) + unset(_openssl_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES CACHE) + unset(_OPENSSL_MSVC_RT_MODE CACHE) + unset(_OPENSSL_MSVC_ARCH_SUFFIX CACHE) + unset(_OPENSSL_PATH_SUFFIXES CACHE) + unset(LIB_EAY_DEBUG CACHE) + unset(LIB_EAY_RELEASE CACHE) + unset(SSL_EAY_DEBUG CACHE) + unset(SSL_EAY_RELEASE CACHE) + unset(LIB_EAY_LIBRARY_DEBUG CACHE) + unset(LIB_EAY_LIBRARY_RELEASE CACHE) + unset(SSL_EAY_LIBRARY_DEBUG CACHE) + unset(SSL_EAY_LIBRARY_RELEASE CACHE) + unset(CMAKE_FIND_LIBRARY_SUFFIXES CACHE) +endmacro() + +macro(find_openssl_both) + clear_openssl_cached_vars() find_package(OpenSSL REQUIRED) set(OPENSSL_SHARED_INCLUDE_DIR ${OPENSSL_INCLUDE_DIR}) set(OPENSSL_SHARED_LIBRARIES ${OPENSSL_LIBRARIES}) + message(STATUS "Discovered OpenSSL shared libraries: ${OPENSSL_SHARED_LIBRARIES}") - unset(OPENSSL_FOUND CACHE) - unset(OPENSSL_INCLUDE_DIR CACHE) - unset(OPENSSL_SSL_LIBRARY CACHE) - unset(OPENSSL_CRYPTO_LIBRARY CACHE) - unset(OPENSSL_LIBRARIES CACHE) - unset(OPENSSL_VERSION CACHE) + clear_openssl_cached_vars() set(OPENSSL_USE_STATIC_LIBS ON) if (MSVC) set(OPENSSL_MSVC_STATIC_RT ON) @@ -47,6 +60,7 @@ macro(find_openssl_both) set(OPENSSL_STATIC_INCLUDE_DIR ${OPENSSL_INCLUDE_DIR}) set(OPENSSL_STATIC_LIBRARIES ${OPENSSL_LIBRARIES}) + message(STATUS "Discovered OpenSSL static libraries: ${OPENSSL_STATIC_LIBRARIES}") endmacro() macro(set_version) diff --git a/src/seabolt/src/CMakeLists.txt b/src/seabolt/src/CMakeLists.txt index 5140d338..85babf65 100644 --- a/src/seabolt/src/CMakeLists.txt +++ b/src/seabolt/src/CMakeLists.txt @@ -88,6 +88,9 @@ if (ON_WINDOWS) target_link_libraries(${SEABOLT_SHARED} PUBLIC ws2_32) + target_link_libraries(${SEABOLT_STATIC} + PUBLIC + ws2_32) endif () if (WITH_TLS_SUPPORT AND WITH_TLS_OPENSSL) @@ -100,13 +103,16 @@ if (WITH_TLS_SUPPORT AND WITH_TLS_OPENSSL) target_link_libraries(${SEABOLT_STATIC} PUBLIC - ${OPENSSL_STATIC_LIBRARIES} - ${CMAKE_DL_LIBS}) + ${OPENSSL_STATIC_LIBRARIES}) if (ON_WINDOWS) target_link_libraries(${SEABOLT_SHARED} PUBLIC crypt32) + + target_link_libraries(${SEABOLT_STATIC} + PUBLIC + crypt32) endif () target_include_directories(${SEABOLT_SHARED} @@ -152,9 +158,9 @@ generate_export_header(${SEABOLT_SHARED} BASE_NAME seabolt EXPORT_FILE_NAME "${CMAKE_BINARY_DIR}/${INSTALL_INCLUDEDIR}/bolt/bolt-exports.h" DEFINE_NO_DEPRECATED) -set_target_properties(${SEABOLT_STATIC} - PROPERTIES - COMPILE_FLAGS -DLIBSHARED_AND_STATIC_STATIC_DEFINE) +target_compile_definitions(${SEABOLT_STATIC} + PUBLIC + SEABOLT_STATIC_DEFINE) target_include_directories(${SEABOLT_SHARED} PUBLIC From 9d70bcfa1f5a0755f5151df15efcdae971774707 Mon Sep 17 00:00:00 2001 From: Ali Ince Date: Thu, 25 Oct 2018 14:51:03 +0100 Subject: [PATCH 06/11] Add DL library dependency to static target --- src/seabolt/src/CMakeLists.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/seabolt/src/CMakeLists.txt b/src/seabolt/src/CMakeLists.txt index 85babf65..513e1864 100644 --- a/src/seabolt/src/CMakeLists.txt +++ b/src/seabolt/src/CMakeLists.txt @@ -103,7 +103,8 @@ if (WITH_TLS_SUPPORT AND WITH_TLS_OPENSSL) target_link_libraries(${SEABOLT_STATIC} PUBLIC - ${OPENSSL_STATIC_LIBRARIES}) + ${OPENSSL_STATIC_LIBRARIES} + ${CMAKE_DL_LIBS}) if (ON_WINDOWS) target_link_libraries(${SEABOLT_SHARED} From 8252e1301d7790f0db14a4790f84e3c1d7eafec5 Mon Sep 17 00:00:00 2001 From: Ali Ince Date: Thu, 25 Oct 2018 16:01:58 +0100 Subject: [PATCH 07/11] Remove Libs.private section from pkg-config file --- CMakeLists.txt | 1 - cmake/Internals.cmake | 36 ++++++++++++++++++------------------ 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8735ea16..1c5838ae 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -162,7 +162,6 @@ Description: The C Connector Library for Neo4j\n\ Version: ${PROJECT_VERSION}\n\ CFlags: -I\${includedir}\n\ Libs: -L\${libdir} -l\${name}\n\ -Libs.private: $\n\ ") install( diff --git a/cmake/Internals.cmake b/cmake/Internals.cmake index 91d6de09..834af72c 100644 --- a/cmake/Internals.cmake +++ b/cmake/Internals.cmake @@ -29,29 +29,29 @@ macro(clear_openssl_cached_vars) unset(OPENSSL_CRYPTO_LIBRARY CACHE) unset(OPENSSL_LIBRARIES CACHE) unset(OPENSSL_VERSION CACHE) - unset(_openssl_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES CACHE) - unset(_OPENSSL_MSVC_RT_MODE CACHE) - unset(_OPENSSL_MSVC_ARCH_SUFFIX CACHE) - unset(_OPENSSL_PATH_SUFFIXES CACHE) - unset(LIB_EAY_DEBUG CACHE) - unset(LIB_EAY_RELEASE CACHE) - unset(SSL_EAY_DEBUG CACHE) - unset(SSL_EAY_RELEASE CACHE) - unset(LIB_EAY_LIBRARY_DEBUG CACHE) - unset(LIB_EAY_LIBRARY_RELEASE CACHE) - unset(SSL_EAY_LIBRARY_DEBUG CACHE) - unset(SSL_EAY_LIBRARY_RELEASE CACHE) - unset(CMAKE_FIND_LIBRARY_SUFFIXES CACHE) + unset(_openssl_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES CACHE) + unset(_OPENSSL_MSVC_RT_MODE CACHE) + unset(_OPENSSL_MSVC_ARCH_SUFFIX CACHE) + unset(_OPENSSL_PATH_SUFFIXES CACHE) + unset(LIB_EAY_DEBUG CACHE) + unset(LIB_EAY_RELEASE CACHE) + unset(SSL_EAY_DEBUG CACHE) + unset(SSL_EAY_RELEASE CACHE) + unset(LIB_EAY_LIBRARY_DEBUG CACHE) + unset(LIB_EAY_LIBRARY_RELEASE CACHE) + unset(SSL_EAY_LIBRARY_DEBUG CACHE) + unset(SSL_EAY_LIBRARY_RELEASE CACHE) + unset(CMAKE_FIND_LIBRARY_SUFFIXES CACHE) endmacro() macro(find_openssl_both) - clear_openssl_cached_vars() + clear_openssl_cached_vars() find_package(OpenSSL REQUIRED) set(OPENSSL_SHARED_INCLUDE_DIR ${OPENSSL_INCLUDE_DIR}) set(OPENSSL_SHARED_LIBRARIES ${OPENSSL_LIBRARIES}) - message(STATUS "Discovered OpenSSL shared libraries: ${OPENSSL_SHARED_LIBRARIES}") + message(STATUS "Discovered OpenSSL shared libraries: ${OPENSSL_SHARED_LIBRARIES}") - clear_openssl_cached_vars() + clear_openssl_cached_vars() set(OPENSSL_USE_STATIC_LIBS ON) if (MSVC) set(OPENSSL_MSVC_STATIC_RT ON) @@ -60,7 +60,7 @@ macro(find_openssl_both) set(OPENSSL_STATIC_INCLUDE_DIR ${OPENSSL_INCLUDE_DIR}) set(OPENSSL_STATIC_LIBRARIES ${OPENSSL_LIBRARIES}) - message(STATUS "Discovered OpenSSL static libraries: ${OPENSSL_STATIC_LIBRARIES}") + message(STATUS "Discovered OpenSSL static libraries: ${OPENSSL_STATIC_LIBRARIES}") endmacro() macro(set_version) @@ -115,4 +115,4 @@ macro(configure_rpath) set(CMAKE_SKIP_RPATH OFF) set(CMAKE_BUILD_RPATH ${CMAKE_CURRENT_BINARY_DIR}/lib) set(CMAKE_MACOSX_RPATH ON) -endmacro() \ No newline at end of file +endmacro() From cdbece5d9ea7212906f49855a9609e1921cb847c Mon Sep 17 00:00:00 2001 From: Ali Ince Date: Thu, 25 Oct 2018 16:45:37 +0100 Subject: [PATCH 08/11] Add legacy stdio definitions library for older versions of MSVC --- src/seabolt/src/CMakeLists.txt | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/seabolt/src/CMakeLists.txt b/src/seabolt/src/CMakeLists.txt index 513e1864..65bbb819 100644 --- a/src/seabolt/src/CMakeLists.txt +++ b/src/seabolt/src/CMakeLists.txt @@ -91,6 +91,12 @@ if (ON_WINDOWS) target_link_libraries(${SEABOLT_STATIC} PUBLIC ws2_32) + + if (MSVC) + target_link_libraries(${SEABOLT_STATIC} + PUBLIC + legacy_stdio_definitions) + endif() endif () if (WITH_TLS_SUPPORT AND WITH_TLS_OPENSSL) From 326082dac9a09a5ddfab067ecab9e2441316926b Mon Sep 17 00:00:00 2001 From: Ali Ince Date: Thu, 25 Oct 2018 22:05:31 +0100 Subject: [PATCH 09/11] Temporary workaround for unresolved __iob function --- src/seabolt/src/bolt/tls.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/seabolt/src/bolt/tls.c b/src/seabolt/src/bolt/tls.c index 8a0e615a..5b8bf848 100644 --- a/src/seabolt/src/bolt/tls.c +++ b/src/seabolt/src/bolt/tls.c @@ -162,3 +162,13 @@ void free_ssl_context(SSL_CTX* ctx) SSL_CTX_free(ctx); } +#if USE_WINSOCK && defined(SEABOLT_STATIC_DEFINE) && defined(_MSC_VER) +FILE * __cdecl __iob_func(void) +{ + FILE* _iob = (FILE*)malloc(3 * sizeof(FILE)); + _iob[0] = *stdin; + _iob[1] = *stdout; + _iob[2] = *stderr; + return _iob; +} +#endif \ No newline at end of file From 6aaaff028e6a9cea1ac69de175614689ca57a912 Mon Sep 17 00:00:00 2001 From: Ali Ince Date: Thu, 25 Oct 2018 22:08:39 +0100 Subject: [PATCH 10/11] Fix boundary problem with string builder --- src/seabolt/src/bolt/string-builder.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/seabolt/src/bolt/string-builder.c b/src/seabolt/src/bolt/string-builder.c index c43913f8..00744aee 100644 --- a/src/seabolt/src/bolt/string-builder.c +++ b/src/seabolt/src/bolt/string-builder.c @@ -49,7 +49,7 @@ void StringBuilder_ensure_buffer(struct StringBuilder* builder, int size_to_add) void StringBuilder_append(struct StringBuilder* builder, const char* string) { - StringBuilder_append_n(builder, string, (int)SIZE_OF_C_STRING(string)); + StringBuilder_append_n(builder, string, (int)strlen(string)); } void StringBuilder_append_n(struct StringBuilder* builder, const char* string, const int len) From e3a6ce92aeeb4c8851b5825bd92e09bb889279d8 Mon Sep 17 00:00:00 2001 From: Ali Ince Date: Thu, 25 Oct 2018 22:10:50 +0100 Subject: [PATCH 11/11] Add newline at the end of the file --- src/seabolt/src/bolt/tls.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/seabolt/src/bolt/tls.c b/src/seabolt/src/bolt/tls.c index 5b8bf848..bd5b47b0 100644 --- a/src/seabolt/src/bolt/tls.c +++ b/src/seabolt/src/bolt/tls.c @@ -171,4 +171,4 @@ FILE * __cdecl __iob_func(void) _iob[2] = *stderr; return _iob; } -#endif \ No newline at end of file +#endif