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, ByteBuffer sockAddrMemory, int addressSize, ByteBuffer iovMemory, int iovLength,
40                      ByteBuffer msgControl, int cmsgHdrDataOffset, short segmentSize) {
41          int memoryPosition = memory.position();
42          memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_NAMELEN, addressSize);
43  
44          int msgControlLen = 0;
45          long msgControlAddr;
46          if (segmentSize > 0) {
47              msgControlLen = Native.CMSG_LEN;
48              CmsgHdr.write(msgControl, cmsgHdrDataOffset, Native.CMSG_LEN, Native.SOL_UDP,
49                      Native.UDP_SEGMENT, segmentSize);
50              msgControlAddr = Buffer.memoryAddress(msgControl) + msgControl.position();
51          } else {
52              // Set to 0 if we not explicit requested GSO.
53              msgControlAddr = 0;
54          }
55          if (Native.SIZEOF_SIZE_T == 4) {
56              memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_NAME, (int) Buffer.memoryAddress(sockAddrMemory));
57              memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_IOV, (int) Buffer.memoryAddress(iovMemory));
58              memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_IOVLEN, iovLength);
59              memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_CONTROL, (int) msgControlAddr);
60              memory.putInt(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_CONTROLLEN, msgControlLen);
61          } else {
62              assert Native.SIZEOF_SIZE_T == 8;
63              memory.putLong(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_NAME, Buffer.memoryAddress(sockAddrMemory));
64              memory.putLong(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_IOV, Buffer.memoryAddress(iovMemory));
65              memory.putLong(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_IOVLEN, iovLength);
66              memory.putLong(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_CONTROL, msgControlAddr);
67              memory.putLong(memoryPosition + Native.MSGHDR_OFFSETOF_MSG_CONTROLLEN, msgControlLen);
68          }
69          // No flags (we assume the memory was memset before)
70      }
71  }