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.util; 18 19 import java.nio.ByteBuffer; 20 21 22 /** 23 * Networking protocol constants. 24 * 25 * Includes: 26 * - constants that describe packet layout 27 * - various helper functions 28 * 29 * @hide 30 */ 31 public final class NetworkConstants { 32 private NetworkConstants() { throw new RuntimeException("no instance permitted"); } 33 34 /** 35 * Ethernet constants. 36 * 37 * See also: 38 * - https://tools.ietf.org/html/rfc894 39 * - https://tools.ietf.org/html/rfc2464 40 * - https://tools.ietf.org/html/rfc7042 41 * - http://www.iana.org/assignments/ethernet-numbers/ethernet-numbers.xhtml 42 * - http://www.iana.org/assignments/ieee-802-numbers/ieee-802-numbers.xhtml 43 */ 44 public static final int ETHER_DST_ADDR_OFFSET = 0; 45 public static final int ETHER_SRC_ADDR_OFFSET = 6; 46 public static final int ETHER_ADDR_LEN = 6; 47 48 public static final int ETHER_TYPE_OFFSET = 12; 49 public static final int ETHER_TYPE_LENGTH = 2; 50 public static final int ETHER_TYPE_ARP = 0x0806; 51 public static final int ETHER_TYPE_IPV4 = 0x0800; 52 public static final int ETHER_TYPE_IPV6 = 0x86dd; 53 54 public static final int ETHER_HEADER_LEN = 14; 55 56 private static final byte FF = asByte(0xff); 57 public static final byte[] ETHER_ADDR_BROADCAST = { 58 FF, FF, FF, FF, FF, FF 59 }; 60 61 public static final int ETHER_MTU = 1500; 62 63 /** 64 * ARP constants. 65 * 66 * See also: 67 * - https://tools.ietf.org/html/rfc826 68 * - http://www.iana.org/assignments/arp-parameters/arp-parameters.xhtml 69 */ 70 public static final int ARP_PAYLOAD_LEN = 28; // For Ethernet+IPv4. 71 public static final int ARP_REQUEST = 1; 72 public static final int ARP_REPLY = 2; 73 public static final int ARP_HWTYPE_RESERVED_LO = 0; 74 public static final int ARP_HWTYPE_ETHER = 1; 75 public static final int ARP_HWTYPE_RESERVED_HI = 0xffff; 76 77 /** 78 * IPv4 constants. 79 * 80 * See als: 81 * - https://tools.ietf.org/html/rfc791 82 */ 83 public static final int IPV4_HEADER_MIN_LEN = 20; 84 public static final int IPV4_IHL_MASK = 0xf; 85 public static final int IPV4_FLAGS_OFFSET = 6; 86 public static final int IPV4_FRAGMENT_MASK = 0x1fff; 87 public static final int IPV4_PROTOCOL_OFFSET = 9; 88 public static final int IPV4_SRC_ADDR_OFFSET = 12; 89 public static final int IPV4_DST_ADDR_OFFSET = 16; 90 public static final int IPV4_ADDR_BITS = 32; 91 public static final int IPV4_ADDR_LEN = 4; 92 93 /** 94 * IPv6 constants. 95 * 96 * See also: 97 * - https://tools.ietf.org/html/rfc2460 98 */ 99 public static final int IPV6_HEADER_LEN = 40; 100 public static final int IPV6_PROTOCOL_OFFSET = 6; 101 public static final int IPV6_SRC_ADDR_OFFSET = 8; 102 public static final int IPV6_DST_ADDR_OFFSET = 24; 103 public static final int IPV6_ADDR_BITS = 128; 104 public static final int IPV6_ADDR_LEN = 16; 105 public static final int IPV6_MIN_MTU = 1280; 106 public static final int RFC7421_PREFIX_LENGTH = 64; 107 public static final int RFC6177_MIN_PREFIX_LENGTH = 48; 108 109 /** 110 * ICMPv6 constants. 111 * 112 * See also: 113 * - https://tools.ietf.org/html/rfc4443 114 * - https://tools.ietf.org/html/rfc4861 115 */ 116 public static final int ICMPV6_HEADER_MIN_LEN = 4; 117 public static final int ICMPV6_ROUTER_SOLICITATION = 133; 118 public static final int ICMPV6_ROUTER_ADVERTISEMENT = 134; 119 public static final int ICMPV6_NEIGHBOR_SOLICITATION = 135; 120 public static final int ICMPV6_NEIGHBOR_ADVERTISEMENT = 136; 121 122 public static final int ICMPV6_ND_OPTION_MIN_LENGTH = 8; 123 public static final int ICMPV6_ND_OPTION_LENGTH_SCALING_FACTOR = 8; 124 public static final int ICMPV6_ND_OPTION_SLLA = 1; 125 public static final int ICMPV6_ND_OPTION_TLLA = 2; 126 public static final int ICMPV6_ND_OPTION_MTU = 5; 127 128 /** 129 * UDP constants. 130 * 131 * See also: 132 * - https://tools.ietf.org/html/rfc768 133 */ 134 public static final int UDP_HEADER_LEN = 8; 135 136 /** 137 * DHCP(v4) constants. 138 * 139 * See also: 140 * - https://tools.ietf.org/html/rfc2131 141 */ 142 public static final int DHCP4_SERVER_PORT = 67; 143 public static final int DHCP4_CLIENT_PORT = 68; 144 145 /** 146 * Utility functions. 147 */ 148 public static byte asByte(int i) { return (byte) i; } 149 150 public static String asString(int i) { return Integer.toString(i); } 151 152 public static int asUint(byte b) { return (b & 0xff); } 153 public static int asUint(short s) { return (s & 0xffff); } 154 } 155