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
use std::{sync::Arc, time::Duration};

use twizzler_async::{Task, Timer};
use twizzler_net::{
    addr::{Ipv4Addr, NodeAddr, ServiceAddr},
    buffer::ManagedBuffer,
    ListenFlags, ListenInfo, NmHandle, RxCompletion, RxRequest, TxRequest,
};

#[repr(C)]
struct IcmpHeader {
    ty: u8,
    code: u8,
    csum: [u8; 2],
    extra: [u8; 4],
}

const ICMP_ECHO_REQUEST: u8 = 8;

fn handle_ping_recv(_buffer: ManagedBuffer) {
    println!("nettest ping recv");
}

fn fill_ping_buffer(_idx: usize, buffer: &mut ManagedBuffer) {
    let icmp_header = IcmpHeader {
        ty: ICMP_ECHO_REQUEST,
        code: 0,
        csum: [0; 2],
        extra: [0; 4],
    };
    buffer.get_data_mut(0).write(icmp_header);
}

fn ping(addr: Ipv4Addr) {
    let handle = Arc::new(twizzler_net::open_nm_handle("ping test").unwrap());

    // Run the async ping code.
    twizzler_async::run(async {
        // Build a new connection info. It's not really a "connection", more of a way to specify a
        // place to listen at. For ping, that's ipv4+icmp, raw.
        let conn_info = ListenInfo::new(NodeAddr::Ipv4(addr), ServiceAddr::Icmp, ListenFlags::RAW);

        println!("sending listen");
        // Start listening here.
        let tx_cmp = handle.submit(TxRequest::Listen(conn_info)).await.unwrap();

        // In response, we get back a connection ID that we can use.
        let listen_id = match tx_cmp {
            twizzler_net::TxCompletion::ListenReady(conn_id) => conn_id,
            _ => panic!("some err"),
        };

        println!("got new listen id {:?}", listen_id);

        // Clone the handle for use in the recv task.
        let handle_clone = handle.clone();
        // Create a receiver task. This task will receive ping responses and then print out ping
        // status messages.
        Task::spawn(async move {
            // Loop until the handle call fails.
            while handle_clone
                .handle(|handle, _id, req| async move {
                    // We got an RxRequest! See what it is.
                    match req {
                        RxRequest::Recv(conn_id, packet_data) => {
                            if conn_id == listen_id {
                                // It's a receive on our connection. Grab the incoming buffer and
                                // handle the ping response.
                                let buffer = handle.get_incoming_buffer(packet_data);
                                handle_ping_recv(buffer);
                            }
                        }
                        // If we need to close, then do so.
                        RxRequest::Close => handle.set_closed(),
                        _ => {}
                    };
                    // Respond to the net manager
                    RxCompletion::Nothing
                })
                .await
                .is_ok()
            {}
        })
        .detach();

        // Meanwhile, submit some pings!
        for i in 0..4 {
            // Let's grab a buffer...
            let mut buffer = handle.allocatable_buffer_controller().allocate().await;
            // And fill out that buffer with a ping packet...
            fill_ping_buffer(i, &mut buffer);
            println!("sending ping buffer");
            // ...and then submit it!
            let _ = handle
                .submit(TxRequest::Send(listen_id, buffer.as_packet_data()))
                .await;
            Timer::after(Duration::from_millis(1000)).await;
            // TODO: or send-to?
        }
    });
}

fn main() {
    println!("Hello from nettest!");
    let handle = Arc::new(twizzler_net::open_nm_handle("nettest").unwrap());
    println!("nettest got nm handle: {:?}", handle);

    ping(Ipv4Addr::localhost());

    twizzler_async::run(async move {
        let mut buffer = handle.allocatable_buffer_controller().allocate().await;
        buffer.copy_in(b"Some Packet Data");
        let packet_data = buffer.as_packet_data();

        let handle_clone = handle.clone();
        Task::spawn(async move {
            loop {
                let _ = handle_clone.handle(handler).await;
            }
        })
        .detach();

        let res = handle.submit(TxRequest::Echo(packet_data)).await.unwrap();
        println!("got txc {:?}", res);

        let res = handle
            .submit(TxRequest::ListenIpv4(Ipv4Addr::localhost()))
            .await;
        println!("setup listen: {:?}", res);

        loop {
            twizzler_async::Timer::after(Duration::from_millis(1000)).await;

            println!("sending...");
            let mut buffer = handle.allocatable_buffer_controller().allocate().await;
            buffer.copy_in(b"Some Ipv4 Packet Data");
            let packet_data = buffer.as_packet_data();
            let res = handle
                .submit(TxRequest::SendToIpv4(Ipv4Addr::localhost(), packet_data))
                .await;
            println!("send got: {:?}", res);
        }
    });
}

async fn handler(handle: &Arc<NmHandle>, id: u32, req: RxRequest) -> RxCompletion {
    println!("got response {} {:?}", id, req);
    match req {
        RxRequest::EchoReply(incoming_data) => {
            let buffer = handle.get_incoming_buffer(incoming_data);
            let incoming_slice = buffer.as_bytes();
            let s = String::from_utf8(incoming_slice.to_vec());
            println!("response incoming slice was {:?}", s);
        }
        RxRequest::RecvFromIpv4(addr, incoming_data) => {
            let buffer = handle.get_incoming_buffer(incoming_data);
            let incoming_slice = buffer.as_bytes();
            let s = String::from_utf8(incoming_slice.to_vec());
            println!("====== >> recv incoming slice was {:?} from {}", s, addr);
        }
        RxRequest::Close => {
            handle.set_closed();
        }
        _ => {}
    }
    RxCompletion::Nothing
}