Network code not compiling on Ubuntu - "'sockaddr' has no member 'sa_len'"

I took excellent samople from:
https://developer.apple.com/forums/thread/109355

it does compile under macOS (I am writing a package) but on Linux (Ubuntu) I got:

error: value of type 'sockaddr' has no member 'sa_len'
socklen_t(addr.pointee.sa_len),

I suppose different underlying C sockert libs?
any clue?

indeed, compared to BSD, Linux traditionally doesn't have sa_len in sockaddr structure.
but with swift we have a superpower to add one:

#if LINUX // put a proper check here
extension sockaddr {
    var sa_len: Int {
        switch Int32(sa_family) {
        case AF_INET: return MemoryLayout<sockaddr_in>.size
        case AF_INET6: return MemoryLayout<sockaddr_in6>.size
        default: return MemoryLayout<sockaddr_storage>.size // something else
        }
    }
}
#endif
3 Likes