2 * Copyright (C) 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package android.net.metrics;
19 import android.annotation.SystemApi;
20 import android.os.Parcel;
21 import android.os.Parcelable;
22 import android.util.SparseArray;
24 import com.android.internal.util.MessageUtils;
27 * Event class used to record error events when parsing DHCP response packets.
31 public final class DhcpErrorEvent implements Parcelable {
32 public static final int L2_ERROR = 1;
33 public static final int L3_ERROR = 2;
34 public static final int L4_ERROR = 3;
35 public static final int DHCP_ERROR = 4;
36 public static final int MISC_ERROR = 5;
38 public static final int L2_TOO_SHORT = makeErrorCode(L2_ERROR, 1);
39 public static final int L2_WRONG_ETH_TYPE = makeErrorCode(L2_ERROR, 2);
41 public static final int L3_TOO_SHORT = makeErrorCode(L3_ERROR, 1);
42 public static final int L3_NOT_IPV4 = makeErrorCode(L3_ERROR, 2);
43 public static final int L3_INVALID_IP = makeErrorCode(L3_ERROR, 3);
45 public static final int L4_NOT_UDP = makeErrorCode(L4_ERROR, 1);
46 public static final int L4_WRONG_PORT = makeErrorCode(L4_ERROR, 2);
48 public static final int BOOTP_TOO_SHORT = makeErrorCode(DHCP_ERROR, 1);
49 public static final int DHCP_BAD_MAGIC_COOKIE = makeErrorCode(DHCP_ERROR, 2);
50 public static final int DHCP_INVALID_OPTION_LENGTH = makeErrorCode(DHCP_ERROR, 3);
51 public static final int DHCP_NO_MSG_TYPE = makeErrorCode(DHCP_ERROR, 4);
52 public static final int DHCP_UNKNOWN_MSG_TYPE = makeErrorCode(DHCP_ERROR, 5);
54 public static final int BUFFER_UNDERFLOW = makeErrorCode(MISC_ERROR, 1);
55 public static final int RECEIVE_ERROR = makeErrorCode(MISC_ERROR, 2);
57 public final String ifName;
58 // error code byte format (MSB to LSB):
60 // byte 1: error subtype
62 // byte 3: optional code
63 public final int errorCode;
66 public DhcpErrorEvent(String ifName, int errorCode) {
68 this.errorCode = errorCode;
71 private DhcpErrorEvent(Parcel in) {
72 this.ifName = in.readString();
73 this.errorCode = in.readInt();
77 public void writeToParcel(Parcel out, int flags) {
78 out.writeString(ifName);
79 out.writeInt(errorCode);
83 public int describeContents() {
87 public static final Parcelable.Creator<DhcpErrorEvent> CREATOR
88 = new Parcelable.Creator<DhcpErrorEvent>() {
89 public DhcpErrorEvent createFromParcel(Parcel in) {
90 return new DhcpErrorEvent(in);
93 public DhcpErrorEvent[] newArray(int size) {
94 return new DhcpErrorEvent[size];
98 public static void logParseError(String ifName, int errorCode) {
101 public static void logReceiveError(String ifName) {
104 public static int errorCodeWithOption(int errorCode, int option) {
105 return (0xFFFF0000 & errorCode) | (0xFF & option);
108 private static int makeErrorCode(int type, int subtype) {
109 return (type << 24) | ((0xFF & subtype) << 16);
113 public String toString() {
114 return String.format("DhcpErrorEvent(%s, %s)", ifName, Decoder.constants.get(errorCode));
117 final static class Decoder {
118 static final SparseArray<String> constants =
119 MessageUtils.findMessageNames(new Class[]{DhcpErrorEvent.class},
120 new String[]{"L2_", "L3_", "L4_", "BOOTP_", "DHCP_", "BUFFER_", "RECEIVE_"});