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.system;
18 
19 import static android.system.OsConstants.ICMP6_ECHO_REQUEST;
20 import static android.system.OsConstants.ICMP_ECHO;
21 
22 import java.io.FileDescriptor;
23 import java.net.SocketAddress;
24 
25 /**
26  * A utility class that can create ICMP header bytes corresponding to C's {@code struct icmphdr}
27  * from linux/icmp.h and {@code struct icmp6hdr} from linux/icmpv6.h. The bytes can be passed to
28  * methods like {@link Os#sendto(FileDescriptor, byte[], int, int, int, SocketAddress)}.
29  *
30  * @hide
31  */
32 public final class IcmpHeaders {
33 
IcmpHeaders()34     private IcmpHeaders() {}
35 
36     /**
37      * Creates the header bytes for an {@link OsConstants#ICMP_ECHO} or
38      * {@link OsConstants#ICMP6_ECHO_REQUEST} message. Code, checksum and identifier are left as
39      * zeros.
40      *
41      * <pre>
42      * 0                   1                   2                   3
43      * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
44      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
45      * |     Type      |     Code      |          Checksum             |
46      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
47      * |           Identifier          |        Sequence Number        |
48      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
49      * </pre>
50      */
createIcmpEchoHdr(boolean ipv4, int seq)51     public static byte[] createIcmpEchoHdr(boolean ipv4, int seq) {
52         byte[] bytes = new byte[8];
53         bytes[0] = ipv4 ? (byte) ICMP_ECHO : (byte) ICMP6_ECHO_REQUEST;
54         // packet[1]: Code is always zero.
55         // packet[2,3]: Checksum is computed by kernel.
56         // packet[4,5]: ID (= port) inserted by kernel.
57         bytes[6] = (byte) (seq >> 8);
58         bytes[7] = (byte) seq;
59         return bytes;
60     }
61 }
62