xref: /third_party/rust/crates/rustix/tests/net/v6.rs (revision b8a62b91)
1//! Test a simple IPv6 socket server and client.
2//!
3//! The client send a message and the server sends one back.
4
5#![cfg(not(any(target_os = "redox", target_os = "wasi")))]
6
7use rustix::net::{
8    accept, bind_v6, connect_v6, getsockname, listen, recv, send, socket, AddressFamily, Ipv6Addr,
9    Protocol, RecvFlags, SendFlags, SocketAddrAny, SocketAddrV6, SocketType,
10};
11use std::sync::{Arc, Condvar, Mutex};
12use std::thread;
13
14const BUFFER_SIZE: usize = 20;
15
16fn server(ready: Arc<(Mutex<u16>, Condvar)>) {
17    let connection_socket = socket(
18        AddressFamily::INET6,
19        SocketType::STREAM,
20        Protocol::default(),
21    )
22    .unwrap();
23
24    let name = SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), 0, 0, 0);
25    bind_v6(&connection_socket, &name).unwrap();
26
27    let who = match getsockname(&connection_socket).unwrap() {
28        SocketAddrAny::V6(addr) => addr,
29        _ => panic!(),
30    };
31
32    listen(&connection_socket, 1).unwrap();
33
34    {
35        let (lock, cvar) = &*ready;
36        let mut port = lock.lock().unwrap();
37        *port = who.port();
38        cvar.notify_all();
39    }
40
41    let mut buffer = vec![0; BUFFER_SIZE];
42    let data_socket = accept(&connection_socket).unwrap();
43    let nread = recv(&data_socket, &mut buffer, RecvFlags::empty()).unwrap();
44    assert_eq!(String::from_utf8_lossy(&buffer[..nread]), "hello, world");
45
46    send(&data_socket, b"goodnight, moon", SendFlags::empty()).unwrap();
47}
48
49fn client(ready: Arc<(Mutex<u16>, Condvar)>) {
50    let port = {
51        let (lock, cvar) = &*ready;
52        let mut port = lock.lock().unwrap();
53        while *port == 0 {
54            port = cvar.wait(port).unwrap();
55        }
56        *port
57    };
58
59    let addr = SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), port, 0, 0);
60    let mut buffer = vec![0; BUFFER_SIZE];
61
62    let data_socket = socket(
63        AddressFamily::INET6,
64        SocketType::STREAM,
65        Protocol::default(),
66    )
67    .unwrap();
68    connect_v6(&data_socket, &addr).unwrap();
69
70    send(&data_socket, b"hello, world", SendFlags::empty()).unwrap();
71
72    let nread = recv(&data_socket, &mut buffer, RecvFlags::empty()).unwrap();
73    assert_eq!(String::from_utf8_lossy(&buffer[..nread]), "goodnight, moon");
74}
75
76#[test]
77fn test_v6() {
78    let ready = Arc::new((Mutex::new(0_u16), Condvar::new()));
79    let ready_clone = Arc::clone(&ready);
80
81    let server = thread::Builder::new()
82        .name("server".to_string())
83        .spawn(move || {
84            server(ready);
85        })
86        .unwrap();
87    let client = thread::Builder::new()
88        .name("client".to_string())
89        .spawn(move || {
90            client(ready_clone);
91        })
92        .unwrap();
93    client.join().unwrap();
94    server.join().unwrap();
95}
96