From fdfeec129ad893da265bb1d18335bbc9a808c9ba Mon Sep 17 00:00:00 2001 From: Mikael Magnusson Date: Mon, 11 Jan 2021 21:22:03 +0100 Subject: Wg-user: Use abstract unix sockets on Android --- sysdep/unix/wg_user.c | 27 ++++++++++++++++++++------- sysdep/unix/wg_user.h | 2 ++ 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/sysdep/unix/wg_user.c b/sysdep/unix/wg_user.c index 5f5428bf..2878ea1d 100644 --- a/sysdep/unix/wg_user.c +++ b/sysdep/unix/wg_user.c @@ -12,18 +12,32 @@ #include "sysdep/linux/wireguard.h" static -char *get_socket_path(const char *ifname, char *buf, uint size) +socklen_t get_socket_path(const char *ifname, char *buf, uint size) { - bsnprintf(buf, size, SOCKET_PATH "%s.sock", ifname); - return buf; + int pos = 0; + + if (size < 1) + return 0; + +#ifdef ANDROID + /* Abstract socket */ + buf[pos++] = 0; +#endif + bsnprintf(buf+pos, size-pos, SOCKET_PATH "%s.sock", ifname); + return pos + strlen(buf+pos); } bool wg_has_userspace(const char *ifname) { struct stat sb; char tmp[sizeof(struct sockaddr_un)]; + socklen_t tmplen = get_socket_path(ifname, tmp, sizeof(tmp)); - if (stat(get_socket_path(ifname, tmp, sizeof(tmp)), &sb) == 0) + if (tmplen > 0 && tmp[0] == 0) + /* System with abstract socket (Android) always use WireGuard's userspace + implementation. */ + return true; + else if (stat(tmp, &sb) == 0) return (sb.st_mode & S_IFMT) == S_IFSOCK; else { @@ -211,14 +225,13 @@ wg_user_set_device(struct pool *pool, struct wg_device *dev) { char path[sizeof(struct sockaddr_un)]; - - bsnprintf(path, sizeof(path), SOCKET_PATH "%s.sock", ifname); + socklen_t pathlen = get_socket_path(ifname, path, sizeof(path)); struct birdsock *sock = sk_new(pool); sock->rx_hook = user_rx_hook; sock->tx_hook = user_tx_hook; sock->err_hook = user_err_hook; - int res = sk_connect_unix(sock, path); + int res = sk_connect_unix(sock, path, pathlen); DBG(L_TRACE "WG: socket %d %d %s", res, sock->fd, path); if (res < 0) { diff --git a/sysdep/unix/wg_user.h b/sysdep/unix/wg_user.h index a5e4251a..5bd9a41a 100644 --- a/sysdep/unix/wg_user.h +++ b/sysdep/unix/wg_user.h @@ -3,7 +3,9 @@ #include "sysdep/config.h" +#ifndef SOCKET_PATH #define SOCKET_PATH PATH_RUNSTATEDIR "/wireguard/" +#endif /* SOCKET_PATH */ struct pool; struct wg_device; -- cgit v1.2.3