Skip to content

Commit

Permalink
Merge pull request #82 from openziti/dev
Browse files Browse the repository at this point in the history
Support ziti_dump, ziti_conn_source_identity, and updated TSDK DNS
  • Loading branch information
smilindave26 authored Feb 22, 2021
2 parents 2203bac + 69d6657 commit 8a331da
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 9 deletions.
3 changes: 3 additions & 0 deletions lib/Ziti-Bridging-Header.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ typedef int (*apply_cb)(dns_manager *dns, const char *host, const char *ip);
extern const char** ziti_all_configs;
extern tls_context *default_tls_context(const char *ca, size_t ca_len);

typedef int (*ziti_printer_cb_wrapper)(void *ctx, const char *msg);
void ziti_dump_wrapper(ziti_context ztx, ziti_printer_cb_wrapper printer, void *ctx);

tunneled_service_t *ziti_sdk_c_on_service_wrapper(ziti_context ziti_ctx, ziti_service *service, int status, tunneler_context tnlr_ctx);

extern int ziti_log_level(void);
Expand Down
27 changes: 22 additions & 5 deletions lib/Ziti.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ import Foundation
private var connections:[ZitiConnection] = []
private var connectionsLock = NSLock()

/// Type used for debug dump
///
/// - Parameters:
/// - msg: debug string
///
/// - Returns: number of characters printed
public typealias ZitiDumpPrinter = (_ msg:String) -> Int32
private var dumpPrinter:ZitiDumpPrinter?

private var id:ZitiIdentity

// MARK: - Initializers
Expand Down Expand Up @@ -509,13 +518,12 @@ import Foundation
return (up, down)
}

