View Javadoc
1   /*
2    * Copyright 2024 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
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   * <pre>{@code
25   * struct msghdr {
26   *     void         *msg_name;       // optional address
27   *     socklen_t    msg_namelen;     // size of address
28   *     struct       iovec*msg_iov;   // scatter/gather array
29   *     size_t       msg_iovlen;      // # elements in msg_iov
30   *     void*        msg_control;     // ancillary data, see below
31   *     size_t       msg_controllen;  // ancillary data buffer len
32   *     int          msg_flags;       // flags on received message
33   * };
34   * }</pre>
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              // Set to 0 if we not explicit requested GSO.
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          // No flags (we assume the memory was memset before)
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 }