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.io.IOException;
21 import java.nio.ByteBuffer;
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36 final class MsgHdr {
37
38 private MsgHdr() { }
39
40 static int iovLength(ByteBuffer memory) {
41 int offset = memory.position() + Native.MSGHDR_OFFSETOF_MSG_IOVLEN;
42 return Native.SIZEOF_SIZE_T == 4 ? memory.getInt(offset) : (int) memory.getLong(offset);
43 }
44
45 static void set(ByteBuffer memory, long iovMemory, int iovLength) {
46 int memoryPosition = memory.position();
47 memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_NAMELEN, 0);
48 if (Native.SIZEOF_SIZE_T == 4) {
49 memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_NAME, 0);
50 memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_IOV, (int) iovMemory);
51 memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_IOVLEN, iovLength);
52 memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_CONTROL, 0);
53 memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_CONTROLLEN, 0);
54 } else {
55 assert Native.SIZEOF_SIZE_T == 8;
56 memory.putLong(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_NAME, 0);
57 memory.putLong(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_IOV, iovMemory);
58 memory.putLong(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_IOVLEN, iovLength);
59 memory.putLong(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_CONTROL, 0);
60 memory.putLong(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_CONTROLLEN, 0);
61 }
62 }
63
64 static void set(ByteBuffer memory, ByteBuffer sockAddrMemory, int addressSize, long iovMemory, int iovLength,
65 ByteBuffer msgControl, int cmsgHdrDataOffset, short segmentSize) {
66 int memoryPosition = memory.position();
67 memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_NAMELEN, addressSize);
68
69 int msgControlLen = 0;
70 long msgControlAddr;
71 if (segmentSize > 0 && msgControl != null && cmsgHdrDataOffset >= 0) {
72 msgControlLen = Native.CMSG_LEN;
73 CmsgHdr.write(msgControl, cmsgHdrDataOffset, Native.CMSG_LEN, Native.SOL_UDP,
74 Native.UDP_SEGMENT, segmentSize);
75 msgControlAddr = Buffer.memoryAddress(msgControl) + msgControl.position();
76 } else {
77
78 msgControlAddr = 0;
79 }
80 long sockAddr = sockAddrMemory == null ? 0 : Buffer.memoryAddress(sockAddrMemory);
81 if (Native.SIZEOF_SIZE_T == 4) {
82 memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_NAME, (int) sockAddr);
83 memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_IOV, (int) iovMemory);
84 memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_IOVLEN, iovLength);
85 memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_CONTROL, (int) msgControlAddr);
86 memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_CONTROLLEN, msgControlLen);
87 } else {
88 assert Native.SIZEOF_SIZE_T == 8;
89 memory.putLong(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_NAME, sockAddr);
90 memory.putLong(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_IOV, iovMemory);
91 memory.putLong(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_IOVLEN, iovLength);
92 memory.putLong(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_CONTROL, msgControlAddr);
93 memory.putLong(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_CONTROLLEN, msgControlLen);
94 }
95
96 }
97
98 static void set(ByteBuffer memory, ByteBuffer sockAddrMemory, int addressSize, ByteBuffer iovMemory, int iovLength,
99 ByteBuffer msgControl, int cmsgHdrDataOffset, short segmentSize) {
100 set(memory, sockAddrMemory, addressSize, Buffer.memoryAddress(iovMemory), iovLength,
101 msgControl, cmsgHdrDataOffset, segmentSize);
102 }
103
104 static void prepSendFd(ByteBuffer memory, int fd, ByteBuffer msgControl,
105 int cmsgHdrDataOffset, ByteBuffer iovMemory, int iovLength) {
106 int memoryPosition = memory.position();
107 long msgControlAddr = Buffer.memoryAddress(msgControl);
108 CmsgHdr.writeScmRights(msgControl, cmsgHdrDataOffset, fd);
109 if (Native.SIZEOF_SIZE_T == 4) {
110 memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_CONTROL, (int) msgControlAddr);
111 memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_CONTROLLEN, Native.MSG_CONTROL_LEN_FOR_FD);
112 memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_IOV, (int) Buffer.memoryAddress(iovMemory));
113 memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_IOVLEN, iovLength);
114 } else {
115 assert Native.SIZEOF_SIZE_T == 8;
116 memory.putLong(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_CONTROL, msgControlAddr);
117 memory.putLong(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_CONTROLLEN, Native.MSG_CONTROL_LEN_FOR_FD);
118 memory.putLong(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_IOV, Buffer.memoryAddress(iovMemory));
119 memory.putLong(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_IOVLEN, iovLength);
120 }
121 }
122
123 static void prepReadFd(ByteBuffer memory, ByteBuffer msgControl, int cmsgHdrDataOffset,
124 ByteBuffer iovMemory, int iovLength) {
125 int memoryPosition = memory.position();
126 long msgControlAddr = Buffer.memoryAddress(msgControl);
127 if (Native.SIZEOF_SIZE_T == 4) {
128 memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_CONTROL, (int) msgControlAddr);
129 memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_CONTROLLEN, Native.MSG_CONTROL_LEN_FOR_FD);
130 memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_IOV, (int) Buffer.memoryAddress(iovMemory));
131 memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_IOVLEN, iovLength);
132 } else {
133 assert Native.SIZEOF_SIZE_T == 8;
134 memory.putLong(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_CONTROL, msgControlAddr);
135 memory.putLong(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_CONTROLLEN, Native.MSG_CONTROL_LEN_FOR_FD);
136 memory.putLong(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_IOV, Buffer.memoryAddress(iovMemory));
137 memory.putLong(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_IOVLEN, iovLength);
138 }
139 }
140
141 static int getCmsgData(ByteBuffer memory, ByteBuffer msgControl, int cmsgHdrDataOffset) throws IOException {
142 int memoryPosition = memory.position();
143 long msgControlLen = Native.SIZEOF_SIZE_T == 4
144 ? memory.getInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_CONTROLLEN) & 0xFFFFFFFFL
145 : memory.getLong(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_CONTROLLEN);
146 if (msgControlLen <= 0) {
147 throw new IOException("Received no ancillary data while expecting a SCM_RIGHTS file descriptor");
148 }
149 int msgFlags = memory.getInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_FLAGS);
150 boolean truncated = (msgFlags & Native.MSG_CTRUNC) != 0;
151 return CmsgHdr.readScmRights(msgControl, cmsgHdrDataOffset, truncated);
152 }
153 }