blob: 63761a2c5619ddc17930febe8acef9c7f9291526 [file] [log] [blame]
David Pursell756e1c82016-01-29 10:39:41 -08001/*
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 Salyzyn547e0dc2016-10-27 08:04:48 -070031#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 Pursell756e1c82016-01-29 10:39:41 -080051
52#include <cutils/sockets.h>
53
Mark Salyzyn547e0dc2016-10-27 08:04:48 -070054#ifndef TEMP_FAILURE_RETRY // _WIN32 does not define
55#define TEMP_FAILURE_RETRY(exp) (exp)
David Pursell756e1c82016-01-29 10:39:41 -080056#endif
57
58int 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 Crosse8ffa442016-09-23 10:06:28 -070068
69int android_get_control_socket(const char* name) {
Mark Salyzyn547e0dc2016-10-27 08:04:48 -070070 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 Crosse8ffa442016-09-23 10:06:28 -070079
80 const char* val = getenv(key);
Mark Salyzyn547e0dc2016-10-27 08:04:48 -070081 free(key);
82 if (!val) return -1;
Colin Crosse8ffa442016-09-23 10:06:28 -070083
84 errno = 0;
Mark Salyzyn547e0dc2016-10-27 08:04:48 -070085 long fd = strtol(val, NULL, 10);
86 if (errno) return -1;
Colin Crosse8ffa442016-09-23 10:06:28 -070087
Mark Salyzyn547e0dc2016-10-27 08:04:48 -070088 // 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 Crosse8ffa442016-09-23 10:06:28 -0700122}