blob: c0bd7c96cfc177a4b195d79e936a2c39136a1534 [file] [log] [blame]
David Pursell815c7be2015-12-09 17:09:54 -08001/*
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 Pursell572bce22016-01-15 14:19:56 -080029// This file provides a class interface for cross-platform socket functionality. The main fastboot
David Pursell815c7be2015-12-09 17:09:54 -080030// engine should not be using this interface directly, but instead should use a higher-level
David Pursell572bce22016-01-15 14:19:56 -080031// interface that enforces the fastboot protocol.
David Pursell815c7be2015-12-09 17:09:54 -080032
33#ifndef SOCKET_H_
34#define SOCKET_H_
35
David Pursellb34e4a02016-02-01 09:42:09 -080036#include <functional>
David Pursell815c7be2015-12-09 17:09:54 -080037#include <memory>
38#include <string>
David Pursellb34e4a02016-02-01 09:42:09 -080039#include <utility>
40#include <vector>
David Pursell815c7be2015-12-09 17:09:54 -080041
David Pursell572bce22016-01-15 14:19:56 -080042#include <android-base/macros.h>
43#include <cutils/sockets.h>
David Pursellb34e4a02016-02-01 09:42:09 -080044#include <gtest/gtest_prod.h>
David Pursell572bce22016-01-15 14:19:56 -080045
46// Socket interface to be implemented for each platform.
47class Socket {
David Pursell815c7be2015-12-09 17:09:54 -080048 public:
David Pursell572bce22016-01-15 14:19:56 -080049 enum class Protocol { kTcp, kUdp };
50
David Pursellc3a46692016-01-29 08:10:50 -080051 // Returns the socket error message. This must be called immediately after a socket failure
52 // before any other system calls are made.
53 static std::string GetErrorMessage();
54
David Pursell815c7be2015-12-09 17:09:54 -080055 // Creates a new client connection. Clients are connected to a specific hostname/port and can
56 // only send to that destination.
57 // On failure, |error| is filled (if non-null) and nullptr is returned.
David Pursell572bce22016-01-15 14:19:56 -080058 static std::unique_ptr<Socket> NewClient(Protocol protocol, const std::string& hostname,
59 int port, std::string* error);
David Pursell815c7be2015-12-09 17:09:54 -080060
61 // Creates a new server bound to local |port|. This is only meant for testing, during normal
62 // fastboot operation the device acts as the server.
David Pursell572bce22016-01-15 14:19:56 -080063 // A UDP server saves sender addresses in Receive(), and uses the most recent address during
David Pursell815c7be2015-12-09 17:09:54 -080064 // calls to Send().
David Pursell572bce22016-01-15 14:19:56 -080065 static std::unique_ptr<Socket> NewServer(Protocol protocol, int port);
David Pursell815c7be2015-12-09 17:09:54 -080066
David Pursell572bce22016-01-15 14:19:56 -080067 // Destructor closes the socket if it's open.
68 virtual ~Socket();
David Pursell815c7be2015-12-09 17:09:54 -080069
David Pursell572bce22016-01-15 14:19:56 -080070 // Sends |length| bytes of |data|. For TCP sockets this will continue trying to send until all
David Pursellb34e4a02016-02-01 09:42:09 -080071 // bytes are transmitted. Returns true on success.
72 virtual bool Send(const void* data, size_t length) = 0;
73
74 // Sends |buffers| using multi-buffer write, which can be significantly faster than making
75 // multiple calls. For UDP sockets |buffers| are all combined into a single datagram; for
76 // TCP sockets this will continue sending until all buffers are fully transmitted. Returns true
77 // on success.
78 //
79 // Note: This is non-functional for UDP server Sockets because it's not currently needed and
80 // would require an additional sendto() variation of multi-buffer write.
81 virtual bool Send(std::vector<cutils_socket_buffer_t> buffers) = 0;
David Pursell815c7be2015-12-09 17:09:54 -080082
83 // Waits up to |timeout_ms| to receive up to |length| bytes of data. |timout_ms| of 0 will
84 // block forever. Returns the number of bytes received or -1 on error/timeout. On timeout
85 // errno will be set to EAGAIN or EWOULDBLOCK.
86 virtual ssize_t Receive(void* data, size_t length, int timeout_ms) = 0;
87
David Pursell572bce22016-01-15 14:19:56 -080088 // Calls Receive() until exactly |length| bytes have been received or an error occurs.
89 virtual ssize_t ReceiveAll(void* data, size_t length, int timeout_ms);
90
David Pursell815c7be2015-12-09 17:09:54 -080091 // Closes the socket. Returns 0 on success, -1 on error.
David Pursell572bce22016-01-15 14:19:56 -080092 virtual int Close();
93
94 // Accepts an incoming TCP connection. No effect for UDP sockets. Returns a new Socket
95 // connected to the client on success, nullptr on failure.
96 virtual std::unique_ptr<Socket> Accept() { return nullptr; }
David Pursell815c7be2015-12-09 17:09:54 -080097
David Pursellc3a46692016-01-29 08:10:50 -080098 // Returns the local port the Socket is bound to or -1 on error.
99 int GetLocalPort();
100
David Pursell815c7be2015-12-09 17:09:54 -0800101 protected:
102 // Protected constructor to force factory function use.
David Pursell572bce22016-01-15 14:19:56 -0800103 Socket(cutils_socket_t sock);
David Pursell815c7be2015-12-09 17:09:54 -0800104
David Pursell572bce22016-01-15 14:19:56 -0800105 // Update the socket receive timeout if necessary.
106 bool SetReceiveTimeout(int timeout_ms);
107
108 cutils_socket_t sock_ = INVALID_SOCKET;
109
David Pursellb34e4a02016-02-01 09:42:09 -0800110 // Non-class functions we want to override during tests to verify functionality. Implementation
111 // should call this rather than using socket_send_buffers() directly.
112 std::function<ssize_t(cutils_socket_t, cutils_socket_buffer_t*, size_t)>
113 socket_send_buffers_function_ = &socket_send_buffers;
114
David Pursell572bce22016-01-15 14:19:56 -0800115 private:
116 int receive_timeout_ms_ = 0;
117
David Pursellb34e4a02016-02-01 09:42:09 -0800118 FRIEND_TEST(SocketTest, TestTcpSendBuffers);
119 FRIEND_TEST(SocketTest, TestUdpSendBuffers);
120
David Pursell572bce22016-01-15 14:19:56 -0800121 DISALLOW_COPY_AND_ASSIGN(Socket);
David Pursell815c7be2015-12-09 17:09:54 -0800122};
123
124#endif // SOCKET_H_