Skip to content

Commit

Permalink
Merge branch 'main' into fix_crash_on_disable_enable
Browse files Browse the repository at this point in the history
  • Loading branch information
ekoby authored Jun 9, 2022
2 parents fa560ff + 0f85dc7 commit bacbb54
Showing 11 changed files with 161 additions and 19 deletions.
4 changes: 2 additions & 2 deletions deps/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -3,8 +3,8 @@ project(ziti-deps)
include(FetchContent)

FetchContent_Declare(uv-mbed
GIT_REPOSITORY https://github.com/netfoundry/uv-mbed.git
GIT_TAG v0.14.4
GIT_REPOSITORY https://github.com/openziti/uv-mbed.git
GIT_TAG v0.14.7
)
set(ENABLE_UM_TESTS OFF CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(uv-mbed)
3 changes: 3 additions & 0 deletions includes/ziti/model_support.h
Original file line number Diff line number Diff line change
@@ -26,6 +26,7 @@

#include "externs.h"
#include "model_collections.h"
#include "types.h"

#if !defined(__DEFINED_ssize_t) && !defined(__ssize_t_defined)
#if _WIN32
@@ -209,6 +210,8 @@ ZITI_FUNC extern type_meta *get_json_meta();

ZITI_FUNC extern type_meta *get_model_map_meta();

ZITI_FUNC extern type_meta *get_duration_meta();

typedef struct timeval timestamp;

int model_map_compare(const model_map *lh, const model_map *rh, type_meta *m);
43 changes: 43 additions & 0 deletions includes/ziti/types.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) 2022. NetFoundry Inc.
//
// 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
//
// https://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 ZITI_SDK_TYPES_H
#define ZITI_SDK_TYPES_H

#include <stddef.h>
#include <stdint.h>

#include "externs.h"

#ifdef __cplusplus
extern "C" {
#endif

/** Nanosecond precision time duration, like Golang's time.Duration */
typedef int64_t duration;

#define DURATION_MILLISECONDS(d) ((d)/MILLISECOND)

#define MICROSECOND ((int64_t)1000)
#define MILLISECOND (1000 * MICROSECOND)
#define SECOND (1000 * MILLISECOND)
#define MINUTE (60 * SECOND)
#define HOUR (60 * MINUTE)

#ifdef __cplusplus
}
#endif

#endif //ZITI_SDK_TYPES_H
19 changes: 18 additions & 1 deletion includes/ziti/ziti_model.h
Original file line number Diff line number Diff line change
@@ -128,13 +128,26 @@ XX(port, int, none, port, __VA_ARGS__) \
XX(forward_port, bool, none, forwardPort, __VA_ARGS__) \
XX(allowed_port_ranges, ziti_port_range, array, allowedPortRanges, __VA_ARGS__) \
XX(allowed_source_addresses, ziti_address, array, allowedSourceAddresses, __VA_ARGS__) \
XX(listen_options, tag, map, listenOptions, __VA_ARGS__)
XX(listen_options, ziti_listen_options, ptr, listenOptions, __VA_ARGS__)

#define ZITI_HOST_CFG_V2_MODEL(XX, ...) \
XX(terminators, ziti_host_cfg_v1, list, terminators, __VA_ARGS__)

#define ZITI_MFA_ENROLLMENT_MODEL(XX, ...) \
XX(is_verified, bool, none, isVerified, __VA_ARGS__) \
XX(recovery_codes, string, array, recoveryCodes, __VA_ARGS__) \
XX(provisioning_url, string, none, provisioningUrl, __VA_ARGS__)

#define ZITI_LISTEN_OPTS_MODEL(XX, ...) \
XX(bind_with_identity, bool, none, bindUsingEdgeIdentity, __VA_ARGS__) \
XX(connect_timeout, duration, none, connectTimeout, __VA_ARGS__) \
XX(connect_timeout_seconds, int, none, connectTimeoutSeconds, __VA_ARGS__) \
XX(cost, int, none, cost, __VA_ARGS__) \
XX(identity, string, none, identity, __VA_ARGS__) \
XX(max_connections, int, none, maxConnections, __VA_ARGS__)\
XX(precendence, string, none, precendence, __VA_ARGS__)


