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