/// Output debugging information to standard out
/// Output debugging information to supplied callback. The output from this command may be useful when submitting issues.
///
/// This method must be called in an interation of the loop
@objc public func dump() {
// TODO: updated `ziti_dump`: void ziti_dump(ziti_context ztx, int (*printer)(void *, const char *, ...), void *ctx)
// ziti_dump(ztx)
log.error("TODO")
@objc public func dump(_ printer: @escaping ZitiDumpPrinter) {
self.dumpPrinter = printer
ziti_dump_wrapper(ztx, Ziti.onDumpPrinter, self.toVoidPtr())
}

/// Checks availability of service
Expand Down Expand Up @@ -565,6 +573,15 @@ import Foundation

// MARK: - Static C Callbacks

static private let onDumpPrinter:ziti_printer_cb_wrapper = { ctx, msg in
guard let mySelf = zitiUnretained(Ziti.self, ctx) else {
log.wtf("invalid context")
return 0
}
let str = msg != nil ? String(cString: msg!) : ""
return mySelf.dumpPrinter?(str) ?? 0
}

static private let onEvent:ziti_event_cb = { ztx, cEvent in
guard let ctx = ziti_app_ctx(ztx), let mySelf = zitiUnretained(Ziti.self, ctx) else {
log.wtf("invalid context", function:"onEvent()")
Expand Down
12 changes: 12 additions & 0 deletions lib/ZitiConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,18 @@ import Foundation
}
}

/// Get the identity of the client that initiated the Ziti connection
///
/// - Returns: Source ID or empty String
///
@objc public func getSourceIdentity() -> String {
var id = ""
if let source_id = ziti_conn_source_identity(self.zConn) {
id = String(cString: source_id)
}
return id
}

/// Send data to the connection peer
///
/// - Parameters:
Expand Down
47 changes: 44 additions & 3 deletions lib/ZitiTunnel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public protocol ZitiTunnelProvider {
func deleteRoute(_ dest:String) -> Int32

func applyDns(_ host:String, _ ip:String) -> Int32
func fallbackDns(_ name:String) -> String?

func writePacket(_ data:Data)
}
Expand All @@ -28,12 +29,16 @@ public class ZitiTunnel : NSObject, ZitiUnretained {
private static let log = ZitiLog(ZitiTunnel.self)
private let log = ZitiTunnel.log

var tunnelProvider:ZitiTunnelProvider?
var tnlr_ctx:tunneler_context?
var tunneler_opts:UnsafeMutablePointer<tunneler_sdk_options>!
var dns:UnsafeMutablePointer<dns_manager>!
let netifDriver:NetifDriver

public init(_ tunnelProvider:ZitiTunnelProvider, _ loop:UnsafeMutablePointer<uv_loop_t>, _ ipAddress:String, _ subnetMask:String) {
public init(_ tunnelProvider:ZitiTunnelProvider, _ loop:UnsafeMutablePointer<uv_loop_t>,
_ ipAddress:String, _ subnetMask:String,
_ ipDNS:String) {
self.tunnelProvider = tunnelProvider
netifDriver = NetifDriver(tunnelProvider: tunnelProvider)
super.init()

Expand All @@ -47,9 +52,17 @@ public class ZitiTunnel : NSObject, ZitiUnretained {
ziti_host: ziti_sdk_c_host))
tnlr_ctx = ziti_tunneler_init(tunneler_opts, loop)

// TODO: change this to `get_tunneler_dns(uv_loop_t *l, uint32_t dns_ip, dns_fallback_cb cb, void *ctx)` to use T SDK's dns...
dns = UnsafeMutablePointer<dns_manager>.allocate(capacity: 1)
dns.initialize(to: dns_manager(
internal_dns: false,
dns_ip: ipStrToUInt32(ipDNS),
dns_port: 53,
apply: ZitiTunnel.apply_dns_cb,
query: ZitiTunnel.dns_query_cb,
loop: loop,
fb_cb: ZitiTunnel.dns_fallback_cb,
fb_ctx: self.toVoidPtr(),
data: self.toVoidPtr()))

let (mask, bits) = calcMaskAndBits(ipAddress, subnetMask)
Expand All @@ -71,6 +84,15 @@ public class ZitiTunnel : NSObject, ZitiUnretained {
return parts.count == 4 && nums.count == 4 && nums.filter { $0 >= 0 && $0 < 256}.count == 4
}

private func ipStrToUInt32(_ str:String) -> UInt32 {
let parts = str.components(separatedBy: ".")
guard isValidIpV4Address(parts) else {
log.error("Unable to convert \"\(str)\" to IP address")
return 0
}
return (UInt32(parts[0])! << 24) | (UInt32(parts[1])! << 16) | (UInt32(parts[2])! << 8) | UInt32(parts[3])!
}

private func calcMaskAndBits(_ ipAddress:String, _ subnetMask:String) -> (UInt32, Int32) {
var mask:UInt32 = 0
var bits:Int32 = 0
Expand Down Expand Up @@ -116,12 +138,31 @@ public class ZitiTunnel : NSObject, ZitiUnretained {

static let apply_dns_cb:apply_cb = { dns, host, ip in
guard let mySelf = zitiUnretained(ZitiTunnel.self, dns?.pointee.data) else {
log.wtf("invalid context", function: "apply_dns_cb()")
log.wtf("invalid context")
return -1
}

let hostStr = host != nil ? String(cString: host!) : ""
let ipStr = ip != nil ? String(cString: ip!) : ""
return mySelf.netifDriver.tunnelProvider?.applyDns(hostStr, ipStr) ?? -1
return mySelf.tunnelProvider?.applyDns(hostStr, ipStr) ?? -1
}

static let dns_query_cb:dns_query = { dns_manager, q_packet, q_len, cb, ctx in
log.wtf("Unexpected call to unimplemented function")
return -1
}

static let dns_fallback_cb:dns_fallback_cb = { name, ctx, addr in
guard let mySelf = zitiUnretained(ZitiTunnel.self, ctx), let name = name, let addr = addr else {
log.wtf("invalid context")
return 3 // NXDOMAIN
}

let nameStr = String(cString: name)
if let ipStr = mySelf.tunnelProvider?.fallbackDns(nameStr), let cStr = ipStr.cString(using: .utf8) {
addr.pointee.s_addr = inet_addr(cStr)
return 0 // NO_ERROR
}
return 3 // NXDOMAIN
}
}
24 changes: 24 additions & 0 deletions lib/ziti.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,30 @@ static const char* _ziti_all[] = {

const char** ziti_all_configs = _ziti_all;

typedef struct ziti_dump_ctx_s {
void *ctx;
ziti_printer_cb_wrapper printer;
} ziti_dump_ctx;

int ziti_dump_printer(void *ctx, const char *fmt, ...) {
ziti_dump_ctx *zdctx = ctx;
static char msg[4096];

va_list vargs;
va_start(vargs, fmt);
vsnprintf(msg, sizeof(msg), fmt, vargs);
va_end(vargs);

return zdctx->printer(zdctx->ctx, msg);
}

void ziti_dump_wrapper(ziti_context ztx, ziti_printer_cb_wrapper printer, void *ctx) {
ziti_dump_ctx zdctx;
zdctx.ctx = ctx;
zdctx.printer = printer;
ziti_dump(ztx, ziti_dump_printer, &zdctx);
}

tunneled_service_t *ziti_sdk_c_on_service_wrapper(ziti_context ziti_ctx, ziti_service *service, int status, tunneler_context tnlr_ctx) {
return ziti_sdk_c_on_service(ziti_ctx, service, status, tnlr_ctx);
}
Expand Down

0 comments on commit 8a331da

Please sign in to comment.