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.nio.ByteBuffer;
21  
22  /**
23   * <pre>{@code
24   * struct msghdr {
25   *     void         *msg_name;       // optional address
26   *     socklen_t    msg_namelen;     // size of address
27   *     struct       iovec*msg_iov;   // scatter/gather array
28   *     size_t       msg_iovlen;      // # elements in msg_iov
29   *     void*        msg_control;     // ancillary data, see below
30   *     size_t       msg_controllen;  // ancillary data buffer len
31   *     int          msg_flags;       // flags on received message
32   * };
33   * }</pre>
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              // Set to 0 if we not explicit requested GSO.
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          // No flags (we assume the memory was memset before)
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 }