Post

Replies

Boosts

Views

Activity

Reply to Is there any difference between swift and C++ for `pid_t` type?
Thanks for response and here issue in pid value change is due to 'var ip: String' in my swift code. I have declared ip as 'String' type in swift and 'unsigned char[16]' type in C. Now I removed structure from swift and defined the struct in a C-style header file, and imported it into Swift using the Bridging-Header. I able to assign the values to all member variables in structure in C-style header file except for 'unsigned char ip[16]' I tried to assign value to unsigned char ip[16] type in C-style header from swift as below let host: String = remoteEndpoint.hostname as String event_info.ip = Array(host.utf8) But I am facing with compilation error Cannot assign value of type 'Array<String.UTF8View.Element>' (aka 'Array<UInt8>') to type '(UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8)' Please help me to assign value to unsigned char[16] type from swift. Sample code and structure defnition already available in the thread.
Topic: Programming Languages SubTopic: Swift Tags:
Sep ’20
Reply to Is there any difference between swift and C++ for `pid_t` type?
Are you calling handleNEEvent from somewhere in Swift code? I am not calling handleNEEvent() directly. But I am passing 'neeventinfos' structure through IOKit in handleNewFlow() to my kernel space. How your neeventinfot in Swift is defined? If it was imported from C++ header, your Swift code would cause build-time error. I have defined two different structures with same variables in both swift code and kernel space.
Topic: Programming Languages SubTopic: Swift Tags:
Sep ’20
Reply to Is there any difference between swift and C++ for `pid_t` type?
Thanks for reply. I am showing pid value using string format in swift and "%d" formatter in c++. Following is the my swift application structure & function. struct ne_event_info_s {   var lport: Int32   var rport: Int32   var ip_size: Int32   var ip: String   var pid: pid_t   var direction : Int32 } extension NEFilterFlow {   var sourceAppAuditTokenQ: audit_token_t? {     guard       let tokenData = self.sourceAppAuditToken,       tokenData.count == MemoryLayout<audit_token_t>.size     else { return nil }     return tokenData.withUnsafeBytes { buf in       buf.baseAddress!.assumingMemoryBound(to: audit_token_t.self).pointee     }   }       var pid: pid_t {     return audit_token_to_pid(sourceAppAuditTokenQ!)   } } override func handleNewFlow(_ flow: NEFilterFlow) -> NEFilterNewFlowVerdict {       guard let socketFlow = flow as? NEFilterSocketFlow,          let remoteEndpoint = socketFlow.remoteEndpoint as? NWHostEndpoint,          let localEndpoint = socketFlow.localEndpoint as? NWHostEndpoint else {            return .allow()        } &#9;&#9;&#9; var event_info = ne_event_info_t(                              lport: Int32(localEndpoint.port) ?? 0,                              rport: Int32(remoteEndpoint.port) ?? 0,                              ip_size: Int32(remoteEndpoint.hostname.count),                              ip: String(remoteEndpoint.hostname),                              pid: flow.pid,                              direction: dir == 2 ? 1 : 0)        os_log("event_info.lport = %@", String(event_info.lport))        os_log("event_info.rport = %@", String(event_info.rport))        os_log("event_info.ip_size = %@", String(event_info.ip_size))        os_log("event_info.pid = %@", String(event_info.pid))        os_log("Direction = %@", String(event_info.direction)) } Following is C++ structure & function. typedef struct ne_event_info_s {   int lport;   int rport;   int ip_size;   unsigned char ip[16];   pid_t pid;   int direction; } ne_event_info_t; void handleNEEvent(ne_event_info_t *info,) {   os_log("handleNEEvent: pid:%d, lport:%d,rport: %d\n", info->pid, info->lport,info->rport); }
Topic: Programming Languages SubTopic: Swift Tags:
Sep ’20