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