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