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.util.internal.PlatformDependent;
19  
20  /**
21   * <pre>{@code
22   * struct msghdr {
23   *     void         *msg_name;       // optional address
24   *     socklen_t    msg_namelen;     // size of address
25   *     struct       iovec*msg_iov;   // scatter/gather array
26   *     size_t       msg_iovlen;      // # elements in msg_iov
27   *     void*        msg_control;     // ancillary data, see below
28   *     size_t       msg_controllen;  // ancillary data buffer len
29   *     int          msg_flags;       // flags on received message
30   * };
31   * }</pre>
32   */
33  final class MsgHdr {
34  
35      private MsgHdr() { }
36  
37      static void write(long memoryAddress, long address, int addressSize,  long iovAddress, int iovLength,
38                        long msgControlAddr, long cmsgHdrDataAddress, short segmentSize) {
39          PlatformDependent.putInt(memoryAddress + Native.MSGHDR_OFFSETOF_MSG_NAMELEN, addressSize);
40  
41          int msgControlLen = 0;
42          if (segmentSize > 0) {
43              msgControlLen = Native.CMSG_LEN;
44              CmsgHdr.write(msgControlAddr, cmsgHdrDataAddress, Native.CMSG_LEN, Native.SOL_UDP,
45                      Native.UDP_SEGMENT, segmentSize);
46          } else {
47              // Set to 0 if we not explicit requested GSO.
48              msgControlAddr = 0;
49          }
50          if (Native.SIZEOF_SIZE_T == 4) {
51              PlatformDependent.putInt(memoryAddress + Native.MSGHDR_OFFSETOF_MSG_NAME, (int) address);
52              PlatformDependent.putInt(memoryAddress + Native.MSGHDR_OFFSETOF_MSG_IOV, (int) iovAddress);
53              PlatformDependent.putInt(memoryAddress + Native.MSGHDR_OFFSETOF_MSG_IOVLEN, iovLength);
54              PlatformDependent.putInt(memoryAddress + Native.MSGHDR_OFFSETOF_MSG_CONTROL, (int) msgControlAddr);
55              PlatformDependent.putInt(memoryAddress + Native.MSGHDR_OFFSETOF_MSG_CONTROLLEN, msgControlLen);
56          } else {
57              assert Native.SIZEOF_SIZE_T == 8;
58              PlatformDependent.putLong(memoryAddress + Native.MSGHDR_OFFSETOF_MSG_NAME, address);
59              PlatformDependent.putLong(memoryAddress + Native.MSGHDR_OFFSETOF_MSG_IOV, iovAddress);
60              PlatformDependent.putLong(memoryAddress + Native.MSGHDR_OFFSETOF_MSG_IOVLEN, iovLength);
61              PlatformDependent.putLong(memoryAddress + Native.MSGHDR_OFFSETOF_MSG_CONTROL, msgControlAddr);
62              PlatformDependent.putLong(memoryAddress + Native.MSGHDR_OFFSETOF_MSG_CONTROLLEN, msgControlLen);
63          }
64          // No flags (we assume the memory was memset before)
65      }
66  }