-
Notifications
You must be signed in to change notification settings - Fork 0
Add nice wrappers for sockaddr and sockaddr_in #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add nice wrappers for sockaddr and sockaddr_in #1
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good so far. Looking forward to docs and hopefully some better names for "address".
guard address.domain == Domain.ipv4 else { return nil } | ||
let value: CInterop.SockAddrIn? = address.withUnsafeBytes { buffer in | ||
guard buffer.count >= MemoryLayout<CInterop.SockAddrIn>.size else { | ||
return nil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When might this happen?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is possible to manually initialize a SocketAddress with garbage data.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Arguably we should reject overlong addresses, but evidently people overallocate sometimes.
} | ||
|
||
@frozen | ||
public struct Address: RawRepresentable { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, here it is. Is there any more descriptive name we can use? Some way to differentiate between the nice Swifty formulation and these raw structs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could call it Host
or HostAddress
, should we?
"Host" doesn't scream in_addr
to me, altough to be fair, neither does SocketAddress.IPv4.Address
...
_inet_ntop() | ||
} | ||
|
||
internal func _inet_ntop() -> String { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be throws
, and then description
would fatal error?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm pretty sure this will only report a failure if we messed up the invocation.
The inet_ntop() call fails if:
[EAFNOSUPPORT] *src was not an AF_INET or AF_INET6 family address.
[ENOSPC] size was not large enough to store the presentation form of the address.
We always pass AF_INET (or AF_INET6), and we always allocate enough memory.
18b96f6
to
c11d8fa
Compare
0c7d597
to
81fdee5
Compare
81fdee5
to
7d7e42e
Compare
Add Swifty wrappers for socket and IP addresses
No description provided.