xref: /aosp_15_r20/external/llvm-libc/src/sys/socket/linux/send.cpp (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
1 //===-- Linux implementation of send --------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "src/sys/socket/send.h"
10 
11 #include <linux/net.h>   // For SYS_SOCKET socketcall number.
12 #include <sys/syscall.h> // For syscall numbers.
13 
14 #include "hdr/types/socklen_t.h"
15 #include "hdr/types/ssize_t.h"
16 #include "hdr/types/struct_sockaddr.h"
17 #include "src/__support/OSUtil/syscall.h" // For internal syscall function.
18 #include "src/__support/common.h"
19 #include "src/errno/libc_errno.h"
20 
21 namespace LIBC_NAMESPACE_DECL {
22 
23 LLVM_LIBC_FUNCTION(ssize_t, send,
24                    (int sockfd, const void *buf, size_t len, int flags)) {
25 #ifdef SYS_send
26   ssize_t ret =
27       LIBC_NAMESPACE::syscall_impl<ssize_t>(SYS_send, sockfd, buf, len, flags);
28 #elif defined(SYS_sendto)
29   ssize_t ret = LIBC_NAMESPACE::syscall_impl<ssize_t>(SYS_sendto, sockfd, buf,
30                                                       len, flags, nullptr, 0);
31 #elif defined(SYS_socketcall)
32   unsigned long sockcall_args[4] = {
33       static_cast<unsigned long>(sockfd), reinterpret_cast<unsigned long>(buf),
34       static_cast<unsigned long>(len), static_cast<unsigned long>(flags)};
35   ssize_t ret = LIBC_NAMESPACE::syscall_impl<ssize_t>(SYS_socketcall, SYS_SEND,
36                                                       sockcall_args);
37 #else
38 #error "socket and socketcall syscalls unavailable for this platform."
39 #endif
40   if (ret < 0) {
41     libc_errno = static_cast<int>(-ret);
42     return -1;
43   }
44   return ret;
45 }
46 
47 } // namespace LIBC_NAMESPACE_DECL
48