10eb8e1b706b577194bab1e23fab5b7d20d5259f1David Pursell/* 20eb8e1b706b577194bab1e23fab5b7d20d5259f1David Pursell * Copyright (C) 2015 The Android Open Source Project 30eb8e1b706b577194bab1e23fab5b7d20d5259f1David Pursell * All rights reserved. 40eb8e1b706b577194bab1e23fab5b7d20d5259f1David Pursell * 50eb8e1b706b577194bab1e23fab5b7d20d5259f1David Pursell * Redistribution and use in source and binary forms, with or without 60eb8e1b706b577194bab1e23fab5b7d20d5259f1David Pursell * modification, are permitted provided that the following conditions 70eb8e1b706b577194bab1e23fab5b7d20d5259f1David Pursell * are met: 80eb8e1b706b577194bab1e23fab5b7d20d5259f1David Pursell * * Redistributions of source code must retain the above copyright 90eb8e1b706b577194bab1e23fab5b7d20d5259f1David Pursell * notice, this list of conditions and the following disclaimer. 100eb8e1b706b577194bab1e23fab5b7d20d5259f1David Pursell * * Redistributions in binary form must reproduce the above copyright 110eb8e1b706b577194bab1e23fab5b7d20d5259f1David Pursell * notice, this list of conditions and the following disclaimer in 120eb8e1b706b577194bab1e23fab5b7d20d5259f1David Pursell * the documentation and/or other materials provided with the 130eb8e1b706b577194bab1e23fab5b7d20d5259f1David Pursell * distribution. 140eb8e1b706b577194bab1e23fab5b7d20d5259f1David Pursell * 150eb8e1b706b577194bab1e23fab5b7d20d5259f1David Pursell * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 160eb8e1b706b577194bab1e23fab5b7d20d5259f1David Pursell * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 170eb8e1b706b577194bab1e23fab5b7d20d5259f1David Pursell * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 180eb8e1b706b577194bab1e23fab5b7d20d5259f1David Pursell * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 190eb8e1b706b577194bab1e23fab5b7d20d5259f1David Pursell * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 200eb8e1b706b577194bab1e23fab5b7d20d5259f1David Pursell * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 210eb8e1b706b577194bab1e23fab5b7d20d5259f1David Pursell * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 220eb8e1b706b577194bab1e23fab5b7d20d5259f1David Pursell * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 230eb8e1b706b577194bab1e23fab5b7d20d5259f1David Pursell * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 240eb8e1b706b577194bab1e23fab5b7d20d5259f1David Pursell * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 250eb8e1b706b577194bab1e23fab5b7d20d5259f1David Pursell * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 260eb8e1b706b577194bab1e23fab5b7d20d5259f1David Pursell * SUCH DAMAGE. 270eb8e1b706b577194bab1e23fab5b7d20d5259f1David Pursell */ 280eb8e1b706b577194bab1e23fab5b7d20d5259f1David Pursell 290eb8e1b706b577194bab1e23fab5b7d20d5259f1David Pursell#include <cutils/sockets.h> 300eb8e1b706b577194bab1e23fab5b7d20d5259f1David Pursell 310eb8e1b706b577194bab1e23fab5b7d20d5259f1David Pursell// https://msdn.microsoft.com/en-us/library/windows/desktop/ms741549(v=vs.85).aspx 320eb8e1b706b577194bab1e23fab5b7d20d5259f1David Pursell// claims WSACleanup() should be called before program exit, but general 330eb8e1b706b577194bab1e23fab5b7d20d5259f1David Pursell// consensus seems to be that it hasn't actually been necessary for a long time, 340eb8e1b706b577194bab1e23fab5b7d20d5259f1David Pursell// likely since Windows 3.1. Additionally, trying to properly use WSACleanup() 350eb8e1b706b577194bab1e23fab5b7d20d5259f1David Pursell// can be extremely tricky and cause deadlock when using threads or atexit(). 360eb8e1b706b577194bab1e23fab5b7d20d5259f1David Pursell// 370eb8e1b706b577194bab1e23fab5b7d20d5259f1David Pursell// Both adb (1) and Chrome (2) purposefully avoid WSACleanup() with no issues. 380eb8e1b706b577194bab1e23fab5b7d20d5259f1David Pursell// (1) https://android.googlesource.com/platform/system/core.git/+/master/adb/sysdeps_win32.cpp 390eb8e1b706b577194bab1e23fab5b7d20d5259f1David Pursell// (2) https://code.google.com/p/chromium/codesearch#chromium/src/net/base/winsock_init.cc 40b34e4a06eeaaeaa42e0de6fdb44fb4202839b996David Pursellextern "C" bool initialize_windows_sockets() { 410eb8e1b706b577194bab1e23fab5b7d20d5259f1David Pursell // There's no harm in calling WSAStartup() multiple times but no benefit 420eb8e1b706b577194bab1e23fab5b7d20d5259f1David Pursell // either, we may as well skip it after the first. 430eb8e1b706b577194bab1e23fab5b7d20d5259f1David Pursell static bool init_success = false; 440eb8e1b706b577194bab1e23fab5b7d20d5259f1David Pursell 450eb8e1b706b577194bab1e23fab5b7d20d5259f1David Pursell if (!init_success) { 460eb8e1b706b577194bab1e23fab5b7d20d5259f1David Pursell WSADATA wsaData; 470eb8e1b706b577194bab1e23fab5b7d20d5259f1David Pursell init_success = (WSAStartup(MAKEWORD(2, 2), &wsaData) == 0); 480eb8e1b706b577194bab1e23fab5b7d20d5259f1David Pursell } 490eb8e1b706b577194bab1e23fab5b7d20d5259f1David Pursell 500eb8e1b706b577194bab1e23fab5b7d20d5259f1David Pursell return init_success; 510eb8e1b706b577194bab1e23fab5b7d20d5259f1David Pursell} 520eb8e1b706b577194bab1e23fab5b7d20d5259f1David Pursell 530eb8e1b706b577194bab1e23fab5b7d20d5259f1David Pursellint socket_close(cutils_socket_t sock) { 540eb8e1b706b577194bab1e23fab5b7d20d5259f1David Pursell return closesocket(sock); 550eb8e1b706b577194bab1e23fab5b7d20d5259f1David Pursell} 56572bce29088521caf7f90c9fa66a8237a7674435David Pursell 57572bce29088521caf7f90c9fa66a8237a7674435David Pursellint socket_set_receive_timeout(cutils_socket_t sock, int timeout_ms) { 58b34e4a06eeaaeaa42e0de6fdb44fb4202839b996David Pursell return setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, 59b34e4a06eeaaeaa42e0de6fdb44fb4202839b996David Pursell reinterpret_cast<char*>(&timeout_ms), sizeof(timeout_ms)); 608385fb299f696cd0ea83124751d2d9b93c872474David Pursell} 618385fb299f696cd0ea83124751d2d9b93c872474David Pursell 628385fb299f696cd0ea83124751d2d9b93c872474David Pursellssize_t socket_send_buffers(cutils_socket_t sock, 63b34e4a06eeaaeaa42e0de6fdb44fb4202839b996David Pursell const cutils_socket_buffer_t* buffers, 648385fb299f696cd0ea83124751d2d9b93c872474David Pursell size_t num_buffers) { 65b34e4a06eeaaeaa42e0de6fdb44fb4202839b996David Pursell if (num_buffers > SOCKET_SEND_BUFFERS_MAX_BUFFERS) { 66b34e4a06eeaaeaa42e0de6fdb44fb4202839b996David Pursell return -1; 67b34e4a06eeaaeaa42e0de6fdb44fb4202839b996David Pursell } 688385fb299f696cd0ea83124751d2d9b93c872474David Pursell 69b34e4a06eeaaeaa42e0de6fdb44fb4202839b996David Pursell WSABUF wsa_buffers[SOCKET_SEND_BUFFERS_MAX_BUFFERS]; 70b34e4a06eeaaeaa42e0de6fdb44fb4202839b996David Pursell for (size_t i = 0; i < num_buffers; ++i) { 71b34e4a06eeaaeaa42e0de6fdb44fb4202839b996David Pursell // It's safe to cast away const here; WSABUF declares non-const 72b34e4a06eeaaeaa42e0de6fdb44fb4202839b996David Pursell // void* because it's used for both send and receive, but since 73b34e4a06eeaaeaa42e0de6fdb44fb4202839b996David Pursell // we're only sending, the data won't be modified. 74b34e4a06eeaaeaa42e0de6fdb44fb4202839b996David Pursell wsa_buffers[i].buf = 75b34e4a06eeaaeaa42e0de6fdb44fb4202839b996David Pursell reinterpret_cast<char*>(const_cast<void*>(buffers[i].data)); 76b34e4a06eeaaeaa42e0de6fdb44fb4202839b996David Pursell wsa_buffers[i].len = buffers[i].length; 77b34e4a06eeaaeaa42e0de6fdb44fb4202839b996David Pursell } 78b34e4a06eeaaeaa42e0de6fdb44fb4202839b996David Pursell 79b34e4a06eeaaeaa42e0de6fdb44fb4202839b996David Pursell DWORD bytes_sent = 0; 80b34e4a06eeaaeaa42e0de6fdb44fb4202839b996David Pursell if (WSASend(sock, wsa_buffers, num_buffers, &bytes_sent, 0, nullptr, 81b34e4a06eeaaeaa42e0de6fdb44fb4202839b996David Pursell nullptr) != SOCKET_ERROR) { 828385fb299f696cd0ea83124751d2d9b93c872474David Pursell return bytes_sent; 838385fb299f696cd0ea83124751d2d9b93c872474David Pursell } 84b34e4a06eeaaeaa42e0de6fdb44fb4202839b996David Pursell 858385fb299f696cd0ea83124751d2d9b93c872474David Pursell return -1; 868385fb299f696cd0ea83124751d2d9b93c872474David Pursell} 8752bd37e63373b410c009e8611508191dfbf31d30Mark Salyzyn 8852bd37e63373b410c009e8611508191dfbf31d30Mark Salyzynint android_get_control_socket(const char* name) { 8952bd37e63373b410c009e8611508191dfbf31d30Mark Salyzyn return -1; 9052bd37e63373b410c009e8611508191dfbf31d30Mark Salyzyn} 91