1 use super::*; 2 use crate::vnet::chunk::ChunkUdp; 3 4 use tokio::sync::{broadcast, mpsc}; 5 6 const DEMO_IP: &str = "1.2.3.4"; 7 8 #[derive(Default)] 9 struct DummyObserver; 10 11 #[async_trait] 12 impl ConnObserver for DummyObserver { 13 async fn write(&self, _c: Box<dyn Chunk + Send + Sync>) -> Result<()> { 14 Ok(()) 15 } 16 17 async fn on_closed(&self, _addr: SocketAddr) {} 18 19 fn determine_source_ip(&self, loc_ip: IpAddr, _dst_ip: IpAddr) -> Option<IpAddr> { 20 Some(loc_ip) 21 } 22 } 23 24 #[tokio::test] 25 async fn test_net_native_interfaces() -> Result<()> { 26 let nw = Net::new(None); 27 assert!(!nw.is_virtual(), "should be false"); 28 29 let interfaces = nw.get_interfaces().await; 30 log::debug!("interfaces: {:?}", interfaces); 31 for ifc in interfaces { 32 let addrs = ifc.addrs(); 33 for addr in addrs { 34 log::debug!("{}", addr) 35 } 36 } 37 38 Ok(()) 39 } 40 41 #[tokio::test] 42 async fn test_net_native_resolve_addr() -> Result<()> { 43 let nw = Net::new(None); 44 assert!(!nw.is_virtual(), "should be false"); 45 46 let udp_addr = nw.resolve_addr(true, "localhost:1234").await?; 47 assert_eq!("127.0.0.1", udp_addr.ip().to_string(), "should match"); 48 assert_eq!(1234, udp_addr.port(), "should match"); 49 50 let result = nw.resolve_addr(false, "127.0.0.1:1234").await; 51 assert!(result.is_err(), "should not match"); 52 53 Ok(()) 54 } 55 56 #[tokio::test] 57 async fn test_net_native_bind() -> Result<()> { 58 let nw = Net::new(None); 59 assert!(!nw.is_virtual(), "should be false"); 60 61 let conn = nw.bind(SocketAddr::from_str("127.0.0.1:0")?).await?; 62 let laddr = conn.local_addr()?; 63 assert_eq!( 64 laddr.ip().to_string(), 65 "127.0.0.1", 66 "local_addr ip should match 127.0.0.1" 67 ); 68 log::debug!("laddr: {}", laddr); 69 70 Ok(()) 71 } 72 73 #[tokio::test] 74 async fn test_net_native_dail() -> Result<()> { 75 let nw = Net::new(None); 76 assert!(!nw.is_virtual(), "should be false"); 77 78 let conn = nw.dail(true, "127.0.0.1:1234").await?; 79 let laddr = conn.local_addr()?; 80 assert_eq!( 81 laddr.ip().to_string(), 82 "127.0.0.1", 83 "local_addr should match 127.0.0.1" 84 ); 85 assert_ne!(laddr.port(), 1234, "local_addr port should match 1234"); 86 log::debug!("laddr: {}", laddr); 87 88 Ok(()) 89 } 90 91 #[tokio::test] 92 async fn test_net_native_loopback() -> Result<()> { 93 let nw = Net::new(None); 94 assert!(!nw.is_virtual(), "should be false"); 95 96 let conn = nw.bind(SocketAddr::from_str("127.0.0.1:0")?).await?; 97 let laddr = conn.local_addr()?; 98 99 let msg = "PING!"; 100 let n = conn.send_to(msg.as_bytes(), laddr).await?; 101 assert_eq!(n, msg.len(), "should match msg size {}", msg.len()); 102 103 let mut buf = vec![0u8; 1000]; 104 let (n, raddr) = conn.recv_from(&mut buf).await?; 105 assert_eq!(n, msg.len(), "should match msg size {}", msg.len()); 106 assert_eq!( 107 msg.as_bytes(), 108 &buf[..n], 109 "should match msg content {}", 110 msg 111 ); 112 assert_eq!(laddr, raddr, "should match addr {}", laddr); 113 114 Ok(()) 115 } 116 117 #[tokio::test] 118 async fn test_net_native_unexpected_operations() -> Result<()> { 119 let mut lo_name = String::new(); 120 let ifcs = ifaces::ifaces()?; 121 for ifc in &ifcs { 122 if let Some(addr) = ifc.addr { 123 if addr.ip().is_loopback() { 124 lo_name = ifc.name.clone(); 125 break; 126 } 127 } 128 } 129 130 let nw = Net::new(None); 131 assert!(!nw.is_virtual(), "should be false"); 132 133 if !lo_name.is_empty() { 134 if let Some(ifc) = nw.get_interface(&lo_name).await { 135 assert_eq!(lo_name, ifc.name, "should match ifc name"); 136 } else { 137 assert!(false, "should succeed"); 138 } 139 } 140 141 let result = nw.get_interface("foo0").await; 142 assert!(result.is_none(), "should be none"); 143 144 //let ips = nw.get_static_ips(); 145 //assert!(ips.is_empty(), "should empty"); 146 147 Ok(()) 148 } 149 150 #[tokio::test] 151 async fn test_net_virtual_interfaces() -> Result<()> { 152 let nw = Net::new(Some(NetConfig::default())); 153 assert!(nw.is_virtual(), "should be true"); 154 155 let interfaces = nw.get_interfaces().await; 156 assert_eq!(2, interfaces.len(), "should be one interface"); 157 158 for ifc in interfaces { 159 match ifc.name.as_str() { 160 LO0_STR => { 161 let addrs = ifc.addrs(); 162 assert_eq!(1, addrs.len(), "should be one address"); 163 } 164 "eth0" => { 165 let addrs = ifc.addrs(); 166 assert!(addrs.is_empty(), "should empty"); 167 } 168 _ => { 169 assert!(false, "unknown interface: {}", ifc.name); 170 } 171 } 172 } 173 174 Ok(()) 175 } 176 177 #[tokio::test] 178 async fn test_net_virtual_interface_by_name() -> Result<()> { 179 let nw = Net::new(Some(NetConfig::default())); 180 assert!(nw.is_virtual(), "should be true"); 181 182 let interfaces = nw.get_interfaces().await; 183 assert_eq!(2, interfaces.len(), "should be one interface"); 184 185 let nic = nw.get_nic()?; 186 let nic = nic.lock().await; 187 if let Some(ifc) = nic.get_interface(LO0_STR).await { 188 assert_eq!(LO0_STR, ifc.name.as_str(), "should match"); 189 let addrs = ifc.addrs(); 190 assert_eq!(1, addrs.len(), "should be one address"); 191 } else { 192 assert!(false, "should got ifc"); 193 } 194 195 if let Some(ifc) = nic.get_interface("eth0").await { 196 assert_eq!("eth0", ifc.name.as_str(), "should match"); 197 let addrs = ifc.addrs(); 198 assert!(addrs.is_empty(), "should empty"); 199 } else { 200 assert!(false, "should got ifc"); 201 } 202 203 let result = nic.get_interface("foo0").await; 204 assert!(result.is_none(), "should fail"); 205 206 Ok(()) 207 } 208 209 #[tokio::test] 210 async fn test_net_virtual_has_ipaddr() -> Result<()> { 211 let nw = Net::new(Some(NetConfig::default())); 212 assert!(nw.is_virtual(), "should be true"); 213 214 let interfaces = nw.get_interfaces().await; 215 assert_eq!(2, interfaces.len(), "should be one interface"); 216 217 { 218 let nic = nw.get_nic()?; 219 let mut nic = nic.lock().await; 220 let ipnet = IpNet::from_str("10.1.2.3/24")?; 221 nic.add_addrs_to_interface("eth0", &[ipnet]).await?; 222 223 if let Some(ifc) = nic.get_interface("eth0").await { 224 let addrs = ifc.addrs(); 225 assert!(!addrs.is_empty(), "should not empty"); 226 } 227 } 228 229 if let Net::VNet(vnet) = &nw { 230 let net = vnet.lock().await; 231 let ip = Ipv4Addr::from_str("127.0.0.1")?.into(); 232 assert!(net.has_ipaddr(ip), "the IP addr {} should exist", ip); 233 234 let ip = Ipv4Addr::from_str("10.1.2.3")?.into(); 235 assert!(net.has_ipaddr(ip), "the IP addr {} should exist", ip); 236 237 let ip = Ipv4Addr::from_str("192.168.1.1")?.into(); 238 assert!(!net.has_ipaddr(ip), "the IP addr {} should exist", ip); 239 } 240 Ok(()) 241 } 242 243 #[tokio::test] 244 async fn test_net_virtual_get_all_ipaddrs() -> Result<()> { 245 let nw = Net::new(Some(NetConfig::default())); 246 assert!(nw.is_virtual(), "should be true"); 247 248 let interfaces = nw.get_interfaces().await; 249 assert_eq!(2, interfaces.len(), "should be one interface"); 250 251 { 252 let nic = nw.get_nic()?; 253 let mut nic = nic.lock().await; 254 let ipnet = IpNet::from_str("10.1.2.3/24")?; 255 nic.add_addrs_to_interface("eth0", &[ipnet]).await?; 256 257 if let Some(ifc) = nic.get_interface("eth0").await { 258 let addrs = ifc.addrs(); 259 assert!(!addrs.is_empty(), "should not empty"); 260 } 261 } 262 263 if let Net::VNet(vnet) = &nw { 264 let net = vnet.lock().await; 265 let ips = net.get_all_ipaddrs(false); 266 assert_eq!(2, ips.len(), "ips should match size {} == 2", ips.len()) 267 } 268 269 Ok(()) 270 } 271 272 #[tokio::test] 273 async fn test_net_virtual_assign_port() -> Result<()> { 274 let mut nw = Net::new(Some(NetConfig::default())); 275 assert!(nw.is_virtual(), "should be true"); 276 277 let addr = DEMO_IP; 278 let start = 1000u16; 279 let end = 1002u16; 280 let space = end + 1 - start; 281 282 let interfaces = nw.get_interfaces().await; 283 assert_eq!(2, interfaces.len(), "should be one interface"); 284 285 { 286 let nic = nw.get_nic()?; 287 let mut nic = nic.lock().await; 288 let ipnet = IpNet::from_str(&format!("{}/24", addr))?; 289 nic.add_addrs_to_interface("eth0", &[ipnet]).await?; 290 } 291 292 if let Net::VNet(vnet) = &mut nw { 293 let vnet = vnet.lock().await; 294 // attempt to assign port with start > end should fail 295 let ip = IpAddr::from_str(addr)?; 296 let result = vnet.assign_port(ip, 3000, 2999).await; 297 assert!(result.is_err(), "assign_port should fail"); 298 299 for i in 0..space { 300 let port = vnet.assign_port(ip, start, end).await?; 301 log::debug!("{} got port: {}", i, port); 302 303 let obs: Arc<Mutex<dyn ConnObserver + Send + Sync>> = 304 Arc::new(Mutex::new(DummyObserver::default())); 305 306 let conn = Arc::new(UdpConn::new(SocketAddr::new(ip, port), None, obs)); 307 308 let vi = vnet.vi.lock().await; 309 let _ = vi.udp_conns.insert(conn).await; 310 } 311 312 { 313 let vi = vnet.vi.lock().await; 314 assert_eq!( 315 space as usize, 316 vi.udp_conns.len().await, 317 "udp_conns should match" 318 ); 319 } 320 321 // attempt to assign again should fail 322 let result = vnet.assign_port(ip, start, end).await; 323 assert!(result.is_err(), "assign_port should fail"); 324 } 325 326 Ok(()) 327 } 328 329 #[tokio::test] 330 async fn test_net_virtual_determine_source_ip() -> Result<()> { 331 let mut nw = Net::new(Some(NetConfig::default())); 332 assert!(nw.is_virtual(), "should be true"); 333 334 let interfaces = nw.get_interfaces().await; 335 assert_eq!(2, interfaces.len(), "should be one interface"); 336 337 { 338 let nic = nw.get_nic()?; 339 let mut nic = nic.lock().await; 340 let ipnet = IpNet::from_str(&format!("{}/24", DEMO_IP))?; 341 nic.add_addrs_to_interface("eth0", &[ipnet]).await?; 342 } 343 344 // Any IP turned into non-loopback IP 345 let any_ip = IpAddr::from_str("0.0.0.0")?; 346 let dst_ip = IpAddr::from_str("27.1.7.135")?; 347 if let Net::VNet(vnet) = &mut nw { 348 let vnet = vnet.lock().await; 349 let vi = vnet.vi.lock().await; 350 let src_ip = vi.determine_source_ip(any_ip, dst_ip); 351 log::debug!("any_ip: {} => {:?}", any_ip, src_ip); 352 assert!(src_ip.is_some(), "shouldn't be none"); 353 if let Some(src_ip) = src_ip { 354 assert_eq!(src_ip.to_string().as_str(), DEMO_IP, "use non-loopback IP"); 355 } 356 } 357 358 // Any IP turned into loopback IP 359 let any_ip = IpAddr::from_str("0.0.0.0")?; 360 let dst_ip = IpAddr::from_str("127.0.0.2")?; 361 if let Net::VNet(vnet) = &mut nw { 362 let vnet = vnet.lock().await; 363 let vi = vnet.vi.lock().await; 364 let src_ip = vi.determine_source_ip(any_ip, dst_ip); 365 log::debug!("any_ip: {} => {:?}", any_ip, src_ip); 366 assert!(src_ip.is_some(), "shouldn't be none"); 367 if let Some(src_ip) = src_ip { 368 assert_eq!(src_ip.to_string().as_str(), "127.0.0.1", "use loopback IP"); 369 } 370 } 371 372 // Non any IP won't change 373 let any_ip = IpAddr::from_str(DEMO_IP)?; 374 let dst_ip = IpAddr::from_str("127.0.0.2")?; 375 if let Net::VNet(vnet) = &mut nw { 376 let vnet = vnet.lock().await; 377 let vi = vnet.vi.lock().await; 378 let src_ip = vi.determine_source_ip(any_ip, dst_ip); 379 log::debug!("any_ip: {} => {:?}", any_ip, src_ip); 380 assert!(src_ip.is_some(), "shouldn't be none"); 381 if let Some(src_ip) = src_ip { 382 assert_eq!(src_ip, any_ip, "IP change"); 383 } 384 } 385 386 Ok(()) 387 } 388 389 #[tokio::test] 390 async fn test_net_virtual_resolve_addr() -> Result<()> { 391 let nw = Net::new(Some(NetConfig::default())); 392 assert!(nw.is_virtual(), "should be true"); 393 394 let udp_addr = nw.resolve_addr(true, "localhost:1234").await?; 395 assert_eq!( 396 "127.0.0.1", 397 udp_addr.ip().to_string().as_str(), 398 "udp addr {} should match 127.0.0.1", 399 udp_addr.ip(), 400 ); 401 assert_eq!( 402 1234, 403 udp_addr.port(), 404 "udp addr {} should match 1234", 405 udp_addr.port() 406 ); 407 408 Ok(()) 409 } 410 411 #[tokio::test] 412 async fn test_net_virtual_loopback1() -> Result<()> { 413 let nw = Net::new(Some(NetConfig::default())); 414 assert!(nw.is_virtual(), "should be true"); 415 416 let conn = nw.bind(SocketAddr::from_str("127.0.0.1:0")?).await?; 417 let laddr = conn.local_addr()?; 418 419 let msg = "PING!"; 420 let n = conn.send_to(msg.as_bytes(), laddr).await?; 421 assert_eq!(n, msg.len(), "should match msg size {}", msg.len()); 422 423 let mut buf = vec![0u8; 1000]; 424 let (n, raddr) = conn.recv_from(&mut buf).await?; 425 assert_eq!(n, msg.len(), "should match msg size {}", msg.len()); 426 assert_eq!( 427 msg.as_bytes(), 428 &buf[..n], 429 "should match msg content {}", 430 msg 431 ); 432 assert_eq!(laddr, raddr, "should match addr {}", laddr); 433 434 Ok(()) 435 } 436 437 #[tokio::test] 438 async fn test_net_virtual_bind_specific_port() -> Result<()> { 439 let nw = Net::new(Some(NetConfig::default())); 440 assert!(nw.is_virtual(), "should be true"); 441 442 let conn = nw.bind(SocketAddr::from_str("127.0.0.1:50916")?).await?; 443 let laddr = conn.local_addr()?; 444 assert_eq!( 445 laddr.ip().to_string().as_str(), 446 "127.0.0.1", 447 "{} should match 127.0.0.1", 448 laddr.ip() 449 ); 450 assert_eq!(laddr.port(), 50916, "{} should match 50916", laddr.port()); 451 452 Ok(()) 453 } 454 455 #[tokio::test] 456 async fn test_net_virtual_dail_lo0() -> Result<()> { 457 let nw = Net::new(Some(NetConfig::default())); 458 assert!(nw.is_virtual(), "should be true"); 459 460 let conn = nw.dail(true, "127.0.0.1:1234").await?; 461 let laddr = conn.local_addr()?; 462 assert_eq!( 463 laddr.ip().to_string().as_str(), 464 "127.0.0.1", 465 "{} should match 127.0.0.1", 466 laddr.ip() 467 ); 468 assert_ne!(laddr.port(), 1234, "{} should != 1234", laddr.port()); 469 470 Ok(()) 471 } 472 473 #[tokio::test] 474 async fn test_net_virtual_dail_eth0() -> Result<()> { 475 let wan = Arc::new(Mutex::new(Router::new(RouterConfig { 476 cidr: "1.2.3.0/24".to_string(), 477 ..Default::default() 478 })?)); 479 480 let nw = Net::new(Some(NetConfig::default())); 481 482 { 483 let nic = nw.get_nic()?; 484 485 let mut w = wan.lock().await; 486 w.add_net(Arc::clone(&nic)).await?; 487 488 let n = nic.lock().await; 489 n.set_router(Arc::clone(&wan)).await?; 490 }; 491 492 let conn = nw.dail(true, "27.3.4.5:1234").await?; 493 let laddr = conn.local_addr()?; 494 assert_eq!( 495 laddr.ip().to_string().as_str(), 496 "1.2.3.1", 497 "{} should match 1.2.3.1", 498 laddr.ip() 499 ); 500 assert!(laddr.port() != 0, "{} should != 0", laddr.port()); 501 502 Ok(()) 503 } 504 505 #[tokio::test] 506 async fn test_net_virtual_resolver() -> Result<()> { 507 let wan = Arc::new(Mutex::new(Router::new(RouterConfig { 508 cidr: "1.2.3.0/24".to_string(), 509 ..Default::default() 510 })?)); 511 512 let nw = Net::new(Some(NetConfig::default())); 513 514 let remote_addr = nw.resolve_addr(true, "127.0.0.1:1234").await?; 515 assert_eq!(remote_addr.to_string(), "127.0.0.1:1234", "should match"); 516 517 let result = nw.resolve_addr(false, "127.0.0.1:1234").await; 518 assert!(result.is_err(), "should not match"); 519 520 { 521 let nic = nw.get_nic()?; 522 523 let mut w = wan.lock().await; 524 w.add_net(Arc::clone(&nic)).await?; 525 w.add_host("test.webrtc.rs".to_owned(), "30.31.32.33".to_owned()) 526 .await?; 527 528 let n = nic.lock().await; 529 n.set_router(Arc::clone(&wan)).await?; 530 } 531 532 let (done_tx, mut done_rx) = mpsc::channel::<()>(1); 533 tokio::spawn(async move { 534 let (conn, raddr) = { 535 let raddr = nw.resolve_addr(true, "test.webrtc.rs:1234").await?; 536 (nw.dail(true, "test.webrtc.rs:1234").await?, raddr) 537 }; 538 539 let laddr = conn.local_addr()?; 540 assert_eq!( 541 laddr.ip().to_string().as_str(), 542 "1.2.3.1", 543 "{} should match 1.2.3.1", 544 laddr.ip() 545 ); 546 547 assert_eq!( 548 raddr.to_string(), 549 "30.31.32.33:1234", 550 "{} should match 30.31.32.33:1234", 551 raddr 552 ); 553 554 drop(done_tx); 555 556 Result::<()>::Ok(()) 557 }); 558 559 let _ = done_rx.recv().await; 560 561 Ok(()) 562 } 563 564 #[tokio::test] 565 async fn test_net_virtual_loopback2() -> Result<()> { 566 let nw = Net::new(Some(NetConfig::default())); 567 568 let conn = nw.bind(SocketAddr::from_str("127.0.0.1:50916")?).await?; 569 let laddr = conn.local_addr()?; 570 assert_eq!( 571 laddr.to_string().as_str(), 572 "127.0.0.1:50916", 573 "{} should match 127.0.0.1:50916", 574 laddr 575 ); 576 577 let mut c = ChunkUdp::new( 578 SocketAddr::from_str("127.0.0.1:4000")?, 579 SocketAddr::from_str("127.0.0.1:50916")?, 580 ); 581 c.user_data = b"Hello!".to_vec(); 582 583 let (recv_ch_tx, mut recv_ch_rx) = mpsc::channel(1); 584 let (done_ch_tx, mut done_ch_rx) = mpsc::channel::<bool>(1); 585 let (close_ch_tx, mut close_ch_rx) = mpsc::channel::<bool>(1); 586 let conn_rx = Arc::clone(&conn); 587 588 tokio::spawn(async move { 589 let mut buf = vec![0u8; 1500]; 590 loop { 591 tokio::select! { 592 result = conn_rx.recv_from(&mut buf) => { 593 let (n, addr) = match result { 594 Ok((n, addr)) => (n, addr), 595 Err(err) => { 596 log::debug!("ReadFrom returned: {}", err); 597 break; 598 } 599 }; 600 601 assert_eq!(6, n, "{} should match 6", n); 602 assert_eq!("127.0.0.1:4000", addr.to_string(), "addr should match"); 603 assert_eq!(b"Hello!", &buf[..n], "buf should match"); 604 605 let _ = recv_ch_tx.send(true).await; 606 } 607 _ = close_ch_rx.recv() => { 608 break; 609 } 610 } 611 } 612 613 drop(done_ch_tx); 614 }); 615 616 if let Net::VNet(vnet) = &nw { 617 let vnet = vnet.lock().await; 618 vnet.on_inbound_chunk(Box::new(c)).await; 619 } else { 620 assert!(false, "must be virtual net"); 621 } 622 623 let _ = recv_ch_rx.recv().await; 624 drop(close_ch_tx); 625 626 let _ = done_ch_rx.recv().await; 627 628 Ok(()) 629 } 630 631 async fn get_ipaddr(nic: &Arc<Mutex<dyn Nic + Send + Sync>>) -> Result<IpAddr> { 632 let n = nic.lock().await; 633 let eth0 = n.get_interface("eth0").await.ok_or(Error::ErrNoInterface)?; 634 let addrs = eth0.addrs(); 635 if addrs.is_empty() { 636 Err(Error::ErrNoAddressAssigned) 637 } else { 638 Ok(addrs[0].addr()) 639 } 640 } 641 642 //use std::io::Write; 643 644 #[tokio::test] 645 async fn test_net_virtual_end2end() -> Result<()> { 646 /*env_logger::Builder::new() 647 .format(|buf, record| { 648 writeln!( 649 buf, 650 "{}:{} [{}] {} - {}", 651 record.file().unwrap_or("unknown"), 652 record.line().unwrap_or(0), 653 record.level(), 654 chrono::Local::now().format("%H:%M:%S.%6f"), 655 record.args() 656 ) 657 }) 658 .filter(None, log::LevelFilter::Trace) 659 .init();*/ 660 661 let wan = Arc::new(Mutex::new(Router::new(RouterConfig { 662 cidr: "1.2.3.0/24".to_string(), 663 ..Default::default() 664 })?)); 665 666 let net1 = Net::new(Some(NetConfig::default())); 667 let ip1 = { 668 let nic = net1.get_nic()?; 669 670 let mut w = wan.lock().await; 671 w.add_net(Arc::clone(&nic)).await?; 672 673 { 674 let n = nic.lock().await; 675 n.set_router(Arc::clone(&wan)).await?; 676 } 677 678 get_ipaddr(&nic).await? 679 }; 680 681 let net2 = Net::new(Some(NetConfig::default())); 682 let ip2 = { 683 let nic = net2.get_nic()?; 684 685 let mut w = wan.lock().await; 686 w.add_net(Arc::clone(&nic)).await?; 687 688 { 689 let n = nic.lock().await; 690 n.set_router(Arc::clone(&wan)).await?; 691 } 692 693 get_ipaddr(&nic).await? 694 }; 695 696 let conn1 = net1.bind(SocketAddr::new(ip1, 1234)).await?; 697 let conn2 = net2.bind(SocketAddr::new(ip2, 5678)).await?; 698 699 { 700 let mut w = wan.lock().await; 701 w.start().await?; 702 } 703 704 let (close_ch_tx, mut close_ch_rx1) = broadcast::channel::<bool>(1); 705 let (done_ch_tx, mut done_ch_rx) = mpsc::channel::<bool>(1); 706 let (conn1_recv_ch_tx, mut conn1_recv_ch_rx) = mpsc::channel(1); 707 let conn1_rx = Arc::clone(&conn1); 708 let conn2_tr = Arc::clone(&conn2); 709 let mut close_ch_rx2 = close_ch_tx.subscribe(); 710 711 // conn1 712 tokio::spawn(async move { 713 let mut buf = vec![0u8; 1500]; 714 loop { 715 log::debug!("conn1: wait for a message.."); 716 tokio::select! { 717 result = conn1_rx.recv_from(&mut buf) =>{ 718 let n = match result{ 719 Ok((n, _)) => n, 720 Err(err) => { 721 log::debug!("ReadFrom returned: {}", err); 722 break; 723 } 724 }; 725 726 log::debug!("conn1 received {:?}", &buf[..n]); 727 let _ = conn1_recv_ch_tx.send(true).await; 728 } 729 _ = close_ch_rx1.recv() => { 730 log::debug!("conn1 received close_ch_rx1"); 731 break; 732 } 733 } 734 } 735 drop(done_ch_tx); 736 log::debug!("conn1 drop done_ch_tx, exit spawn"); 737 }); 738 739 // conn2 740 tokio::spawn(async move { 741 let mut buf = vec![0u8; 1500]; 742 loop { 743 log::debug!("conn2: wait for a message.."); 744 tokio::select! { 745 result = conn2_tr.recv_from(&mut buf) =>{ 746 let (n, addr) = match result{ 747 Ok((n, addr)) => (n, addr), 748 Err(err) => { 749 log::debug!("ReadFrom returned: {}", err); 750 break; 751 } 752 }; 753 754 log::debug!("conn2 received {:?}", &buf[..n]); 755 756 // echo back to conn1 757 let n = conn2_tr.send_to(b"Good-bye!", addr).await?; 758 assert_eq!( 9, n, "should match"); 759 } 760 _ = close_ch_rx2.recv() => { 761 log::debug!("conn1 received close_ch_rx2"); 762 break; 763 } 764 } 765 } 766 767 log::debug!("conn2 exit spawn"); 768 769 Result::<()>::Ok(()) 770 }); 771 772 log::debug!("conn1: sending"); 773 let n = conn1.send_to(b"Hello!", conn2.local_addr()?).await?; 774 assert_eq!(6, n, "should match"); 775 776 let _ = conn1_recv_ch_rx.recv().await; 777 log::debug!("main recv conn1_recv_ch_rx"); 778 drop(close_ch_tx); 779 log::debug!("main drop close_ch_tx"); 780 let _ = done_ch_rx.recv().await; 781 log::debug!("main recv done_ch_rx"); 782 Ok(()) 783 } 784 785 //use std::io::Write; 786 787 #[tokio::test] 788 async fn test_net_virtual_two_ips_on_a_nic() -> Result<()> { 789 /*env_logger::Builder::new() 790 .format(|buf, record| { 791 writeln!( 792 buf, 793 "{}:{} [{}] {} - {}", 794 record.file().unwrap_or("unknown"), 795 record.line().unwrap_or(0), 796 record.level(), 797 chrono::Local::now().format("%H:%M:%S.%6f"), 798 record.args() 799 ) 800 }) 801 .filter(None, log::LevelFilter::Trace) 802 .init();*/ 803 804 let wan = Arc::new(Mutex::new(Router::new(RouterConfig { 805 cidr: "1.2.3.0/24".to_string(), 806 ..Default::default() 807 })?)); 808 809 let net = Net::new(Some(NetConfig { 810 static_ips: vec![DEMO_IP.to_owned(), "1.2.3.5".to_owned()], 811 ..Default::default() 812 })); 813 { 814 let nic = net.get_nic()?; 815 816 let mut w = wan.lock().await; 817 w.add_net(Arc::clone(&nic)).await?; 818 819 let n = nic.lock().await; 820 n.set_router(Arc::clone(&wan)).await?; 821 } 822 823 // start the router 824 { 825 let mut w = wan.lock().await; 826 w.start().await?; 827 } 828 829 let (conn1, conn2) = ( 830 net.bind(SocketAddr::new(Ipv4Addr::from_str(DEMO_IP)?.into(), 1234)) 831 .await?, 832 net.bind(SocketAddr::new(Ipv4Addr::from_str("1.2.3.5")?.into(), 1234)) 833 .await?, 834 ); 835 836 let (close_ch_tx, mut close_ch_rx1) = broadcast::channel::<bool>(1); 837 let (done_ch_tx, mut done_ch_rx) = mpsc::channel::<bool>(1); 838 let (conn1_recv_ch_tx, mut conn1_recv_ch_rx) = mpsc::channel(1); 839 let conn1_rx = Arc::clone(&conn1); 840 let conn2_tr = Arc::clone(&conn2); 841 let mut close_ch_rx2 = close_ch_tx.subscribe(); 842 843 // conn1 844 tokio::spawn(async move { 845 let mut buf = vec![0u8; 1500]; 846 loop { 847 log::debug!("conn1: wait for a message.."); 848 tokio::select! { 849 result = conn1_rx.recv_from(&mut buf) =>{ 850 let n = match result{ 851 Ok((n, _)) => n, 852 Err(err) => { 853 log::debug!("ReadFrom returned: {}", err); 854 break; 855 } 856 }; 857 858 log::debug!("conn1 received {:?}", &buf[..n]); 859 let _ = conn1_recv_ch_tx.send(true).await; 860 } 861 _ = close_ch_rx1.recv() => { 862 log::debug!("conn1 received close_ch_rx1"); 863 break; 864 } 865 } 866 } 867 drop(done_ch_tx); 868 log::debug!("conn1 drop done_ch_tx, exit spawn"); 869 }); 870 871 // conn2 872 tokio::spawn(async move { 873 let mut buf = vec![0u8; 1500]; 874 loop { 875 log::debug!("conn2: wait for a message.."); 876 tokio::select! { 877 result = conn2_tr.recv_from(&mut buf) =>{ 878 let (n, addr) = match result{ 879 Ok((n, addr)) => (n, addr), 880 Err(err) => { 881 log::debug!("ReadFrom returned: {}", err); 882 break; 883 } 884 }; 885 886 log::debug!("conn2 received {:?}", &buf[..n]); 887 888 // echo back to conn1 889 let n = conn2_tr.send_to(b"Good-bye!", addr).await?; 890 assert_eq!( 9, n, "should match"); 891 } 892 _ = close_ch_rx2.recv() => { 893 log::debug!("conn1 received close_ch_rx2"); 894 break; 895 } 896 } 897 } 898 899 log::debug!("conn2 exit spawn"); 900 901 Result::<()>::Ok(()) 902 }); 903 904 log::debug!("conn1: sending"); 905 let n = conn1.send_to(b"Hello!", conn2.local_addr()?).await?; 906 assert_eq!(6, n, "should match"); 907 908 let _ = conn1_recv_ch_rx.recv().await; 909 log::debug!("main recv conn1_recv_ch_rx"); 910 drop(close_ch_tx); 911 log::debug!("main drop close_ch_tx"); 912 let _ = done_ch_rx.recv().await; 913 log::debug!("main recv done_ch_rx"); 914 Ok(()) 915 } 916