Hugo Benichi | 6241874 | 2016-04-13 10:13:32 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
| 17 | package android.net.metrics; |
| 18 | |
Remi NGUYEN VAN | 7b84fb3 | 2019-01-19 21:13:24 +0900 | [diff] [blame] | 19 | import android.annotation.SystemApi; |
| 20 | import android.annotation.TestApi; |
Hugo Benichi | 6241874 | 2016-04-13 10:13:32 +0900 | [diff] [blame] | 21 | import android.os.Parcel; |
| 22 | import android.os.Parcelable; |
Hugo Benichi | 5df9d72 | 2016-04-25 17:16:35 +0900 | [diff] [blame] | 23 | import android.util.SparseArray; |
| 24 | |
| 25 | import com.android.internal.util.MessageUtils; |
Hugo Benichi | 6241874 | 2016-04-13 10:13:32 +0900 | [diff] [blame] | 26 | |
| 27 | /** |
Hugo Benichi | cf6b12f | 2016-07-04 11:28:05 +0900 | [diff] [blame] | 28 | * Event class used to record error events when parsing DHCP response packets. |
| 29 | * {@hide} |
Hugo Benichi | 6241874 | 2016-04-13 10:13:32 +0900 | [diff] [blame] | 30 | */ |
Remi NGUYEN VAN | 7b84fb3 | 2019-01-19 21:13:24 +0900 | [diff] [blame] | 31 | @SystemApi |
| 32 | @TestApi |
| 33 | public final class DhcpErrorEvent implements IpConnectivityLog.Event { |
Hugo Benichi | 6241874 | 2016-04-13 10:13:32 +0900 | [diff] [blame] | 34 | public static final int L2_ERROR = 1; |
| 35 | public static final int L3_ERROR = 2; |
| 36 | public static final int L4_ERROR = 3; |
| 37 | public static final int DHCP_ERROR = 4; |
| 38 | public static final int MISC_ERROR = 5; |
| 39 | |
| 40 | public static final int L2_TOO_SHORT = makeErrorCode(L2_ERROR, 1); |
| 41 | public static final int L2_WRONG_ETH_TYPE = makeErrorCode(L2_ERROR, 2); |
| 42 | |
| 43 | public static final int L3_TOO_SHORT = makeErrorCode(L3_ERROR, 1); |
| 44 | public static final int L3_NOT_IPV4 = makeErrorCode(L3_ERROR, 2); |
| 45 | public static final int L3_INVALID_IP = makeErrorCode(L3_ERROR, 3); |
| 46 | |
| 47 | public static final int L4_NOT_UDP = makeErrorCode(L4_ERROR, 1); |
| 48 | public static final int L4_WRONG_PORT = makeErrorCode(L4_ERROR, 2); |
| 49 | |
| 50 | public static final int BOOTP_TOO_SHORT = makeErrorCode(DHCP_ERROR, 1); |
| 51 | public static final int DHCP_BAD_MAGIC_COOKIE = makeErrorCode(DHCP_ERROR, 2); |
| 52 | public static final int DHCP_INVALID_OPTION_LENGTH = makeErrorCode(DHCP_ERROR, 3); |
| 53 | public static final int DHCP_NO_MSG_TYPE = makeErrorCode(DHCP_ERROR, 4); |
| 54 | public static final int DHCP_UNKNOWN_MSG_TYPE = makeErrorCode(DHCP_ERROR, 5); |
Hugo Benichi | 006e061 | 2016-10-05 21:07:19 +0900 | [diff] [blame] | 55 | public static final int DHCP_NO_COOKIE = makeErrorCode(DHCP_ERROR, 6); |
Hugo Benichi | 6241874 | 2016-04-13 10:13:32 +0900 | [diff] [blame] | 56 | |
| 57 | public static final int BUFFER_UNDERFLOW = makeErrorCode(MISC_ERROR, 1); |
Hugo Benichi | 2677b19 | 2016-04-18 09:04:04 +0900 | [diff] [blame] | 58 | public static final int RECEIVE_ERROR = makeErrorCode(MISC_ERROR, 2); |
Hugo Benichi | e0ea7fe | 2016-10-05 18:33:21 +0900 | [diff] [blame] | 59 | public static final int PARSING_ERROR = makeErrorCode(MISC_ERROR, 3); |
Hugo Benichi | 6241874 | 2016-04-13 10:13:32 +0900 | [diff] [blame] | 60 | |
| 61 | // error code byte format (MSB to LSB): |
| 62 | // byte 0: error type |
| 63 | // byte 1: error subtype |
| 64 | // byte 2: unused |
| 65 | // byte 3: optional code |
Remi NGUYEN VAN | 7b84fb3 | 2019-01-19 21:13:24 +0900 | [diff] [blame] | 66 | /** @hide */ |
Hugo Benichi | 6241874 | 2016-04-13 10:13:32 +0900 | [diff] [blame] | 67 | public final int errorCode; |
| 68 | |
Hugo Benichi | 948a859 | 2017-03-16 16:33:47 +0900 | [diff] [blame] | 69 | public DhcpErrorEvent(int errorCode) { |
Hugo Benichi | 6241874 | 2016-04-13 10:13:32 +0900 | [diff] [blame] | 70 | this.errorCode = errorCode; |
| 71 | } |
| 72 | |
| 73 | private DhcpErrorEvent(Parcel in) { |
| 74 | this.errorCode = in.readInt(); |
| 75 | } |
| 76 | |
Remi NGUYEN VAN | 7b84fb3 | 2019-01-19 21:13:24 +0900 | [diff] [blame] | 77 | /** @hide */ |
Hugo Benichi | cf6b12f | 2016-07-04 11:28:05 +0900 | [diff] [blame] | 78 | @Override |
Hugo Benichi | 6241874 | 2016-04-13 10:13:32 +0900 | [diff] [blame] | 79 | public void writeToParcel(Parcel out, int flags) { |
| 80 | out.writeInt(errorCode); |
| 81 | } |
| 82 | |
Remi NGUYEN VAN | 7b84fb3 | 2019-01-19 21:13:24 +0900 | [diff] [blame] | 83 | /** @hide */ |
Hugo Benichi | cf6b12f | 2016-07-04 11:28:05 +0900 | [diff] [blame] | 84 | @Override |
Hugo Benichi | 627b424 | 2016-04-15 16:56:28 +0900 | [diff] [blame] | 85 | public int describeContents() { |
| 86 | return 0; |
| 87 | } |
| 88 | |
Remi NGUYEN VAN | 7b84fb3 | 2019-01-19 21:13:24 +0900 | [diff] [blame] | 89 | /** @hide */ |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 90 | public static final @android.annotation.NonNull Parcelable.Creator<DhcpErrorEvent> CREATOR |
Hugo Benichi | 6241874 | 2016-04-13 10:13:32 +0900 | [diff] [blame] | 91 | = new Parcelable.Creator<DhcpErrorEvent>() { |
| 92 | public DhcpErrorEvent createFromParcel(Parcel in) { |
| 93 | return new DhcpErrorEvent(in); |
| 94 | } |
| 95 | |
| 96 | public DhcpErrorEvent[] newArray(int size) { |
| 97 | return new DhcpErrorEvent[size]; |
| 98 | } |
| 99 | }; |
| 100 | |
Hugo Benichi | 2677b19 | 2016-04-18 09:04:04 +0900 | [diff] [blame] | 101 | public static int errorCodeWithOption(int errorCode, int option) { |
| 102 | return (0xFFFF0000 & errorCode) | (0xFF & option); |
Hugo Benichi | 6241874 | 2016-04-13 10:13:32 +0900 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | private static int makeErrorCode(int type, int subtype) { |
| 106 | return (type << 24) | ((0xFF & subtype) << 16); |
| 107 | } |
Hugo Benichi | 5df9d72 | 2016-04-25 17:16:35 +0900 | [diff] [blame] | 108 | |
| 109 | @Override |
| 110 | public String toString() { |
Hugo Benichi | 948a859 | 2017-03-16 16:33:47 +0900 | [diff] [blame] | 111 | return String.format("DhcpErrorEvent(%s)", Decoder.constants.get(errorCode)); |
Hugo Benichi | 5df9d72 | 2016-04-25 17:16:35 +0900 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | final static class Decoder { |
Hugo Benichi | e0ea7fe | 2016-10-05 18:33:21 +0900 | [diff] [blame] | 115 | static final SparseArray<String> constants = MessageUtils.findMessageNames( |
| 116 | new Class[]{DhcpErrorEvent.class}, |
| 117 | new String[]{"L2_", "L3_", "L4_", "BOOTP_", "DHCP_", "BUFFER_", "RECEIVE_", |
| 118 | "PARSING_"}); |
Hugo Benichi | 5df9d72 | 2016-04-25 17:16:35 +0900 | [diff] [blame] | 119 | } |
Hugo Benichi | 6241874 | 2016-04-13 10:13:32 +0900 | [diff] [blame] | 120 | } |