DhcpErrorEvent.java revision 948a85948dcccea4aaa9d70a74405308581b0925
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    // error code byte format (MSB to LSB):
58    // byte 0: error type
59    // byte 1: error subtype
60    // byte 2: unused
61    // byte 3: optional code
62    public final int errorCode;
63
64    public DhcpErrorEvent(int errorCode) {
65        this.errorCode = errorCode;
66    }
67
68    private DhcpErrorEvent(Parcel in) {
69        this.errorCode = in.readInt();
70    }
71
72    @Override
73    public void writeToParcel(Parcel out, int flags) {
74        out.writeInt(errorCode);
75    }
76
77    @Override
78    public int describeContents() {
79        return 0;
80    }
81
82    public static final Parcelable.Creator<DhcpErrorEvent> CREATOR
83        = new Parcelable.Creator<DhcpErrorEvent>() {
84        public DhcpErrorEvent createFromParcel(Parcel in) {
85            return new DhcpErrorEvent(in);
86        }
87
88        public DhcpErrorEvent[] newArray(int size) {
89            return new DhcpErrorEvent[size];
90        }
91    };
92
93    public static int errorCodeWithOption(int errorCode, int option) {
94        return (0xFFFF0000 & errorCode) | (0xFF & option);
95    }
96
97    private static int makeErrorCode(int type, int subtype) {
98        return (type << 24) | ((0xFF & subtype) << 16);
99    }
100
101    @Override
102    public String toString() {
103        return String.format("DhcpErrorEvent(%s)", Decoder.constants.get(errorCode));
104    }
105
106    final static class Decoder {
107        static final SparseArray<String> constants = MessageUtils.findMessageNames(
108                new Class[]{DhcpErrorEvent.class},
109                new String[]{"L2_", "L3_", "L4_", "BOOTP_", "DHCP_", "BUFFER_", "RECEIVE_",
110                "PARSING_"});
111    }
112}
113