summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorElliott Hughes2017-11-10 12:22:07 -0600
committerElliott Hughes2017-11-10 15:18:10 -0600
commit8e9aeb9053695a14539653093937158d15385ca6 (patch)
treeb02f7ad2149812eea2cb2802b7f238856b66619c /libcutils/socket_inaddr_any_server_windows.cpp
parent6707ef139d9786887649e3e3c2e3e251a95dc96d (diff)
downloadplatform-system-core-8e9aeb9053695a14539653093937158d15385ca6.tar.gz
platform-system-core-8e9aeb9053695a14539653093937158d15385ca6.tar.xz
platform-system-core-8e9aeb9053695a14539653093937158d15385ca6.zip
Move libcutils source to C++.
Just the minimial changes to get this to actually build, because otherwise we always bog down trying to rewrite everything (when the real answer is usually "stop using libcutils, it's awful"). This doesn't move a handful of files: two are basically just BSD libc source, a couple have outstanding code reviews, and one can be deleted (but I'll do that in a separate change). I'm also skipping the presubmit hooks because otherwise clang-format wants to reformat everything. I'll follow up with that... Bug: N/A Test: builds Change-Id: I06403f465b67c8e493bad466dd76b1151eed5993
Diffstat (limited to 'libcutils/socket_inaddr_any_server_windows.cpp')
-rw-r--r--libcutils/socket_inaddr_any_server_windows.cpp79
1 files changed, 79 insertions, 0 deletions
diff --git a/libcutils/socket_inaddr_any_server_windows.cpp b/libcutils/socket_inaddr_any_server_windows.cpp
new file mode 100644
index 000000000..1d7320698
--- /dev/null
+++ b/libcutils/socket_inaddr_any_server_windows.cpp
@@ -0,0 +1,79 @@
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#include <errno.h>
32
33#define LISTEN_BACKLOG 4
34
35extern bool initialize_windows_sockets();
36
37SOCKET socket_inaddr_any_server(int port, int type) {
38 if (!initialize_windows_sockets()) {
39 return INVALID_SOCKET;
40 }
41
42 SOCKET sock = socket(AF_INET6, type, 0);
43 if (sock == INVALID_SOCKET) {
44 return INVALID_SOCKET;
45 }
46
47 // Enforce exclusive addresses so nobody can steal the port from us (1),
48 // and enable dual-stack so both IPv4 and IPv6 work (2).
49 // (1) https://msdn.microsoft.com/en-us/library/windows/desktop/ms740621(v=vs.85).aspx.
50 // (2) https://msdn.microsoft.com/en-us/library/windows/desktop/bb513665(v=vs.85).aspx.
51 int exclusive = 1;
52 DWORD v6_only = 0;
53 if (setsockopt(sock, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, (char*)&exclusive,
54 sizeof(exclusive)) == SOCKET_ERROR ||
55 setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&v6_only,
56 sizeof(v6_only)) == SOCKET_ERROR) {
57 closesocket(sock);
58 return INVALID_SOCKET;
59 }
60
61 // Bind the socket to our local port.
62 struct sockaddr_in6 addr;
63 memset(&addr, 0, sizeof(addr));
64 addr.sin6_family = AF_INET6;
65 addr.sin6_port = htons(port);
66 addr.sin6_addr = in6addr_any;
67 if (bind(sock, (struct sockaddr*)&addr, sizeof(addr)) == SOCKET_ERROR) {
68 closesocket(sock);
69 return INVALID_SOCKET;
70 }
71
72 // Start listening for connections if this is a TCP socket.
73 if (type == SOCK_STREAM && listen(sock, LISTEN_BACKLOG) == SOCKET_ERROR) {
74 closesocket(sock);
75 return INVALID_SOCKET;
76 }
77
78 return sock;
79}