1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
#[derive(Clone, Copy, Debug)]
#[repr(C, align(8))]
pub struct SockAddrIpv4 {
sin_family: linux_unsafe::sa_family_t,
sin_port: u16, sin_addr: Ipv4Addr,
sin_zero: [u8; 8],
}
impl SockAddrIpv4 {
#[inline]
pub const fn new(host_addr: Ipv4Addr, port: u16) -> Self {
Self {
sin_family: AF_INET,
sin_port: port.to_be(),
sin_addr: host_addr,
sin_zero: [0; 8],
}
}
#[inline(always)]
pub const fn host_address(&self) -> Ipv4Addr {
self.sin_addr
}
#[inline(always)]
pub const fn port(&self) -> u16 {
self.sin_port.to_be() }
}
#[derive(Clone, Copy, Debug)]
#[repr(C)]
pub struct Ipv4Addr {
s_addr: u32, }
impl Ipv4Addr {
pub const ANY: Self = Self { s_addr: 0x00000000 };
pub const NONE: Self = Self { s_addr: 0xffffffff };
pub const BROADCAST: Self = Self { s_addr: 0xffffffff };
pub const DUMMY: Self = Self {
s_addr: 0xc0000008_u32.to_be(),
};
pub const LOOPBACK: Self = Self {
s_addr: 0x7f000001_u32.to_be(),
};
pub const UNSPEC_GROUP: Self = Self {
s_addr: 0xe0000000_u32.to_be(),
};
pub const ALLHOSTS_GROUP: Self = Self {
s_addr: 0xe0000001_u32.to_be(),
};
pub const ALLRTRS_GROUP: Self = Self {
s_addr: 0xe0000002_u32.to_be(),
};
pub const ALLSNOOPERS_GROUP: Self = Self {
s_addr: 0xe000006a_u32.to_be(),
};
#[inline(always)]
pub const fn from_u32(raw: u32) -> Self {
Self {
s_addr: raw.to_be(),
}
}
#[inline(always)]
pub const fn from_octets(raw: [u8; 4]) -> Self {
Self::from_u32(u32::from_be_bytes(raw))
}
#[inline(always)]
pub const fn as_u32(&self) -> u32 {
self.s_addr.to_be() }
#[inline(always)]
pub const fn as_octets(&self) -> [u8; 4] {
self.as_u32().to_be_bytes()
}
pub const fn to_ipv6_mapped(&self) -> Ipv6Addr {
let our_octets = self.as_octets();
let mut new_octets = [0_u8; 16];
new_octets[10] = 0xff;
new_octets[11] = 0xff;
new_octets[12] = our_octets[0];
new_octets[13] = our_octets[1];
new_octets[14] = our_octets[2];
new_octets[15] = our_octets[3];
Ipv6Addr::from_octets(new_octets)
}
}
#[derive(Clone, Copy, Debug)]
#[repr(C, align(8))]
pub struct SockAddrIpv6 {
sin6_family: linux_unsafe::sa_family_t,
sin6_port: u16, sin6_flowinfo: u32,
sin6_addr: Ipv6Addr,
sin6_scope_id: u32,
}
impl SockAddrIpv6 {
#[inline]
pub const fn new(host_addr: Ipv6Addr, port: u16) -> Self {
Self {
sin6_family: AF_INET6,
sin6_port: port.to_be(),
sin6_addr: host_addr,
sin6_flowinfo: 0,
sin6_scope_id: 0,
}
}
#[inline]
pub const fn new_with_scope(host_addr: Ipv6Addr, port: u16, scope_id: u32) -> Self {
Self {
sin6_family: AF_INET6,
sin6_port: port.to_be(),
sin6_addr: host_addr,
sin6_flowinfo: 0,
sin6_scope_id: scope_id,
}
}
#[inline(always)]
pub const fn host_address(&self) -> Ipv6Addr {
self.sin6_addr
}
#[inline(always)]
pub const fn port(&self) -> u16 {
self.sin6_port.to_be() }
#[inline(always)]
pub const fn scope_id(&self) -> u32 {
self.sin6_scope_id
}
}
#[derive(Clone, Copy, Debug)]
#[repr(C)]
pub struct Ipv6Addr {
s6_addr: [u8; 16],
}
impl Ipv6Addr {
pub const ANY: Self = Self {
s6_addr: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
};
pub const LOOPBACK: Self = Self {
s6_addr: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
};
pub const LINKLOCAL_ALLNODES: Self = Self {
s6_addr: [0xff, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
};
pub const LINKLOCAL_ALLROUTERS: Self = Self {
s6_addr: [0xff, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2],
};
pub const INTERFACELOCAL_ALLNODES: Self = Self {
s6_addr: [0xff, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
};
pub const INTERFACELOCAL_ALLROUTERS: Self = Self {
s6_addr: [0xff, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2],
};
pub const SITELOCAL_ALLROUTERS: Self = Self {
s6_addr: [0xff, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2],
};
#[inline(always)]
pub const fn from_octets(raw: [u8; 16]) -> Self {
Self { s6_addr: raw }
}
#[inline(always)]
pub const fn as_octets(&self) -> [u8; 16] {
self.s6_addr
}
}
#[derive(Clone, Copy)]
#[repr(transparent)]
pub struct SockAddrIp(SockAddrIpInner);
#[derive(Clone, Copy)]
#[repr(C, align(8))]
union SockAddrIpInner {
jt: SockAddrJustTag,
v4: SockAddrIpv4,
v6: SockAddrIpv6,
}
#[derive(Clone, Copy, Debug)]
#[repr(C, align(8))]
struct SockAddrJustTag {
family: linux_unsafe::sa_family_t,
}
impl SockAddrIp {
pub fn new(host_address: impl Into<IpAddr>, port: u16) -> Self {
let host_address = host_address.into();
match host_address {
IpAddr::V4(addr) => Self(SockAddrIpInner {
v4: SockAddrIpv4::new(addr, port),
}),
IpAddr::V6(addr) => Self(SockAddrIpInner {
v6: SockAddrIpv6::new(addr, port),
}),
}
}
pub const fn address_family(&self) -> linux_unsafe::sa_family_t {
unsafe { self.0.jt.family }
}
pub const fn host_address(&self) -> IpAddr {
match self.address_family() {
AF_INET => IpAddr::V4(unsafe { self.0.v4 }.host_address()),
AF_INET6 => IpAddr::V6(unsafe { self.0.v6 }.host_address()),
_ => unreachable!(),
}
}
#[inline(always)]
pub const fn port(&self) -> u16 {
match self.address_family() {
AF_INET => unsafe { self.0.v4 }.port(),
AF_INET6 => unsafe { self.0.v6 }.port(),
_ => unreachable!(),
}
}
}
impl core::fmt::Debug for SockAddrIp {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self.address_family() {
AF_INET => unsafe { self.0.v4 }.fmt(f),
AF_INET6 => unsafe { self.0.v6 }.fmt(f),
_ => unreachable!(),
}
}
}
#[derive(Clone, Copy, Debug)]
pub enum IpAddr {
V4(Ipv4Addr),
V6(Ipv6Addr),
}
impl IpAddr {
pub const fn to_ipv6_mapped(self) -> Ipv6Addr {
match self {
IpAddr::V4(addr) => addr.to_ipv6_mapped(),
IpAddr::V6(addr) => addr,
}
}
}
pub const AF_INET: linux_unsafe::sa_family_t = 2;
pub const AF_INET6: linux_unsafe::sa_family_t = 10;
pub const IPPROTO_TCP: super::SocketProtocolFixed<tcp::TcpSocketDevice> =
unsafe { super::socket_protocol(6) };
pub const IPPROTO_ICMP: linux_unsafe::int = 1;
pub const IPPROTO_IGMP: linux_unsafe::int = 4;
pub const IPPROTO_EGP: linux_unsafe::int = 8;
pub const IPPROTO_PUP: linux_unsafe::int = 12;
pub const IPPROTO_UDP: linux_unsafe::int = 17;
pub const IPPROTO_IDP: linux_unsafe::int = 22;
pub const IPPROTO_TP: linux_unsafe::int = 29;
pub const IPPROTO_DCCP: linux_unsafe::int = 33;
pub const IPPROTO_IPV6: linux_unsafe::int = 41;
pub const IPPROTO_RSVP: linux_unsafe::int = 46;
pub const IPPROTO_GRE: linux_unsafe::int = 47;
pub const IPPROTO_ESP: linux_unsafe::int = 50;
pub const IPPROTO_AH: linux_unsafe::int = 51;
pub const IPPROTO_MTP: linux_unsafe::int = 92;
pub const IPPROTO_ENCAP: linux_unsafe::int = 98;
pub const IPPROTO_PIM: linux_unsafe::int = 103;
pub const IPPROTO_COMP: linux_unsafe::int = 108;
pub const IPPROTO_L2TP: linux_unsafe::int = 115;
pub const IPPROTO_SCTP: linux_unsafe::int = 132;
pub const IPPROTO_UDPLITE: linux_unsafe::int = 136;
pub const IPPROTO_MPLS: linux_unsafe::int = 137;
pub const IPPROTO_ETHERNET: linux_unsafe::int = 143;
pub const IPPROTO_RAW: linux_unsafe::int = 255;
pub const IPPROTO_MPTCP: linux_unsafe::int = 262;
unsafe impl super::SockAddr for SockAddrIpv4 {
#[inline(always)]
unsafe fn sockaddr_raw_const(&self) -> (*const linux_unsafe::void, linux_unsafe::socklen_t) {
(
self as *const Self as *const _,
core::mem::size_of::<Self>() as linux_unsafe::socklen_t,
)
}
#[inline(always)]
unsafe fn sockaddr_raw_mut(&mut self) -> (*mut linux_unsafe::void, linux_unsafe::socklen_t) {
(
self as *mut Self as *mut _,
core::mem::size_of::<Self>() as linux_unsafe::socklen_t,
)
}
}
unsafe impl super::SockAddr for SockAddrIpv6 {
#[inline(always)]
unsafe fn sockaddr_raw_const(&self) -> (*const linux_unsafe::void, linux_unsafe::socklen_t) {
(
self as *const Self as *const _,
core::mem::size_of::<Self>() as linux_unsafe::socklen_t,
)
}
#[inline(always)]
unsafe fn sockaddr_raw_mut(&mut self) -> (*mut linux_unsafe::void, linux_unsafe::socklen_t) {
(
self as *mut Self as *mut _,
core::mem::size_of::<Self>() as linux_unsafe::socklen_t,
)
}
}
unsafe impl super::SockAddr for SockAddrIp {
#[inline(always)]
unsafe fn sockaddr_raw_const(&self) -> (*const linux_unsafe::void, linux_unsafe::socklen_t) {
match self.address_family() {
AF_INET => self.0.v4.sockaddr_raw_const(),
AF_INET6 => self.0.v6.sockaddr_raw_const(),
_ => unreachable!(), }
}
#[inline(always)]
unsafe fn sockaddr_raw_mut(&mut self) -> (*mut linux_unsafe::void, linux_unsafe::socklen_t) {
match self.address_family() {
AF_INET => self.0.v4.sockaddr_raw_mut(),
AF_INET6 => self.0.v6.sockaddr_raw_mut(),
_ => unreachable!(), }
}
}
impl From<Ipv4Addr> for IpAddr {
fn from(value: Ipv4Addr) -> Self {
IpAddr::V4(value)
}
}
impl From<Ipv6Addr> for IpAddr {
fn from(value: Ipv6Addr) -> Self {
IpAddr::V6(value)
}
}
#[cfg(feature = "std")]
extern crate std;
#[cfg(feature = "std")]
impl Ipv4Addr {
#[inline]
pub const fn from_std(addr: std::net::Ipv4Addr) -> Self {
Self::from_octets(addr.octets())
}
}
#[cfg(feature = "std")]
impl From<std::net::Ipv4Addr> for Ipv4Addr {
fn from(value: std::net::Ipv4Addr) -> Self {
Self::from_std(value)
}
}
#[cfg(feature = "std")]
impl Ipv6Addr {
#[inline]
pub const fn from_std(addr: std::net::Ipv6Addr) -> Self {
Self::from_octets(addr.octets())
}
}
#[cfg(feature = "std")]
impl From<std::net::Ipv6Addr> for Ipv6Addr {
fn from(value: std::net::Ipv6Addr) -> Self {
Self::from_std(value)
}
}
#[cfg(feature = "std")]
impl IpAddr {
#[inline]
pub const fn from_std(addr: std::net::IpAddr) -> Self {
match addr {
std::net::IpAddr::V4(addr) => Self::V4(Ipv4Addr::from_octets(addr.octets())),
std::net::IpAddr::V6(addr) => Self::V6(Ipv6Addr::from_octets(addr.octets())),
}
}
}
#[cfg(feature = "std")]
impl From<std::net::IpAddr> for IpAddr {
fn from(value: std::net::IpAddr) -> Self {
Self::from_std(value)
}
}
#[derive(Clone, Copy)]
pub struct Ipv4SocketDevice;
impl crate::fd::ioctl::IoDevice for Ipv4SocketDevice {}
unsafe impl crate::fd::ioctl::SubDevice<super::SocketDevice> for Ipv4SocketDevice {}
#[derive(Clone, Copy)]
pub struct Ipv6SocketDevice;
impl crate::fd::ioctl::IoDevice for Ipv6SocketDevice {}
unsafe impl crate::fd::ioctl::SubDevice<super::SocketDevice> for Ipv6SocketDevice {}
pub mod tcp;