libadb-ffi 0.9.0

C ABI for libadb
Documentation
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
//! The reverse/accept C API: accept a channel the device opens toward
//! the host, then exchange bytes over it. A minimal raw device drives
//! the wire directly.

use std::ffi::{c_void, CString};
use std::io::{Read as _, Write as _};
use std::net::{TcpListener, TcpStream};
use std::ptr;

#[path = "common/common.rs"]
mod common;
use common::{header, CMD_CNXN, CMD_OKAY, CMD_WRTE, MAX_PAYLOAD, VERSION};

const CMD_OPEN: u32 = 0x4e45_504f;
const CMD_CLSE: u32 = 0x4553_4c43;

unsafe extern "C" fn never_signs(
    _u: *mut c_void,
    _t: *const u8,
    _tl: usize,
    _o: *mut u8,
    _oc: usize,
    _ol: *mut usize,
) -> i32 {
    adb::AdbStatus::Auth as i32
}

fn recv_pkt(s: &mut TcpStream) -> Option<(u32, u32, u32, Vec<u8>)> {
    let mut h = [0u8; 24];
    s.read_exact(&mut h).ok()?;
    let cmd = u32::from_le_bytes([h[0], h[1], h[2], h[3]]);
    let arg0 = u32::from_le_bytes([h[4], h[5], h[6], h[7]]);
    let arg1 = u32::from_le_bytes([h[8], h[9], h[10], h[11]]);
    let len = u32::from_le_bytes([h[12], h[13], h[14], h[15]]) as usize;
    let mut payload = vec![0u8; len];
    if len > 0 {
        s.read_exact(&mut payload).ok()?;
    }
    Some((cmd, arg0, arg1, payload))
}

/// Handshake, then immediately open one channel toward the host
/// (device id 500, destination `tcp:7000`). After the host's READY,
/// echo the first WRTE with an `echo:` prefix, then close.
fn opening_adbd() -> (String, std::thread::JoinHandle<()>) {
    let listener = TcpListener::bind("127.0.0.1:0").unwrap();
    let addr = listener.local_addr().unwrap().to_string();
    let device = std::thread::spawn(move || {
        let (mut s, _) = listener.accept().unwrap();
        s.set_nodelay(true).unwrap();

        // Handshake.
        let (_c, _a0, _a1, _p) = recv_pkt(&mut s).unwrap(); // CNXN
        s.write_all(&header(
            CMD_CNXN,
            VERSION,
            MAX_PAYLOAD,
            b"device::features=shell_v2,delayed_ack",
        ))
        .unwrap();

        // Open a channel toward the host: device local id 500, credit
        // in arg1 (delayed ack), destination tcp:7000.
        s.write_all(&header(CMD_OPEN, 500, 4096, b"tcp:7000\0"))
            .unwrap();

        // The host answers READY(host_id, 500, credit) on accept.
        let (cmd, host_id, mine, _credit) = recv_pkt(&mut s).unwrap();
        assert_eq!(cmd, CMD_OKAY, "expected READY for our OPEN");
        assert_eq!(mine, 500, "READY names our local id");

        // The host's WRTE (its data on the channel): ack + echo back.
        let (cmd, _a0, _a1, data) = recv_pkt(&mut s).unwrap();
        assert_eq!(cmd, CMD_WRTE);
        // delayed-ack: every OKAY carries a credit refill.
        s.write_all(&header(CMD_OKAY, 500, host_id, &1000u32.to_le_bytes()))
            .unwrap();
        let mut echo = b"echo:".to_vec();
        echo.extend_from_slice(&data);
        s.write_all(&header(CMD_WRTE, 500, host_id, &echo)).unwrap();
        // Consume the host's ack for our echo, then idle.
        let _ = recv_pkt(&mut s);
    });
    (addr, device)
}

#[test]
fn accept_an_opened_channel_and_exchange() {
    let (addr, device) = opening_adbd();
    let uri = CString::new(format!("tcp://{addr}")).unwrap();
    let banner = CString::new("host::features=shell_v2,delayed_ack").unwrap();
    let pubkey = b"unused\0";
    let auth = adb::adb_authenticator_t {
        public_key: pubkey.as_ptr(),
        public_key_len: pubkey.len(),
        sign: Some(never_signs),
        user_data: ptr::null_mut(),
    };

    let mut conn = ptr::null_mut();
    let st = unsafe {
        adb::adb_connect_with_authenticator(uri.as_ptr(), &auth, banner.as_ptr(), &mut conn)
    };
    assert_eq!(st, adb::AdbStatus::Ok, "connect");

    let mut dest = [0u8; 64];
    let mut dest_len = 0usize;
    let st = unsafe { adb::adb_accept_channel(conn, dest.as_mut_ptr(), dest.len(), &mut dest_len) };
    assert_eq!(st, adb::AdbStatus::Ok, "accept_channel");
    assert_eq!(&dest[..dest_len], b"tcp:7000\0", "destination reported");

    let mut id = 0u64;
    let st = unsafe { adb::adb_incoming_accept(conn, &mut id) };
    assert_eq!(st, adb::AdbStatus::Ok, "incoming_accept");

    let st = unsafe { adb::adb_write_channel(conn, id, b"hi".as_ptr(), 2) };
    assert_eq!(st, adb::AdbStatus::Ok, "write");

    let mut buf = [0u8; 32];
    let mut n = 0usize;
    let st = unsafe { adb::adb_read_channel(conn, id, buf.as_mut_ptr(), buf.len(), &mut n) };
    assert_eq!(st, adb::AdbStatus::Ok, "read");
    assert_eq!(&buf[..n], b"echo:hi");

    device
        .join()
        .expect("the fake device saw the exchange it expected");
    unsafe { adb::adb_connection_free(conn) };
}

/// Serve one `reverse:` service channel: expect the host's OPEN for
/// `expected_dest`, answer READY, push `reply` in one WRTE, then close
/// — consuming the host's ack and its answering CLSE on the way.
fn serve_service(s: &mut TcpStream, dev_id: u32, expected_dest: &[u8], reply: &[u8]) {
    let (cmd, host_id, _a1, payload) = recv_pkt(s).unwrap();
    assert_eq!(cmd, CMD_OPEN, "expected the service OPEN");
    assert_eq!(payload, expected_dest, "service destination");
    s.write_all(&header(CMD_OKAY, dev_id, host_id, &[]))
        .unwrap();
    s.write_all(&header(CMD_WRTE, dev_id, host_id, reply))
        .unwrap();
    let (cmd, _, _, _) = recv_pkt(s).unwrap();
    assert_eq!(cmd, CMD_OKAY, "expected the host's ack");
    s.write_all(&header(CMD_CLSE, dev_id, host_id, &[]))
        .unwrap();
    let (cmd, _, _, _) = recv_pkt(s).unwrap();
    assert_eq!(cmd, CMD_CLSE, "expected the host to free the channel");
}

/// A device answering the whole rule-service conversation the test
/// below drives, in the exact frames a real adbd was probed to send.
fn rule_service_adbd() -> (String, std::thread::JoinHandle<()>) {
    let listener = TcpListener::bind("127.0.0.1:0").unwrap();
    let addr = listener.local_addr().unwrap().to_string();
    let device = std::thread::spawn(move || {
        let (mut s, _) = listener.accept().unwrap();
        s.set_nodelay(true).unwrap();

        let _ = recv_pkt(&mut s).unwrap(); // CNXN
        s.write_all(&header(
            CMD_CNXN,
            VERSION,
            MAX_PAYLOAD,
            b"device::features=shell_v2",
        ))
        .unwrap();

        serve_service(
            &mut s,
            700,
            b"reverse:forward:tcp:0;tcp:6200\0",
            b"OKAY000538547",
        );
        serve_service(
            &mut s,
            701,
            b"reverse:forward:tcp:0;tcp:6201\0",
            b"OKAY000538548",
        );
        serve_service(
            &mut s,
            702,
            b"reverse:list-forward\0",
            b"0038host-19 tcp:6100 tcp:6100\nhost-19 localabstract:\xff tcp:7\n",
        );
        serve_service(&mut s, 703, b"reverse:killforward:tcp:6100\0", b"OKAY");
        serve_service(&mut s, 704, b"reverse:killforward-all\0", b"OKAY");
    });
    (addr, device)
}

#[test]
fn reverse_rules_flow_through_the_split_adapter() {
    let (addr, device_thread) = rule_service_adbd();
    let uri = CString::new(format!("tcp://{addr}")).unwrap();
    let banner = CString::new("host::features=shell_v2").unwrap();
    let pubkey = b"unused\0";
    let auth = adb::adb_authenticator_t {
        public_key: pubkey.as_ptr(),
        public_key_len: pubkey.len(),
        sign: Some(never_signs),
        user_data: ptr::null_mut(),
    };
    let mut conn = ptr::null_mut();
    let st = unsafe {
        adb::adb_connect_with_authenticator(uri.as_ptr(), &auth, banner.as_ptr(), &mut conn)
    };
    assert_eq!(st, adb::AdbStatus::Ok, "connect");

    let device = CString::new("tcp:0").unwrap();
    let host = CString::new("tcp:6200").unwrap();
    let mut data = [0u8; 64];
    let mut len = 0usize;
    let st = unsafe {
        adb::adb_reverse_forward(
            conn,
            device.as_ptr(),
            host.as_ptr(),
            data.as_mut_ptr(),
            data.len(),
            &mut len,
        )
    };
    assert_eq!(st, adb::AdbStatus::Ok, "reverse_forward");
    assert_eq!(&data[..len], b"38547", "the bound port comes back");

    // Truncate-and-report: a 2-byte buffer still learns the full size.
    let host = CString::new("tcp:6201").unwrap();
    let mut tiny = [0u8; 2];
    let mut len = 0usize;
    let st = unsafe {
        adb::adb_reverse_forward(
            conn,
            device.as_ptr(),
            host.as_ptr(),
            tiny.as_mut_ptr(),
            tiny.len(),
            &mut len,
        )
    };
    assert_eq!(st, adb::AdbStatus::Ok, "truncated reverse_forward");
    assert_eq!(len, 5, "full length reported despite the short buffer");
    assert_eq!(&tiny, b"38", "the truncated prefix is written");

    let mut listing = [0u8; 128];
    let mut len = 0usize;
    let st = unsafe { adb::adb_reverse_list(conn, listing.as_mut_ptr(), listing.len(), &mut len) };
    assert_eq!(st, adb::AdbStatus::Ok, "reverse_list");
    assert_eq!(
        &listing[..len],
        b"host-19 tcp:6100 tcp:6100\nhost-19 localabstract:\xff tcp:7\n",
        "the device's text, a spec that is not UTF-8 included"
    );

    let rule = CString::new("tcp:6100").unwrap();
    let st = unsafe { adb::adb_reverse_remove(conn, rule.as_ptr()) };
    assert_eq!(st, adb::AdbStatus::Ok, "reverse_remove");

    let st = unsafe { adb::adb_reverse_remove_all(conn) };
    assert_eq!(st, adb::AdbStatus::Ok, "reverse_remove_all");

    device_thread
        .join()
        .expect("the fake device saw every frame it expected");
    unsafe { adb::adb_connection_free(conn) };
}

/// A device answering `forward` each way adbd does: a fixed port, the
/// bare `OKAY` of a rule that already exists, and a vsock port past
/// `i32::MAX`, which adbd's `%d` prints negative.
fn port_reporting_adbd() -> (String, std::thread::JoinHandle<()>) {
    let listener = TcpListener::bind("127.0.0.1:0").unwrap();
    let addr = listener.local_addr().unwrap().to_string();
    let device = std::thread::spawn(move || {
        let (mut s, _) = listener.accept().unwrap();
        s.set_nodelay(true).unwrap();

        let _ = recv_pkt(&mut s).unwrap(); // CNXN
        s.write_all(&header(
            CMD_CNXN,
            VERSION,
            MAX_PAYLOAD,
            b"device::features=shell_v2",
        ))
        .unwrap();

        let fixed = b"reverse:forward:tcp:6100;tcp:6200\0";
        serve_service(&mut s, 720, fixed, b"OKAY00046100");
        serve_service(&mut s, 721, fixed, b"OKAY");
        serve_service(
            &mut s,
            722,
            b"reverse:forward:vsock:0;tcp:6200\0",
            b"OKAY000b-1294967296",
        );
    });
    (addr, device)
}

#[test]
fn reverse_forward_writes_the_reported_port_or_nothing() {
    let (addr, device_thread) = port_reporting_adbd();
    let uri = CString::new(format!("tcp://{addr}")).unwrap();
    let banner = CString::new("host::features=shell_v2").unwrap();
    let pubkey = b"unused\0";
    let auth = adb::adb_authenticator_t {
        public_key: pubkey.as_ptr(),
        public_key_len: pubkey.len(),
        sign: Some(never_signs),
        user_data: ptr::null_mut(),
    };
    let mut conn = ptr::null_mut();
    let st = unsafe {
        adb::adb_connect_with_authenticator(uri.as_ptr(), &auth, banner.as_ptr(), &mut conn)
    };
    assert_eq!(st, adb::AdbStatus::Ok, "connect");

    let forward = |spec: &str| {
        let device = CString::new(spec).unwrap();
        let host = CString::new("tcp:6200").unwrap();
        let mut data = [0u8; 16];
        let mut len = usize::MAX;
        let st = unsafe {
            adb::adb_reverse_forward(
                conn,
                device.as_ptr(),
                host.as_ptr(),
                data.as_mut_ptr(),
                data.len(),
                &mut len,
            )
        };
        assert_eq!(st, adb::AdbStatus::Ok, "reverse_forward {spec}");
        data[..len].to_vec()
    };

    assert_eq!(forward("tcp:6100"), b"6100", "a fixed port");
    assert_eq!(forward("tcp:6100"), b"", "a rule that already exists");
    assert_eq!(
        forward("vsock:0"),
        b"3000000000",
        "the unsigned port, not what `%d` printed"
    );

    device_thread
        .join()
        .expect("the fake device saw every frame it expected");
    unsafe { adb::adb_connection_free(conn) };
}

/// Two OPENs land before the host looks: reporting is idempotent on
/// the staged request (800 until its verdict), and each verdict
/// answers exactly the staged one — 800 rejected, then 801 accepted.
fn two_opens_adbd() -> (String, std::thread::JoinHandle<()>) {
    let listener = TcpListener::bind("127.0.0.1:0").unwrap();
    let addr = listener.local_addr().unwrap().to_string();
    let device = std::thread::spawn(move || {
        let (mut s, _) = listener.accept().unwrap();
        s.set_nodelay(true).unwrap();

        let _ = recv_pkt(&mut s).unwrap(); // CNXN
        s.write_all(&header(
            CMD_CNXN,
            VERSION,
            MAX_PAYLOAD,
            b"device::features=shell_v2",
        ))
        .unwrap();

        s.write_all(&header(CMD_OPEN, 800, 0, b"tcp:7100\0"))
            .unwrap();
        s.write_all(&header(CMD_OPEN, 801, 0, b"tcp:7200\0"))
            .unwrap();

        // The verdicts, in the order the host hands them down: CLSE
        // for the rejected 800, then READY for the accepted 801.
        let (cmd, _a0, mine, _p) = recv_pkt(&mut s).unwrap();
        assert_eq!(cmd, CMD_CLSE, "expected CLSE for the rejected request");
        assert_eq!(mine, 800, "the reject must answer the staged OPEN");
        let (cmd, _host_id, mine, _p) = recv_pkt(&mut s).unwrap();
        assert_eq!(cmd, CMD_OKAY, "expected READY for the staged request");
        assert_eq!(mine, 801, "the verdict must answer the staged OPEN");
    });
    (addr, device)
}

#[test]
fn a_verdict_answers_the_reported_request() {
    let (addr, device) = two_opens_adbd();
    let uri = CString::new(format!("tcp://{addr}")).unwrap();
    let banner = CString::new("host::features=shell_v2").unwrap();
    let pubkey = b"unused\0";
    let auth = adb::adb_authenticator_t {
        public_key: pubkey.as_ptr(),
        public_key_len: pubkey.len(),
        sign: Some(never_signs),
        user_data: ptr::null_mut(),
    };
    let mut conn = ptr::null_mut();
    let st = unsafe {
        adb::adb_connect_with_authenticator(uri.as_ptr(), &auth, banner.as_ptr(), &mut conn)
    };
    assert_eq!(st, adb::AdbStatus::Ok, "connect");

    // Truncate-and-report on the staged request: probe the length with
    // no buffer first, then fetch the bytes — the same request twice.
    let mut len = 0usize;
    let st = unsafe { adb::adb_accept_channel(conn, ptr::null_mut(), 0, &mut len) };
    assert_eq!(st, adb::AdbStatus::Ok, "length probe");
    assert_eq!(len, b"tcp:7100\0".len(), "full length reported");

    let mut dest = [0u8; 32];
    let st = unsafe { adb::adb_accept_channel(conn, dest.as_mut_ptr(), dest.len(), &mut len) };
    assert_eq!(st, adb::AdbStatus::Ok, "repeated report");
    assert_eq!(
        &dest[..len],
        b"tcp:7100\0",
        "a repeated report answers with the staged request, not the next one"
    );

    // The verdict consumes the staged request — the device asserts the
    // CLSE names 800, not whatever sits at the queue's head.
    let st = unsafe { adb::adb_incoming_reject(conn) };
    assert_eq!(st, adb::AdbStatus::Ok, "reject the staged request");

    // Only now does reporting move on to the second OPEN.
    let st = unsafe { adb::adb_accept_channel(conn, dest.as_mut_ptr(), dest.len(), &mut len) };
    assert_eq!(st, adb::AdbStatus::Ok, "next report");
    assert_eq!(
        &dest[..len],
        b"tcp:7200\0",
        "the next OPEN follows the verdict"
    );
    let mut id = 0u64;
    let st = unsafe { adb::adb_incoming_accept(conn, &mut id) };
    assert_eq!(st, adb::AdbStatus::Ok, "accept the staged request");

    // Nothing is staged any more.
    let st = unsafe { adb::adb_incoming_reject(conn) };
    assert_eq!(
        st,
        adb::AdbStatus::InvalidArg,
        "the verdict consumed the stage"
    );

    device
        .join()
        .expect("the fake device saw the verdicts it expected");
    unsafe { adb::adb_connection_free(conn) };
}

#[test]
fn a_device_refusal_surfaces_as_adb_err_reverse() {
    let listener = TcpListener::bind("127.0.0.1:0").unwrap();
    let addr = listener.local_addr().unwrap().to_string();
    let device = std::thread::spawn(move || {
        let (mut s, _) = listener.accept().unwrap();
        s.set_nodelay(true).unwrap();
        let _ = recv_pkt(&mut s).unwrap(); // CNXN
        s.write_all(&header(
            CMD_CNXN,
            VERSION,
            MAX_PAYLOAD,
            b"device::features=shell_v2",
        ))
        .unwrap();
        serve_service(
            &mut s,
            705,
            b"reverse:forward:garbage;tcp:1\0",
            b"FAIL0014bad forward: garbage",
        );
    });

    let uri = CString::new(format!("tcp://{addr}")).unwrap();
    let banner = CString::new("host::features=shell_v2").unwrap();
    let pubkey = b"unused\0";
    let auth = adb::adb_authenticator_t {
        public_key: pubkey.as_ptr(),
        public_key_len: pubkey.len(),
        sign: Some(never_signs),
        user_data: ptr::null_mut(),
    };
    let mut conn = ptr::null_mut();
    let st = unsafe {
        adb::adb_connect_with_authenticator(uri.as_ptr(), &auth, banner.as_ptr(), &mut conn)
    };
    assert_eq!(st, adb::AdbStatus::Ok, "connect");

    let device_spec = CString::new("garbage").unwrap();
    let host_spec = CString::new("tcp:1").unwrap();
    let st = unsafe {
        adb::adb_reverse_forward(
            conn,
            device_spec.as_ptr(),
            host_spec.as_ptr(),
            ptr::null_mut(),
            0,
            ptr::null_mut(),
        )
    };
    assert_eq!(
        st,
        adb::AdbStatus::Reverse,
        "a device refusal is its own status, not an internal error"
    );
    let msg = unsafe { std::ffi::CStr::from_ptr(adb::adb_last_error()) };
    assert!(
        msg.to_string_lossy().contains("bad forward: garbage"),
        "the device's message reaches the caller: {msg:?}"
    );

    device.join().expect("the fake device saw the exchange");
    unsafe { adb::adb_connection_free(conn) };
}

#[test]
fn reject_before_any_accept_is_an_error() {
    // No pending request: reject and accept both complain rather than
    // block or panic.
    let addr = {
        let listener = TcpListener::bind("127.0.0.1:0").unwrap();
        let addr = listener.local_addr().unwrap().to_string();
        std::thread::spawn(move || {
            let (mut s, _) = listener.accept().unwrap();
            s.set_nodelay(true).unwrap();
            let _ = recv_pkt(&mut s);
            let _ = s.write_all(&header(
                CMD_CNXN,
                VERSION,
                MAX_PAYLOAD,
                b"device::features=shell_v2",
            ));
            std::thread::sleep(std::time::Duration::from_millis(200));
        });
        addr
    };
    let uri = CString::new(format!("tcp://{addr}")).unwrap();
    let banner = CString::new("host::features=shell_v2").unwrap();
    let pubkey = b"unused\0";
    let auth = adb::adb_authenticator_t {
        public_key: pubkey.as_ptr(),
        public_key_len: pubkey.len(),
        sign: Some(never_signs),
        user_data: ptr::null_mut(),
    };
    let mut conn = ptr::null_mut();
    let st = unsafe {
        adb::adb_connect_with_authenticator(uri.as_ptr(), &auth, banner.as_ptr(), &mut conn)
    };
    assert_eq!(st, adb::AdbStatus::Ok, "connect");

    let st = unsafe { adb::adb_incoming_reject(conn) };
    assert_eq!(
        st,
        adb::AdbStatus::InvalidArg,
        "reject with nothing pending"
    );

    unsafe { adb::adb_connection_free(conn) };
}