Skip to content

Commit

Permalink
Merge pull request #37 from openziti/update-sdk-c
Browse files Browse the repository at this point in the history
Update sdk c
  • Loading branch information
smilindave26 committed Nov 8, 2020
2 parents c806e03 + b006487 commit 740413b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 26 deletions.
40 changes: 21 additions & 19 deletions lib/Ziti.swift
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,11 @@ import Foundation

static private let onMacResponse:ZitiPostureChecks.MacResponse = { ctx, macArray in
let macCtx = ctx as! ZitiMacContext
//log.info("MAC posture response: \(macArray)",function:"onMacResponse()")
guard let macArray = macArray else {
macCtx.cb(macCtx.ztx, ctx.id, nil, 0)
return
}

withArrayOfCStrings(macArray) { arr in
let cp = copyStringArray(arr, Int32(arr.count))
macCtx.cb(macCtx.ztx, ctx.id, cp, Int32(macArray.count))
Expand All @@ -604,13 +608,11 @@ import Foundation

static private let onOsResponse:ZitiPostureChecks.OsResponse = { ctx, type, version, build in
let osCtx = ctx as! ZitiOsContext

//log.info("OS posture response: type=\(type), version=\(version), build=\(build)", function:"onOsResponse()")


// C SDK didn't use `const` for strings, so need to copy 'em
let cType = copyString(type.cString(using: .utf8))
let cVersion = copyString(version.cString(using: .utf8))
let cBuild = copyString(build.cString(using: .utf8))
let cType = type != nil ? copyString(type!.cString(using: .utf8)) : nil
let cVersion = version != nil ? copyString(version!.cString(using: .utf8)) : nil
let cBuild = build != nil ? copyString(build!.cString(using: .utf8)) : nil

osCtx.cb(osCtx.ztx, osCtx.id, cType, cVersion, cBuild)

Expand All @@ -636,17 +638,19 @@ import Foundation

static private let onProcessResponse:ZitiPostureChecks.ProcessResponse = { ctx, path, isRunning, hash, signers in
let pCtx = ctx as! ZitiProcessContext

//log.info("OS process response: path=\(path), isRunning=\(isRunning), hash=\(hash), signers=\(signers)", function:"onProcessResponse()")


// C SDK didn't use `const` for strings, so need to copy 'em
let cPath = copyString(path.cString(using: .utf8))
let cHash = copyString(hash.cString(using: .utf8))
let cHash = hash != nil ? copyString(hash!.cString(using: .utf8)) : nil

withArrayOfCStrings(signers) { arr in
let cp = copyStringArray(arr, Int32(arr.count))
pCtx.cb(pCtx.ztx, ctx.id, cPath, isRunning, cHash, cp, Int32(signers.count))
freeStringArray(cp)
if let signers = signers {
withArrayOfCStrings(signers) { arr in
let cp = copyStringArray(arr, Int32(arr.count))
pCtx.cb(pCtx.ztx, ctx.id, cPath, isRunning, cHash, cp, Int32(signers.count))
freeStringArray(cp)
}
} else {
pCtx.cb(pCtx.ztx, pCtx.id, cPath, isRunning, cHash, nil, 0)
}

freeString(cPath)
Expand All @@ -668,11 +672,9 @@ import Foundation

static private let onDomainResponse:ZitiPostureChecks.DomainResponse = { ctx, domain in
let dCtx = ctx as! ZitiDomainContext

//log.info("Domain posture response: domain=\(domain)", function:"onDomainResponse()")


// C SDK didn't use `const` for strings, so need to copy 'em
let cDomain = copyString(domain.cString(using: .utf8))
let cDomain = domain != nil ? copyString(domain!.cString(using: .utf8)) : nil
dCtx.cb(dCtx.ztx, dCtx.id, cDomain)
freeString(cDomain)
}
Expand Down
8 changes: 4 additions & 4 deletions lib/ZitiPostureChecks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import Foundation
/// - Parameters:
/// - ctx: posture context
/// - macAddresses: array of the mac addresses the host currently can access. Values should be hex strings. `nil` signifies not supported.
public typealias MacResponse = (_ ctx:ZitiPostureContext, _ macAddresses:[String]) -> Void
public typealias MacResponse = (_ ctx:ZitiPostureContext, _ macAddresses:[String]?) -> Void

/// Postture query for MAC address
///
Expand All @@ -49,7 +49,7 @@ import Foundation
/// - Parameters:
/// - ctx: posture context
/// - domain: Host domain. `nil` signifies not supported.
public typealias DomainResponse = (_ ctx:ZitiPostureContext, _ domain:String) -> Void
public typealias DomainResponse = (_ ctx:ZitiPostureContext, _ domain:String?) -> Void

/// Postture query for Domain
///
Expand All @@ -65,7 +65,7 @@ import Foundation
/// - type: type of operating sytem (e.g., "macOS", "iOS")
/// - version: OS version
/// - build: OS build. `nil` signifies not supported
public typealias OsResponse = (_ ctx:ZitiPostureContext, _ type:String, _ version:String, _ build:String) -> Void
public typealias OsResponse = (_ ctx:ZitiPostureContext, _ type:String?, _ version:String?, _ build:String?) -> Void

/// Postture query for Operating System
///
Expand All @@ -83,7 +83,7 @@ import Foundation
/// - hash: sha512 hash of the process's binary file
/// - signers: sha1 hex string fingerprints of the binary or `nil` if not supported
public typealias ProcessResponse =
(_ ctx:ZitiPostureContext, _ path:String, _ isRunning:Bool, _ hash:String, _ signers:[String]) -> Void
(_ ctx:ZitiPostureContext, _ path:String, _ isRunning:Bool, _ hash:String?, _ signers:[String]?) -> Void

/// Postture query for process information
///
Expand Down
8 changes: 5 additions & 3 deletions lib/ziti.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,22 @@ static const char* _ziti_all[] = {
const char** ziti_all_configs = _ziti_all;

char **copyStringArray(char *const arr[], int count) {
size_t sz = sizeof(char (**));
if (count == 0) return 0;

size_t sz = sizeof(char**);
char **arrCpy = calloc((count + 1), sz);
memcpy(arrCpy, arr, count * sz);
return arrCpy;
}

void freeStringArray(char **arr) {
free(arr);
if (arr) free(arr);
}

char *copyString(const char *str) {
return strdup(str);
}

void freeString(char *str) {
free(str);
if (str) free(str);
}

0 comments on commit 740413b

Please sign in to comment.