Skip to content

withSockAddr() unsafe #180

@lhoward

Description

@lhoward

As I understand it, withSockAddr() unsafely casts an existential to a sockaddr, which does work reliably, possibly for alignment reasons. On my system it fails (returns garbage) with domain sockets. Round-tripping via sockaddr_storage seems to work for me.

In my unrelated SocketAddress package [1], withSockAddr is a requirement of the protocol, so you get a pointer to the non-existential (underlying C type).

public protocol SocketAddress: Sendable {
  static var family: sa_family_t { get }

  init(family: sa_family_t, presentationAddress: String) throws
  func withSockAddr<T>(_ body: (_ sa: UnsafePointer<sockaddr>) throws -> T) rethrows -> T

  var presentationAddress: String { get throws }
  var presentationAddressNoPort: String { get throws }

  var port: UInt16 { get throws }
  var size: socklen_t { get }
}

[1] https://github.com/PADL/SocketAddress

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions