Skip to content
This repository has been archived by the owner on Sep 14, 2019. It is now read-only.

Commit

Permalink
Merge pull request #7 from mwielpue/bugfix/EAF-587-java-links-cannot-…
Browse files Browse the repository at this point in the history
…connect

EAF-587 the broker will now change the host address to something comp…
  • Loading branch information
Norbert Heusser authored Nov 7, 2017
2 parents 7092afd + 3feb6a1 commit a87486e
Show file tree
Hide file tree
Showing 9 changed files with 186 additions and 24 deletions.
2 changes: 1 addition & 1 deletion broker/include/broker/extension.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ extern "C" {
#endif

struct ExtensionConfig {
char* brokerUrl;
const char* brokerUrl;
uv_loop_t* loop;
};

Expand Down
2 changes: 2 additions & 0 deletions broker/include/broker/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ void broker_utils_send_static_list_resp(RemoteDSLink *link, json_t *req);

void broker_utils_send_disconnected_list_resp(RemoteDSLink *link, json_t *req);

const char* setHostFrom(const char* protocol,const char* address, const char* port);

#ifdef __cplusplus
}
#endif
Expand Down
25 changes: 3 additions & 22 deletions broker/src/broker.c
Original file line number Diff line number Diff line change
Expand Up @@ -471,17 +471,10 @@ void broker_stop(Broker* broker) {
dslink_free(extension);
}
}
dslink_free(broker->extensionConfig.brokerUrl);
dslink_free((void*)broker->extensionConfig.brokerUrl);
dslink_list_free_all_nodes(&broker->extensions);
}

static int isipv6address(const char* host)
{
int i = 0;
for(; host[i]; host[i]==':' ? i++ : *host++);
return i>0;
}

int broker_init_extensions(Broker* broker, json_t* config) {
list_init(&broker->extensions);

Expand Down Expand Up @@ -571,26 +564,14 @@ int broker_init_extensions(Broker* broker, json_t* config) {
return -1;
}

int len = strlen(httpsHost)+strlen(httpsPort)+16+1;
broker->extensionConfig.brokerUrl = dslink_malloc(len);
if(isipv6address(httpsHost)) {
snprintf(broker->extensionConfig.brokerUrl, len, "https://[%s]:%s/conn", httpsHost, httpsPort);
} else {
snprintf(broker->extensionConfig.brokerUrl, len, "https://%s:%s/conn", httpsHost, httpsPort);
}
broker->extensionConfig.brokerUrl = setHostFrom("https", httpsHost, httpsPort);
} else {
if(!httpEnabled) {
log_err("Cannot load extensions. At least http has to be enabled.");
return -1;
}

int len = strlen(httpHost)+strlen(httpPort)+15+1;
broker->extensionConfig.brokerUrl = dslink_malloc(len);
if(isipv6address(httpHost)) {
snprintf(broker->extensionConfig.brokerUrl, len, "http://[%s]:%s/conn", httpHost, httpPort);
} else {
snprintf(broker->extensionConfig.brokerUrl, len, "http://%s:%s/conn", httpHost, httpPort);
}
broker->extensionConfig.brokerUrl = setHostFrom("http", httpHost, httpPort);
}

broker->extensionConfig.loop = mainLoop;
Expand Down
23 changes: 23 additions & 0 deletions broker/src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include "broker/net/ws.h"
#include "broker/utils.h"

#include <string.h>

void broker_free_handle(uv_handle_t *handle) {
dslink_free(handle);
}
Expand Down Expand Up @@ -108,3 +110,24 @@ void broker_utils_send_disconnected_list_resp(RemoteDSLink *link, json_t *req) {
broker_ws_send_obj(link, top);
json_decref(top);
}

const char* setHostFrom(const char* protocol,const char* address, const char* port)
{
char* host = NULL;

if(dslink_isipv6address(address)) {
const char* temp = dslink_checkIpv6Address(address);
int len = strlen(temp)+strlen(port)+16+1;
host = dslink_malloc(len);
snprintf(host, len, "%s://[%s]:%s/conn", protocol, temp, port);
} else {
const char* temp = dslink_checkIpv4Address(address);
int len = strlen(temp)+strlen(port)+16+1;
host = dslink_malloc(len);
snprintf(host, len, "%s://%s:%s/conn", protocol, temp, port);
}

return host;
}


4 changes: 4 additions & 0 deletions sdk/include/dslink/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ size_t dslink_create_ts(char *buf, size_t bufLen);

int dslink_sleep(long ms);

const char* dslink_checkIpv4Address(const char* address);
const char* dslink_checkIpv6Address(const char* address);
int dslink_isipv6address(const char* host);

#ifdef __cplusplus
}
#endif
Expand Down
36 changes: 36 additions & 0 deletions sdk/src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,39 @@ int dslink_sleep(long ms) {

return nanosleep(&req, NULL);
}

const char* dslink_checkIpv4Address(const char* address)
{
const char* host = address;
if(strcmp("0.0.0.0", address) == 0) {
static char* localhost = "127.0.0.1";
host = localhost;
}

return host;
}

const char* dslink_checkIpv6Address(const char* address)
{
const char* host = address;
static char* localhost = "::1";

if(strcmp("::/128", address) == 0 || strcmp("::/0", address) == 0) {
host = localhost;
} else {
size_t span = strspn(address, "0:");
if(address[span] == '\0') {
host = localhost;
}
}

return host;
}

