Skip to content

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

Merged
merged 8 commits into from
Feb 28, 2021

Conversation

lorentey
Copy link

No description provided.

Copy link
Owner

@milseman milseman left a 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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When might this happen?

Copy link
Author

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.

Copy link
Author

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 {
Copy link
Owner

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?

Copy link
Author

@lorentey lorentey Feb 28, 2021

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 {
Copy link
Owner

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?

Copy link
Author

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.

@milseman milseman force-pushed the network_receptacles branch from 18b96f6 to c11d8fa Compare February 28, 2021 03:17
@lorentey lorentey force-pushed the network_receptacles branch from 0c7d597 to 81fdee5 Compare February 28, 2021 03:28
@lorentey lorentey force-pushed the network_receptacles branch from 81fdee5 to 7d7e42e Compare February 28, 2021 04:00
@milseman milseman merged commit e55c19d into milseman:network_receptacles Feb 28, 2021
milseman added a commit that referenced this pull request Apr 10, 2021
Add Swifty wrappers for socket and IP addresses
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants