blob: c36f144584922f9b73500ebf7b7ec5fadbef9da4 [file] [log] [blame]
Chalard Jean48c6c7d2020-06-25 23:39:15 +09001/*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.testutils;
18
19import android.net.util.PacketReader;
20import android.os.Handler;
21
22import androidx.annotation.NonNull;
23import androidx.annotation.Nullable;
24
25import com.android.net.module.util.ArrayTrackRecord;
26
27import java.io.FileDescriptor;
28import java.io.FileOutputStream;
29import java.io.IOException;
30import java.nio.ByteBuffer;
31import java.util.Arrays;
32import java.util.function.Predicate;
33
34import kotlin.Lazy;
35import kotlin.LazyKt;
36
Chalard Jeane06c5c82020-06-26 00:16:27 +090037/**
38 * A packet reader that runs on a TAP interface.
39 *
40 * It also implements facilities to reply to received packets.
41 */
Chalard Jean48c6c7d2020-06-25 23:39:15 +090042public class TapPacketReader extends PacketReader {
43 private final FileDescriptor mTapFd;
44 private final ArrayTrackRecord<byte[]> mReceivedPackets = new ArrayTrackRecord<>();
45 private final Lazy<ArrayTrackRecord<byte[]>.ReadHead> mReadHead =
46 LazyKt.lazy(mReceivedPackets::newReadHead);
47
48 public TapPacketReader(Handler h, FileDescriptor tapFd, int maxPacketSize) {
49 super(h, maxPacketSize);
50 mTapFd = tapFd;
51 }
52
53 @Override
54 protected FileDescriptor createFd() {
55 return mTapFd;
56 }
57
58 @Override
59 protected void handlePacket(byte[] recvbuf, int length) {
60 final byte[] newPacket = Arrays.copyOf(recvbuf, length);
61 if (!mReceivedPackets.add(newPacket)) {
62 throw new AssertionError("More than " + Integer.MAX_VALUE + " packets outstanding!");
63 }
64 }
65
66 /**
67 * Get the next packet that was received on the interface.
68 */
69 @Nullable
70 public byte[] popPacket(long timeoutMs) {
71 return mReadHead.getValue().poll(timeoutMs, packet -> true);
72 }
73
74 /**
75 * Get the next packet that was received on the interface and matches the specified filter.
76 */
77 @Nullable
78 public byte[] popPacket(long timeoutMs, @NonNull Predicate<byte[]> filter) {
79 return mReadHead.getValue().poll(timeoutMs, filter::test);
80 }
81
82 /**
83 * Get the {@link ArrayTrackRecord} that records all packets received by the reader since its
84 * creation.
85 */
86 public ArrayTrackRecord<byte[]> getReceivedPackets() {
87 return mReceivedPackets;
88 }
89
Chalard Jeane06c5c82020-06-26 00:16:27 +090090 /*
91 * Send a response on the TAP interface.
92 *
93 * The passed ByteBuffer is flipped after use.
94 *
95 * @param packet The packet to send.
96 * @throws IOException if the interface can't be written to.
97 */
Chalard Jean48c6c7d2020-06-25 23:39:15 +090098 public void sendResponse(final ByteBuffer packet) throws IOException {
99 try (FileOutputStream out = new FileOutputStream(mTapFd)) {
100 byte[] packetBytes = new byte[packet.limit()];
101 packet.get(packetBytes);
102 packet.flip(); // So we can reuse it in the future.
103 out.write(packetBytes);
104 }
105 }
106}