| 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 |  | 
|  | 33 | #ifndef SOCKET_H_ | 
|  | 34 | #define SOCKET_H_ | 
|  | 35 |  | 
| David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 36 | #include <memory> | 
|  | 37 | #include <string> | 
|  | 38 |  | 
| David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 39 | #include <android-base/macros.h> | 
|  | 40 | #include <cutils/sockets.h> | 
|  | 41 |  | 
|  | 42 | // Socket interface to be implemented for each platform. | 
|  | 43 | class Socket { | 
| David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 44 | public: | 
| David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 45 | enum class Protocol { kTcp, kUdp }; | 
|  | 46 |  | 
| David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 47 | // Creates a new client connection. Clients are connected to a specific hostname/port and can | 
|  | 48 | // only send to that destination. | 
|  | 49 | // On failure, |error| is filled (if non-null) and nullptr is returned. | 
| David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 50 | static std::unique_ptr<Socket> NewClient(Protocol protocol, const std::string& hostname, | 
|  | 51 | int port, std::string* error); | 
| David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 52 |  | 
|  | 53 | // Creates a new server bound to local |port|. This is only meant for testing, during normal | 
|  | 54 | // fastboot operation the device acts as the server. | 
| David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 55 | // 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] | 56 | // calls to Send(). | 
| David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 57 | static std::unique_ptr<Socket> NewServer(Protocol protocol, int port); | 
| David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 58 |  | 
| David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 59 | // Destructor closes the socket if it's open. | 
|  | 60 | virtual ~Socket(); | 
| David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 61 |  | 
| David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 62 | // Sends |length| bytes of |data|. For TCP sockets this will continue trying to send until all | 
|  | 63 | // bytes are transmitted. Returns the number of bytes actually sent or -1 on error. | 
| David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 64 | virtual ssize_t Send(const void* data, size_t length) = 0; | 
|  | 65 |  | 
|  | 66 | // Waits up to |timeout_ms| to receive up to |length| bytes of data. |timout_ms| of 0 will | 
|  | 67 | // block forever. Returns the number of bytes received or -1 on error/timeout. On timeout | 
|  | 68 | // errno will be set to EAGAIN or EWOULDBLOCK. | 
|  | 69 | virtual ssize_t Receive(void* data, size_t length, int timeout_ms) = 0; | 
|  | 70 |  | 
| David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 71 | // Calls Receive() until exactly |length| bytes have been received or an error occurs. | 
|  | 72 | virtual ssize_t ReceiveAll(void* data, size_t length, int timeout_ms); | 
|  | 73 |  | 
| David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 74 | // Closes the socket. Returns 0 on success, -1 on error. | 
| David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 75 | virtual int Close(); | 
|  | 76 |  | 
|  | 77 | // Accepts an incoming TCP connection. No effect for UDP sockets. Returns a new Socket | 
|  | 78 | // connected to the client on success, nullptr on failure. | 
|  | 79 | virtual std::unique_ptr<Socket> Accept() { return nullptr; } | 
| David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 80 |  | 
|  | 81 | protected: | 
|  | 82 | // Protected constructor to force factory function use. | 
| David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 83 | Socket(cutils_socket_t sock); | 
| David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 84 |  | 
| David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 85 | // Update the socket receive timeout if necessary. | 
|  | 86 | bool SetReceiveTimeout(int timeout_ms); | 
|  | 87 |  | 
|  | 88 | cutils_socket_t sock_ = INVALID_SOCKET; | 
|  | 89 |  | 
|  | 90 | private: | 
|  | 91 | int receive_timeout_ms_ = 0; | 
|  | 92 |  | 
|  | 93 | DISALLOW_COPY_AND_ASSIGN(Socket); | 
| David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame] | 94 | }; | 
|  | 95 |  | 
|  | 96 | #endif  // SOCKET_H_ |