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, long iovMemory, int iovLength) {
40 int memoryPosition = memory.position();
41 memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_NAMELEN, 0);
42 if (Native.SIZEOF_SIZE_T == 4) {
43 memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_NAME, 0);
44 memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_IOV, (int) iovMemory);
45 memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_IOVLEN, iovLength);
46 memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_CONTROL, 0);
47 memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_CONTROLLEN, 0);
48 } else {
49 assert Native.SIZEOF_SIZE_T == 8;
50 memory.putLong(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_NAME, 0);
51 memory.putLong(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_IOV, iovMemory);
52 memory.putLong(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_IOVLEN, iovLength);
53 memory.putLong(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_CONTROL, 0);
54 memory.putLong(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_CONTROLLEN, 0);
55 }
56 }
57
58 static void set(ByteBuffer memory, ByteBuffer sockAddrMemory, int addressSize, long iovMemory, int iovLength,
59 ByteBuffer msgControl, int cmsgHdrDataOffset, short segmentSize) {
60 int memoryPosition = memory.position();
61 memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_NAMELEN, addressSize);
62
63 int msgControlLen = 0;
64 long msgControlAddr;
65 if (segmentSize > 0 && msgControl != null && cmsgHdrDataOffset >= 0) {
66 msgControlLen = Native.CMSG_LEN;
67 CmsgHdr.write(msgControl, cmsgHdrDataOffset, Native.CMSG_LEN, Native.SOL_UDP,
68 Native.UDP_SEGMENT, segmentSize);
69 msgControlAddr = Buffer.memoryAddress(msgControl) + msgControl.position();
70 } else {
71
72 msgControlAddr = 0;
73 }
74 long sockAddr = sockAddrMemory == null ? 0 : Buffer.memoryAddress(sockAddrMemory);
75 if (Native.SIZEOF_SIZE_T == 4) {
76 memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_NAME, (int) sockAddr);
77 memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_IOV, (int) iovMemory);
78 memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_IOVLEN, iovLength);
79 memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_CONTROL, (int) msgControlAddr);
80 memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_CONTROLLEN, msgControlLen);
81 } else {
82 assert Native.SIZEOF_SIZE_T == 8;
83 memory.putLong(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_NAME, sockAddr);
84 memory.putLong(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_IOV, iovMemory);
85 memory.putLong(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_IOVLEN, iovLength);
86 memory.putLong(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_CONTROL, msgControlAddr);
87 memory.putLong(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_CONTROLLEN, msgControlLen);
88 }
89
90 }
91
92 static void set(ByteBuffer memory, ByteBuffer sockAddrMemory, int addressSize, ByteBuffer iovMemory, int iovLength,
93 ByteBuffer msgControl, int cmsgHdrDataOffset, short segmentSize) {
94 set(memory, sockAddrMemory, addressSize, Buffer.memoryAddress(iovMemory), iovLength,
95 msgControl, cmsgHdrDataOffset, segmentSize);
96 }
97
98 static void prepSendFd(ByteBuffer memory, int fd, ByteBuffer msgControl,
99 int cmsgHdrDataOffset, ByteBuffer iovMemory, int iovLength) {
100 int memoryPosition = memory.position();
101 long msgControlAddr = Buffer.memoryAddress(msgControl);
102 CmsgHdr.writeScmRights(msgControl, cmsgHdrDataOffset, fd);
103 if (Native.SIZEOF_SIZE_T == 4) {
104 memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_CONTROL, (int) msgControlAddr);
105 memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_CONTROLLEN, Native.MSG_CONTROL_LEN_FOR_FD);
106 memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_IOV, (int) Buffer.memoryAddress(iovMemory));
107 memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_IOVLEN, iovLength);
108 } else {
109 assert Native.SIZEOF_SIZE_T == 8;
110 memory.putLong(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_CONTROL, msgControlAddr);
111 memory.putLong(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_CONTROLLEN, Native.MSG_CONTROL_LEN_FOR_FD);
112 memory.putLong(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_IOV, Buffer.memoryAddress(iovMemory));
113 memory.putLong(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_IOVLEN, iovLength);
114 }
115 }
116
117 static void prepReadFd(ByteBuffer memory, ByteBuffer msgControl, int cmsgHdrDataOffset,
118 ByteBuffer iovMemory, int iovLength) {
119 int memoryPosition = memory.position();
120 long msgControlAddr = Buffer.memoryAddress(msgControl);
121 if (Native.SIZEOF_SIZE_T == 4) {
122 memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_CONTROL, (int) msgControlAddr);
123 memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_CONTROLLEN, Native.MSG_CONTROL_LEN_FOR_FD);
124 memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_IOV, (int) Buffer.memoryAddress(iovMemory));
125 memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_IOVLEN, iovLength);
126 } else {
127 assert Native.SIZEOF_SIZE_T == 8;
128 memory.putLong(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_CONTROL, msgControlAddr);
129 memory.putLong(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_CONTROLLEN, Native.MSG_CONTROL_LEN_FOR_FD);
130 memory.putLong(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_IOV, Buffer.memoryAddress(iovMemory));
131 memory.putLong(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_IOVLEN, iovLength);
132 }
133 }
134
135 static int getCmsgData(ByteBuffer memory, ByteBuffer msgControl, int cmsgHdrDataOffset) {
136 return CmsgHdr.readScmRights(msgControl, cmsgHdrDataOffset);
137 }
138 }