DhcpErrorEvent.java revision 23e0e805c543f3edad54ec71c6bc21147946511d
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
17package android.net.metrics;
18
19import android.annotation.SystemApi;
20import android.os.Parcel;
21import android.os.Parcelable;
22
23/**
24 * {@hide} Event class used to record error events when parsing DHCP response packets.
25 */
26@SystemApi
27public final class DhcpErrorEvent extends IpConnectivityEvent implements Parcelable {
28    public static final int L2_ERROR   = 1;
29    public static final int L3_ERROR   = 2;
30    public static final int L4_ERROR   = 3;
31    public static final int DHCP_ERROR = 4;
32    public static final int MISC_ERROR = 5;
33
34    public static final int L2_TOO_SHORT               = makeErrorCode(L2_ERROR, 1);
35    public static final int L2_WRONG_ETH_TYPE          = makeErrorCode(L2_ERROR, 2);
36
37    public static final int L3_TOO_SHORT               = makeErrorCode(L3_ERROR, 1);
38    public static final int L3_NOT_IPV4                = makeErrorCode(L3_ERROR, 2);
39    public static final int L3_INVALID_IP              = makeErrorCode(L3_ERROR, 3);
40
41    public static final int L4_NOT_UDP                 = makeErrorCode(L4_ERROR, 1);
42    public static final int L4_WRONG_PORT              = makeErrorCode(L4_ERROR, 2);
43
44    public static final int BOOTP_TOO_SHORT            = makeErrorCode(DHCP_ERROR, 1);
45    public static final int DHCP_BAD_MAGIC_COOKIE      = makeErrorCode(DHCP_ERROR, 2);
46    public static final int DHCP_INVALID_OPTION_LENGTH = makeErrorCode(DHCP_ERROR, 3);
47    public static final int DHCP_NO_MSG_TYPE           = makeErrorCode(DHCP_ERROR, 4);
48    public static final int DHCP_UNKNOWN_MSG_TYPE      = makeErrorCode(DHCP_ERROR, 5);
49
50    public static final int BUFFER_UNDERFLOW           = makeErrorCode(MISC_ERROR, 1);
51    public static final int RECEIVE_ERROR              = makeErrorCode(MISC_ERROR, 2);
52
53    public final String ifName;
54    // error code byte format (MSB to LSB):
55    // byte 0: error type
56    // byte 1: error subtype
57    // byte 2: unused
58    // byte 3: optional code
59    public final int errorCode;
60
61    private DhcpErrorEvent(String ifName, int errorCode) {
62        this.ifName = ifName;
63        this.errorCode = errorCode;
64    }
65
66    private DhcpErrorEvent(Parcel in) {
67        this.ifName = in.readString();
68        this.errorCode = in.readInt();
69    }
70
71    public void writeToParcel(Parcel out, int flags) {
72        out.writeString(ifName);
73        out.writeInt(errorCode);
74    }
75
76    public int describeContents() {
77        return 0;
78    }
79
80    public static final Parcelable.Creator<DhcpErrorEvent> CREATOR
81        = new Parcelable.Creator<DhcpErrorEvent>() {
82        public DhcpErrorEvent createFromParcel(Parcel in) {
83            return new DhcpErrorEvent(in);
84        }
85
86        public DhcpErrorEvent[] newArray(int size) {
87            return new DhcpErrorEvent[size];
88        }
89    };
90
91    public static void logParseError(String ifName, int errorCode) {
92        logEvent(IPCE_DHCP_PARSE_ERROR, new DhcpErrorEvent(ifName, errorCode));
93    }
94
95    public static void logReceiveError(String ifName) {
96        logEvent(IPCE_DHCP_RECV_ERROR, new DhcpErrorEvent(ifName, RECEIVE_ERROR));
97    }
98
99    public static int errorCodeWithOption(int errorCode, int option) {
100        return (0xFFFF0000 & errorCode) | (0xFF & option);
101    }
102
103    private static int makeErrorCode(int type, int subtype) {
104        return (type << 24) | ((0xFF & subtype) << 16);
105    }
106}
107