Hi, new native dev here. I'm building UDP socket e...
# kotlin-native
s
Hi, new native dev here. I'm building UDP socket exchange using objective C APIs, and I'm struggling to convert both sockaddr_in and sockaddr_in6 to CFDataRef to feed into CFSocketConnectToAddress. Related code:
Copy code
alloc<sockaddr_in6> {
  sin6_family = AF_INET6.toUByte()
  sin6_port = portNumber.toUShort()
  inet_pton(AF_INET6, address, sin6_addr.ptr)
}.run {
  CFDataCreate(kCFAllocatorDefault, this.ptr /* What goes in here??? */, sizeOf<sockaddr_in6>())
}
Anybody?
j
Cannot try it, but I'd expect the code more like this:
Copy code
memscoped {
 val addr = alloc<sockaddr_in6> {
    sin6_family = AF_INET6.toUByte()
    sin6_port = portNumber.toUShort()
    inet_pton(AF_INET6, address, sin6_addr.ptr)
  }
  CFDataCreate(kCFAllocatorDefault, addr.ptr, sizeOf<sockaddr_in6>())
}
s
My code was already in a memScoped block, should have clarified that, my bad. Given that, your suggestion is the same thing just with different semantics (run+this as opposed to a explicitly defined variable), and it gives the same issue, addr.ptr is a pointer to sockaddr_in6, not to UInt8Var as expected by CFDataCreate
j
Ah OK; maybe it'll be easier if you share a working example in swift & people here can help with the syntax conversion to Kotlin?
j
The following compiles for me; and I believe it does what you expect it to do, but cannot test in any way 😉
_memScoped_ *{*
*val* address = *"1234:5678"*
*val* addr = _alloc_<sockaddr_in6>()
addr.*sin6_family* = _AF_INET6_._toUByte_()
addr.*sin6_port* = 80._toUShort_()
_inet_pton_(_AF_INET6_, address, addr._ptr_)
_CFDataCreate_ (_kCFAllocatorDefault_, addr._ptr_._toLong_()._toCPointer_(), _sizeOf_<sockaddr_in6>())
}
s
Ah so that's how it's done then, great, thanks. I can't really test it myself right now either, but hopefully soon. Thanks again!