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 17 package android.net.metrics; 18 19 import android.annotation.SystemApi; 20 import android.os.Parcel; 21 import android.os.Parcelable; 22 import android.util.SparseArray; 23 24 import 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 31 public 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 /** {@hide} */ 54 public static final int DHCP_NO_COOKIE = makeErrorCode(DHCP_ERROR, 6); 55 56 public static final int BUFFER_UNDERFLOW = makeErrorCode(MISC_ERROR, 1); 57 public static final int RECEIVE_ERROR = makeErrorCode(MISC_ERROR, 2); 58 /** {@hide} */ 59 public static final int PARSING_ERROR = makeErrorCode(MISC_ERROR, 3); 60 61 public final String ifName; 62 // error code byte format (MSB to LSB): 63 // byte 0: error type 64 // byte 1: error subtype 65 // byte 2: unused 66 // byte 3: optional code 67 public final int errorCode; 68 69 /** {@hide} */ 70 public DhcpErrorEvent(String ifName, int errorCode) { 71 this.ifName = ifName; 72 this.errorCode = errorCode; 73 } 74 75 private DhcpErrorEvent(Parcel in) { 76 this.ifName = in.readString(); 77 this.errorCode = in.readInt(); 78 } 79 80 @Override 81 public void writeToParcel(Parcel out, int flags) { 82 out.writeString(ifName); 83 out.writeInt(errorCode); 84 } 85 86 @Override 87 public int describeContents() { 88 return 0; 89 } 90 91 public static final Parcelable.Creator<DhcpErrorEvent> CREATOR 92 = new Parcelable.Creator<DhcpErrorEvent>() { 93 public DhcpErrorEvent createFromParcel(Parcel in) { 94 return new DhcpErrorEvent(in); 95 } 96 97 public DhcpErrorEvent[] newArray(int size) { 98 return new DhcpErrorEvent[size]; 99 } 100 }; 101 102 public static void logParseError(String ifName, int errorCode) { 103 } 104 105 public static void logReceiveError(String ifName) { 106 } 107 108 public static int errorCodeWithOption(int errorCode, int option) { 109 return (0xFFFF0000 & errorCode) | (0xFF & option); 110 } 111 112 private static int makeErrorCode(int type, int subtype) { 113 return (type << 24) | ((0xFF & subtype) << 16); 114 } 115 116 @Override 117 public String toString() { 118 return String.format("DhcpErrorEvent(%s, %s)", ifName, Decoder.constants.get(errorCode)); 119 } 120 121 final static class Decoder { 122 static final SparseArray<String> constants = MessageUtils.findMessageNames( 123 new Class[]{DhcpErrorEvent.class}, 124 new String[]{"L2_", "L3_", "L4_", "BOOTP_", "DHCP_", "BUFFER_", "RECEIVE_", 125 "PARSING_"}); 126 } 127 } 128