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, ByteBuffer 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) Buffer.memoryAddress(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, Buffer.memoryAddress(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 prepSendFd(ByteBuffer memory, int fd, ByteBuffer msgControl,
93                             int cmsgHdrDataOffset, ByteBuffer iovMemory, int iovLength) {
94          int memoryPosition = memory.position();
95          long msgControlAddr = Buffer.memoryAddress(msgControl);
96          CmsgHdr.writeScmRights(msgControl, cmsgHdrDataOffset, fd);
97          if (Native.SIZEOF_SIZE_T == 4) {
98              memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_CONTROL, (int) msgControlAddr);
99              memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_CONTROLLEN, Native.MSG_CONTROL_LEN_FOR_FD);
100             memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_IOV, (int) Buffer.memoryAddress(iovMemory));
101             memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_IOVLEN, iovLength);
102         } else {
103             assert Native.SIZEOF_SIZE_T == 8;
104             memory.putLong(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_CONTROL, msgControlAddr);
105             memory.putLong(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_CONTROLLEN, Native.MSG_CONTROL_LEN_FOR_FD);
106             memory.putLong(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_IOV, Buffer.memoryAddress(iovMemory));
107             memory.putLong(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_IOVLEN, iovLength);
108         }
109     }
110 
111     static void prepReadFd(ByteBuffer memory, ByteBuffer msgControl, int cmsgHdrDataOffset,
112                            ByteBuffer iovMemory, int iovLength) {
113         int memoryPosition = memory.position();
114         long msgControlAddr = Buffer.memoryAddress(msgControl);
115         if (Native.SIZEOF_SIZE_T == 4) {
116             memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_CONTROL, (int) msgControlAddr);
117             memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_CONTROLLEN, Native.MSG_CONTROL_LEN_FOR_FD);
118             memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_IOV, (int) Buffer.memoryAddress(iovMemory));
119             memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_IOVLEN, iovLength);
120         } else {
121             assert Native.SIZEOF_SIZE_T == 8;
122             memory.putLong(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_CONTROL, msgControlAddr);
123             memory.putLong(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_CONTROLLEN, Native.MSG_CONTROL_LEN_FOR_FD);
124             memory.putLong(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_IOV, Buffer.memoryAddress(iovMemory));
125             memory.putLong(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_IOVLEN, iovLength);
126         }
127     }
128 
129     static int getCmsgData(ByteBuffer memory, ByteBuffer msgControl, int cmsgHdrDataOffset) {
130         return CmsgHdr.readScmRights(msgControl, cmsgHdrDataOffset);
131     }
132 }