| David Pursell | 0eb8e1b | 2016-01-14 17:18:27 -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 <cutils/sockets.h> | 
|  | 30 |  | 
|  | 31 | // https://msdn.microsoft.com/en-us/library/windows/desktop/ms741549(v=vs.85).aspx | 
|  | 32 | // claims WSACleanup() should be called before program exit, but general | 
|  | 33 | // consensus seems to be that it hasn't actually been necessary for a long time, | 
|  | 34 | // likely since Windows 3.1. Additionally, trying to properly use WSACleanup() | 
|  | 35 | // can be extremely tricky and cause deadlock when using threads or atexit(). | 
|  | 36 | // | 
|  | 37 | // Both adb (1) and Chrome (2) purposefully avoid WSACleanup() with no issues. | 
|  | 38 | // (1) https://android.googlesource.com/platform/system/core.git/+/master/adb/sysdeps_win32.cpp | 
|  | 39 | // (2) https://code.google.com/p/chromium/codesearch#chromium/src/net/base/winsock_init.cc | 
| Elliott Hughes | 8e9aeb9 | 2017-11-10 10:22:07 -0800 | [diff] [blame] | 40 | bool initialize_windows_sockets() { | 
| David Pursell | 0eb8e1b | 2016-01-14 17:18:27 -0800 | [diff] [blame] | 41 | // There's no harm in calling WSAStartup() multiple times but no benefit | 
|  | 42 | // either, we may as well skip it after the first. | 
|  | 43 | static bool init_success = false; | 
|  | 44 |  | 
|  | 45 | if (!init_success) { | 
|  | 46 | WSADATA wsaData; | 
|  | 47 | init_success = (WSAStartup(MAKEWORD(2, 2), &wsaData) == 0); | 
|  | 48 | } | 
|  | 49 |  | 
|  | 50 | return init_success; | 
|  | 51 | } | 
|  | 52 |  | 
|  | 53 | int socket_close(cutils_socket_t sock) { | 
|  | 54 | return closesocket(sock); | 
|  | 55 | } | 
| David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 56 |  | 
| David Pursell | 8385fb2 | 2016-01-29 13:49:25 -0800 | [diff] [blame] | 57 | ssize_t socket_send_buffers(cutils_socket_t sock, | 
| David Pursell | b34e4a0 | 2016-02-01 09:42:09 -0800 | [diff] [blame] | 58 | const cutils_socket_buffer_t* buffers, | 
| David Pursell | 8385fb2 | 2016-01-29 13:49:25 -0800 | [diff] [blame] | 59 | size_t num_buffers) { | 
| David Pursell | b34e4a0 | 2016-02-01 09:42:09 -0800 | [diff] [blame] | 60 | if (num_buffers > SOCKET_SEND_BUFFERS_MAX_BUFFERS) { | 
|  | 61 | return -1; | 
|  | 62 | } | 
| David Pursell | 8385fb2 | 2016-01-29 13:49:25 -0800 | [diff] [blame] | 63 |  | 
| David Pursell | b34e4a0 | 2016-02-01 09:42:09 -0800 | [diff] [blame] | 64 | WSABUF wsa_buffers[SOCKET_SEND_BUFFERS_MAX_BUFFERS]; | 
|  | 65 | for (size_t i = 0; i < num_buffers; ++i) { | 
|  | 66 | // It's safe to cast away const here; WSABUF declares non-const | 
|  | 67 | // void* because it's used for both send and receive, but since | 
|  | 68 | // we're only sending, the data won't be modified. | 
|  | 69 | wsa_buffers[i].buf = | 
|  | 70 | reinterpret_cast<char*>(const_cast<void*>(buffers[i].data)); | 
|  | 71 | wsa_buffers[i].len = buffers[i].length; | 
|  | 72 | } | 
|  | 73 |  | 
|  | 74 | DWORD bytes_sent = 0; | 
|  | 75 | if (WSASend(sock, wsa_buffers, num_buffers, &bytes_sent, 0, nullptr, | 
|  | 76 | nullptr) != SOCKET_ERROR) { | 
| David Pursell | 8385fb2 | 2016-01-29 13:49:25 -0800 | [diff] [blame] | 77 | return bytes_sent; | 
|  | 78 | } | 
| David Pursell | b34e4a0 | 2016-02-01 09:42:09 -0800 | [diff] [blame] | 79 |  | 
| David Pursell | 8385fb2 | 2016-01-29 13:49:25 -0800 | [diff] [blame] | 80 | return -1; | 
|  | 81 | } | 
| Mark Salyzyn | 52bd37e | 2016-11-07 09:39:30 -0800 | [diff] [blame] | 82 |  | 
| Dan Willemsen | 528f144 | 2017-11-29 18:06:11 -0800 | [diff] [blame] | 83 | int android_get_control_socket(const char*) { | 
| Mark Salyzyn | 52bd37e | 2016-11-07 09:39:30 -0800 | [diff] [blame] | 84 | return -1; | 
|  | 85 | } |