Skip to content

Commit

Permalink
multi-tun support for windows
Browse files Browse the repository at this point in the history
  • Loading branch information
dovholuknf committed Oct 15, 2024
1 parent 16206d8 commit dd2d7d7
Show file tree
Hide file tree
Showing 16 changed files with 635 additions and 323 deletions.
1 change: 1 addition & 0 deletions lib/ziti-tunnel/include/ziti/ziti_tunnel.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,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);

#define TNL_IP_MEM_POOL(XX, ...) \
XX(name, model_string, none, Name, __VA_ARGS__) \
Expand Down
2 changes: 1 addition & 1 deletion programs/ziti-edge-tunnel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ 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
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
12 changes: 7 additions & 5 deletions programs/ziti-edge-tunnel/include/windows/windows-scripts.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@

#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);
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, wchar_t* tun_name, int metric);
void update_symlink(uv_loop_t *symlink_loop, char* symlink, char* filename);

Expand Down
90 changes: 52 additions & 38 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);

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 @@ -152,14 +158,11 @@ bool save_tunnel_status_to_file() {
}
saved = true;
fclose(config);
ZITI_LOG(DEBUG, "Saved current tunnel status into Config file %s", config_file_name);
ZITI_LOG(INFO, "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));
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

0 comments on commit dd2d7d7

Please sign in to comment.