-
Notifications
You must be signed in to change notification settings - Fork 645
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 NIOBSDSocket.ProtocolSubtype
#2317
Conversation
// } | ||
extension NIOBSDSocket.ProtocolSubtype { | ||
/// internet protocol, pseudo protocol number | ||
public static let ip = Self(rawValue: 0) |
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.
These protocol subtypes are not necessarily stable from OS release to OS release. As much as possible, can we use the symbolic names?
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.
Where can I find the symbolic names?
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.
Are you sure that they can change? I thought that these are the numbers used in the IP header protocol
field and therefore should be and need to be OS independent.
258
(Divert pseudo-protocol [non IANA]) and 262
(MTCP) are outliers and I'm not quite sure how they fit into this picture as they exceed the 8-bit available in the IPv4 protocol
(IPv6 nextHeader
) field.
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.
Some of these have that effect, but some do not. The protocol subtype field has uses outside of raw sockets.
In this case, these all match IPPROTO_*
constants in the various libcs. I suspect netinet/in.h
has most of them.
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 have made NIOBSDSocket.ProtocolSubtype
internal and moved all known protocol types to a new type called NIOIPProtocol
which is public. Should I still use the IPPROTO_*
constants for those as well?
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 think if we can point to the IANA registry that's sufficient.
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.
Not sure I can follow. What do you mean by pointing to the IANA registry?
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.
IANA (the Internet Assigned Numbers Authority) holds registries for most extension points in IETF protocols. In the case of the IPv4 next-level protocol field or IPv6 next header field, that registry is this one. You can skip most of that registry: I'd limit myself to assignments with a reference to an RFC or IANA. But that's the authoritative reference for the numbers, so we should reference it as the source for the numbers we choose.
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.
Done in 30e96bb
Make `ProtocolSubtype` internal
e9fe575
to
60d0b87
Compare
/// called "Protocol" to identify the next level protocol. This is an 8 | ||
/// bit field. In Internet Protocol version 6 (IPv6) [RFC8200], this field | ||
/// is called the "Next Header" field. | ||
public struct NIOIPProtocol: RawRepresentable, Hashable { |
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.
For the sake of convenience it might be nice to add a CustomStringConvertible
implementation that switches over the named static let
s and prints those names as well as the raw number.
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.
done in 904c5fd
public var rawValue: RawValue | ||
|
||
@inlinable | ||
public init(rawValue: RawValue) { |
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.
Can we add an init(_ rawValue: Int)
as well? It's useful to be able to construct this from a general Int
.
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.
Is it really that useful? I can't think of an instance where I would want to use that and NIOIPProtocol(rawValue: .init(someInt))
isn't too bad. This would also crash at runtime unless we make it an optional init.
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 think so. It remains helpful for cases where you can easily get hold of an Int
, and represents good practice in the Swift ecosystem (where Int
is the currency numerical type). The crash at runtime is a red herring: if the user has an Int
, we just force them to write the crash in their code instead of ours. The type very clearly communicates that it can only hold a UInt8
, so there's no ambiguity, and we can safely save the user the hassle of writing that code.
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.
Done in 35cb11f
} | ||
|
||
extension NIOBSDSocket.ProtocolSubtype { | ||
static let ip = Self(rawValue: 0) |
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 we add an MPTCP
static let
here to avoid using the raw value elsewhere?
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.
Done in fadd5c1
@@ -212,10 +212,10 @@ extension NIOBSDSocket { | |||
|
|||
// The protocol subtype for MPTCP. | |||
// Returns nil if mptcp is not supported. | |||
static var mptcpProtocolSubtype: Int? { | |||
static var mptcpProtocolSubtype: NIOBSDSocket.ProtocolSubtype? { |
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 we remove this in favour of NIOBSDSocket.ProtocolSubtype.mptcp
?
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.
Done in fadd5c1
Sources/NIOCore/IPProtocol.swift
Outdated
// Use for experimentation and testing (253) - [RFC3692] | ||
public static let useForExperimentationAndTesting1 = Self(rawValue: 253) | ||
// Use for experimentation and testing (254) - [RFC3692] | ||
public static let useForExperimentationAndTesting2 = Self(rawValue: 254) |
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'd skip these bottom two names: we can just leave them undefined.
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.
Done in 904c5fd
} | ||
|
||
extension NIOBSDSocket.ProtocolSubtype { | ||
static let ip = Self(rawValue: 0) |
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 don't think 0
implies IP
: I think it just implies "default".
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.
Done in dddf452
30e96bb
to
6ee625c
Compare
and remove `NIOBSDSocket.mptcpProtocolSubtype`
@swift-server-bot test this please |
Looks like the syscall wrapper test has broken. Probably this is the result of another dependency getting added to actually building that project (listed in |
Motivation:
We want to support custom IP protocols through POSIX Raw Socket API. To allow this, users will need to specify the IP sub protocol type they want to use.
Modifications:
NIOBSDSocket.ProtocolSubtype
with a lot of well known protocolsNIOBSDSocket.ProtocolSubtype
throughDatagramChannel
which we will reuse for the Raw Socket APIResult:
DatagramChannel
is prepared to be used in aRawSocketBootstrap