1//===-- Linux implementation of bind --------------------------------------===//
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/bind.h"
10
11#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
12#include "src/__support/common.h"
13
14#include "src/errno/libc_errno.h"
15
16#include <linux/net.h> // For SYS_SOCKET socketcall number.
17#include <sys/syscall.h> // For syscall numbers.
18
19namespace LIBC_NAMESPACE {
20
21LLVM_LIBC_FUNCTION(int, bind,
22 (int domain, const struct sockaddr *address,
23 socklen_t address_len)) {
24#ifdef SYS_socket
25 int ret =
26 LIBC_NAMESPACE::syscall_impl<int>(SYS_bind, ts: domain, ts: address, ts: address_len);
27#elif defined(SYS_socketcall)
28 unsigned long sockcall_args[3] = {static_cast<unsigned long>(domain),
29 reinterpret_cast<unsigned long>(address),
30 static_cast<unsigned long>(address_len)};
31 int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_socketcall, SYS_BIND,
32 sockcall_args);
33#else
34#error "socket and socketcall syscalls unavailable for this platform."
35#endif
36 if (ret < 0) {
37 libc_errno = -ret;
38 return -1;
39 }
40 return ret;
41}
42
43} // namespace LIBC_NAMESPACE
44

source code of libc/src/sys/socket/linux/bind.cpp