Home | History | Annotate | Download | only in cutils
      1 /*
      2  * Copyright (C) 2006 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifndef __CUTILS_SOCKETS_H
     18 #define __CUTILS_SOCKETS_H
     19 
     20 #include <errno.h>
     21 #include <limits.h>
     22 #include <stdio.h>
     23 #include <stdlib.h>
     24 #include <string.h>
     25 #include <stdbool.h>
     26 
     27 #if defined(_WIN32)
     28 
     29 #include <winsock2.h>
     30 #include <ws2tcpip.h>
     31 
     32 typedef int  socklen_t;
     33 typedef SOCKET cutils_socket_t;
     34 
     35 #else
     36 
     37 #include <sys/socket.h>
     38 #include <netinet/in.h>
     39 
     40 typedef int cutils_socket_t;
     41 #define INVALID_SOCKET (-1)
     42 
     43 #endif
     44 
     45 #define ANDROID_SOCKET_ENV_PREFIX	"ANDROID_SOCKET_"
     46 #define ANDROID_SOCKET_DIR		"/dev/socket"
     47 
     48 #ifdef __cplusplus
     49 extern "C" {
     50 #endif
     51 
     52 /*
     53  * android_get_control_socket - simple helper function to get the file
     54  * descriptor of our init-managed Unix domain socket. `name' is the name of the
     55  * socket, as given in init.rc. Returns -1 on error.
     56  */
     57 int android_get_control_socket(const char* name);
     58 
     59 /*
     60  * See also android.os.LocalSocketAddress.Namespace
     61  */
     62 // Linux "abstract" (non-filesystem) namespace
     63 #define ANDROID_SOCKET_NAMESPACE_ABSTRACT 0
     64 // Android "reserved" (/dev/socket) namespace
     65 #define ANDROID_SOCKET_NAMESPACE_RESERVED 1
     66 // Normal filesystem namespace
     67 #define ANDROID_SOCKET_NAMESPACE_FILESYSTEM 2
     68 
     69 /*
     70  * Functions to create sockets for some common usages.
     71  *
     72  * All these functions are implemented for Unix, but only a few are implemented
     73  * for Windows. Those which are can be identified by the cutils_socket_t
     74  * return type. The idea is to be able to use this return value with the
     75  * standard Unix socket functions on any platform.
     76  *
     77  * On Unix the returned cutils_socket_t is a standard int file descriptor and
     78  * can always be used as normal with all file descriptor functions.
     79  *
     80  * On Windows utils_socket_t is an unsigned int pointer, and is only valid
     81  * with functions that specifically take a socket, e.g. send(), sendto(),
     82  * recv(), and recvfrom(). General file descriptor functions such as read(),
     83  * write(), and close() will not work with utils_socket_t and will require
     84  * special handling.
     85  *
     86  * These functions return INVALID_SOCKET (-1) on failure for all platforms.
     87  */
     88 cutils_socket_t socket_network_client(const char* host, int port, int type);
     89 int socket_network_client_timeout(const char* host, int port, int type,
     90                                   int timeout, int* getaddrinfo_error);
     91 int socket_local_server(const char* name, int namespaceId, int type);
     92 int socket_local_server_bind(int s, const char* name, int namespaceId);
     93 int socket_local_client_connect(int fd, const char *name, int namespaceId,
     94                                 int type);
     95 int socket_local_client(const char* name, int namespaceId, int type);
     96 cutils_socket_t socket_inaddr_any_server(int port, int type);
     97 
     98 /*
     99  * Closes a cutils_socket_t. Windows doesn't allow calling close() on a socket
    100  * so this is a cross-platform way to close a cutils_socket_t.
    101  *
    102  * Returns 0 on success.
    103  */
    104 int socket_close(cutils_socket_t sock);
    105 
    106 /*
    107  * Sets socket receive timeout using SO_RCVTIMEO. Setting |timeout_ms| to 0
    108  * disables receive timeouts.
    109  *
    110  * Return 0 on success.
    111  */
    112 int socket_set_receive_timeout(cutils_socket_t sock, int timeout_ms);
    113 
    114 /*
    115  * Returns the local port the socket is bound to or -1 on error.
    116  */
    117 int socket_get_local_port(cutils_socket_t sock);
    118 
    119 /*
    120  * Sends to a socket from multiple buffers; wraps writev() on Unix or WSASend()
    121  * on Windows. This can give significant speedup compared to calling send()
    122  * multiple times.
    123  *
    124  * Example usage:
    125  *   cutils_socket_buffer_t buffers[2] = { {data0, len0}, {data1, len1} };
    126  *   socket_send_buffers(sock, buffers, 2);
    127  *
    128  * If you try to pass more than SOCKET_SEND_BUFFERS_MAX_BUFFERS buffers into
    129  * this function it will return -1 without sending anything.
    130  *
    131  * Returns the number of bytes written or -1 on error.
    132  */
    133 typedef struct {
    134   const void* data;
    135   size_t length;
    136 } cutils_socket_buffer_t;
    137 
    138 #define SOCKET_SEND_BUFFERS_MAX_BUFFERS 16
    139 
    140 ssize_t socket_send_buffers(cutils_socket_t sock,
    141                             const cutils_socket_buffer_t* buffers,
    142                             size_t num_buffers);
    143 
    144 /*
    145  * socket_peer_is_trusted - Takes a socket which is presumed to be a
    146  * connected local socket (e.g. AF_LOCAL) and returns whether the peer
    147  * (the userid that owns the process on the other end of that socket)
    148  * is one of the two trusted userids, root or shell.
    149  *
    150  * Note: This only works as advertised on the Android OS and always
    151  * just returns true when called on other operating systems.
    152  */
    153 extern bool socket_peer_is_trusted(int fd);
    154 
    155 #ifdef __cplusplus
    156 }
    157 #endif
    158 
    159 #endif /* __CUTILS_SOCKETS_H */
    160