/src/swift-nio/Sources/NIOCore/IPProtocol.swift
Line | Count | Source |
1 | | //===----------------------------------------------------------------------===// |
2 | | // |
3 | | // This source file is part of the SwiftNIO open source project |
4 | | // |
5 | | // Copyright (c) 2022 Apple Inc. and the SwiftNIO project authors |
6 | | // Licensed under Apache License v2.0 |
7 | | // |
8 | | // See LICENSE.txt for license information |
9 | | // See CONTRIBUTORS.txt for the list of SwiftNIO project authors |
10 | | // |
11 | | // SPDX-License-Identifier: Apache-2.0 |
12 | | // |
13 | | //===----------------------------------------------------------------------===// |
14 | | |
15 | | /// In the Internet Protocol version 4 (IPv4) [RFC791] there is a field |
16 | | /// called "Protocol" to identify the next level protocol. This is an 8 |
17 | | /// bit field. In Internet Protocol version 6 (IPv6) [RFC8200], this field |
18 | | /// is called the "Next Header" field. |
19 | | public struct NIOIPProtocol: RawRepresentable, Hashable, Sendable { |
20 | | public typealias RawValue = UInt8 |
21 | | public var rawValue: RawValue |
22 | | |
23 | | @inlinable |
24 | 0 | public init(rawValue: RawValue) { |
25 | 0 | self.rawValue = rawValue |
26 | 0 | } |
27 | | } |
28 | | |
29 | | extension NIOIPProtocol { |
30 | | /// - precondition: `rawValue` must fit into an `UInt8` |
31 | 0 | public init(_ rawValue: Int) { |
32 | 0 | self.init(rawValue: UInt8(rawValue)) |
33 | 0 | } |
34 | | } |
35 | | |
36 | | // Subset of https://www.iana.org/assignments/protocol-numbers/protocol-numbers.xhtml with an RFC |
37 | | extension NIOIPProtocol { |
38 | | /// IPv6 Hop-by-Hop Option - [RFC8200] |
39 | | @inlinable |
40 | 0 | public static var hopopt: NIOIPProtocol { |
41 | 0 | Self(rawValue: 0) |
42 | 0 | } |
43 | | /// Internet Control Message - [RFC792] |
44 | | @inlinable |
45 | 0 | public static var icmp: NIOIPProtocol { |
46 | 0 | Self(rawValue: 1) |
47 | 0 | } |
48 | | /// Internet Group Management - [RFC1112] |
49 | | @inlinable |
50 | 0 | public static var igmp: NIOIPProtocol { |
51 | 0 | Self(rawValue: 2) |
52 | 0 | } |
53 | | /// Gateway-to-Gateway - [RFC823] |
54 | | @inlinable |
55 | 0 | public static var ggp: NIOIPProtocol { |
56 | 0 | Self(rawValue: 3) |
57 | 0 | } |
58 | | /// IPv4 encapsulation - [RFC2003] |
59 | | @inlinable |
60 | 0 | public static var ipv4: NIOIPProtocol { |
61 | 0 | Self(rawValue: 4) |
62 | 0 | } |
63 | | /// Stream - [RFC1190][RFC1819] |
64 | | @inlinable |
65 | 0 | public static var st: NIOIPProtocol { |
66 | 0 | Self(rawValue: 5) |
67 | 0 | } |
68 | | /// Transmission Control - [RFC9293] |
69 | | @inlinable |
70 | 0 | public static var tcp: NIOIPProtocol { |
71 | 0 | Self(rawValue: 6) |
72 | 0 | } |
73 | | /// Exterior Gateway Protocol - [RFC888][David_Mills] |
74 | | @inlinable |
75 | 0 | public static var egp: NIOIPProtocol { |
76 | 0 | Self(rawValue: 8) |
77 | 0 | } |
78 | | /// Network Voice Protocol - [RFC741][Steve_Casner] |
79 | | @inlinable |
80 | 0 | public static var nvpIi: NIOIPProtocol { |
81 | 0 | Self(rawValue: 11) |
82 | 0 | } |
83 | | /// User Datagram - [RFC768][Jon_Postel] |
84 | | @inlinable |
85 | 0 | public static var udp: NIOIPProtocol { |
86 | 0 | Self(rawValue: 17) |
87 | 0 | } |
88 | | /// Host Monitoring - [RFC869][Bob_Hinden] |
89 | | @inlinable |
90 | 0 | public static var hmp: NIOIPProtocol { |
91 | 0 | Self(rawValue: 20) |
92 | 0 | } |
93 | | /// Reliable Data Protocol - [RFC908][Bob_Hinden] |
94 | | @inlinable |
95 | 0 | public static var rdp: NIOIPProtocol { |
96 | 0 | Self(rawValue: 27) |
97 | 0 | } |
98 | | /// Internet Reliable Transaction - [RFC938][Trudy_Miller] |
99 | | @inlinable |
100 | 0 | public static var irtp: NIOIPProtocol { |
101 | 0 | Self(rawValue: 28) |
102 | 0 | } |
103 | | /// ISO Transport Protocol Class 4 - [RFC905][<mystery contact>] |
104 | | @inlinable |
105 | 0 | public static var isoTp4: NIOIPProtocol { |
106 | 0 | Self(rawValue: 29) |
107 | 0 | } |
108 | | /// Bulk Data Transfer Protocol - [RFC969][David_Clark] |
109 | | @inlinable |
110 | 0 | public static var netblt: NIOIPProtocol { |
111 | 0 | Self(rawValue: 30) |
112 | 0 | } |
113 | | /// Datagram Congestion Control Protocol - [RFC4340] |
114 | | @inlinable |
115 | 0 | public static var dccp: NIOIPProtocol { |
116 | 0 | Self(rawValue: 33) |
117 | 0 | } |
118 | | /// IPv6 encapsulation - [RFC2473] |
119 | | @inlinable |
120 | 0 | public static var ipv6: NIOIPProtocol { |
121 | 0 | Self(rawValue: 41) |
122 | 0 | } |
123 | | /// Reservation Protocol - [RFC2205][RFC3209][Bob_Braden] |
124 | | @inlinable |
125 | 0 | public static var rsvp: NIOIPProtocol { |
126 | 0 | Self(rawValue: 46) |
127 | 0 | } |
128 | | /// Generic Routing Encapsulation - [RFC2784][Tony_Li] |
129 | | @inlinable |
130 | 0 | public static var gre: NIOIPProtocol { |
131 | 0 | Self(rawValue: 47) |
132 | 0 | } |
133 | | /// Dynamic Source Routing Protocol - [RFC4728] |
134 | | @inlinable |
135 | 0 | public static var dsr: NIOIPProtocol { |
136 | 0 | Self(rawValue: 48) |
137 | 0 | } |
138 | | /// Encap Security Payload - [RFC4303] |
139 | | @inlinable |
140 | 0 | public static var esp: NIOIPProtocol { |
141 | 0 | Self(rawValue: 50) |
142 | 0 | } |
143 | | /// Authentication Header - [RFC4302] |
144 | | @inlinable |
145 | 0 | public static var ah: NIOIPProtocol { |
146 | 0 | Self(rawValue: 51) |
147 | 0 | } |
148 | | /// NBMA Address Resolution Protocol - [RFC1735] |
149 | | @inlinable |
150 | 0 | public static var narp: NIOIPProtocol { |
151 | 0 | Self(rawValue: 54) |
152 | 0 | } |
153 | | /// ICMP for IPv6 - [RFC8200] |
154 | | @inlinable |
155 | 0 | public static var ipv6Icmp: NIOIPProtocol { |
156 | 0 | Self(rawValue: 58) |
157 | 0 | } |
158 | | /// No Next Header for IPv6 - [RFC8200] |
159 | | @inlinable |
160 | 0 | public static var ipv6Nonxt: NIOIPProtocol { |
161 | 0 | Self(rawValue: 59) |
162 | 0 | } |
163 | | /// Destination Options for IPv6 - [RFC8200] |
164 | | @inlinable |
165 | 0 | public static var ipv6Opts: NIOIPProtocol { |
166 | 0 | Self(rawValue: 60) |
167 | 0 | } |
168 | | /// EIGRP - [RFC7868] |
169 | | @inlinable |
170 | 0 | public static var eigrp: NIOIPProtocol { |
171 | 0 | Self(rawValue: 88) |
172 | 0 | } |
173 | | /// OSPFIGP - [RFC1583][RFC2328][RFC5340][John_Moy] |
174 | | @inlinable |
175 | 0 | public static var ospfigp: NIOIPProtocol { |
176 | 0 | Self(rawValue: 89) |
177 | 0 | } |
178 | | /// Ethernet-within-IP Encapsulation - [RFC3378] |
179 | | @inlinable |
180 | 0 | public static var etherip: NIOIPProtocol { |
181 | 0 | Self(rawValue: 97) |
182 | 0 | } |
183 | | /// Encapsulation Header - [RFC1241][Robert_Woodburn] |
184 | | @inlinable |
185 | 0 | public static var encap: NIOIPProtocol { |
186 | 0 | Self(rawValue: 98) |
187 | 0 | } |
188 | | /// Protocol Independent Multicast - [RFC7761][Dino_Farinacci] |
189 | | @inlinable |
190 | 0 | public static var pim: NIOIPProtocol { |
191 | 0 | Self(rawValue: 103) |
192 | 0 | } |
193 | | /// IP Payload Compression Protocol - [RFC2393] |
194 | | @inlinable |
195 | 0 | public static var ipcomp: NIOIPProtocol { |
196 | 0 | Self(rawValue: 108) |
197 | 0 | } |
198 | | /// Virtual Router Redundancy Protocol - [RFC5798] |
199 | | @inlinable |
200 | 0 | public static var vrrp: NIOIPProtocol { |
201 | 0 | Self(rawValue: 112) |
202 | 0 | } |
203 | | /// Layer Two Tunneling Protocol - [RFC3931][Bernard_Aboba] |
204 | | @inlinable |
205 | 0 | public static var l2tp: NIOIPProtocol { |
206 | 0 | Self(rawValue: 115) |
207 | 0 | } |
208 | | /// Fibre Channel - [Murali_Rajagopal][RFC6172] |
209 | | @inlinable |
210 | 0 | public static var fc: NIOIPProtocol { |
211 | 0 | Self(rawValue: 133) |
212 | 0 | } |
213 | | /// MANET Protocols - [RFC5498] |
214 | | @inlinable |
215 | 0 | public static var manet: NIOIPProtocol { |
216 | 0 | Self(rawValue: 138) |
217 | 0 | } |
218 | | /// Host Identity Protocol - [RFC7401] |
219 | | @inlinable |
220 | 0 | public static var hip: NIOIPProtocol { |
221 | 0 | Self(rawValue: 139) |
222 | 0 | } |
223 | | /// Shim6 Protocol - [RFC5533] |
224 | | @inlinable |
225 | 0 | public static var shim6: NIOIPProtocol { |
226 | 0 | Self(rawValue: 140) |
227 | 0 | } |
228 | | /// Wrapped Encapsulating Security Payload - [RFC5840] |
229 | | @inlinable |
230 | 0 | public static var wesp: NIOIPProtocol { |
231 | 0 | Self(rawValue: 141) |
232 | 0 | } |
233 | | /// Robust Header Compression - [RFC5858] |
234 | | @inlinable |
235 | 0 | public static var rohc: NIOIPProtocol { |
236 | 0 | Self(rawValue: 142) |
237 | 0 | } |
238 | | /// Ethernet - [RFC8986] |
239 | | @inlinable |
240 | 0 | public static var ethernet: NIOIPProtocol { |
241 | 0 | Self(rawValue: 143) |
242 | 0 | } |
243 | | /// AGGFRAG encapsulation payload for ESP - [RFC-ietf-ipsecme-iptfs-19] |
244 | | @inlinable |
245 | 0 | public static var aggfrag: NIOIPProtocol { |
246 | 0 | Self(rawValue: 144) |
247 | 0 | } |
248 | | } |
249 | | |
250 | | extension NIOIPProtocol: CustomStringConvertible { |
251 | 0 | private var name: String? { |
252 | 0 | switch self { |
253 | 0 | case .hopopt: return "IPv6 Hop-by-Hop Option" |
254 | 0 | case .icmp: return "Internet Control Message" |
255 | 0 | case .igmp: return "Internet Group Management" |
256 | 0 | case .ggp: return "Gateway-to-Gateway" |
257 | 0 | case .ipv4: return "IPv4 encapsulation" |
258 | 0 | case .st: return "Stream" |
259 | 0 | case .tcp: return "Transmission Control" |
260 | 0 | case .egp: return "Exterior Gateway Protocol" |
261 | 0 | case .nvpIi: return "Network Voice Protocol" |
262 | 0 | case .udp: return "User Datagram" |
263 | 0 | case .hmp: return "Host Monitoring" |
264 | 0 | case .rdp: return "Reliable Data Protocol" |
265 | 0 | case .irtp: return "Internet Reliable Transaction" |
266 | 0 | case .isoTp4: return "ISO Transport Protocol Class 4" |
267 | 0 | case .netblt: return "Bulk Data Transfer Protocol" |
268 | 0 | case .dccp: return "Datagram Congestion Control Protocol" |
269 | 0 | case .ipv6: return "IPv6 encapsulation" |
270 | 0 | case .rsvp: return "Reservation Protocol" |
271 | 0 | case .gre: return "Generic Routing Encapsulation" |
272 | 0 | case .dsr: return "Dynamic Source Routing Protocol" |
273 | 0 | case .esp: return "Encap Security Payload" |
274 | 0 | case .ah: return "Authentication Header" |
275 | 0 | case .narp: return "NBMA Address Resolution Protocol" |
276 | 0 | case .ipv6Icmp: return "ICMP for IPv6" |
277 | 0 | case .ipv6Nonxt: return "No Next Header for IPv6" |
278 | 0 | case .ipv6Opts: return "Destination Options for IPv6" |
279 | 0 | case .eigrp: return "EIGRP" |
280 | 0 | case .ospfigp: return "OSPFIGP" |
281 | 0 | case .etherip: return "Ethernet-within-IP Encapsulation" |
282 | 0 | case .encap: return "Encapsulation Header" |
283 | 0 | case .pim: return "Protocol Independent Multicast" |
284 | 0 | case .ipcomp: return "IP Payload Compression Protocol" |
285 | 0 | case .vrrp: return "Virtual Router Redundancy Protocol" |
286 | 0 | case .l2tp: return "Layer Two Tunneling Protocol" |
287 | 0 | case .fc: return "Fibre Channel" |
288 | 0 | case .manet: return "MANET Protocols" |
289 | 0 | case .hip: return "Host Identity Protocol" |
290 | 0 | case .shim6: return "Shim6 Protocol" |
291 | 0 | case .wesp: return "Wrapped Encapsulating Security Payload" |
292 | 0 | case .rohc: return "Robust Header Compression" |
293 | 0 | case .ethernet: return "Ethernet" |
294 | 0 | case .aggfrag: return "AGGFRAG encapsulation payload for ESP" |
295 | 0 | default: return nil |
296 | 0 | } |
297 | 0 | } |
298 | | |
299 | 0 | public var description: String { |
300 | 0 | let name = self.name ?? "Unknown Protocol" |
301 | 0 | return "\(name) - \(rawValue)" |
302 | 0 | } |
303 | | } |