1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.channel.uring;
17
18 import io.netty.channel.unix.Buffer;
19
20 import java.nio.ByteBuffer;
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 final class MsgHdr {
36
37 private MsgHdr() { }
38
39 static void set(ByteBuffer memory, ByteBuffer sockAddrMemory, int addressSize, ByteBuffer iovMemory, int iovLength,
40 ByteBuffer msgControl, int cmsgHdrDataOffset, short segmentSize) {
41 int memoryPosition = memory.position();
42 memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_NAMELEN, addressSize);
43
44 int msgControlLen = 0;
45 long msgControlAddr;
46 if (segmentSize > 0) {
47 msgControlLen = Native.CMSG_LEN;
48 CmsgHdr.write(msgControl, cmsgHdrDataOffset, Native.CMSG_LEN, Native.SOL_UDP,
49 Native.UDP_SEGMENT, segmentSize);
50 msgControlAddr = Buffer.memoryAddress(msgControl) + msgControl.position();
51 } else {
52
53 msgControlAddr = 0;
54 }
55 if (Native.SIZEOF_SIZE_T == 4) {
56 memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_NAME, (int) Buffer.memoryAddress(sockAddrMemory));
57 memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_IOV, (int) Buffer.memoryAddress(iovMemory));
58 memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_IOVLEN, iovLength);
59 memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_CONTROL, (int) msgControlAddr);
60 memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_CONTROLLEN, msgControlLen);
61 } else {
62 assert Native.SIZEOF_SIZE_T == 8;
63 memory.putLong(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_NAME, Buffer.memoryAddress(sockAddrMemory));
64 memory.putLong(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_IOV, Buffer.memoryAddress(iovMemory));
65 memory.putLong(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_IOVLEN, iovLength);
66 memory.putLong(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_CONTROL, msgControlAddr);
67 memory.putLong(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_CONTROLLEN, msgControlLen);
68 }
69
70 }
71
72 static void prepSendFd(ByteBuffer memory, int fd, ByteBuffer msgControl,
73 int cmsgHdrDataOffset, ByteBuffer iovMemory, int iovLength) {
74 int memoryPosition = memory.position();
75 long msgControlAddr = Buffer.memoryAddress(msgControl);
76 CmsgHdr.writeScmRights(msgControl, cmsgHdrDataOffset, fd);
77 if (Native.SIZEOF_SIZE_T == 4) {
78 memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_CONTROL, (int) msgControlAddr);
79 memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_CONTROLLEN, Native.MSG_CONTROL_LEN_FOR_FD);
80 memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_IOV, (int) Buffer.memoryAddress(iovMemory));
81 memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_IOVLEN, iovLength);
82 } else {
83 assert Native.SIZEOF_SIZE_T == 8;
84 memory.putLong(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_CONTROL, msgControlAddr);
85 memory.putLong(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_CONTROLLEN, Native.MSG_CONTROL_LEN_FOR_FD);
86 memory.putLong(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_IOV, Buffer.memoryAddress(iovMemory));
87 memory.putLong(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_IOVLEN, iovLength);
88 }
89 }
90
91 static void prepReadFd(ByteBuffer memory, ByteBuffer msgControl, int cmsgHdrDataOffset,
92 ByteBuffer iovMemory, int iovLength) {
93 int memoryPosition = memory.position();
94 long msgControlAddr = Buffer.memoryAddress(msgControl);
95 if (Native.SIZEOF_SIZE_T == 4) {
96 memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_CONTROL, (int) msgControlAddr);
97 memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_CONTROLLEN, Native.MSG_CONTROL_LEN_FOR_FD);
98 memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_IOV, (int) Buffer.memoryAddress(iovMemory));
99 memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_IOVLEN, iovLength);
100 } else {
101 assert Native.SIZEOF_SIZE_T == 8;
102 memory.putLong(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_CONTROL, msgControlAddr);
103 memory.putLong(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_CONTROLLEN, Native.MSG_CONTROL_LEN_FOR_FD);
104 memory.putLong(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_IOV, Buffer.memoryAddress(iovMemory));
105 memory.putLong(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_IOVLEN, iovLength);
106 }
107 }
108
109 static int getCmsgData(ByteBuffer memory, ByteBuffer msgControl, int cmsgHdrDataOffset) {
110 return CmsgHdr.readScmRights(msgControl, cmsgHdrDataOffset);
111 }
112 }