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 | |
jeffhao | 2b8f76c | 2011-05-05 14:25:36 -0700 | [diff] [blame] | 17 | #include <cutils/sockets.h> |
Mark Salyzyn | 1271716 | 2014-04-29 15:49:14 -0700 | [diff] [blame] | 18 | #include <log/log.h> |
jeffhao | 2b8f76c | 2011-05-05 14:25:36 -0700 | [diff] [blame] | 19 | |
Elliott Hughes | 9b828ad | 2015-07-30 08:47:35 -0700 | [diff] [blame] | 20 | #if defined(__ANDROID__) |
jeffhao | 2b8f76c | 2011-05-05 14:25:36 -0700 | [diff] [blame] | 21 | /* For the socket trust (credentials) check */ |
| 22 | #include <private/android_filesystem_config.h> |
Mark Salyzyn | 1271716 | 2014-04-29 15:49:14 -0700 | [diff] [blame] | 23 | #define __android_unused |
| 24 | #else |
| 25 | #define __android_unused __attribute__((__unused__)) |
jeffhao | 2b8f76c | 2011-05-05 14:25:36 -0700 | [diff] [blame] | 26 | #endif |
| 27 | |
Mark Salyzyn | 1271716 | 2014-04-29 15:49:14 -0700 | [diff] [blame] | 28 | bool socket_peer_is_trusted(int fd __android_unused) |
jeffhao | 2b8f76c | 2011-05-05 14:25:36 -0700 | [diff] [blame] | 29 | { |
Elliott Hughes | 9b828ad | 2015-07-30 08:47:35 -0700 | [diff] [blame] | 30 | #if defined(__ANDROID__) |
jeffhao | 2b8f76c | 2011-05-05 14:25:36 -0700 | [diff] [blame] | 31 | struct ucred cr; |
| 32 | socklen_t len = sizeof(cr); |
| 33 | int n = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &len); |
| 34 | |
| 35 | if (n != 0) { |
Steve Block | 01dda20 | 2012-01-06 14:13:42 +0000 | [diff] [blame] | 36 | ALOGE("could not get socket credentials: %s\n", strerror(errno)); |
jeffhao | 2b8f76c | 2011-05-05 14:25:36 -0700 | [diff] [blame] | 37 | return false; |
| 38 | } |
| 39 | |
| 40 | if ((cr.uid != AID_ROOT) && (cr.uid != AID_SHELL)) { |
Steve Block | 01dda20 | 2012-01-06 14:13:42 +0000 | [diff] [blame] | 41 | ALOGE("untrusted userid on other end of socket: userid %d\n", cr.uid); |
jeffhao | 2b8f76c | 2011-05-05 14:25:36 -0700 | [diff] [blame] | 42 | return false; |
| 43 | } |
| 44 | #endif |
| 45 | |
| 46 | return true; |
| 47 | } |
David Pursell | 0eb8e1b | 2016-01-14 17:18:27 -0800 | [diff] [blame] | 48 | |
| 49 | int socket_close(int sock) { |
David Pursell | 8385fb2 | 2016-01-29 13:49:25 -0800 | [diff] [blame^] | 50 | return close(sock); |
David Pursell | 0eb8e1b | 2016-01-14 17:18:27 -0800 | [diff] [blame] | 51 | } |
David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 52 | |
| 53 | int socket_set_receive_timeout(cutils_socket_t sock, int timeout_ms) { |
David Pursell | 8385fb2 | 2016-01-29 13:49:25 -0800 | [diff] [blame^] | 54 | struct timeval tv; |
| 55 | tv.tv_sec = timeout_ms / 1000; |
| 56 | tv.tv_usec = (timeout_ms % 1000) * 1000; |
| 57 | return setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); |
| 58 | } |
| 59 | |
| 60 | cutils_socket_buffer_t make_cutils_socket_buffer(void* data, size_t length) { |
| 61 | cutils_socket_buffer_t buffer; |
| 62 | buffer.iov_base = data; |
| 63 | buffer.iov_len = length; |
| 64 | return buffer; |
| 65 | } |
| 66 | |
| 67 | ssize_t socket_send_buffers(cutils_socket_t sock, |
| 68 | cutils_socket_buffer_t* buffers, |
| 69 | size_t num_buffers) { |
| 70 | return writev(sock, buffers, num_buffers); |
David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 71 | } |