diff options
author | Mikael Magnusson <mikma@users.sourceforge.net> | 2020-05-11 22:54:27 +0200 |
---|---|---|
committer | Mikael Magnusson <mikma@users.sourceforge.net> | 2020-05-11 22:54:27 +0200 |
commit | 9d93bc7e728b6fb098161a04160e7bd2798ffc01 (patch) | |
tree | ea025aace6c2336396990aa96abaad742582d590 /sysdep/unix/wg_user.c | |
parent | 6b3e5c45c43c75484a8d731013b380a58e90ca54 (diff) |
WIP Use abstract unix sockets on Android
Diffstat (limited to 'sysdep/unix/wg_user.c')
-rw-r--r-- | sysdep/unix/wg_user.c | 27 |
1 files changed, 20 insertions, 7 deletions
diff --git a/sysdep/unix/wg_user.c b/sysdep/unix/wg_user.c index db92ea8e..2a7e78b1 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; +#end + 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 { @@ -210,14 +224,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) { |