| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-05 20:47:30 +10:00
										 |  |  | #include <AK/IPv4Address.h>
 | 
					
						
							|  |  |  | #include <AK/Types.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-06 15:04:03 +01:00
										 |  |  | #include <LibCore/Notifier.h>
 | 
					
						
							|  |  |  | #include <LibCore/TCPServer.h>
 | 
					
						
							|  |  |  | #include <LibCore/TCPSocket.h>
 | 
					
						
							| 
									
										
										
										
											2021-05-16 12:13:19 +02:00
										 |  |  | #include <fcntl.h>
 | 
					
						
							| 
									
										
										
										
											2019-08-05 20:47:30 +10:00
										 |  |  | #include <stdio.h>
 | 
					
						
							|  |  |  | #include <sys/socket.h>
 | 
					
						
							| 
									
										
										
										
											2021-03-12 17:29:37 +01:00
										 |  |  | #include <unistd.h>
 | 
					
						
							| 
									
										
										
										
											2019-08-05 20:47:30 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-23 15:31:30 +02:00
										 |  |  | #ifndef SOCK_NONBLOCK
 | 
					
						
							|  |  |  | #    include <sys/ioctl.h>
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  | namespace Core { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TCPServer::TCPServer(Object* parent) | 
					
						
							|  |  |  |     : Object(parent) | 
					
						
							| 
									
										
										
										
											2019-08-05 20:47:30 +10:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-05-23 15:31:30 +02:00
										 |  |  | #ifdef SOCK_NONBLOCK
 | 
					
						
							| 
									
										
										
										
											2019-08-05 20:47:30 +10:00
										 |  |  |     m_fd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0); | 
					
						
							| 
									
										
										
										
											2020-05-23 15:31:30 +02:00
										 |  |  | #else
 | 
					
						
							|  |  |  |     m_fd = socket(AF_INET, SOCK_STREAM, 0); | 
					
						
							|  |  |  |     int option = 1; | 
					
						
							|  |  |  |     ioctl(m_fd, FIONBIO, &option); | 
					
						
							|  |  |  |     fcntl(m_fd, F_SETFD, FD_CLOEXEC); | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2021-02-23 20:42:32 +01:00
										 |  |  |     VERIFY(m_fd >= 0); | 
					
						
							| 
									
										
										
										
											2019-08-05 20:47:30 +10:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  | TCPServer::~TCPServer() | 
					
						
							| 
									
										
										
										
											2019-08-05 20:47:30 +10:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-08-08 15:26:10 +03:00
										 |  |  |     ::close(m_fd); | 
					
						
							| 
									
										
										
										
											2019-08-05 20:47:30 +10:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  | bool TCPServer::listen(const IPv4Address& address, u16 port) | 
					
						
							| 
									
										
										
										
											2019-08-05 20:47:30 +10:00
										 |  |  | { | 
					
						
							|  |  |  |     if (m_listening) | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  |     auto socket_address = SocketAddress(address, port); | 
					
						
							| 
									
										
										
										
											2019-08-05 20:47:30 +10:00
										 |  |  |     auto in = socket_address.to_sockaddr_in(); | 
					
						
							| 
									
										
										
										
											2020-09-28 21:59:13 +02:00
										 |  |  |     if (::bind(m_fd, (const sockaddr*)&in, sizeof(in)) < 0) { | 
					
						
							|  |  |  |         perror("TCPServer::listen: bind"); | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-08-05 20:47:30 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-28 21:59:13 +02:00
										 |  |  |     if (::listen(m_fd, 5) < 0) { | 
					
						
							|  |  |  |         perror("TCPServer::listen: listen"); | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-08-05 20:47:30 +10:00
										 |  |  |     m_listening = true; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-10 14:15:56 +01:00
										 |  |  |     m_notifier = Notifier::construct(m_fd, Notifier::Event::Read, this); | 
					
						
							| 
									
										
										
										
											2019-08-05 20:47:30 +10:00
										 |  |  |     m_notifier->on_ready_to_read = [this] { | 
					
						
							|  |  |  |         if (on_ready_to_accept) | 
					
						
							|  |  |  |             on_ready_to_accept(); | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-25 21:40:56 +02:00
										 |  |  | void TCPServer::set_blocking(bool blocking) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int flags = fcntl(m_fd, F_GETFL, 0); | 
					
						
							|  |  |  |     VERIFY(flags >= 0); | 
					
						
							|  |  |  |     if (blocking) | 
					
						
							|  |  |  |         flags = fcntl(m_fd, F_SETFL, flags & ~O_NONBLOCK); | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |         flags = fcntl(m_fd, F_SETFL, flags | O_NONBLOCK); | 
					
						
							|  |  |  |     VERIFY(flags == 0); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  | RefPtr<TCPSocket> TCPServer::accept() | 
					
						
							| 
									
										
										
										
											2019-08-05 20:47:30 +10:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-02-23 20:42:32 +01:00
										 |  |  |     VERIFY(m_listening); | 
					
						
							| 
									
										
										
										
											2019-08-05 20:47:30 +10:00
										 |  |  |     sockaddr_in in; | 
					
						
							|  |  |  |     socklen_t in_size = sizeof(in); | 
					
						
							| 
									
										
										
										
											2021-05-16 12:13:19 +02:00
										 |  |  | #ifndef AK_OS_MACOS
 | 
					
						
							|  |  |  |     int accepted_fd = ::accept4(m_fd, (sockaddr*)&in, &in_size, SOCK_NONBLOCK | SOCK_CLOEXEC); | 
					
						
							|  |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2019-08-05 20:47:30 +10:00
										 |  |  |     int accepted_fd = ::accept(m_fd, (sockaddr*)&in, &in_size); | 
					
						
							| 
									
										
										
										
											2021-05-16 12:13:19 +02:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-05 20:47:30 +10:00
										 |  |  |     if (accepted_fd < 0) { | 
					
						
							|  |  |  |         perror("accept"); | 
					
						
							|  |  |  |         return nullptr; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-16 12:13:19 +02:00
										 |  |  | #ifdef AK_OS_MACOS
 | 
					
						
							|  |  |  |     int option = 1; | 
					
						
							|  |  |  |     (void)ioctl(m_fd, FIONBIO, &option); | 
					
						
							|  |  |  |     (void)fcntl(accepted_fd, F_SETFD, FD_CLOEXEC); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  |     return TCPSocket::construct(accepted_fd); | 
					
						
							| 
									
										
										
										
											2019-08-05 20:47:30 +10:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-10-07 19:07:24 +11:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  | Optional<IPv4Address> TCPServer::local_address() const | 
					
						
							| 
									
										
										
										
											2019-10-07 19:07:24 +11:00
										 |  |  | { | 
					
						
							|  |  |  |     if (m_fd == -1) | 
					
						
							|  |  |  |         return {}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     sockaddr_in address; | 
					
						
							|  |  |  |     socklen_t len = sizeof(address); | 
					
						
							|  |  |  |     if (getsockname(m_fd, (sockaddr*)&address, &len) != 0) | 
					
						
							| 
									
										
										
										
											2019-11-04 13:07:41 +01:00
										 |  |  |         return {}; | 
					
						
							| 
									
										
										
										
											2019-10-07 19:07:24 +11:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return IPv4Address(address.sin_addr.s_addr); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  | Optional<u16> TCPServer::local_port() const | 
					
						
							| 
									
										
										
										
											2019-10-07 19:07:24 +11:00
										 |  |  | { | 
					
						
							|  |  |  |     if (m_fd == -1) | 
					
						
							| 
									
										
										
										
											2019-11-04 13:07:41 +01:00
										 |  |  |         return {}; | 
					
						
							| 
									
										
										
										
											2019-10-07 19:07:24 +11:00
										 |  |  | 
 | 
					
						
							|  |  |  |     sockaddr_in address; | 
					
						
							|  |  |  |     socklen_t len = sizeof(address); | 
					
						
							|  |  |  |     if (getsockname(m_fd, (sockaddr*)&address, &len) != 0) | 
					
						
							| 
									
										
										
										
											2019-11-04 13:07:41 +01:00
										 |  |  |         return {}; | 
					
						
							| 
									
										
										
										
											2019-10-07 19:07:24 +11:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return ntohs(address.sin_port); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | } |