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 | |
Elliott Hughes | 8e9aeb9 | 2017-11-10 10:22:07 -0800 | [diff] [blame] | 17 | #include <cutils/sockets.h> |
| 18 | |
Mark Salyzyn | 52bd37e | 2016-11-07 09:39:30 -0800 | [diff] [blame] | 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | #include <sys/socket.h> |
David Pursell | b34e4a0 | 2016-02-01 09:42:09 -0800 | [diff] [blame] | 23 | #include <sys/uio.h> |
Mark Salyzyn | 52bd37e | 2016-11-07 09:39:30 -0800 | [diff] [blame] | 24 | #include <sys/un.h> |
Mark Salyzyn | cfd5b08 | 2016-10-17 14:28:00 -0700 | [diff] [blame] | 25 | #include <time.h> |
| 26 | #include <unistd.h> |
David Pursell | b34e4a0 | 2016-02-01 09:42:09 -0800 | [diff] [blame] | 27 | |
Mark Salyzyn | 52bd37e | 2016-11-07 09:39:30 -0800 | [diff] [blame] | 28 | #include "android_get_control_env.h" |
| 29 | |
David Pursell | 0eb8e1b | 2016-01-14 17:18:27 -0800 | [diff] [blame] | 30 | int socket_close(int sock) { |
David Pursell | 8385fb2 | 2016-01-29 13:49:25 -0800 | [diff] [blame] | 31 | return close(sock); |
David Pursell | 0eb8e1b | 2016-01-14 17:18:27 -0800 | [diff] [blame] | 32 | } |
David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 33 | |
| 34 | int socket_set_receive_timeout(cutils_socket_t sock, int timeout_ms) { |
David Pursell | b34e4a0 | 2016-02-01 09:42:09 -0800 | [diff] [blame] | 35 | timeval tv; |
David Pursell | 8385fb2 | 2016-01-29 13:49:25 -0800 | [diff] [blame] | 36 | tv.tv_sec = timeout_ms / 1000; |
| 37 | tv.tv_usec = (timeout_ms % 1000) * 1000; |
| 38 | return setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); |
| 39 | } |
| 40 | |
David Pursell | 8385fb2 | 2016-01-29 13:49:25 -0800 | [diff] [blame] | 41 | ssize_t socket_send_buffers(cutils_socket_t sock, |
David Pursell | b34e4a0 | 2016-02-01 09:42:09 -0800 | [diff] [blame] | 42 | const cutils_socket_buffer_t* buffers, |
David Pursell | 8385fb2 | 2016-01-29 13:49:25 -0800 | [diff] [blame] | 43 | size_t num_buffers) { |
David Pursell | b34e4a0 | 2016-02-01 09:42:09 -0800 | [diff] [blame] | 44 | if (num_buffers > SOCKET_SEND_BUFFERS_MAX_BUFFERS) { |
| 45 | return -1; |
| 46 | } |
| 47 | |
| 48 | iovec iovec_buffers[SOCKET_SEND_BUFFERS_MAX_BUFFERS]; |
| 49 | for (size_t i = 0; i < num_buffers; ++i) { |
| 50 | // It's safe to cast away const here; iovec declares non-const |
| 51 | // void* because it's used for both send and receive, but since |
| 52 | // we're only sending, the data won't be modified. |
| 53 | iovec_buffers[i].iov_base = const_cast<void*>(buffers[i].data); |
| 54 | iovec_buffers[i].iov_len = buffers[i].length; |
| 55 | } |
| 56 | |
| 57 | return writev(sock, iovec_buffers, num_buffers); |
David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 58 | } |
Mark Salyzyn | 52bd37e | 2016-11-07 09:39:30 -0800 | [diff] [blame] | 59 | |
Elliott Hughes | 7acb0d3 | 2019-03-21 14:42:55 -0700 | [diff] [blame^] | 60 | #if defined(__ANDROID__) |
Mark Salyzyn | 52bd37e | 2016-11-07 09:39:30 -0800 | [diff] [blame] | 61 | int android_get_control_socket(const char* name) { |
| 62 | int fd = __android_get_control_from_env(ANDROID_SOCKET_ENV_PREFIX, name); |
| 63 | |
| 64 | if (fd < 0) return fd; |
| 65 | |
| 66 | // Compare to UNIX domain socket name, must match! |
| 67 | struct sockaddr_un addr; |
| 68 | socklen_t addrlen = sizeof(addr); |
Bernie Innocenti | 4351bb0 | 2018-05-28 14:56:57 +0900 | [diff] [blame] | 69 | int ret = getsockname(fd, (struct sockaddr*)&addr, &addrlen); |
Mark Salyzyn | 52bd37e | 2016-11-07 09:39:30 -0800 | [diff] [blame] | 70 | if (ret < 0) return -1; |
Mark Salyzyn | 52bd37e | 2016-11-07 09:39:30 -0800 | [diff] [blame] | 71 | |
Bernie Innocenti | 4351bb0 | 2018-05-28 14:56:57 +0900 | [diff] [blame] | 72 | constexpr char prefix[] = ANDROID_SOCKET_DIR "/"; |
| 73 | constexpr size_t prefix_size = sizeof(prefix) - sizeof('\0'); |
| 74 | if ((strncmp(addr.sun_path, prefix, prefix_size) == 0) && |
| 75 | (strcmp(addr.sun_path + prefix_size, name) == 0)) { |
| 76 | // It is what we think it is |
| 77 | return fd; |
| 78 | } |
| 79 | return -1; |
Mark Salyzyn | 52bd37e | 2016-11-07 09:39:30 -0800 | [diff] [blame] | 80 | } |
Elliott Hughes | 7acb0d3 | 2019-03-21 14:42:55 -0700 | [diff] [blame^] | 81 | #else |
| 82 | int android_get_control_socket(const char*) { |
| 83 | return -1; |
| 84 | } |
| 85 | #endif |