1 // Copyright 2014 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 
17 #ifdef _MSC_VER
18 #include "aemu/base/msvc.h"
19 #endif
20 
21 #include <sys/types.h>
22 
23 namespace android {
24 namespace base {
25 
26 // Close a socket, preserves errno to ensure it can be used in
27 // error-handling code paths.
28 void socketClose(int socket);
29 
30 // Try to receive up to |bufferLen| bytes from |socket| into |buffer|.
31 // Return the number of bytes actually read, 0 in case of disconnection,
32 // or -1/errno in case of error. Note that this loops around EINTR as
33 // a convenience.
34 ssize_t socketRecv(int socket, void* buffer, size_t bufferLen);
35 
36 // Try to send up to |bufferLen| bytes to |socket| from |buffer|.
37 // Return the number of bytes actually sent, 0 in case of disconnection,
38 // or -1/errno in case of error. Note that this loops around EINTR as
39 // a convenience. Also, this will *not* generate a SIGPIPE signal when
40 // writing to a broken pipe (but errno will be set to EPIPE).
41 ssize_t socketSend(int socket, const void* buffer, size_t bufferLen);
42 
43 // Same as socketSend() but loop around transient writes.
44 // Returns true if all bytes were sent, false otherwise.
45 bool socketSendAll(int socket, const void* buffer, size_t bufferLen);
46 
47 // Same as socketRecv() but loop around transient reads.
48 // Returns ture if all bytes were received, false otherwise.
49 bool socketRecvAll(int socket, void* buffer, size_t bufferLen);
50 
51 // Shutdown all writes to a socket.
52 void socketShutdownWrites(int socket);
53 
54 // Shutdown all reads from a socket.
55 void socketShutdownReads(int socket);
56 
57 // Set the socket descriptor |socket| to non-blocking mode.
58 void socketSetNonBlocking(int socket);
59 
60 // Set the socket descriptor |socket| to blocking mode.
61 void socketSetBlocking(int socket);
62 
63 // Disable TCP Nagle algorithm for |socket|.
64 void socketSetNoDelay(int socket);
65 
66 // Bind and listen on TCP |port| on loopback interface (i.e. 127.0.0.1).
67 // Return new socket on success, or -1/errno on error.
68 int socketTcp4LoopbackServer(int port);
69 
70 // Bind and listen on TCP |port| on IPv6 loopback interface (i.e. ::1).
71 // Return new socket on success, or -1/errno on error.
72 int socketTcp6LoopbackServer(int port);
73 
74 // Connect to TCP |port| on loopback interface (i.e. 127.0.0.1).
75 // Return new socket on success, or -1/errno on error.
76 int socketTcp4LoopbackClient(int port);
77 
78 // Connecto TCP |port| on IPV6 loopback interface (i.e. ::1).
79 // Return new socket on success, or -1/errno on error.
80 int socketTcp6LoopbackClient(int port);
81 
82 // Accept a connection on server |socket|, and return the new connection
83 // socket descriptor, or -1/errno on error.
84 int socketAcceptAny(int socket);
85 
86 // Create a pair of non-blocking sockets connected to each other, this is
87 // equivalent to calling the Unix function:
88 //     socketpair(AF_LOCAL, SOCK_STREAM, 0, &fds);
89 //
90 // On Windows this will use a pair of TCP loopback sockets instead.
91 // Return 0 in success, or -1/errno on error.
92 int socketCreatePair(int *s1, int* s2);
93 
94 // Create a new TCP-based socket. At the moment, this should only be used
95 // for unit-testing.
96 int socketCreateTcp4();
97 int socketCreateTcp6();
98 
99 // Return the port number of a TCP or UDP socket, or -1/errno otherwise.
100 int socketGetPort(int socket);
101 
102 // Return the port number of a TCP socket peer, if available, or
103 // -1/errno otherwise.
104 int socketGetPeerPort(int socket);
105 
106 }  // namespace base
107 }  // namespace android
108