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