Permalink
Browse files

add Android support (#609)

### Motivation:

make swift-nio compatible with Android, and pass all test in Android.

### Modifications:

* add missed `ifaddrs` implementation
* add missed thread related functions - `CNIOLinux_pthread_getname_np`, `CNIOLinux_pthread_setaffinity_np` and `CNIOLinux_pthread_getaffinity_np`
* fix types incositency between Linux and Android, e.g. `Epoll.swift` class
* make bytes counter explicitely 64bits to avoid overflow on 32bit, in `PendingWritesManager.swift` and `PendingDatagramWritesState.swift`
* fix issue with Unix domain socket, first byte need to be zero in Android
* several incosistency fixes between Linux and Android api.

### Result:

now swift-nio works on Android. All tests passed!
  • Loading branch information...
albertaleksieiev authored and weissi committed Sep 27, 2018
1 parent 81e9732 commit a59293cbcdf31e39c67a57d2eaeb4f0f54d197ea
View
@@ -68,3 +68,11 @@ This product contains "sha1.c" and "sha1.h" from FreeBSD (Copyright (C) 1995, 19
* https://opensource.org/licenses/BSD-3-Clause
* HOMEPAGE:
* https://github.com/freebsd/freebsd/tree/master/sys/crypto
---
This product contains "ifaddrs-android.c" and "ifaddrs-android.h" from dxr.mozilla.org. Webrtc is the owner, but we use edited version from mozilla repo (Copyright (c) 2011, The WebRTC project authors. All rights reserved)
* LICENSE (BSD):
* https://webrtc.googlesource.com/src/+/master/LICENSE
* HOMEPAGE:
* https://webrtc.googlesource.com/
@@ -0,0 +1,242 @@
/*
Copyright (c) 2011, The WebRTC project authors. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
* Neither the name of Google nor the names of its contributors may
be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifdef __ANDROID__
#include "ifaddrs-android.h"
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/utsname.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/if.h>
#include <unistd.h>
#include <errno.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
struct netlinkrequest {
struct nlmsghdr header;
struct ifaddrmsg msg;
};
static const int kMaxReadSize = 4096;
static int set_ifname(struct ifaddrs* ifaddr, int interface) {
char buf[IFNAMSIZ] = {0};
char* name = if_indextoname(interface, buf);
if (name == NULL) {
return -1;
}
ifaddr->ifa_name = malloc(strlen(name) + 1);
strncpy(ifaddr->ifa_name, name, strlen(name) + 1);
return 0;
}
static int set_flags(struct ifaddrs* ifaddr) {
int fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd == -1) {
return -1;
}
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, ifaddr->ifa_name, IFNAMSIZ - 1);
int rc = ioctl(fd, SIOCGIFFLAGS, &ifr);
close(fd);
if (rc == -1) {
return -1;
}
ifaddr->ifa_flags = ifr.ifr_flags;
return 0;
}
static int set_addresses(struct ifaddrs* ifaddr, struct ifaddrmsg* msg, void* data,
size_t len) {
if (msg->ifa_family == AF_INET) {
struct sockaddr_in* sa = malloc(sizeof(struct sockaddr_in));
memset(sa, 0, sizeof(struct sockaddr_in));
sa->sin_family = AF_INET;
memcpy(&sa->sin_addr, data, len);
ifaddr->ifa_addr = (struct sockaddr*)sa;
} else if (msg->ifa_family == AF_INET6) {
struct sockaddr_in6* sa = malloc(sizeof(struct sockaddr_in6));
memset(sa, 0, sizeof(struct sockaddr_in6));
sa->sin6_family = AF_INET6;
sa->sin6_scope_id = msg->ifa_index;
memcpy(&sa->sin6_addr, data, len);
ifaddr->ifa_addr = (struct sockaddr*)sa;
} else {
return -1;
}
return 0;
}
static int make_prefixes(struct ifaddrs* ifaddr, int family, int prefixlen) {
char* prefix = NULL;
if (family == AF_INET) {
struct sockaddr_in* mask = malloc(sizeof(struct sockaddr_in));
memset(mask, 0, sizeof(struct sockaddr_in));
mask->sin_family = AF_INET;
memset(&mask->sin_addr, 0, sizeof(struct in_addr));
ifaddr->ifa_netmask = (struct sockaddr*)mask;
if (prefixlen > 32) {
prefixlen = 32;
}
prefix = (char*)&mask->sin_addr;
} else if (family == AF_INET6) {
struct sockaddr_in6* mask = malloc(sizeof(struct sockaddr_in6));
memset(mask, 0, sizeof(struct sockaddr_in6));
mask->sin6_family = AF_INET6;
memset(&mask->sin6_addr, 0, sizeof(struct in6_addr));
ifaddr->ifa_netmask = (struct sockaddr*)mask;
if (prefixlen > 128) {
prefixlen = 128;
}
prefix = (char*)&mask->sin6_addr;
} else {
return -1;
}
for (int i = 0; i < (prefixlen / 8); i++) {
*prefix++ = 0xFF;
}
char remainder = 0xff;
remainder <<= (8 - prefixlen % 8);
*prefix = remainder;
return 0;
}
static int populate_ifaddrs(struct ifaddrs* ifaddr, struct ifaddrmsg* msg, void* bytes,
size_t len) {
if (set_ifname(ifaddr, msg->ifa_index) != 0) {
return -1;
}
if (set_flags(ifaddr) != 0) {
return -1;
}
if (set_addresses(ifaddr, msg, bytes, len) != 0) {
return -1;
}
if (make_prefixes(ifaddr, msg->ifa_family, msg->ifa_prefixlen) != 0) {
return -1;
}
return 0;
}
int android_getifaddrs(struct ifaddrs** result) {
int fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
if (fd < 0) {
return -1;
}
struct netlinkrequest ifaddr_request;
memset(&ifaddr_request, 0, sizeof(ifaddr_request));
ifaddr_request.header.nlmsg_flags = NLM_F_ROOT | NLM_F_REQUEST;
ifaddr_request.header.nlmsg_type = RTM_GETADDR;
ifaddr_request.header.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
ssize_t count = send(fd, &ifaddr_request, ifaddr_request.header.nlmsg_len, 0);
if ((size_t)count != ifaddr_request.header.nlmsg_len) {
close(fd);
return -1;
}
struct ifaddrs* start = NULL;
struct ifaddrs* current = NULL;
char buf[kMaxReadSize];
ssize_t amount_read = recv(fd, &buf, kMaxReadSize, 0);
while (amount_read > 0) {
struct nlmsghdr* header = (struct nlmsghdr*)&buf[0];
size_t header_size = (size_t)amount_read;
for ( ; NLMSG_OK(header, header_size);
header = NLMSG_NEXT(header, header_size)) {
switch (header->nlmsg_type) {
case NLMSG_DONE:
/* Success. Return. */
*result = start;
close(fd);
return 0;
case NLMSG_ERROR:
close(fd);
android_freeifaddrs(start);
return -1;
case RTM_NEWADDR: {
struct ifaddrmsg* address_msg =
(struct ifaddrmsg*)NLMSG_DATA(header);
struct rtattr* rta = IFA_RTA(address_msg);
ssize_t payload_len = IFA_PAYLOAD(header);
while (RTA_OK(rta, payload_len)) {
if (rta->rta_type == IFA_ADDRESS) {
int family = address_msg->ifa_family;
if (family == AF_INET || family == AF_INET6) {
struct ifaddrs* newest = malloc(sizeof(struct ifaddrs));
memset(newest, 0, sizeof(struct ifaddrs));
if (current) {
current->ifa_next = newest;
} else {
start = newest;
}
if (populate_ifaddrs(newest, address_msg, RTA_DATA(rta),
RTA_PAYLOAD(rta)) != 0) {
android_freeifaddrs(start);
*result = NULL;
return -1;
}
current = newest;
}
}
rta = RTA_NEXT(rta, payload_len);
}
break;
}
}
}
amount_read = recv(fd, &buf, kMaxReadSize, 0);
}
close(fd);
android_freeifaddrs(start);
return -1;
}
void android_freeifaddrs(struct ifaddrs* addrs) {
struct ifaddrs* last = NULL;
struct ifaddrs* cursor = addrs;
while (cursor) {
free(cursor->ifa_name);
free(cursor->ifa_addr);
free(cursor->ifa_netmask);
last = cursor;
cursor = cursor->ifa_next;
free(last);
}
}
#endif /* defined(ANDROID) */
@@ -23,6 +23,10 @@
#include <sched.h>
#include <errno.h>
#ifdef __ANDROID__
#include <pthread.h>
#include "ifaddrs-android.h"
#endif
// Some explanation is required here.
//
@@ -0,0 +1,62 @@
#ifdef __ANDROID__
/*
Copyright (c) 2011, The WebRTC project authors. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
* Neither the name of Google nor the names of its contributors may
be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef WEBRTC_BASE_IFADDRS_ANDROID_H_
#define WEBRTC_BASE_IFADDRS_ANDROID_H_
#include <stdio.h>
#include <sys/socket.h>
#include <netdb.h>
/* Implementation of getifaddrs for Android.
* Fills out a list of ifaddr structs (see below) which contain information
* about every network interface available on the host.
* See 'man getifaddrs' on Linux or OS X (nb: it is not a POSIX function). */
struct ifaddrs {
struct ifaddrs* ifa_next;
char* ifa_name;
unsigned int ifa_flags;
struct sockaddr* ifa_addr;
struct sockaddr* ifa_netmask;
union {
struct sockaddr *ifu_broadaddr;
struct sockaddr *ifu_dstaddr;
} ifa_ifu;
};
int android_getifaddrs(struct ifaddrs** result);
void android_freeifaddrs(struct ifaddrs* addrs);
#endif /* WEBRTC_BASE_IFADDRS_ANDROID_H_ */
#endif
View
@@ -17,7 +17,8 @@
#include <c_nio_linux.h>
#include <pthread.h>
#include <sched.h>
#include <stdio.h>
#include <sys/prctl.h>
_Static_assert(sizeof(CNIOLinux_mmsghdr) == sizeof(struct mmsghdr),
"sizes of CNIOLinux_mmsghdr and struct mmsghdr differ");
@@ -43,14 +44,52 @@ int CNIOLinux_pthread_setname_np(pthread_t thread, const char *name) {
}
int CNIOLinux_pthread_getname_np(pthread_t thread, char *name, size_t len) {
#ifdef __ANDROID__
// https://android.googlesource.com/platform/bionic/+/master/libc/bionic/pthread_setname_np.cpp#51
if (thread == pthread_self()) {
return TEMP_FAILURE_RETRY(prctl(PR_GET_NAME, name)) == -1 ? -1 : 0;
}
char comm_name[64];
snprintf(comm_name, sizeof(comm_name), "/proc/self/task/%d/comm", pthread_gettid_np(thread));
int fd = TEMP_FAILURE_RETRY(open(comm_name, O_CLOEXEC | O_RDONLY));
if (fd == -1) return -1;
ssize_t n = TEMP_FAILURE_RETRY(read(fd, name, len));
close(fd);
if (n == -1) return -1;
// The kernel adds a trailing '\n' to the /proc file,
// so this is actually the normal case for short names.
if (n > 0 && name[n - 1] == '\n') {
name[n - 1] = '\0';
return 0;
}
if (n >= 0 && len <= SSIZE_MAX && n == (ssize_t)len) return 1;
name[n] = '\0';
return 0;
#else
return pthread_getname_np(thread, name, len);
#endif
}
int CNIOLinux_pthread_setaffinity_np(pthread_t thread, size_t cpusetsize, const cpu_set_t *cpuset) {
#ifdef __ANDROID__
return sched_setaffinity(pthread_gettid_np(thread), cpusetsize, cpuset);
#else
return pthread_setaffinity_np(thread, cpusetsize, cpuset);
#endif
}
int CNIOLinux_pthread_getaffinity_np(pthread_t thread, size_t cpusetsize, cpu_set_t *cpuset) {
#ifdef __ANDROID__
return sched_getaffinity(pthread_gettid_np(thread), cpusetsize, cpuset);
#else
return pthread_getaffinity_np(thread, cpusetsize, cpuset);
#endif
}
void CNIOLinux_CPU_SET(int cpu, cpu_set_t *set) {
Oops, something went wrong.

0 comments on commit a59293c

Please sign in to comment.