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 | |
| 29 | #include "socket.h" |
| 30 | |
| 31 | #include <errno.h> |
| 32 | #include <netdb.h> |
| 33 | |
| 34 | #include <android-base/stringprintf.h> |
| 35 | #include <cutils/sockets.h> |
| 36 | |
| 37 | class UnixUdpSocket : public UdpSocket { |
| 38 | public: |
| 39 | enum class Type { kClient, kServer }; |
| 40 | |
| 41 | UnixUdpSocket(int fd, Type type); |
| 42 | ~UnixUdpSocket() override; |
| 43 | |
| 44 | ssize_t Send(const void* data, size_t length) override; |
| 45 | ssize_t Receive(void* data, size_t length, int timeout_ms) override; |
| 46 | int Close() override; |
| 47 | |
| 48 | private: |
| 49 | int fd_; |
| 50 | int receive_timeout_ms_ = 0; |
| 51 | std::unique_ptr<sockaddr_storage> addr_; |
| 52 | socklen_t addr_size_ = 0; |
| 53 | |
| 54 | DISALLOW_COPY_AND_ASSIGN(UnixUdpSocket); |
| 55 | }; |
| 56 | |
| 57 | UnixUdpSocket::UnixUdpSocket(int fd, Type type) : fd_(fd) { |
| 58 | // Only servers need to remember addresses; clients are connected to a server in NewUdpClient() |
| 59 | // so will send to that server without needing to specify the address again. |
| 60 | if (type == Type::kServer) { |
| 61 | addr_.reset(new sockaddr_storage); |
| 62 | addr_size_ = sizeof(*addr_); |
| 63 | memset(addr_.get(), 0, addr_size_); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | UnixUdpSocket::~UnixUdpSocket() { |
| 68 | Close(); |
| 69 | } |
| 70 | |
| 71 | ssize_t UnixUdpSocket::Send(const void* data, size_t length) { |
| 72 | return TEMP_FAILURE_RETRY( |
| 73 | sendto(fd_, data, length, 0, reinterpret_cast<sockaddr*>(addr_.get()), addr_size_)); |
| 74 | } |
| 75 | |
| 76 | ssize_t UnixUdpSocket::Receive(void* data, size_t length, int timeout_ms) { |
| 77 | // Only set socket timeout if it's changed. |
| 78 | if (receive_timeout_ms_ != timeout_ms) { |
| 79 | timeval tv; |
| 80 | tv.tv_sec = timeout_ms / 1000; |
| 81 | tv.tv_usec = (timeout_ms % 1000) * 1000; |
| 82 | if (setsockopt(fd_, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) { |
| 83 | return -1; |
| 84 | } |
| 85 | receive_timeout_ms_ = timeout_ms; |
| 86 | } |
| 87 | |
| 88 | socklen_t* addr_size_ptr = nullptr; |
| 89 | if (addr_ != nullptr) { |
| 90 | // Reset addr_size as it may have been modified by previous recvfrom() calls. |
| 91 | addr_size_ = sizeof(*addr_); |
| 92 | addr_size_ptr = &addr_size_; |
| 93 | } |
| 94 | return TEMP_FAILURE_RETRY(recvfrom(fd_, data, length, 0, |
| 95 | reinterpret_cast<sockaddr*>(addr_.get()), addr_size_ptr)); |
| 96 | } |
| 97 | |
| 98 | int UnixUdpSocket::Close() { |
| 99 | int result = 0; |
| 100 | if (fd_ != -1) { |
| 101 | result = close(fd_); |
| 102 | fd_ = -1; |
| 103 | } |
| 104 | return result; |
| 105 | } |
| 106 | |
| 107 | std::unique_ptr<UdpSocket> UdpSocket::NewUdpClient(const std::string& host, int port, |
| 108 | std::string* error) { |
| 109 | int getaddrinfo_error = 0; |
| 110 | int fd = socket_network_client_timeout(host.c_str(), port, SOCK_DGRAM, 0, &getaddrinfo_error); |
| 111 | if (fd == -1) { |
| 112 | if (error) { |
| 113 | *error = android::base::StringPrintf( |
| 114 | "Failed to connect to %s:%d: %s", host.c_str(), port, |
| 115 | getaddrinfo_error ? gai_strerror(getaddrinfo_error) : strerror(errno)); |
| 116 | } |
| 117 | return nullptr; |
| 118 | } |
| 119 | |
| 120 | return std::unique_ptr<UdpSocket>(new UnixUdpSocket(fd, UnixUdpSocket::Type::kClient)); |
| 121 | } |
| 122 | |
| 123 | std::unique_ptr<UdpSocket> UdpSocket::NewUdpServer(int port) { |
| 124 | int fd = socket_inaddr_any_server(port, SOCK_DGRAM); |
| 125 | if (fd == -1) { |
| 126 | // This is just used in testing, no need for an error message. |
| 127 | return nullptr; |
| 128 | } |
| 129 | |
| 130 | return std::unique_ptr<UdpSocket>(new UnixUdpSocket(fd, UnixUdpSocket::Type::kServer)); |
| 131 | } |