Nathan Harold | 8086539 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | package android.net; |
| 17 | |
| 18 | import android.os.Parcel; |
| 19 | import android.os.ParcelFileDescriptor; |
| 20 | import android.os.Parcelable; |
Aaron Huang | b24f3da | 2021-12-06 15:18:42 +0800 | [diff] [blame] | 21 | |
Nathan Harold | 8086539 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 22 | import java.io.FileDescriptor; |
| 23 | import java.io.IOException; |
| 24 | |
| 25 | /** |
| 26 | * This class is used to return a UDP Socket and corresponding status from the IpSecService to an |
| 27 | * IpSecManager.UdpEncapsulationSocket. |
| 28 | * |
| 29 | * @hide |
| 30 | */ |
| 31 | public final class IpSecUdpEncapResponse implements Parcelable { |
| 32 | private static final String TAG = "IpSecUdpEncapResponse"; |
| 33 | |
| 34 | public final int resourceId; |
| 35 | public final int port; |
| 36 | public final int status; |
| 37 | // There is a weird asymmetry with FileDescriptor: you can write a FileDescriptor |
| 38 | // but you read a ParcelFileDescriptor. To circumvent this, when we receive a FD |
| 39 | // from the user, we immediately create a ParcelFileDescriptor DUP, which we invalidate |
| 40 | // on writeParcel() by setting the flag to do close-on-write. |
| 41 | // TODO: tests to ensure this doesn't leak |
| 42 | public final ParcelFileDescriptor fileDescriptor; |
| 43 | |
| 44 | // Parcelable Methods |
| 45 | |
| 46 | @Override |
| 47 | public int describeContents() { |
| 48 | return (fileDescriptor != null) ? Parcelable.CONTENTS_FILE_DESCRIPTOR : 0; |
| 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public void writeToParcel(Parcel out, int flags) { |
| 53 | out.writeInt(status); |
| 54 | out.writeInt(resourceId); |
| 55 | out.writeInt(port); |
| 56 | out.writeParcelable(fileDescriptor, Parcelable.PARCELABLE_WRITE_RETURN_VALUE); |
| 57 | } |
| 58 | |
| 59 | public IpSecUdpEncapResponse(int inStatus) { |
| 60 | if (inStatus == IpSecManager.Status.OK) { |
| 61 | throw new IllegalArgumentException("Valid status implies other args must be provided"); |
| 62 | } |
| 63 | status = inStatus; |
| 64 | resourceId = IpSecManager.INVALID_RESOURCE_ID; |
| 65 | port = -1; |
| 66 | fileDescriptor = null; // yes I know it's redundant, but readability |
| 67 | } |
| 68 | |
| 69 | public IpSecUdpEncapResponse(int inStatus, int inResourceId, int inPort, FileDescriptor inFd) |
| 70 | throws IOException { |
| 71 | if (inStatus == IpSecManager.Status.OK && inFd == null) { |
| 72 | throw new IllegalArgumentException("Valid status implies FD must be non-null"); |
| 73 | } |
| 74 | status = inStatus; |
| 75 | resourceId = inResourceId; |
| 76 | port = inPort; |
| 77 | fileDescriptor = (status == IpSecManager.Status.OK) ? ParcelFileDescriptor.dup(inFd) : null; |
| 78 | } |
| 79 | |
| 80 | private IpSecUdpEncapResponse(Parcel in) { |
| 81 | status = in.readInt(); |
| 82 | resourceId = in.readInt(); |
| 83 | port = in.readInt(); |
Bernardo Rufino | 2ace102 | 2022-01-14 17:35:36 +0000 | [diff] [blame] | 84 | fileDescriptor = in.readParcelable(ParcelFileDescriptor.class.getClassLoader(), android.os.ParcelFileDescriptor.class); |
Nathan Harold | 8086539 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Aaron Huang | b24f3da | 2021-12-06 15:18:42 +0800 | [diff] [blame] | 87 | @android.annotation.NonNull |
| 88 | public static final Parcelable.Creator<IpSecUdpEncapResponse> CREATOR = |
Nathan Harold | 8086539 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 89 | new Parcelable.Creator<IpSecUdpEncapResponse>() { |
| 90 | public IpSecUdpEncapResponse createFromParcel(Parcel in) { |
| 91 | return new IpSecUdpEncapResponse(in); |
| 92 | } |
| 93 | |
| 94 | public IpSecUdpEncapResponse[] newArray(int size) { |
| 95 | return new IpSecUdpEncapResponse[size]; |
| 96 | } |
| 97 | }; |
| 98 | } |