jeffhao | 2b8f76c | 2011-05-05 14:25:36 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
Mark Salyzyn | cfd5b08 | 2016-10-17 14:28:00 -0700 | [diff] [blame^] | 17 | #define LOG_TAG "socket-unix" |
| 18 | |
David Pursell | b34e4a0 | 2016-02-01 09:42:09 -0800 | [diff] [blame] | 19 | #include <sys/uio.h> |
Mark Salyzyn | cfd5b08 | 2016-10-17 14:28:00 -0700 | [diff] [blame^] | 20 | #include <time.h> |
| 21 | #include <unistd.h> |
David Pursell | b34e4a0 | 2016-02-01 09:42:09 -0800 | [diff] [blame] | 22 | |
Mark Salyzyn | 23ed4c2 | 2016-09-28 13:33:27 -0700 | [diff] [blame] | 23 | #include <android/log.h> |
Mark Salyzyn | ff2dcd9 | 2016-09-28 15:54:45 -0700 | [diff] [blame] | 24 | #include <cutils/sockets.h> |
jeffhao | 2b8f76c | 2011-05-05 14:25:36 -0700 | [diff] [blame] | 25 | |
Elliott Hughes | 9b828ad | 2015-07-30 08:47:35 -0700 | [diff] [blame] | 26 | #if defined(__ANDROID__) |
jeffhao | 2b8f76c | 2011-05-05 14:25:36 -0700 | [diff] [blame] | 27 | /* For the socket trust (credentials) check */ |
| 28 | #include <private/android_filesystem_config.h> |
Mark Salyzyn | 1271716 | 2014-04-29 15:49:14 -0700 | [diff] [blame] | 29 | #define __android_unused |
| 30 | #else |
| 31 | #define __android_unused __attribute__((__unused__)) |
jeffhao | 2b8f76c | 2011-05-05 14:25:36 -0700 | [diff] [blame] | 32 | #endif |
| 33 | |
David Pursell | b34e4a0 | 2016-02-01 09:42:09 -0800 | [diff] [blame] | 34 | bool socket_peer_is_trusted(int fd __android_unused) { |
Elliott Hughes | 9b828ad | 2015-07-30 08:47:35 -0700 | [diff] [blame] | 35 | #if defined(__ANDROID__) |
David Pursell | b34e4a0 | 2016-02-01 09:42:09 -0800 | [diff] [blame] | 36 | ucred cr; |
jeffhao | 2b8f76c | 2011-05-05 14:25:36 -0700 | [diff] [blame] | 37 | socklen_t len = sizeof(cr); |
| 38 | int n = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &len); |
| 39 | |
| 40 | if (n != 0) { |
Steve Block | 01dda20 | 2012-01-06 14:13:42 +0000 | [diff] [blame] | 41 | ALOGE("could not get socket credentials: %s\n", strerror(errno)); |
jeffhao | 2b8f76c | 2011-05-05 14:25:36 -0700 | [diff] [blame] | 42 | return false; |
| 43 | } |
| 44 | |
| 45 | if ((cr.uid != AID_ROOT) && (cr.uid != AID_SHELL)) { |
Steve Block | 01dda20 | 2012-01-06 14:13:42 +0000 | [diff] [blame] | 46 | ALOGE("untrusted userid on other end of socket: userid %d\n", cr.uid); |
jeffhao | 2b8f76c | 2011-05-05 14:25:36 -0700 | [diff] [blame] | 47 | return false; |
| 48 | } |
| 49 | #endif |
| 50 | |
| 51 | return true; |
| 52 | } |
David Pursell | 0eb8e1b | 2016-01-14 17:18:27 -0800 | [diff] [blame] | 53 | |
| 54 | int socket_close(int sock) { |
David Pursell | 8385fb2 | 2016-01-29 13:49:25 -0800 | [diff] [blame] | 55 | return close(sock); |
David Pursell | 0eb8e1b | 2016-01-14 17:18:27 -0800 | [diff] [blame] | 56 | } |
David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 57 | |
| 58 | int socket_set_receive_timeout(cutils_socket_t sock, int timeout_ms) { |
David Pursell | b34e4a0 | 2016-02-01 09:42:09 -0800 | [diff] [blame] | 59 | timeval tv; |
David Pursell | 8385fb2 | 2016-01-29 13:49:25 -0800 | [diff] [blame] | 60 | tv.tv_sec = timeout_ms / 1000; |
| 61 | tv.tv_usec = (timeout_ms % 1000) * 1000; |
| 62 | return setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); |
| 63 | } |
| 64 | |
David Pursell | 8385fb2 | 2016-01-29 13:49:25 -0800 | [diff] [blame] | 65 | ssize_t socket_send_buffers(cutils_socket_t sock, |
David Pursell | b34e4a0 | 2016-02-01 09:42:09 -0800 | [diff] [blame] | 66 | const cutils_socket_buffer_t* buffers, |
David Pursell | 8385fb2 | 2016-01-29 13:49:25 -0800 | [diff] [blame] | 67 | size_t num_buffers) { |
David Pursell | b34e4a0 | 2016-02-01 09:42:09 -0800 | [diff] [blame] | 68 | if (num_buffers > SOCKET_SEND_BUFFERS_MAX_BUFFERS) { |
| 69 | return -1; |
| 70 | } |
| 71 | |
| 72 | iovec iovec_buffers[SOCKET_SEND_BUFFERS_MAX_BUFFERS]; |
| 73 | for (size_t i = 0; i < num_buffers; ++i) { |
| 74 | // It's safe to cast away const here; iovec declares non-const |
| 75 | // void* because it's used for both send and receive, but since |
| 76 | // we're only sending, the data won't be modified. |
| 77 | iovec_buffers[i].iov_base = const_cast<void*>(buffers[i].data); |
| 78 | iovec_buffers[i].iov_len = buffers[i].length; |
| 79 | } |
| 80 | |
| 81 | return writev(sock, iovec_buffers, num_buffers); |
David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 82 | } |