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