1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.channel.uring;
17
18 import java.nio.ByteBuffer;
19
20
21
22
23
24
25
26
27
28
29
30 final class CmsgHdr {
31
32 private CmsgHdr() { }
33
34 static void write(ByteBuffer cmsghdr, int cmsgHdrDataOffset,
35 int cmsgLen, int cmsgLevel, int cmsgType, short segmentSize) {
36 int cmsghdrPosition = cmsghdr.position();
37 if (Native.SIZEOF_SIZE_T == 4) {
38 cmsghdr.putInt(cmsghdrPosition + Native.CMSG_OFFSETOF_CMSG_LEN, cmsgLen);
39 } else {
40 assert Native.SIZEOF_SIZE_T == 8;
41 cmsghdr.putLong(cmsghdrPosition + Native.CMSG_OFFSETOF_CMSG_LEN, cmsgLen);
42 }
43 cmsghdr.putInt(cmsghdrPosition + Native.CMSG_OFFSETOF_CMSG_LEVEL, cmsgLevel);
44 cmsghdr.putInt(cmsghdrPosition + Native.CMSG_OFFSETOF_CMSG_TYPE, cmsgType);
45 cmsghdr.putShort(cmsghdrPosition + cmsgHdrDataOffset, segmentSize);
46 }
47
48 static void writeScmRights(ByteBuffer cmsghdr, int cmsgHdrDataOffset, int fd) {
49 int cmsghdrPosition = cmsghdr.position();
50 if (Native.SIZEOF_SIZE_T == 4) {
51 cmsghdr.putInt(cmsghdrPosition + Native.CMSG_OFFSETOF_CMSG_LEN, Native.CMSG_LEN_FOR_FD);
52 } else {
53 assert Native.SIZEOF_SIZE_T == 8;
54 cmsghdr.putLong(cmsghdrPosition + Native.CMSG_OFFSETOF_CMSG_LEN, Native.CMSG_LEN_FOR_FD);
55 }
56 cmsghdr.putInt(cmsghdrPosition + Native.CMSG_OFFSETOF_CMSG_LEVEL, Native.SOL_SOCKET);
57 cmsghdr.putInt(cmsghdrPosition + Native.CMSG_OFFSETOF_CMSG_TYPE, Native.SCM_RIGHTS);
58 cmsghdr.putInt(cmsghdrPosition + cmsgHdrDataOffset, fd);
59 }
60
61 static int readScmRights(ByteBuffer cmsghdr, int cmsgHdrDataOffset) {
62 return cmsghdr.getInt(cmsghdr.position() + cmsgHdrDataOffset);
63 }
64 }