Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <AK/Assertions.h>
28#include <Kernel/Syscall.h>
29#include <errno.h>
30#include <stdio.h>
31#include <sys/socket.h>
32
33extern "C" {
34
35int socket(int domain, int type, int protocol)
36{
37 int rc = syscall(SC_socket, domain, type, protocol);
38 __RETURN_WITH_ERRNO(rc, rc, -1);
39}
40
41int bind(int sockfd, const sockaddr* addr, socklen_t addrlen)
42{
43 int rc = syscall(SC_bind, sockfd, addr, addrlen);
44 __RETURN_WITH_ERRNO(rc, rc, -1);
45}
46
47int listen(int sockfd, int backlog)
48{
49 int rc = syscall(SC_listen, sockfd, backlog);
50 __RETURN_WITH_ERRNO(rc, rc, -1);
51}
52
53int accept(int sockfd, sockaddr* addr, socklen_t* addrlen)
54{
55 int rc = syscall(SC_accept, sockfd, addr, addrlen);
56 __RETURN_WITH_ERRNO(rc, rc, -1);
57}
58
59int connect(int sockfd, const sockaddr* addr, socklen_t addrlen)
60{
61 int rc = syscall(SC_connect, sockfd, addr, addrlen);
62 __RETURN_WITH_ERRNO(rc, rc, -1);
63}
64
65int shutdown(int sockfd, int how)
66{
67 int rc = syscall(SC_shutdown, sockfd, how);
68 __RETURN_WITH_ERRNO(rc, rc, -1);
69}
70
71ssize_t sendto(int sockfd, const void* data, size_t data_length, int flags, const struct sockaddr* addr, socklen_t addr_length)
72{
73 Syscall::SC_sendto_params params { sockfd, { data, data_length }, flags, addr, addr_length };
74 int rc = syscall(SC_sendto, ¶ms);
75 __RETURN_WITH_ERRNO(rc, rc, -1);
76}
77
78ssize_t send(int sockfd, const void* data, size_t data_length, int flags)
79{
80 return sendto(sockfd, data, data_length, flags, nullptr, 0);
81}
82
83ssize_t recvfrom(int sockfd, void* buffer, size_t buffer_length, int flags, struct sockaddr* addr, socklen_t* addr_length)
84{
85 Syscall::SC_recvfrom_params params { sockfd, { buffer, buffer_length }, flags, addr, addr_length };
86 int rc = syscall(SC_recvfrom, ¶ms);
87 __RETURN_WITH_ERRNO(rc, rc, -1);
88}
89
90ssize_t recv(int sockfd, void* buffer, size_t buffer_length, int flags)
91{
92 return recvfrom(sockfd, buffer, buffer_length, flags, nullptr, nullptr);
93}
94
95int getsockopt(int sockfd, int level, int option, void* value, socklen_t* value_size)
96{
97 Syscall::SC_getsockopt_params params { sockfd, level, option, value, value_size };
98 int rc = syscall(SC_getsockopt, ¶ms);
99 __RETURN_WITH_ERRNO(rc, rc, -1);
100}
101
102int setsockopt(int sockfd, int level, int option, const void* value, socklen_t value_size)
103{
104 Syscall::SC_setsockopt_params params { sockfd, level, option, value, value_size };
105 int rc = syscall(SC_setsockopt, ¶ms);
106 __RETURN_WITH_ERRNO(rc, rc, -1);
107}
108
109int getsockname(int sockfd, struct sockaddr* addr, socklen_t* addrlen)
110{
111 int rc = syscall(SC_getsockname, sockfd, addr, addrlen);
112 __RETURN_WITH_ERRNO(rc, rc, -1);
113}
114
115int getpeername(int sockfd, struct sockaddr* addr, socklen_t* addrlen)
116{
117 int rc = syscall(SC_getpeername, sockfd, addr, addrlen);
118 __RETURN_WITH_ERRNO(rc, rc, -1);
119}
120}