int dslink_isipv6address(const char* host)
{
int i = 0;
for(; host[i]; host[i]==':' ? i++ : *host++);
return i>0;
}

1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ set(SDK_TEST_SET

set(BROKER_TEST_SET
"node_test"
"utils_test"
)

function(add_memcheck_test name)
Expand Down
37 changes: 37 additions & 0 deletions test/broker/utils_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "cmocka_init.h"

#include <broker/utils.h>

static
void setHostFrom_test(void** state)
{
(void) state;

assert_string_equal(setHostFrom("http", "localhost", "12345"), "http://localhost:12345/conn" );
assert_string_equal(setHostFrom("http", "anyhost.de", "12345"), "http://anyhost.de:12345/conn" );
assert_string_equal(setHostFrom("http", "1.2.3.4", "12345"), "http://1.2.3.4:12345/conn" );
assert_string_equal(setHostFrom("http", "0:1:2:3:4:5:6:7", "8888"), "http://[0:1:2:3:4:5:6:7]:8888/conn" );
assert_string_equal(setHostFrom("http", "0:1:2::7:8", "8888"), "http://[0:1:2::7:8]:8888/conn" );

// Special case IPv4 unspecified address
assert_string_equal(setHostFrom("http", "0.0.0.0", "12345"), "http://127.0.0.1:12345/conn" );

// Special case IPv6 unspecified address
assert_string_equal(setHostFrom("http", "::/0", "12345"), "http://[::1]:12345/conn" );
assert_string_equal(setHostFrom("http", "::/128", "23456"), "http://[::1]:23456/conn" );
assert_string_equal(setHostFrom("http", "::1", "1234"), "http://[::1]:1234/conn" );
assert_string_equal(setHostFrom("http", "0:0:0::0", "12345"), "http://[::1]:12345/conn" );
assert_string_equal(setHostFrom("http", "0:0::0:0", "12345"), "http://[::1]:12345/conn" );
assert_string_equal(setHostFrom("http", "0:0:0:0:0:0:0:0", "12345"), "http://[::1]:12345/conn" );


}


int main() {
const struct CMUnitTest tests[] = {
cmocka_unit_test(setHostFrom_test)
};

return cmocka_run_group_tests(tests, NULL, NULL);
}
80 changes: 79 additions & 1 deletion test/sdk/utils_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,90 @@ void url_parse_test(void **state)
assert_true(8463u == url->port);
assert_string_equal("/conn", url->uri);
}
{
const char address[] = "http://[::/128]:8100/conn";

Url* url = dslink_url_parse(address);
assert_non_null(url);
assert_string_equal("::/128", url->host);
assert_string_equal("http", url->scheme);
assert_false(url->secure);
assert_true(8100u == url->port);
assert_string_equal("/conn", url->uri);
}
}

static
void ipv6_test(void** state)
{
(void)state;

const char* ipv6 = "::1";
assert_true(dslink_isipv6address(ipv6));
ipv6 = "2001:420:44e7:1300:b039:6611:2e10:e963";
assert_true(dslink_isipv6address(ipv6));
ipv6 = "2001:db8::ff00:42:8329";
assert_true(dslink_isipv6address(ipv6));
ipv6 = "::192.0.2.128";
assert_true(dslink_isipv6address(ipv6));
ipv6 = "::1/128";
assert_true(dslink_isipv6address(ipv6));

const char* ipv4 = "127.0.0.1";
assert_false(dslink_isipv6address(ipv4));
ipv4 = "0.0.0.0";
assert_false(dslink_isipv6address(ipv4));
ipv4 = "127.0.0.1";
assert_false(dslink_isipv6address(ipv4));
}

static
void checkIpv4Address_test(void** state)
{
(void)state;

const char* ipv4 = "127.0.0.1";
const char* host = dslink_checkIpv4Address(ipv4);
assert_string_equal("127.0.0.1", host);

ipv4 = "0.0.0.0";
host = dslink_checkIpv4Address(ipv4);
assert_string_equal("127.0.0.1", host);
}

static
void checkIpv6Address_test(void** state)
{
(void)state;

const char* ipv6 = "2001:420:44e7:1300:b039:6611:2e10:e963";
const char* host = dslink_checkIpv6Address(ipv6);
assert_string_equal("2001:420:44e7:1300:b039:6611:2e10:e963", host);

ipv6 = "::1";
host = dslink_checkIpv6Address(ipv6);
assert_string_equal("::1", host);

ipv6 = "0:0:0:0:0:0:0:0";
host = dslink_checkIpv6Address(ipv6);
assert_string_equal("::1", host);

ipv6 = "::/128";
host = dslink_checkIpv6Address(ipv6);
assert_string_equal("::1", host);

ipv6 = "::/0";
host = dslink_checkIpv6Address(ipv6);
assert_string_equal("::1", host);
}

int main() {
const struct CMUnitTest tests[] = {
cmocka_unit_test(utils_str_replace_all_test),
cmocka_unit_test(url_parse_test)
cmocka_unit_test(url_parse_test),
cmocka_unit_test(ipv6_test),
cmocka_unit_test(checkIpv4Address_test),
cmocka_unit_test(checkIpv6Address_test)
};

return cmocka_run_group_tests(tests, NULL, NULL);
Expand Down

0 comments on commit a87486e

Please sign in to comment.