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