David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 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 |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
| 28 | |
David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 29 | // This file provides a class interface for cross-platform socket functionality. The main fastboot |
David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 30 | // engine should not be using this interface directly, but instead should use a higher-level |
David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 31 | // interface that enforces the fastboot protocol. |
David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 32 | |
Elliott Hughes | 6ebec93 | 2018-04-10 14:22:13 -0700 | [diff] [blame] | 33 | #pragma once |
David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 34 | |
David Pursell | b34e4a0 | 2016-02-01 09:42:09 -0800 | [diff] [blame] | 35 | #include <functional> |
David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 36 | #include <memory> |
| 37 | #include <string> |
David Pursell | b34e4a0 | 2016-02-01 09:42:09 -0800 | [diff] [blame] | 38 | #include <utility> |
| 39 | #include <vector> |
David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 40 | |
David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 41 | #include <android-base/macros.h> |
| 42 | #include <cutils/sockets.h> |
David Pursell | b34e4a0 | 2016-02-01 09:42:09 -0800 | [diff] [blame] | 43 | #include <gtest/gtest_prod.h> |
David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 44 | |
| 45 | // Socket interface to be implemented for each platform. |
| 46 | class Socket { |
David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 47 | public: |
David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 48 | enum class Protocol { kTcp, kUdp }; |
| 49 | |
David Pursell | c3a4669 | 2016-01-29 08:10:50 -0800 | [diff] [blame] | 50 | // Returns the socket error message. This must be called immediately after a socket failure |
| 51 | // before any other system calls are made. |
| 52 | static std::string GetErrorMessage(); |
| 53 | |
David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 54 | // Creates a new client connection. Clients are connected to a specific hostname/port and can |
| 55 | // only send to that destination. |
| 56 | // On failure, |error| is filled (if non-null) and nullptr is returned. |
David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 57 | static std::unique_ptr<Socket> NewClient(Protocol protocol, const std::string& hostname, |
| 58 | int port, std::string* error); |
David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 59 | |
| 60 | // Creates a new server bound to local |port|. This is only meant for testing, during normal |
| 61 | // fastboot operation the device acts as the server. |
David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 62 | // A UDP server saves sender addresses in Receive(), and uses the most recent address during |
David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 63 | // calls to Send(). |
David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 64 | static std::unique_ptr<Socket> NewServer(Protocol protocol, int port); |
David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 65 | |
David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 66 | // Destructor closes the socket if it's open. |
| 67 | virtual ~Socket(); |
David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 68 | |
David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 69 | // Sends |length| bytes of |data|. For TCP sockets this will continue trying to send until all |
David Pursell | b34e4a0 | 2016-02-01 09:42:09 -0800 | [diff] [blame] | 70 | // bytes are transmitted. Returns true on success. |
| 71 | virtual bool Send(const void* data, size_t length) = 0; |
| 72 | |
| 73 | // Sends |buffers| using multi-buffer write, which can be significantly faster than making |
| 74 | // multiple calls. For UDP sockets |buffers| are all combined into a single datagram; for |
| 75 | // TCP sockets this will continue sending until all buffers are fully transmitted. Returns true |
| 76 | // on success. |
| 77 | // |
| 78 | // Note: This is non-functional for UDP server Sockets because it's not currently needed and |
| 79 | // would require an additional sendto() variation of multi-buffer write. |
| 80 | virtual bool Send(std::vector<cutils_socket_buffer_t> buffers) = 0; |
David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 81 | |
| 82 | // Waits up to |timeout_ms| to receive up to |length| bytes of data. |timout_ms| of 0 will |
David Pursell | c742a7f | 2016-02-04 15:21:58 -0800 | [diff] [blame] | 83 | // block forever. Returns the number of bytes received or -1 on error/timeout; see |
| 84 | // ReceiveTimedOut() to distinguish between the two. |
David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 85 | virtual ssize_t Receive(void* data, size_t length, int timeout_ms) = 0; |
| 86 | |
David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 87 | // Calls Receive() until exactly |length| bytes have been received or an error occurs. |
| 88 | virtual ssize_t ReceiveAll(void* data, size_t length, int timeout_ms); |
| 89 | |
David Pursell | c742a7f | 2016-02-04 15:21:58 -0800 | [diff] [blame] | 90 | // Returns true if the last Receive() call timed out normally and can be retried; fatal errors |
| 91 | // or successful reads will return false. |
| 92 | bool ReceiveTimedOut() { return receive_timed_out_; } |
| 93 | |
David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 94 | // Closes the socket. Returns 0 on success, -1 on error. |
David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 95 | virtual int Close(); |
| 96 | |
| 97 | // Accepts an incoming TCP connection. No effect for UDP sockets. Returns a new Socket |
| 98 | // connected to the client on success, nullptr on failure. |
| 99 | virtual std::unique_ptr<Socket> Accept() { return nullptr; } |
David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 100 | |
David Pursell | c3a4669 | 2016-01-29 08:10:50 -0800 | [diff] [blame] | 101 | // Returns the local port the Socket is bound to or -1 on error. |
| 102 | int GetLocalPort(); |
| 103 | |
David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 104 | protected: |
| 105 | // Protected constructor to force factory function use. |
Chih-Hung Hsieh | 034c475 | 2016-07-12 13:50:44 -0700 | [diff] [blame] | 106 | explicit Socket(cutils_socket_t sock); |
David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 107 | |
David Pursell | c742a7f | 2016-02-04 15:21:58 -0800 | [diff] [blame] | 108 | // Blocks up to |timeout_ms| until a read is possible on |sock_|, and sets |receive_timed_out_| |
| 109 | // as appropriate to help distinguish between normal timeouts and fatal errors. Returns true if |
| 110 | // a subsequent recv() on |sock_| will complete without blocking or if |timeout_ms| <= 0. |
| 111 | bool WaitForRecv(int timeout_ms); |
David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 112 | |
| 113 | cutils_socket_t sock_ = INVALID_SOCKET; |
David Pursell | c742a7f | 2016-02-04 15:21:58 -0800 | [diff] [blame] | 114 | bool receive_timed_out_ = false; |
David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 115 | |
David Pursell | b34e4a0 | 2016-02-01 09:42:09 -0800 | [diff] [blame] | 116 | // Non-class functions we want to override during tests to verify functionality. Implementation |
| 117 | // should call this rather than using socket_send_buffers() directly. |
| 118 | std::function<ssize_t(cutils_socket_t, cutils_socket_buffer_t*, size_t)> |
| 119 | socket_send_buffers_function_ = &socket_send_buffers; |
| 120 | |
David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 121 | private: |
David Pursell | b34e4a0 | 2016-02-01 09:42:09 -0800 | [diff] [blame] | 122 | FRIEND_TEST(SocketTest, TestTcpSendBuffers); |
| 123 | FRIEND_TEST(SocketTest, TestUdpSendBuffers); |
| 124 | |
David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 125 | DISALLOW_COPY_AND_ASSIGN(Socket); |
David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 126 | }; |