Skip to content

Commit

Permalink
add hostname and domain to EvnInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
ekoby committed Sep 26, 2023
1 parent 40816e1 commit 20bdc26
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 11 deletions.
6 changes: 4 additions & 2 deletions inc_internal/internal_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,13 @@ XX(app_version, string, none, appVersion, __VA_ARGS__)
XX(os, string, none, os, __VA_ARGS__) \
XX(os_release, string, none, osRelease, __VA_ARGS__) \
XX(os_version, string, none, osVersion, __VA_ARGS__) \
XX(arch, string, none, arch, __VA_ARGS__)
XX(arch, string, none, arch, __VA_ARGS__) \
XX(hostname, string, none, hostname, __VA_ARGS__) \
XX(domain, string, none, domain, __VA_ARGS__)

#define ZITI_AUTH_REQ(XX, ...) \
XX(sdk_info, ziti_sdk_info, none, sdkInfo, __VA_ARGS__) \
XX(env_info, ziti_env_info, none, envInfo, __VA_ARGS__) \
XX(env_info, ziti_env_info, ptr, envInfo, __VA_ARGS__) \
XX(config_types, string, list, configTypes, __VA_ARGS__)

#define ZITI_ENROLLMENT_RESP(XX, ...) \
Expand Down
2 changes: 2 additions & 0 deletions inc_internal/zt_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,8 @@ extern void ziti_send_event(ziti_context ztx, const ziti_event_t *e);

void reject_dial_request(uint32_t conn_id, ziti_channel_t *ch, int32_t req_id, const char *reason);

const ziti_env_info* get_env_info();

extern uv_timer_t *new_ztx_timer(ziti_context ztx);

#ifdef __cplusplus
Expand Down
46 changes: 46 additions & 0 deletions library/internal_model.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,16 @@

#if _WIN32
#include <stdint.h>
#include <lmwksta.h>
#include <LMAPIbuf.h>
#pragma comment(lib, "netapi32.lib")

typedef uint32_t in_addr_t;
#define strcasecmp stricmp
#else

#include <arpa/inet.h>
#include <unistd.h>

#endif

Expand Down Expand Up @@ -166,6 +171,47 @@ int ziti_service_get_config(ziti_service *service, const char *cfg_type, void *c
return ZITI_OK;
}

static uv_once_t info_once;
static ziti_env_info s_info;
static void ziti_info_init() {
static uv_utsname_t os_info;
static char s_hostname[UV_MAXHOSTNAMESIZE];
static char s_domain[UV_MAXHOSTNAMESIZE];

uv_os_uname(&os_info);
s_info.os = os_info.sysname;
s_info.os_release = os_info.release;
s_info.os_version = os_info.version;
s_info.arch = os_info.machine;
size_t len = sizeof(s_hostname);
uv_os_gethostname(s_hostname, &len);
#if _WIN32
DWORD domain_len = sizeof(s_domain);
DWORD rc = 0;
rc = GetComputerNameExA(ComputerNameDnsDomain, s_domain, &domain_len);

if (domain_len == 0) {
WKSTA_INFO_100 *info;
rc = NetWkstaGetInfo(NULL, 100, (LPBYTE *) &info);
if (rc == 0) {
wsprintfA(s_domain, "%ls", info->wki100_langroup);
}
NetApiBufferFree(info);
}
#else
len = sizeof(s_domain);
getdomainname(s_domain, len);
#endif

s_info.hostname = s_hostname;
s_info.domain = s_domain;
}

const ziti_env_info* get_env_info() {
uv_once(&info_once, ziti_info_init);
return &s_info;
}

static int cmp_ziti_address0(ziti_address *lh, ziti_address *rh) {
null_checks(lh, rh)

Expand Down
10 changes: 1 addition & 9 deletions library/ziti_ctrl.c
Original file line number Diff line number Diff line change
Expand Up @@ -454,9 +454,6 @@ void ziti_ctrl_login(
void(*cb)(ziti_api_session *, const ziti_error *, void *),
void *ctx) {

uv_utsname_t osInfo;
uv_os_uname(&osInfo);

ziti_auth_req authreq = {
.sdk_info = {
.type = "ziti-sdk-c",
Expand All @@ -466,12 +463,7 @@ void ziti_ctrl_login(
.app_id = (char *) APP_ID,
.app_version = (char *) APP_VERSION,
},
.env_info = {
.os = osInfo.sysname,
.os_release = osInfo.release,
.os_version = osInfo.version,
.arch = osInfo.machine,
},
.env_info = (ziti_env_info *)get_env_info(),
.config_types = {0}
};
if (cfg_types) {
Expand Down
9 changes: 9 additions & 0 deletions tests/util_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include "catch2/catch_test_macros.hpp"
#include "catch2/matchers/catch_matchers_string.hpp"
#include "utils.h"
#include "internal_model.h"
#include "zt_internal.h"

#if _WIN32
#include <io.h>
Expand Down Expand Up @@ -79,4 +81,11 @@ TEST_CASE("read_file_stdin", "[util]") {

free(content);
uv_fs_req_cleanup(&req);
}

TEST_CASE("check hostname/domainname") {

const ziti_env_info *info = get_env_info();
printf("hostname = %s\n", info->hostname);
printf("domain = %s\n", info->domain);
}

0 comments on commit 20bdc26

Please sign in to comment.