#ifdef __cplusplus
extern "C" {
#endif
@@ -203,10 +216,14 @@ DECLARE_MODEL(ziti_port_range, ZITI_PORT_RANGE_MODEL)

DECLARE_MODEL(ziti_intercept_cfg_v1, ZITI_INTERCEPT_CFG_V1_MODEL)

DECLARE_MODEL(ziti_listen_options, ZITI_LISTEN_OPTS_MODEL)

DECLARE_MODEL(ziti_server_cfg_v1, ZITI_SERVER_CFG_V1_MODEL)

DECLARE_MODEL(ziti_host_cfg_v1, ZITI_HOST_CFG_V1_MODEL)

DECLARE_MODEL(ziti_host_cfg_v2, ZITI_HOST_CFG_V2_MODEL)

DECLARE_MODEL(ziti_auth_query_mfa, ZITI_AUTH_QUERY_MFA_MODEL)

DECLARE_MODEL(ziti_mfa_enrollment, ZITI_MFA_ENROLLMENT_MODEL)
1 change: 1 addition & 0 deletions library/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@ set(ZITI_HEADER_FILES
${PROJECT_SOURCE_DIR}/includes/ziti/ziti_buffer.h
${PROJECT_SOURCE_DIR}/includes/ziti/zitilib.h
${PROJECT_SOURCE_DIR}/includes/ziti/model_collections.h
${PROJECT_SOURCE_DIR}/includes/ziti/types.h
)

SET(ZITI_SRC_FILES
2 changes: 0 additions & 2 deletions library/channel.c
Original file line number Diff line number Diff line change
@@ -698,8 +698,6 @@ static void reconnect_cb(uv_timer_t *t) {

ch->state = Connecting;

uv_mbed_free(&ch->connection);
uv_mbed_init(ch->loop, &ch->connection, ch->connection.tls);
ch->connection.data = ch;
CH_LOG(DEBUG, "connecting to %s:%d", ch->host, ch->port);
int rc = uv_mbed_connect(req, &ch->connection, ch->host, ch->port, on_channel_connect_internal);
4 changes: 4 additions & 0 deletions library/internal_model.c
Original file line number Diff line number Diff line change
@@ -44,10 +44,14 @@ IMPL_MODEL(ziti_port_range, ZITI_PORT_RANGE_MODEL)

IMPL_MODEL(ziti_intercept_cfg_v1, ZITI_INTERCEPT_CFG_V1_MODEL)

IMPL_MODEL(ziti_listen_options, ZITI_LISTEN_OPTS_MODEL)

IMPL_MODEL(ziti_server_cfg_v1, ZITI_SERVER_CFG_V1_MODEL)

IMPL_MODEL(ziti_host_cfg_v1, ZITI_HOST_CFG_V1_MODEL)

IMPL_MODEL(ziti_host_cfg_v2, ZITI_HOST_CFG_V2_MODEL)

IMPL_MODEL(ziti_id_cfg, ZITI_ID_CFG_MODEL)

IMPL_MODEL(ziti_config, ZITI_CONFIG_MODEL)
61 changes: 59 additions & 2 deletions library/model_support.c
Original file line number Diff line number Diff line change
@@ -1223,6 +1223,7 @@ static void _free_map(model_map *m) {
}

static type_meta bool_META = {
.name = "bool",
.size = sizeof(bool),
.comparer = (_cmp_f) _cmp_bool,
.parser = (_parse_f) (_parse_bool),
@@ -1231,6 +1232,7 @@ static type_meta bool_META = {
};

static type_meta int_META = {
.name = "int",
.size = sizeof(int),
.comparer = (_cmp_f) _cmp_int,
.parser = (_parse_f) _parse_int,
@@ -1239,6 +1241,7 @@ static type_meta int_META = {
};

static type_meta string_META = {
.name = "string",
.size = sizeof(char *),
.comparer = (_cmp_f) _cmp_string,
.parser = (_parse_f) _parse_string,
@@ -1247,6 +1250,7 @@ static type_meta string_META = {
};

static type_meta timestamp_META = {
.name = "timestamp",
.size = sizeof(struct timeval),
.comparer = (_cmp_f) _cmp_timeval,
.parser = (_parse_f) _parse_timeval,
@@ -1255,6 +1259,7 @@ static type_meta timestamp_META = {
};

static type_meta json_META = {
.name = "json",
.size = sizeof(char *),
.comparer = (_cmp_f) _cmp_string,
.parser = (_parse_f) _parse_json,
@@ -1263,6 +1268,7 @@ static type_meta json_META = {
};

static type_meta map_META = {
.name = "map",
.size = sizeof(model_map),
.comparer = (_cmp_f) _cmp_map,
.parser = (_parse_f) _parse_map,
@@ -1271,12 +1277,12 @@ static type_meta map_META = {
};

static type_meta tag_META = {
.name = "tag",
.size = sizeof(tag),
.comparer = (_cmp_f) _cmp_tag,
.parser = (_parse_f) _parse_tag,
.jsonifier = (_to_json_f) tag_to_json,
.destroyer = (_free_f) _free_tag,

};

type_meta *get_bool_meta() { return &bool_META; }
@@ -1291,4 +1297,55 @@ type_meta *get_json_meta() { return &json_META; }

type_meta *get_model_map_meta() { return &map_META; }

type_meta *get_tag_meta() { return &tag_META; }
type_meta *get_tag_meta() { return &tag_META; }

static int cmp_duration (const duration *lh, const duration *rh) {
null_checks(lh, rh)
duration diff = *lh - *rh;
return diff < 0 ? -1 : (diff > 0 ? 1 : 0);
}

static int parse_duration(duration *val, const char *json, jsmntok_t *tok) {
char *str;
if (tok->type != JSMN_STRING) return -1;
const char *start = json + tok->start;
const char *end = json + tok->end;
char *endp;
duration v = (duration) strtol(start, &endp, 10);
size_t tu_len = end - endp;
if (tu_len == 1) { // single char timeunit: s,m,h
switch (*endp) {
case 's': v *= SECOND; break;
case 'm': v *= MINUTE; break;
case 'h': v *= HOUR; break;
default: return -1;
}
} else if (tu_len == 2) {
if (strncmp(endp, "ms", 2) == 0) {
v *= MILLISECOND;
} else {
return -1;
}
} else {
return -1;
}

*val = v;
return 1;
}

static int duration_to_json(duration *d, string_buf_t *buf, int indent, int flags) {
char json[32];
int rc = snprintf(json, sizeof(json), "\"%lldms\"", (long long)DURATION_MILLISECONDS(*d));
return string_buf_append(buf, json);
}
type_meta *get_duration_meta() {
static type_meta _meta = {
.name = "duration",
.comparer = (_cmp_f) cmp_duration,
.parser = (_parse_f) parse_duration,
.jsonifier = (_to_json_f) duration_to_json,
.destroyer = _free_noop,
};
return &_meta;
}
1 change: 1 addition & 0 deletions library/ziti.c
Original file line number Diff line number Diff line change
@@ -995,6 +995,7 @@ static void update_services(ziti_service_array services, const ziti_error *error
ZTX_LOG(VERBOSE, "api session partially authenticated, waiting for api session state change");
return;
} else {
FREE(ztx->last_update);
update_ctrl_status(ztx, ZITI_CONTROLLER_UNAVAILABLE, error->message);
}
return;
11 changes: 0 additions & 11 deletions programs/ziti-prox-c/proxy.c
Original file line number Diff line number Diff line change
@@ -272,17 +272,6 @@ static void update_listener(ziti_service *service, int status, struct listener *
TRY(uv, uv_tcp_bind(&l->server, (const struct sockaddr *) addr, 0));
TRY(uv, uv_listen((uv_stream_t *) &l->server, 5, on_client));
free(addr);

// this is for illustration purposes only
ziti_client_cfg_v1 intercept;
int rc = ziti_service_get_config(service, "ziti-tunneler-client.v1", &intercept,
(int (*)(void *, const char *, size_t)) parse_ziti_client_cfg_v1);
if (rc < 0) {
ZITI_LOG(ERROR, "failed to parse client intercept");
} else {
ZITI_LOG(INFO, "should intercepting %s:%d", intercept.hostname, intercept.port);
free_ziti_client_cfg_v1(&intercept);
}
} else {
if (uv_is_active((const uv_handle_t *) &l->server)) {
ZITI_LOG(WARN, "service %s is not available. stopping listener[%d]", l->service_name, l->port);
31 changes: 30 additions & 1 deletion tests/model_tests.cpp
Original file line number Diff line number Diff line change
@@ -768,4 +768,33 @@ TEST_CASE("lists model", "[model]") {
printf("%s\n", json_out);

free_ListsObj(&lists);
}
}

#define DURATION_MODEL(XX, ...) \
XX(timeout, duration, none, timeout, __VA_ARGS__)
DECLARE_MODEL(Duration, DURATION_MODEL)
IMPL_MODEL(Duration, DURATION_MODEL)

TEST_CASE("parse duration", "[model]") {
using std::make_tuple;
std::string test_input;
duration expected_output = 3 * SECOND;
std::tie( test_input, expected_output ) =
GENERATE( table<std::string, duration>(
{
make_tuple(R"({"timeout": "3s"})", (duration)(3 * SECOND)),
make_tuple(R"({"timeout": "3m"})", (duration)(3 * MINUTE)),
make_tuple(R"({"timeout": "3h"})", 3 * HOUR),
make_tuple(R"({"timeout": "3ms"})", 3 * MILLISECOND)
})
);


Duration d = {0};
// run the test
CHECK(parse_Duration(&d, test_input.c_str(), test_input.length()) == test_input.length());
// capture the input data to go with the outputs.
CAPTURE(test_input);
// check it matches the pre-calculated data
REQUIRE(d.timeout == expected_output);
}

0 comments on commit bacbb54

Please sign in to comment.