David Pursell | 756e1c8 | 2016-01-29 10:39:41 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | // This file contains socket implementation that can be shared between |
| 30 | // platforms as long as the correct headers are included. |
Mark Salyzyn | 547e0dc | 2016-10-27 08:04:48 -0700 | [diff] [blame] | 31 | #define _GNU_SOURCE 1 // For asprintf |
| 32 | |
| 33 | #include <ctype.h> |
| 34 | #include <errno.h> |
| 35 | #include <fcntl.h> |
| 36 | #include <limits.h> |
| 37 | #if !defined(_WIN32) |
| 38 | #include <netinet/in.h> |
| 39 | #endif |
| 40 | #include <stdio.h> |
| 41 | #include <stdlib.h> |
| 42 | #include <string.h> |
| 43 | #include <sys/stat.h> |
| 44 | #include <sys/types.h> |
| 45 | #if !defined(_WIN32) |
| 46 | #include <sys/un.h> |
| 47 | #endif |
| 48 | #include <unistd.h> |
| 49 | |
| 50 | #include <string> |
David Pursell | 756e1c8 | 2016-01-29 10:39:41 -0800 | [diff] [blame] | 51 | |
| 52 | #include <cutils/sockets.h> |
| 53 | |
Mark Salyzyn | 547e0dc | 2016-10-27 08:04:48 -0700 | [diff] [blame] | 54 | #ifndef TEMP_FAILURE_RETRY // _WIN32 does not define |
| 55 | #define TEMP_FAILURE_RETRY(exp) (exp) |
David Pursell | 756e1c8 | 2016-01-29 10:39:41 -0800 | [diff] [blame] | 56 | #endif |
| 57 | |
| 58 | int socket_get_local_port(cutils_socket_t sock) { |
| 59 | sockaddr_storage addr; |
| 60 | socklen_t addr_size = sizeof(addr); |
| 61 | |
| 62 | if (getsockname(sock, reinterpret_cast<sockaddr*>(&addr), &addr_size) == 0) { |
| 63 | // sockaddr_in and sockaddr_in6 always overlap the port field. |
| 64 | return ntohs(reinterpret_cast<sockaddr_in*>(&addr)->sin_port); |
| 65 | } |
| 66 | return -1; |
| 67 | } |
Colin Cross | e8ffa44 | 2016-09-23 10:06:28 -0700 | [diff] [blame] | 68 | |
| 69 | int android_get_control_socket(const char* name) { |
Mark Salyzyn | 547e0dc | 2016-10-27 08:04:48 -0700 | [diff] [blame] | 70 | char *key = NULL; |
| 71 | if (asprintf(&key, ANDROID_SOCKET_ENV_PREFIX "%s", name) < 0) return -1; |
| 72 | if (!key) return -1; |
| 73 | |
| 74 | char *cp = key; |
| 75 | while (*cp) { |
| 76 | if (!isalnum(*cp)) *cp = '_'; |
| 77 | ++cp; |
| 78 | } |
Colin Cross | e8ffa44 | 2016-09-23 10:06:28 -0700 | [diff] [blame] | 79 | |
| 80 | const char* val = getenv(key); |
Mark Salyzyn | 547e0dc | 2016-10-27 08:04:48 -0700 | [diff] [blame] | 81 | free(key); |
| 82 | if (!val) return -1; |
Colin Cross | e8ffa44 | 2016-09-23 10:06:28 -0700 | [diff] [blame] | 83 | |
| 84 | errno = 0; |
Mark Salyzyn | 547e0dc | 2016-10-27 08:04:48 -0700 | [diff] [blame] | 85 | long fd = strtol(val, NULL, 10); |
| 86 | if (errno) return -1; |
Colin Cross | e8ffa44 | 2016-09-23 10:06:28 -0700 | [diff] [blame] | 87 | |
Mark Salyzyn | 547e0dc | 2016-10-27 08:04:48 -0700 | [diff] [blame] | 88 | // validity checking |
| 89 | if ((fd < 0) || (fd > INT_MAX)) return -1; |
| 90 | #if defined(_SC_OPEN_MAX) |
| 91 | if (fd >= sysconf(_SC_OPEN_MAX)) return -1; |
| 92 | #elif defined(OPEN_MAX) |
| 93 | if (fd >= OPEN_MAX) return -1; |
| 94 | #elif defined(_POSIX_OPEN_MAX) |
| 95 | if (fd >= _POSIX_OPEN_MAX) return -1; |
| 96 | #endif |
| 97 | |
| 98 | #if defined(F_GETFD) |
| 99 | if (TEMP_FAILURE_RETRY(fcntl(fd, F_GETFD)) < 0) return -1; |
| 100 | #elif defined(F_GETFL) |
| 101 | if (TEMP_FAILURE_RETRY(fcntl(fd, F_GETFL)) < 0) return -1; |
| 102 | #else |
| 103 | struct stat s; |
| 104 | if (TEMP_FAILURE_RETRY(fstat(fd, &s)) < 0) return -1; |
| 105 | #endif |
| 106 | |
| 107 | #if !defined(_WIN32) |
| 108 | struct sockaddr_un addr; |
| 109 | socklen_t addrlen = sizeof(addr); |
| 110 | int ret = TEMP_FAILURE_RETRY(getsockname(fd, (struct sockaddr *)&addr, &addrlen)); |
| 111 | if (ret < 0) return -1; |
| 112 | char *path = NULL; |
| 113 | if (asprintf(&path, ANDROID_SOCKET_DIR"/%s", name) < 0) return -1; |
| 114 | if (!path) return -1; |
| 115 | int cmp = strcmp(addr.sun_path, path); |
| 116 | free(path); |
| 117 | if (cmp != 0) return -1; |
| 118 | #endif |
| 119 | |
| 120 | // It is what we think it is |
| 121 | return static_cast<int>(fd); |
Colin Cross | e8ffa44 | 2016-09-23 10:06:28 -0700 | [diff] [blame] | 122 | } |