blob: d4dca633dcfac77d68ce03b7ecaa4227e63a9ae6 [file] [log] [blame]
jeffhao2b8f76c2011-05-05 14:25:36 -07001/*
2 * Copyright (C) 2011 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
jeffhao2b8f76c2011-05-05 14:25:36 -070017#include <cutils/sockets.h>
David Pursellb34e4a02016-02-01 09:42:09 -080018
19#include <sys/uio.h>
20
Mark Salyzyn23ed4c22016-09-28 13:33:27 -070021#include <android/log.h>
jeffhao2b8f76c2011-05-05 14:25:36 -070022
Elliott Hughes9b828ad2015-07-30 08:47:35 -070023#if defined(__ANDROID__)
jeffhao2b8f76c2011-05-05 14:25:36 -070024/* For the socket trust (credentials) check */
25#include <private/android_filesystem_config.h>
Mark Salyzyn12717162014-04-29 15:49:14 -070026#define __android_unused
27#else
28#define __android_unused __attribute__((__unused__))
jeffhao2b8f76c2011-05-05 14:25:36 -070029#endif
30
David Pursellb34e4a02016-02-01 09:42:09 -080031bool socket_peer_is_trusted(int fd __android_unused) {
Elliott Hughes9b828ad2015-07-30 08:47:35 -070032#if defined(__ANDROID__)
David Pursellb34e4a02016-02-01 09:42:09 -080033 ucred cr;
jeffhao2b8f76c2011-05-05 14:25:36 -070034 socklen_t len = sizeof(cr);
35 int n = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &len);
36
37 if (n != 0) {
Steve Block01dda202012-01-06 14:13:42 +000038 ALOGE("could not get socket credentials: %s\n", strerror(errno));
jeffhao2b8f76c2011-05-05 14:25:36 -070039 return false;
40 }
41
42 if ((cr.uid != AID_ROOT) && (cr.uid != AID_SHELL)) {
Steve Block01dda202012-01-06 14:13:42 +000043 ALOGE("untrusted userid on other end of socket: userid %d\n", cr.uid);
jeffhao2b8f76c2011-05-05 14:25:36 -070044 return false;
45 }
46#endif
47
48 return true;
49}
David Pursell0eb8e1b2016-01-14 17:18:27 -080050
51int socket_close(int sock) {
David Pursell8385fb22016-01-29 13:49:25 -080052 return close(sock);
David Pursell0eb8e1b2016-01-14 17:18:27 -080053}
David Pursell572bce22016-01-15 14:19:56 -080054
55int socket_set_receive_timeout(cutils_socket_t sock, int timeout_ms) {
David Pursellb34e4a02016-02-01 09:42:09 -080056 timeval tv;
David Pursell8385fb22016-01-29 13:49:25 -080057 tv.tv_sec = timeout_ms / 1000;
58 tv.tv_usec = (timeout_ms % 1000) * 1000;
59 return setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
60}
61
David Pursell8385fb22016-01-29 13:49:25 -080062ssize_t socket_send_buffers(cutils_socket_t sock,
David Pursellb34e4a02016-02-01 09:42:09 -080063 const cutils_socket_buffer_t* buffers,
David Pursell8385fb22016-01-29 13:49:25 -080064 size_t num_buffers) {
David Pursellb34e4a02016-02-01 09:42:09 -080065 if (num_buffers > SOCKET_SEND_BUFFERS_MAX_BUFFERS) {
66 return -1;
67 }
68
69 iovec iovec_buffers[SOCKET_SEND_BUFFERS_MAX_BUFFERS];
70 for (size_t i = 0; i < num_buffers; ++i) {
71 // It's safe to cast away const here; iovec declares non-const
72 // void* because it's used for both send and receive, but since
73 // we're only sending, the data won't be modified.
74 iovec_buffers[i].iov_base = const_cast<void*>(buffers[i].data);
75 iovec_buffers[i].iov_len = buffers[i].length;
76 }
77
78 return writev(sock, iovec_buffers, num_buffers);
David Pursell572bce22016-01-15 14:19:56 -080079}