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, 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              // 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) 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          // No flags (we assume the memory was memset before)
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 }