xref: /llvm-project/libc/src/sys/socket/linux/recv.cpp (revision cdfdc857cbab0418b7e5116fd4255eb5566588bd)
1f6b4c34dSMichael Jones //===-- Linux implementation of recv --------------------------------------===//
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/recv.h"
10f6b4c34dSMichael Jones 
11ef66936dSMichael Jones #include <linux/net.h>   // For SYS_SOCKET socketcall number.
12ef66936dSMichael Jones #include <sys/syscall.h> // For syscall numbers.
13ef66936dSMichael 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"
19aeb18ebbSMichael Jones #include "src/__support/macros/sanitizer.h"
20f6b4c34dSMichael Jones #include "src/errno/libc_errno.h"
21f6b4c34dSMichael Jones 
22f6b4c34dSMichael Jones namespace LIBC_NAMESPACE_DECL {
23f6b4c34dSMichael Jones 
24f6b4c34dSMichael Jones LLVM_LIBC_FUNCTION(ssize_t, recv,
25*cdfdc857SMichael Jones                    (int sockfd, void *buf, size_t len, int flags)) {
26f6b4c34dSMichael Jones #ifdef SYS_recv
27f6b4c34dSMichael Jones   ssize_t ret =
28ef66936dSMichael Jones       LIBC_NAMESPACE::syscall_impl<ssize_t>(SYS_recv, sockfd, buf, len, flags);
29f6b4c34dSMichael Jones #elif defined(SYS_recvfrom)
30ef66936dSMichael Jones   ssize_t ret = LIBC_NAMESPACE::syscall_impl<ssize_t>(
31ef66936dSMichael Jones       SYS_recvfrom, sockfd, buf, len, flags, nullptr, nullptr);
32f6b4c34dSMichael Jones #elif defined(SYS_socketcall)
33f6b4c34dSMichael Jones   unsigned long sockcall_args[4] = {
34f6b4c34dSMichael Jones       static_cast<unsigned long>(sockfd), reinterpret_cast<unsigned long>(buf),
35f6b4c34dSMichael Jones       static_cast<unsigned long>(len), static_cast<unsigned long>(flags)};
36ef66936dSMichael Jones   ssize_t ret = LIBC_NAMESPACE::syscall_impl<ssize_t>(SYS_socketcall, SYS_RECV,
37f6b4c34dSMichael Jones                                                       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   }
45aeb18ebbSMichael Jones 
46aeb18ebbSMichael Jones   MSAN_UNPOISON(buf, ret);
47aeb18ebbSMichael Jones 
48f6b4c34dSMichael Jones   return ret;
49f6b4c34dSMichael Jones }
50f6b4c34dSMichael Jones 
51f6b4c34dSMichael Jones } // namespace LIBC_NAMESPACE_DECL
52