• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /**
23  * Corresponds to C's {@code struct icmphdr} from linux/icmp.h and {@code struct icmp6hdr} from
24  * linux/icmpv6.h
25  *
26  * @hide
27  */
28 public final class StructIcmpHdr {
29     private byte[] packet;
30 
StructIcmpHdr()31     private StructIcmpHdr() {
32         packet =  new byte[8];
33     }
34 
35     /*
36      * Echo or Echo Reply Message
37      *
38      * 0                   1                   2                   3
39      * 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
40      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41      * |     Type      |     Code      |          Checksum             |
42      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43      * |           Identifier          |        Sequence Number        |
44      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
45      * |     Data ...
46      * +-+-+-+-+-
47      */
IcmpEchoHdr(boolean ipv4, int seq)48     public static StructIcmpHdr IcmpEchoHdr(boolean ipv4, int seq) {
49         StructIcmpHdr hdr = new StructIcmpHdr();
50         hdr.packet[0] = ipv4 ? (byte) ICMP_ECHO : (byte) ICMP6_ECHO_REQUEST;
51         // packet[1]: Code is always zero.
52         // packet[2,3]: Checksum is computed by kernel.
53         // packet[4,5]: ID (= port) inserted by kernel.
54         hdr.packet[6] = (byte) (seq >> 8);
55         hdr.packet[7] = (byte) seq;
56         return hdr;
57     }
58 
getBytes()59     public byte[] getBytes() {
60         return packet.clone();
61     }
62 }
63