Hi, I just ended up here while trying to make the various SimpleTunnel targets build by updating to the latest Swift3 Syntax (I guess that the project
was updated in April, prior to the various DIspatch and UnsafeRowPointer API changes).
I though it may have been easier to continue on this post given the subject, but I am happy to open a new question if you think it is more appropriate.
I think I've managed to update most of the code but I'm currently stuck with a few cases on UnsafeRawPointer migration to call the socket C API (UDPServerConnection.swift in the tunnel_server target):
Previously this compiled:
let bytesRead = withUnsafeMutablePointer(&socketAddress) {
recvfrom(UDPSocket, UnsafeMutablePointer<Void>(response), response.count, 0, UnsafeMutablePointer($0), &socketAddressLength)
}
In the lastest Swift3 release, the necxt-to-last argemunet of the call to recvfrom,UnsafeMutablePointer($0), throws a compiler error. I have changed access to what it should represent the a pointer to the socket address using 'withMemoryReboudTo' and that brought me to replacing the above with:
let bytesRead = withUnsafeMutablePointer(to: &socketAddress) {
$0.withMemoryRebound(to: sockaddr_storage.self, capacity: 1, {
recvfrom(UDPSocket, UnsafeMutableRawPointer(mutating: response), response.count, 0, $0, &socketAddressLength)
})
}
But now it comes the tricky part: the compiler now warns me that it 'Cannot convert valure of type 'UnsafeMutablePointer<_> to expected argument type <UnsafeMutablePointer<sockaddr>!>''.
Effectively, the recvfrom interface expects a value of type UnsafeMutablePointer<sockaddr>! as the next to last parameter, whilst $0 is of type sockadd_storage. The same applies for other calls to sendTo where it complains about passing a sokaddr_in to an argument expecting type sockaddr.
As far as I can see, this was the same in the pre-update code above and it has never been an issue.
From my little understanding of both Swift-s RawPointer's and the C socket API ( oh well, that doesn't help but everybody has to start somwhere..) and extensive poking around, it looks like sockddr_storage is meant to fit sockaddr_in or sockaddr and the C functions would be accepting any of those struct to then typecast them to the correct type:
http://stackoverflow.com/questions/16010622/reasoning-behind-c-sockets-sockaddr-and-sockaddr-storage
As a consequence, it would have been prefectly fine to call those functions with a pointer to any of those structs as long as the function was able to typecast to the correct type as runtime.
Is it possible that the compiler moaning above is related to the fact that we cannot use the new 'typed' UnsafePointers as an argument to the
expected 'UNsafeMutablePointer<sockaddr>'?
Or am I completely misunderstanding the issue here and the problem lies in the way I've converted to code?
As a side note, have I missed a more up-to-date version of the sample code built with xCode 8.1 I could use / compare my chaanges to?
Thanks in advance