@@ -17,14 +17,14 @@ use crate::packet::ipv6::MutableIpv6Packet;
17
17
use crate :: packet:: udp;
18
18
use crate :: packet:: udp:: { MutableUdpPacket , UdpPacket } ;
19
19
use crate :: packet:: Packet ;
20
- use std:: iter:: Iterator ;
21
- use pnet_base:: core_net:: { IpAddr , Ipv4Addr , Ipv6Addr } ;
22
- use std:: sync:: mpsc:: channel;
23
- use std:: thread;
24
20
use crate :: transport:: TransportProtocol :: { Ipv4 , Ipv6 } ;
25
21
use crate :: transport:: {
26
22
ipv4_packet_iter, transport_channel, udp_packet_iter, TransportChannelType , TransportProtocol ,
27
23
} ;
24
+ use pnet_base:: core_net:: { IpAddr , Ipv4Addr , Ipv6Addr } ;
25
+ use std:: iter:: Iterator ;
26
+ use std:: sync:: mpsc:: channel;
27
+ use std:: thread;
28
28
29
29
const IPV4_HEADER_LEN : usize = 20 ;
30
30
const IPV6_HEADER_LEN : usize = 40 ;
@@ -51,7 +51,8 @@ fn ipv6_destination() -> Ipv6Addr {
51
51
const TEST_PROTO : IpNextHeaderProtocol = IpNextHeaderProtocols :: Test1 ;
52
52
53
53
fn build_ipv4_header ( packet : & mut [ u8 ] , offset : usize ) {
54
- let mut ip_header = MutableIpv4Packet :: new ( & mut packet[ offset..] ) . expect ( "could not create MutableIpv4Packet" ) ;
54
+ let mut ip_header =
55
+ MutableIpv4Packet :: new ( & mut packet[ offset..] ) . expect ( "could not create MutableIpv4Packet" ) ;
55
56
56
57
let total_len = ( IPV4_HEADER_LEN + UDP_HEADER_LEN + TEST_DATA_LEN ) as u16 ;
57
58
@@ -67,7 +68,8 @@ fn build_ipv4_header(packet: &mut [u8], offset: usize) {
67
68
}
68
69
69
70
fn build_ipv6_header ( packet : & mut [ u8 ] , offset : usize ) {
70
- let mut ip_header = MutableIpv6Packet :: new ( & mut packet[ offset..] ) . expect ( "could not create MutableIpv6Packet" ) ;
71
+ let mut ip_header =
72
+ MutableIpv6Packet :: new ( & mut packet[ offset..] ) . expect ( "could not create MutableIpv6Packet" ) ;
71
73
72
74
ip_header. set_version ( 6 ) ;
73
75
ip_header. set_payload_length ( ( UDP_HEADER_LEN + TEST_DATA_LEN ) as u16 ) ;
@@ -78,7 +80,8 @@ fn build_ipv6_header(packet: &mut [u8], offset: usize) {
78
80
}
79
81
80
82
fn build_udp_header ( packet : & mut [ u8 ] , offset : usize ) {
81
- let mut udp_header = MutableUdpPacket :: new ( & mut packet[ offset..] ) . expect ( "could not create MutableUdpPacket" ) ;
83
+ let mut udp_header =
84
+ MutableUdpPacket :: new ( & mut packet[ offset..] ) . expect ( "could not create MutableUdpPacket" ) ;
82
85
83
86
udp_header. set_source ( 1234 ) ; // Arbitrary port number
84
87
udp_header. set_destination ( 1234 ) ;
@@ -126,8 +129,14 @@ fn build_udp4_packet(
126
129
} ;
127
130
128
131
let slice = & mut packet[ ( start + IPV4_HEADER_LEN as usize ) ..] ;
129
- let checksum = udp:: ipv4_checksum ( & UdpPacket :: new ( slice) . expect ( "could not create UdpPacket" ) , & source, & dest) ;
130
- MutableUdpPacket :: new ( slice) . expect ( "could not create MutableUdpPacket" ) . set_checksum ( checksum) ;
132
+ let checksum = udp:: ipv4_checksum (
133
+ & UdpPacket :: new ( slice) . expect ( "could not create UdpPacket" ) ,
134
+ & source,
135
+ & dest,
136
+ ) ;
137
+ MutableUdpPacket :: new ( slice)
138
+ . expect ( "could not create MutableUdpPacket" )
139
+ . set_checksum ( checksum) ;
131
140
}
132
141
133
142
fn build_udp6_packet ( packet : & mut [ u8 ] , start : usize , msg : & str ) {
@@ -148,7 +157,9 @@ fn build_udp6_packet(packet: &mut [u8], start: usize, msg: &str) {
148
157
& ipv6_source ( ) ,
149
158
& ipv6_destination ( ) ,
150
159
) ;
151
- MutableUdpPacket :: new ( slice) . expect ( "could not create MutableUdpPacket" ) . set_checksum ( checksum) ;
160
+ MutableUdpPacket :: new ( slice)
161
+ . expect ( "could not create MutableUdpPacket" )
162
+ . set_checksum ( checksum) ;
152
163
}
153
164
154
165
// OSes have a nasty habit of tweaking IP fields, so we only check
@@ -204,7 +215,8 @@ fn layer4(ip: IpAddr, header_len: usize) {
204
215
assert_eq ! ( addr, ip) ;
205
216
assert_eq ! (
206
217
header,
207
- UdpPacket :: new( & packet[ header_len..packet_len] ) . expect( "could not create UdpPacket" )
218
+ UdpPacket :: new( & packet[ header_len..packet_len] )
219
+ . expect( "could not create UdpPacket" )
208
220
) ;
209
221
break ;
210
222
}
@@ -215,7 +227,8 @@ fn layer4(ip: IpAddr, header_len: usize) {
215
227
}
216
228
} ) ;
217
229
218
- rx. recv ( ) . expect ( "failed to receive message through channel" ) ;
230
+ rx. recv ( )
231
+ . expect ( "failed to receive message through channel" ) ;
219
232
match ttx. send_to ( udp, ip) {
220
233
Ok ( res) => assert_eq ! ( res as usize , UDP_HEADER_LEN + TEST_DATA_LEN ) ,
221
234
Err ( e) => panic ! ( "layer4_test failed: {}" , e) ,
@@ -281,7 +294,8 @@ fn layer3_ipv4() {
281
294
. expect ( "could not create UdpPacket" ) ;
282
295
assert_eq ! (
283
296
udp_header,
284
- UdpPacket :: new( & packet[ IPV4_HEADER_LEN ..] ) . expect( "could not create UdpPacket" )
297
+ UdpPacket :: new( & packet[ IPV4_HEADER_LEN ..] )
298
+ . expect( "could not create UdpPacket" )
285
299
) ;
286
300
287
301
assert_eq ! (
@@ -297,8 +311,12 @@ fn layer3_ipv4() {
297
311
}
298
312
} ) ;
299
313
300
- rx. recv ( ) . expect ( "unable to receive message through channel" ) ;
301
- match ttx. send_to ( Ipv4Packet :: new ( & packet[ ..] ) . expect ( "could not create Ipv4Packet" ) , send_addr) {
314
+ rx. recv ( )
315
+ . expect ( "unable to receive message through channel" ) ;
316
+ match ttx. send_to (
317
+ Ipv4Packet :: new ( & packet[ ..] ) . expect ( "could not create Ipv4Packet" ) ,
318
+ send_addr,
319
+ ) {
302
320
Ok ( res) => assert_eq ! ( res as usize , packet. len( ) ) ,
303
321
Err ( e) => panic ! ( "layer3_ipv4_test failed: {}" , e) ,
304
322
}
@@ -355,9 +373,18 @@ fn layer2() {
355
373
let mut packet = [ 0u8 ; ETHERNET_HEADER_LEN + IPV4_HEADER_LEN + UDP_HEADER_LEN + TEST_DATA_LEN ] ;
356
374
357
375
{
358
- let mut ethernet_header = MutableEthernetPacket :: new ( & mut packet[ ..] ) . expect ( "could not create MutableEthernetPacket" ) ;
359
- ethernet_header. set_source ( interface. mac . expect ( "could not find mac address for test interface" ) ) ;
360
- ethernet_header. set_destination ( interface. mac . expect ( "could not find mac address for test interface" ) ) ;
376
+ let mut ethernet_header = MutableEthernetPacket :: new ( & mut packet[ ..] )
377
+ . expect ( "could not create MutableEthernetPacket" ) ;
378
+ ethernet_header. set_source (
379
+ interface
380
+ . mac
381
+ . expect ( "could not find mac address for test interface" ) ,
382
+ ) ;
383
+ ethernet_header. set_destination (
384
+ interface
385
+ . mac
386
+ . expect ( "could not find mac address for test interface" ) ,
387
+ ) ;
361
388
ethernet_header. set_ethertype ( EtherTypes :: Ipv4 ) ;
362
389
}
363
390
@@ -394,8 +421,12 @@ fn layer2() {
394
421
if i == 10_000 {
395
422
panic ! ( "layer2: did not find matching packet after 10_000 iterations" ) ;
396
423
}
397
- if EthernetPacket :: new ( & packet[ ..] ) . expect ( "failed to create EthernetPacket" ) . payload ( )
398
- == EthernetPacket :: new ( eh) . expect ( "failed to create EthernetPacket" ) . payload ( )
424
+ if EthernetPacket :: new ( & packet[ ..] )
425
+ . expect ( "failed to create EthernetPacket" )
426
+ . payload ( )
427
+ == EthernetPacket :: new ( eh)
428
+ . expect ( "failed to create EthernetPacket" )
429
+ . payload ( )
399
430
{
400
431
return ;
401
432
}
@@ -408,7 +439,8 @@ fn layer2() {
408
439
}
409
440
} ) ;
410
441
411
- rx. recv ( ) . expect ( "failed to receive message through channel" ) ;
442
+ rx. recv ( )
443
+ . expect ( "failed to receive message through channel" ) ;
412
444
match dltx. send_to ( & packet[ ..] , None ) {
413
445
Some ( Ok ( ( ) ) ) => ( ) ,
414
446
Some ( Err ( e) ) => panic ! ( "layer2_test failed: {}" , e) ,
@@ -434,9 +466,18 @@ fn layer2_timeouts() {
434
466
let mut packet = [ 0u8 ; ETHERNET_HEADER_LEN + IPV4_HEADER_LEN + UDP_HEADER_LEN + TEST_DATA_LEN ] ;
435
467
436
468
{
437
- let mut ethernet_header = MutableEthernetPacket :: new ( & mut packet[ ..] ) . expect ( "failed to create MutableEthernetPacket" ) ;
438
- ethernet_header. set_source ( interface. mac . expect ( "missing mac address for test interface" ) ) ;
439
- ethernet_header. set_destination ( interface. mac . expect ( "missing mac address for test interface" ) ) ;
469
+ let mut ethernet_header = MutableEthernetPacket :: new ( & mut packet[ ..] )
470
+ . expect ( "failed to create MutableEthernetPacket" ) ;
471
+ ethernet_header. set_source (
472
+ interface
473
+ . mac
474
+ . expect ( "missing mac address for test interface" ) ,
475
+ ) ;
476
+ ethernet_header. set_destination (
477
+ interface
478
+ . mac
479
+ . expect ( "missing mac address for test interface" ) ,
480
+ ) ;
440
481
ethernet_header. set_ethertype ( EtherTypes :: Ipv4 ) ;
441
482
}
442
483
@@ -487,7 +528,8 @@ fn layer2_timeouts() {
487
528
}
488
529
}
489
530
} ) ;
490
- rx. recv ( ) . expect ( "failed to receive message through channel" ) ;
531
+ rx. recv ( )
532
+ . expect ( "failed to receive message through channel" ) ;
491
533
492
534
// Wait a while
493
535
thread:: sleep ( Duration :: from_millis ( 1000 ) ) ;
0 commit comments