1 /*
2  * Copyright (C) 2021 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 libcore.util.NonNull;
20 
21 import java.nio.ByteBuffer;
22 import java.nio.ByteOrder;
23 
24 /**
25  * Corresponds to C's {@code struct cmsghdr}.
26  *
27  */
28 public final class StructCmsghdr {
29     /** Originating protocol */
30     public final int cmsg_level;
31 
32     /** Protocol-specific type */
33     public final int cmsg_type;
34 
35     /** message data sent/received */
36     @NonNull public final byte[] cmsg_data;
37 
StructCmsghdr(int cmsg_level, int cmsg_type, short value)38     public StructCmsghdr(int cmsg_level, int cmsg_type, short value) {
39         // Short.Size unit is bits, ByteBuffer data unit is bytes
40         ByteBuffer buf = ByteBuffer.allocate(Short.SIZE / 8);
41         buf.order(ByteOrder.nativeOrder());
42         buf.putShort(value);
43 
44         this.cmsg_level = cmsg_level;
45         this.cmsg_type = cmsg_type;
46         this.cmsg_data = buf.array();
47     }
48 
StructCmsghdr(int cmsg_level, int cmsg_type, @NonNull byte[] value)49     public StructCmsghdr(int cmsg_level, int cmsg_type, @NonNull byte[] value) {
50         this.cmsg_level = cmsg_level;
51         this.cmsg_type = cmsg_type;
52         this.cmsg_data = value;
53     }
54 
55 }
56