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 {
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     public 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      * ICMP common (v4/v6) constants.
111      *
112      * See also:
113      *     - https://tools.ietf.org/html/rfc792
114      *     - https://tools.ietf.org/html/rfc4443
115      */
116     public static final int ICMP_HEADER_TYPE_OFFSET = 0;
117     public static final int ICMP_HEADER_CODE_OFFSET = 1;
118     public static final int ICMP_HEADER_CHECKSUM_OFFSET = 2;
119     public static final int ICMP_ECHO_IDENTIFIER_OFFSET = 4;
120     public static final int ICMP_ECHO_SEQUENCE_NUMBER_OFFSET = 6;
121     public static final int ICMP_ECHO_DATA_OFFSET = 8;
122 
123     /**
124      * ICMPv4 constants.
125      *
126      * See also:
127      *     - https://tools.ietf.org/html/rfc792
128      */
129     public static final int ICMPV4_ECHO_REQUEST_TYPE = 8;
130 
131     /**
132      * ICMPv6 constants.
133      *
134      * See also:
135      *     - https://tools.ietf.org/html/rfc4443
136      *     - https://tools.ietf.org/html/rfc4861
137      */
138     public static final int ICMPV6_HEADER_MIN_LEN = 4;
139     public static final int ICMPV6_ECHO_REQUEST_TYPE = 128;
140     public static final int ICMPV6_ECHO_REPLY_TYPE = 129;
141     public static final int ICMPV6_ROUTER_SOLICITATION    = 133;
142     public static final int ICMPV6_ROUTER_ADVERTISEMENT   = 134;
143     public static final int ICMPV6_NEIGHBOR_SOLICITATION  = 135;
144     public static final int ICMPV6_NEIGHBOR_ADVERTISEMENT = 136;
145 
146     public static final int ICMPV6_ND_OPTION_MIN_LENGTH = 8;
147     public static final int ICMPV6_ND_OPTION_LENGTH_SCALING_FACTOR = 8;
148     public static final int ICMPV6_ND_OPTION_SLLA = 1;
149     public static final int ICMPV6_ND_OPTION_TLLA = 2;
150     public static final int ICMPV6_ND_OPTION_MTU  = 5;
151 
152 
153     /**
154      * UDP constants.
155      *
156      * See also:
157      *     - https://tools.ietf.org/html/rfc768
158      */
159     public static final int UDP_HEADER_LEN = 8;
160 
161     /**
162      * DHCP(v4) constants.
163      *
164      * See also:
165      *     - https://tools.ietf.org/html/rfc2131
166      */
167     public static final int DHCP4_SERVER_PORT = 67;
168     public static final int DHCP4_CLIENT_PORT = 68;
169 
170     /**
171      * DNS constants.
172      *
173      * See also:
174      *     - https://tools.ietf.org/html/rfc1035
175      */
176     public static final int DNS_SERVER_PORT = 53;
177 
178     /**
179      * Utility functions.
180      */
asByte(int i)181     public static byte asByte(int i) { return (byte) i; }
182 
asString(int i)183     public static String asString(int i) { return Integer.toString(i); }
184 
asUint(byte b)185     public static int asUint(byte b) { return (b & 0xff); }
asUint(short s)186     public static int asUint(short s) { return (s & 0xffff); }
187 }
188