blob: 91318a2459d2449e5eabe18ffc9d7cea8d0ac83c [file] [log] [blame]
Hugo Benichi62418742016-04-13 10:13:32 +09001/*
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
17package android.net.metrics;
18
Remi NGUYEN VAN7b84fb32019-01-19 21:13:24 +090019import android.annotation.SystemApi;
20import android.annotation.TestApi;
Hugo Benichi62418742016-04-13 10:13:32 +090021import android.os.Parcel;
22import android.os.Parcelable;
Hugo Benichi5df9d722016-04-25 17:16:35 +090023import android.util.SparseArray;
24
25import com.android.internal.util.MessageUtils;
Hugo Benichi62418742016-04-13 10:13:32 +090026
27/**
Hugo Benichicf6b12f2016-07-04 11:28:05 +090028 * Event class used to record error events when parsing DHCP response packets.
29 * {@hide}
Hugo Benichi62418742016-04-13 10:13:32 +090030 */
Remi NGUYEN VAN7b84fb32019-01-19 21:13:24 +090031@SystemApi
32@TestApi
33public final class DhcpErrorEvent implements IpConnectivityLog.Event {
Hugo Benichi62418742016-04-13 10:13:32 +090034 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 Benichi006e0612016-10-05 21:07:19 +090055 public static final int DHCP_NO_COOKIE = makeErrorCode(DHCP_ERROR, 6);
Hugo Benichi62418742016-04-13 10:13:32 +090056
57 public static final int BUFFER_UNDERFLOW = makeErrorCode(MISC_ERROR, 1);
Hugo Benichi2677b192016-04-18 09:04:04 +090058 public static final int RECEIVE_ERROR = makeErrorCode(MISC_ERROR, 2);
Hugo Benichie0ea7fe2016-10-05 18:33:21 +090059 public static final int PARSING_ERROR = makeErrorCode(MISC_ERROR, 3);
Hugo Benichi62418742016-04-13 10:13:32 +090060
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 VAN7b84fb32019-01-19 21:13:24 +090066 /** @hide */
Hugo Benichi62418742016-04-13 10:13:32 +090067 public final int errorCode;
68
Hugo Benichi948a8592017-03-16 16:33:47 +090069 public DhcpErrorEvent(int errorCode) {
Hugo Benichi62418742016-04-13 10:13:32 +090070 this.errorCode = errorCode;
71 }
72
73 private DhcpErrorEvent(Parcel in) {
74 this.errorCode = in.readInt();
75 }
76
Remi NGUYEN VAN7b84fb32019-01-19 21:13:24 +090077 /** @hide */
Hugo Benichicf6b12f2016-07-04 11:28:05 +090078 @Override
Hugo Benichi62418742016-04-13 10:13:32 +090079 public void writeToParcel(Parcel out, int flags) {
80 out.writeInt(errorCode);
81 }
82
Remi NGUYEN VAN7b84fb32019-01-19 21:13:24 +090083 /** @hide */
Hugo Benichicf6b12f2016-07-04 11:28:05 +090084 @Override
Hugo Benichi627b4242016-04-15 16:56:28 +090085 public int describeContents() {
86 return 0;
87 }
88
Remi NGUYEN VAN7b84fb32019-01-19 21:13:24 +090089 /** @hide */
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -070090 public static final @android.annotation.NonNull Parcelable.Creator<DhcpErrorEvent> CREATOR
Hugo Benichi62418742016-04-13 10:13:32 +090091 = 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 Benichi2677b192016-04-18 09:04:04 +0900101 public static int errorCodeWithOption(int errorCode, int option) {
102 return (0xFFFF0000 & errorCode) | (0xFF & option);
Hugo Benichi62418742016-04-13 10:13:32 +0900103 }
104
105 private static int makeErrorCode(int type, int subtype) {
106 return (type << 24) | ((0xFF & subtype) << 16);
107 }
Hugo Benichi5df9d722016-04-25 17:16:35 +0900108
109 @Override
110 public String toString() {
Hugo Benichi948a8592017-03-16 16:33:47 +0900111 return String.format("DhcpErrorEvent(%s)", Decoder.constants.get(errorCode));
Hugo Benichi5df9d722016-04-25 17:16:35 +0900112 }
113
114 final static class Decoder {
Hugo Benichie0ea7fe2016-10-05 18:33:21 +0900115 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 Benichi5df9d722016-04-25 17:16:35 +0900119 }
Hugo Benichi62418742016-04-13 10:13:32 +0900120}