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