xref: /aosp_15_r20/external/llvm-libc/src/sys/socket/linux/sendto.cpp (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
1 //===-- Linux implementation of sendto ------------------------------------===//
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/sendto.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, sendto,
24                    (int sockfd, const void *buf, size_t len, int flags,
25                     const struct sockaddr *dest_addr, socklen_t addrlen)) {
26 #ifdef SYS_sendto
27   ssize_t ret = LIBC_NAMESPACE::syscall_impl<ssize_t>(
28       SYS_sendto, sockfd, buf, len, flags, dest_addr, addrlen);
29 #elif defined(SYS_socketcall)
30   unsigned long sockcall_args[6] = {static_cast<unsigned long>(sockfd),
31                                     reinterpret_cast<unsigned long>(buf),
32                                     static_cast<unsigned long>(len),
33                                     static_cast<unsigned long>(flags),
34                                     reinterpret_cast<unsigned long>(dest_addr),
35                                     static_cast<unsigned long>(addrlen)};
36   ssize_t ret = LIBC_NAMESPACE::syscall_impl<ssize_t>(
37       SYS_socketcall, SYS_SENDTO, sockcall_args);
38 #else
39 #error "socket and socketcall syscalls unavailable for this platform."
40 #endif
41   if (ret < 0) {
42     libc_errno = static_cast<int>(-ret);
43     return -1;
44   }
45   return ret;
46 }
47 
48 } // namespace LIBC_NAMESPACE_DECL
49