Finn Williams | 2ed809c | 2020-04-20 21:21:07 +0100 | [diff] [blame] | 1 | // |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 2 | // Copyright © 2020 Arm Ltd and Contributors. All rights reserved. |
Finn Williams | 2ed809c | 2020-04-20 21:21:07 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
Finn Williams | 2ed809c | 2020-04-20 21:21:07 +0100 | [diff] [blame] | 5 | |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 6 | #include <server/include/basePipeServer/ConnectionHandler.hpp> |
Finn Williams | badcc3f | 2020-05-22 14:28:15 +0100 | [diff] [blame] | 7 | |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 8 | #include <string> |
Finn Williams | 2ed809c | 2020-04-20 21:21:07 +0100 | [diff] [blame] | 9 | |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 10 | namespace arm |
Finn Williams | 2ed809c | 2020-04-20 21:21:07 +0100 | [diff] [blame] | 11 | { |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 12 | |
| 13 | namespace pipe |
| 14 | { |
| 15 | |
Finn Williams | 2ed809c | 2020-04-20 21:21:07 +0100 | [diff] [blame] | 16 | ConnectionHandler::ConnectionHandler(const std::string& udsNamespace, const bool setNonBlocking) |
| 17 | { |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 18 | arm::pipe::Initialize(); |
Finn Williams | 2ed809c | 2020-04-20 21:21:07 +0100 | [diff] [blame] | 19 | m_ListeningSocket = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0); |
| 20 | |
| 21 | if (-1 == m_ListeningSocket) |
| 22 | { |
| 23 | throw SocketConnectionException(": Socket construction failed: ", 1, 1); |
| 24 | } |
| 25 | |
| 26 | sockaddr_un udsAddress; |
| 27 | memset(&udsAddress, 0, sizeof(sockaddr_un)); |
| 28 | // We've set the first element of sun_path to be 0, skip over it and copy the namespace after it. |
| 29 | memcpy(udsAddress.sun_path + 1, udsNamespace.c_str(), strlen(udsNamespace.c_str())); |
| 30 | udsAddress.sun_family = AF_UNIX; |
| 31 | |
| 32 | // Bind the socket to the UDS namespace. |
| 33 | if (-1 == bind(m_ListeningSocket, reinterpret_cast<const sockaddr*>(&udsAddress), sizeof(sockaddr_un))) |
| 34 | { |
| 35 | throw SocketConnectionException(": Binding on socket failed: ", m_ListeningSocket, errno); |
| 36 | } |
| 37 | // Listen for connections. |
| 38 | if (-1 == listen(m_ListeningSocket, 1)) |
| 39 | { |
| 40 | throw SocketConnectionException(": Listen call on socket failed: ", m_ListeningSocket, errno); |
| 41 | } |
| 42 | |
| 43 | if (setNonBlocking) |
| 44 | { |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 45 | arm::pipe::SetNonBlocking(m_ListeningSocket); |
Finn Williams | 2ed809c | 2020-04-20 21:21:07 +0100 | [diff] [blame] | 46 | } |
| 47 | } |
| 48 | |
| 49 | std::unique_ptr<BasePipeServer> ConnectionHandler::GetNewBasePipeServer(const bool echoPackets) |
| 50 | { |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 51 | arm::pipe::Socket clientConnection = arm::pipe::Accept(m_ListeningSocket, nullptr, nullptr, SOCK_CLOEXEC); |
Finn Williams | 2ed809c | 2020-04-20 21:21:07 +0100 | [diff] [blame] | 52 | if (clientConnection < 1) |
| 53 | { |
| 54 | return nullptr; |
| 55 | } |
| 56 | return std::make_unique<BasePipeServer>(clientConnection, echoPackets); |
| 57 | } |
| 58 | |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 59 | } // namespace pipe |
| 60 | } // namespace arm |