https://kotlinlang.org logo
#kotlin-native
Title
# kotlin-native
s

stoyicker

12/12/2023, 11:26 PM
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

Jan Holešovský

12/13/2023, 7:20 AM
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

stoyicker

12/13/2023, 9:00 AM
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

Jan Holešovský

12/13/2023, 9:53 AM
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

Jan Holešovský

12/14/2023, 7:11 AM
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

stoyicker

12/14/2023, 9:13 AM
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!
2 Views