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 
19 import android.annotation.NonNull;
20 import android.annotation.SystemApi;
21 import android.annotation.TestApi;
22 import android.os.Parcel;
23 import android.os.Parcelable;
24 import android.util.SparseArray;
25 
26 import com.android.internal.util.MessageUtils;
27 
28 /**
29  * Event class used to record error events when parsing DHCP response packets.
30  * {@hide}
31  */
32 @SystemApi
33 @TestApi
34 public final class DhcpErrorEvent implements IpConnectivityLog.Event {
35     public static final int L2_ERROR   = 1;
36     public static final int L3_ERROR   = 2;
37     public static final int L4_ERROR   = 3;
38     public static final int DHCP_ERROR = 4;
39     public static final int MISC_ERROR = 5;
40 
41     // error code byte format (MSB to LSB):
42     // byte 0: error type
43     // byte 1: error subtype
44     // byte 2: unused
45     // byte 3: optional code
46     /** @hide */
47     public final int errorCode;
48 
49     private static final int L2_ERROR_TYPE = L2_ERROR << 8;
50     private static final int L3_ERROR_TYPE = L3_ERROR << 8;
51     private static final int L4_ERROR_TYPE = L4_ERROR << 8;
52     private static final int DHCP_ERROR_TYPE = DHCP_ERROR << 8;
53     private static final int MISC_ERROR_TYPE = MISC_ERROR << 8;
54 
55     public static final int L2_TOO_SHORT               = (L2_ERROR_TYPE | 0x1) << 16;
56     public static final int L2_WRONG_ETH_TYPE          = (L2_ERROR_TYPE | 0x2) << 16;
57 
58     public static final int L3_TOO_SHORT               = (L3_ERROR_TYPE | 0x1) << 16;
59     public static final int L3_NOT_IPV4                = (L3_ERROR_TYPE | 0x2) << 16;
60     public static final int L3_INVALID_IP              = (L3_ERROR_TYPE | 0x3) << 16;
61 
62     public static final int L4_NOT_UDP                 = (L4_ERROR_TYPE | 0x1) << 16;
63     public static final int L4_WRONG_PORT              = (L4_ERROR_TYPE | 0x2) << 16;
64 
65     public static final int BOOTP_TOO_SHORT            = (DHCP_ERROR_TYPE | 0x1) << 16;
66     public static final int DHCP_BAD_MAGIC_COOKIE      = (DHCP_ERROR_TYPE | 0x2) << 16;
67     public static final int DHCP_INVALID_OPTION_LENGTH = (DHCP_ERROR_TYPE | 0x3) << 16;
68     public static final int DHCP_NO_MSG_TYPE           = (DHCP_ERROR_TYPE | 0x4) << 16;
69     public static final int DHCP_UNKNOWN_MSG_TYPE      = (DHCP_ERROR_TYPE | 0x5) << 16;
70     public static final int DHCP_NO_COOKIE             = (DHCP_ERROR_TYPE | 0x6) << 16;
71 
72     public static final int BUFFER_UNDERFLOW           = (MISC_ERROR_TYPE | 0x1) << 16;
73     public static final int RECEIVE_ERROR              = (MISC_ERROR_TYPE | 0x2) << 16;
74     public static final int PARSING_ERROR              = (MISC_ERROR_TYPE | 0x3) << 16;
75 
DhcpErrorEvent(int errorCode)76     public DhcpErrorEvent(int errorCode) {
77         this.errorCode = errorCode;
78     }
79 
DhcpErrorEvent(Parcel in)80     private DhcpErrorEvent(Parcel in) {
81         this.errorCode = in.readInt();
82     }
83 
84     /** @hide */
85     @Override
writeToParcel(Parcel out, int flags)86     public void writeToParcel(Parcel out, int flags) {
87         out.writeInt(errorCode);
88     }
89 
90     /** @hide */
91     @Override
describeContents()92     public int describeContents() {
93         return 0;
94     }
95 
96     /** @hide */
97     public static final @android.annotation.NonNull Parcelable.Creator<DhcpErrorEvent> CREATOR
98         = new Parcelable.Creator<DhcpErrorEvent>() {
99         public DhcpErrorEvent createFromParcel(Parcel in) {
100             return new DhcpErrorEvent(in);
101         }
102 
103         public DhcpErrorEvent[] newArray(int size) {
104             return new DhcpErrorEvent[size];
105         }
106     };
107 
errorCodeWithOption(int errorCode, int option)108     public static int errorCodeWithOption(int errorCode, int option) {
109         return (0xFFFF0000 & errorCode) | (0xFF & option);
110     }
111 
112     @NonNull
113     @Override
toString()114     public String toString() {
115         return String.format("DhcpErrorEvent(%s)", Decoder.constants.get(errorCode));
116     }
117 
118     final static class Decoder {
119         static final SparseArray<String> constants = MessageUtils.findMessageNames(
120                 new Class[]{DhcpErrorEvent.class},
121                 new String[]{"L2_", "L3_", "L4_", "BOOTP_", "DHCP_", "BUFFER_", "RECEIVE_",
122                 "PARSING_"});
123     }
124 }
125