Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multi-tun support for windows #1020

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/ziti-tunnel/include/ziti/ziti_tunnel.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ extern "C" {
#define MAXPATHLEN PATH_MAX
#endif

#define DEFAULT_EXECUTABLE_NAME "ziti-edge-tunnel"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrong place for it


/** keys used in app_data model map */
extern const char *DST_PROTO_KEY; // "dst_protocol"
extern const char *DST_IP_KEY; // "dst_ip"
Expand Down Expand Up @@ -201,6 +203,7 @@ extern void ziti_tunnel_set_log_level(int lvl);
typedef void (*ziti_tunnel_async_fn)(uv_loop_t *loop, void *ctx);
extern void ziti_tunnel_async_send(tunneler_context tctx, ziti_tunnel_async_fn f, void *arg);

size_t find_other_zets(model_list* ipcs, const char* base, const char* prefix);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrong place -- should not be in the library code


#define TNL_IP_MEM_POOL(XX, ...) \
XX(name, model_string, none, Name, __VA_ARGS__) \
Expand Down
3 changes: 1 addition & 2 deletions programs/ziti-edge-tunnel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ set(ZITI_INSTANCE_COMMON
include/model/events.h
include/model/dtos.h
instance.c
include/identity-utils.h
include/instance.h
config-utils.c
include/config-utils.h
instance-config.c
include/instance-config.h
)
Expand Down
37 changes: 11 additions & 26 deletions programs/ziti-edge-tunnel/config-utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,19 @@
#include <unistd.h>
#endif

const char* app_data = "APPDATA";
#if _WIN32
#define realpath(rel, abs) _fullpath(abs, rel, FILENAME_MAX)
#endif

static char* identifier_path = NULL;

char* get_system_config_path() {
char* config_path = malloc(FILENAME_MAX * sizeof(char));
char* get_system_config_path(const char* base_dir) {
char actual_base_path[FILENAME_MAX];
realpath(base_dir, actual_base_path);

char* config_path = calloc(FILENAME_MAX, sizeof(char) + 1);
#if _WIN32
snprintf(config_path, FILENAME_MAX, "%s\\NetFoundry", getenv(app_data));
snprintf(config_path, FILENAME_MAX, "%s%cNetFoundry", actual_base_path, PATH_SEP);
#elif __linux__
snprintf(config_path, FILENAME_MAX, "/var/lib/ziti");
#else
Expand All @@ -37,7 +43,7 @@ char* get_system_config_path() {
return config_path;
}

char* get_identifier_path() {
const char* get_identifier_path() {
return identifier_path;
}

Expand All @@ -47,24 +53,3 @@ void set_identifier_path(char* id_path) {
}
}

char* get_config_file_name(char* config_path) {
if (config_path != NULL) {
char* config_file_name = calloc(FILENAME_MAX, sizeof(char));
snprintf(config_file_name, FILENAME_MAX, "%s/config.json", config_path);
return config_file_name;
} else {
return "config.json";
}

}

char* get_backup_config_file_name(char* config_path) {
if (config_path != NULL) {
char* bkp_config_file_name = calloc(FILENAME_MAX, sizeof(char));
snprintf(bkp_config_file_name, FILENAME_MAX, "%s/config.json.backup", config_path);
return bkp_config_file_name;
} else {
return "config.json.backup";
}
}

26 changes: 0 additions & 26 deletions programs/ziti-edge-tunnel/include/config-utils.h

This file was deleted.

11 changes: 10 additions & 1 deletion programs/ziti-edge-tunnel/include/instance-config.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,19 @@

#ifndef ZITI_TUNNEL_SDK_C_INSTANCE_CONFIG_H
#define ZITI_TUNNEL_SDK_C_INSTANCE_CONFIG_H
#include <uv.h>
#include <stdbool.h>

#define DEFAULT_STATE_FILE_NAME "config.json"

char* get_system_config_path(const char* base_path);
void set_identifier_path(char* id_dir);
const char* get_identifier_path();

bool load_tunnel_status_from_file(uv_loop_t *ziti_loop);
bool save_tunnel_status_to_file();
void initialize_instance_config();
void initialize_instance_config(const char* config_dir);
void cleanup_instance_config();

char* get_config_file_name();
#endif //ZITI_TUNNEL_SDK_C_INSTANCE_CONFIG_H
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ int get_api_page_size();

tunnel_identity_array get_tunnel_identities_for_metrics();

void normalize_identifier(char *str);

void set_config_dir(const char *path);
void set_tun_name(const char *name);
char* get_zet_instance_id(const char* discriminator);

#ifdef __cplusplus
}
Expand Down
4 changes: 3 additions & 1 deletion programs/ziti-edge-tunnel/include/model/dtos.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ XX(ServiceVersion, service_version, ptr, ServiceVersion, __VA_ARGS__) \
XX(TunIpv4, model_string, none, TunIpv4, __VA_ARGS__) \
XX(TunPrefixLength, model_number, none, TunIpv4Mask, __VA_ARGS__) \
XX(AddDns, model_bool, none, AddDns, __VA_ARGS__) \
XX(ApiPageSize, model_number, none, ApiPageSize, __VA_ARGS__)
XX(ApiPageSize, model_number, none, ApiPageSize, __VA_ARGS__) \
XX(TunName, model_string, none, TunName, __VA_ARGS__)\
XX(ConfigDir, model_string, none, ConfigDir, __VA_ARGS__)

#define IP_INFO(XX, ...) \
XX(Ip, model_string, none, Ip, __VA_ARGS__) \
Expand Down
14 changes: 8 additions & 6 deletions programs/ziti-edge-tunnel/include/windows/windows-scripts.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@

#include "ziti/model_support.h"

void add_nrpt_rules(uv_loop_t *nrpt_loop, model_map *hostnames, const char* dns_ip);
void remove_nrpt_rules(uv_loop_t *nrpt_loop, model_map *hostnames);
void remove_all_nrpt_rules();
bool is_nrpt_policies_effective(const char* tns_ip);
void remove_and_add_nrpt_rules(uv_loop_t *nrpt_loop, model_map *hostnames, const char* dns_ip);
void update_interface_metric(uv_loop_t *ziti_loop, wchar_t* tun_name, int metric);
bool is_nrpt_policies_effective(const char* tns_ip, char* zet_id);

void add_nrpt_rules(uv_loop_t *nrpt_loop, model_map *hostnames, const char* dns_ip, const char* discriminator);
void remove_nrpt_rules(uv_loop_t *nrpt_loop, model_map *hostnames, const char* discriminator);
void remove_and_add_nrpt_rules(uv_loop_t *nrpt_loop, model_map *hostnames, const char* dns_ip, const char* discriminator);
void remove_all_nrpt_rules(char* zet_id, bool exact);

void update_interface_metric(uv_loop_t *ziti_loop, const wchar_t* tun_name, int metric);
void update_symlink(uv_loop_t *symlink_loop, char* symlink, char* filename);

#endif //ZITI_TUNNEL_SDK_C_WINDOWS_SCRIPTS_H
88 changes: 51 additions & 37 deletions programs/ziti-edge-tunnel/instance-config.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,39 @@
limitations under the License.
*/

#include "instance-config.h"
#include <stdio.h>
#include <stdlib.h>
#include <config-utils.h>
#include <string.h>
#include "identity-utils.h"
#include "instance.h"
#include <ziti/ziti_log.h>

#if _WIN32
#define realpath(rel, abs) _fullpath(abs, rel, MAX_PATH)
#endif

// to store the whole tunnel status data
#define MIN_BUFFER_LEN 512

static uv_sem_t sem;
static unsigned int sem_value = 1;
static int sem_initialized = -1;

void initialize_instance_config() {
static char* config_dir = NULL;
static bool config_saving_enabled = true;

///
/// \param path represents the location of the configuration to be used
void initialize_instance_config(const char* path) {
if (!path) {
//indicates this was started with -i (not -I). Operations should not do anything in this case
return;
}

char actual_config_dir[FILENAME_MAX];
realpath(path, actual_config_dir);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we check the return value of realpath?


config_dir = strdup(actual_config_dir); //save the location of the config dir
sem_initialized = uv_sem_init(&sem, sem_value);
if (sem_initialized < 0) {
ZITI_LOG(WARN, "Could not initialize lock for the config, config file may not be updated");
Expand Down Expand Up @@ -66,74 +84,62 @@ bool load_config_from_file(char* config_file_name) {
}

bool load_tunnel_status_from_file(uv_loop_t* ziti_loop) {
char* config_path = get_system_config_path();

uv_fs_t fs;
int check = uv_fs_mkdir(ziti_loop, &fs, config_path, 0755, NULL);
int check = uv_fs_mkdir(ziti_loop, &fs, config_dir, 0755, NULL);
if (check == 0) {
ZITI_LOG(TRACE, "config path is created at %s", config_path);
ZITI_LOG(TRACE, "config path is created at %s", config_dir);
} else if (check == UV_EEXIST) {
ZITI_LOG(TRACE, "config path exists at %s", config_path);
ZITI_LOG(TRACE, "config path exists at %s", config_dir);
} else {
ZITI_LOG(ERROR, "error creating %s: %s", config_path, uv_strerror(check));
ZITI_LOG(ERROR, "error creating %s: %s", config_dir, uv_strerror(check));
return false;
}
bool loaded = false;

char* config_file_name = get_config_file_name(config_path);
char* bkp_config_file_name = get_backup_config_file_name(config_path);
char* config_file_name = get_config_file_name();
ZITI_LOG(INFO,"Loading config file from %s", config_file_name);

// try to load tunnel status from config file
loaded = load_config_from_file(config_file_name);

// try to load tunnel status from backup config file
// not able to load the tunnel status
if (!loaded) {
loaded = load_config_from_file(bkp_config_file_name);
ZITI_LOG(WARN, "Config file %s cannot be read, does not exist, or is invalid. a new config file will be written if possible.", config_file_name);
}

// not able to load the tunnel status from both the config and backup files
if (!loaded) {
ZITI_LOG(WARN, "Config files %s and the backup file cannot be read or they do not exist, will create a new config file or the old one will be overwritten", config_file_name);
}
set_config_dir(config_dir);

free(config_file_name);
free(bkp_config_file_name);
free(config_path);
return loaded;
}

bool save_tunnel_status_to_file() {
if(!config_saving_enabled) {
ZITI_LOG(DEBUG, "config_saving_enabled is false. not saving config");
return true;
}
if(!config_dir) {
// no base_dir indicates using -i not -I
ZITI_LOG(DEBUG, "skipping save_tunnel_status_to_file. config_dir is not set");
return true;
}
size_t json_len;
char* tunnel_status = get_tunnel_config(&json_len);
bool saved = false;

if (json_len > 0) {
char* config_path = get_system_config_path();

char* config_file_name = get_config_file_name(config_path);
char* bkp_config_file_name = get_backup_config_file_name(config_path);
char* config_path = get_system_config_path(config_dir);
char* config_file_name = get_config_file_name();

if (sem_initialized == 0) {
uv_sem_wait(&sem);
} else {
ZITI_LOG(ZITI_WTF, "Could not save the config file [%s] due to semaphore lock not initialized error.", config_file_name);
free(config_file_name);
free(bkp_config_file_name);
free(config_path);
free(tunnel_status);
return saved;
}
//copy config to backup file
int rem = remove(bkp_config_file_name);
if (rem == 0) {
ZITI_LOG(DEBUG, "Deleted backup config file %s", bkp_config_file_name);
}
if (rename(config_file_name, bkp_config_file_name) == 0) {
ZITI_LOG(DEBUG, "Copied config file to backup config file %s", bkp_config_file_name);
} else {
ZITI_LOG(ERROR, "Could not copy config file [%s] to backup config file, the config might not exists at the moment", config_file_name);
}

// write tunnel status to the config file
FILE* config = fopen(config_file_name, "w");
Expand All @@ -155,11 +161,8 @@ bool save_tunnel_status_to_file() {
ZITI_LOG(DEBUG, "Saved current tunnel status into Config file %s", config_file_name);
}
uv_sem_post(&sem);

ZITI_LOG(TRACE, "Cleaning up resources used for the backup of tunnel config file %s", config_file_name);

free(config_file_name);
free(bkp_config_file_name);
free(config_path);
}
free(tunnel_status);
Expand All @@ -176,4 +179,15 @@ void cleanup_instance_config() {
} else {
ZITI_LOG(ZITI_WTF, "Could not clean instance config. The semaphore is not initialized.");
}
}

char* get_config_file_name() {
if (config_dir != NULL) {
char* config_file_name = calloc(FILENAME_MAX, sizeof(char));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

leak

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a bad practice -- since it's sometimes allocated that caller has no idea if it can be freed

snprintf(config_file_name, FILENAME_MAX, "%s%c%s", config_dir, PATH_SEP, DEFAULT_STATE_FILE_NAME);
normalize_identifier(config_file_name);
return config_file_name;
} else {
return DEFAULT_STATE_FILE_NAME;
}
}
Loading
Loading