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#pragma once
28
29#include <AK/HashTable.h>
30#include <AK/NonnullRefPtrVector.h>
31#include <AK/RefCounted.h>
32#include <AK/RefPtr.h>
33#include <Kernel/FileSystem/File.h>
34#include <Kernel/KResult.h>
35#include <Kernel/Lock.h>
36#include <Kernel/Net/NetworkAdapter.h>
37#include <Kernel/UnixTypes.h>
38
39namespace Kernel {
40
41enum class ShouldBlock {
42 No = 0,
43 Yes = 1
44};
45
46class FileDescription;
47
48class Socket : public File {
49public:
50 static KResultOr<NonnullRefPtr<Socket>> create(int domain, int type, int protocol);
51 virtual ~Socket() override;
52
53 int domain() const { return m_domain; }
54 int type() const { return m_type; }
55 int protocol() const { return m_protocol; }
56
57 bool is_shut_down_for_writing() const { return m_shut_down_for_writing; }
58 bool is_shut_down_for_reading() const { return m_shut_down_for_reading; }
59
60 enum class SetupState {
61 Unstarted, // we haven't tried to set the socket up yet
62 InProgress, // we're in the process of setting things up - for TCP maybe we've sent a SYN packet
63 Completed, // the setup process is complete, but not necessarily successful
64 };
65
66 enum class Role : u8 {
67 None,
68 Listener,
69 Accepted,
70 Connected,
71 Connecting
72 };
73
74 static const char* to_string(SetupState setup_state)
75 {
76 switch (setup_state) {
77 case SetupState::Unstarted:
78 return "Unstarted";
79 case SetupState::InProgress:
80 return "InProgress";
81 case SetupState::Completed:
82 return "Completed";
83 default:
84 return "None";
85 }
86 }
87
88 SetupState setup_state() const { return m_setup_state; }
89 void set_setup_state(SetupState setup_state);
90
91 virtual Role role(const FileDescription&) const { return m_role; }
92
93 bool is_connected() const { return m_connected; }
94 void set_connected(bool connected) { m_connected = connected; }
95
96 bool can_accept() const { return !m_pending.is_empty(); }
97 RefPtr<Socket> accept();
98
99 KResult shutdown(int how);
100
101 virtual KResult bind(const sockaddr*, socklen_t) = 0;
102 virtual KResult connect(FileDescription&, const sockaddr*, socklen_t, ShouldBlock) = 0;
103 virtual KResult listen(size_t) = 0;
104 virtual void get_local_address(sockaddr*, socklen_t*) = 0;
105 virtual void get_peer_address(sockaddr*, socklen_t*) = 0;
106 virtual bool is_local() const { return false; }
107 virtual bool is_ipv4() const { return false; }
108 virtual void attach(FileDescription&) = 0;
109 virtual void detach(FileDescription&) = 0;
110 virtual ssize_t sendto(FileDescription&, const void*, size_t, int flags, const sockaddr*, socklen_t) = 0;
111 virtual ssize_t recvfrom(FileDescription&, void*, size_t, int flags, sockaddr*, socklen_t*) = 0;
112
113 virtual KResult setsockopt(int level, int option, const void*, socklen_t);
114 virtual KResult getsockopt(FileDescription&, int level, int option, void*, socklen_t*);
115
116 pid_t origin_pid() const { return m_origin.pid; }
117 uid_t origin_uid() const { return m_origin.uid; }
118 gid_t origin_gid() const { return m_origin.gid; }
119 pid_t acceptor_pid() const { return m_acceptor.pid; }
120 uid_t acceptor_uid() const { return m_acceptor.uid; }
121 gid_t acceptor_gid() const { return m_acceptor.gid; }
122 const RefPtr<NetworkAdapter> bound_interface() const { return m_bound_interface; }
123
124 Lock& lock() { return m_lock; }
125
126 // ^File
127 virtual ssize_t read(FileDescription&, u8*, ssize_t) override final;
128 virtual ssize_t write(FileDescription&, const u8*, ssize_t) override final;
129 virtual String absolute_path(const FileDescription&) const override = 0;
130
131 bool has_receive_timeout() const { return m_receive_timeout.tv_sec || m_receive_timeout.tv_usec; }
132 const timeval& receive_timeout() const { return m_receive_timeout; }
133
134 bool has_send_timeout() const { return m_send_timeout.tv_sec || m_send_timeout.tv_usec; }
135 const timeval& send_timeout() const { return m_send_timeout; }
136
137protected:
138 Socket(int domain, int type, int protocol);
139
140 KResult queue_connection_from(NonnullRefPtr<Socket>);
141
142 size_t backlog() const { return m_backlog; }
143 void set_backlog(size_t backlog) { m_backlog = backlog; }
144
145 virtual const char* class_name() const override { return "Socket"; }
146
147 virtual void shut_down_for_reading() {}
148 virtual void shut_down_for_writing() {}
149
150 Role m_role { Role::None };
151
152protected:
153 ucred m_origin { 0, 0, 0 };
154 ucred m_acceptor { 0, 0, 0 };
155
156private:
157 virtual bool is_socket() const final { return true; }
158
159 Lock m_lock { "Socket" };
160
161 int m_domain { 0 };
162 int m_type { 0 };
163 int m_protocol { 0 };
164 size_t m_backlog { 0 };
165 SetupState m_setup_state { SetupState::Unstarted };
166 bool m_connected { false };
167 bool m_shut_down_for_reading { false };
168 bool m_shut_down_for_writing { false };
169
170 RefPtr<NetworkAdapter> m_bound_interface { nullptr };
171
172 timeval m_receive_timeout { 0, 0 };
173 timeval m_send_timeout { 0, 0 };
174
175 NonnullRefPtrVector<Socket> m_pending;
176};
177
178template <typename SocketType>
179class SocketHandle {
180public:
181 SocketHandle() {}
182
183 SocketHandle(NonnullRefPtr<SocketType>&& socket)
184 : m_socket(move(socket))
185 {
186 if (m_socket)
187 m_socket->lock().lock();
188 }
189
190 SocketHandle(SocketHandle&& other)
191 : m_socket(move(other.m_socket))
192 {
193 }
194
195 ~SocketHandle()
196 {
197 if (m_socket)
198 m_socket->lock().unlock();
199 }
200
201 SocketHandle(const SocketHandle&) = delete;
202 SocketHandle& operator=(const SocketHandle&) = delete;
203
204 operator bool() const { return m_socket; }
205
206 SocketType* operator->() { return &socket(); }
207 const SocketType* operator->() const { return &socket(); }
208
209 SocketType& socket() { return *m_socket; }
210 const SocketType& socket() const { return *m_socket; }
211
212private:
213 RefPtr<SocketType> m_socket;
214};
215
216}