2012-07-29 12:50:54 +00:00
|
|
|
/*
|
2013-09-18 07:06:28 +00:00
|
|
|
Serval DNA named sockets
|
|
|
|
Copyright 2013 Serval Project Inc.
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
2012-07-29 12:50:54 +00:00
|
|
|
|
2013-09-18 07:06:28 +00:00
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
2012-07-29 12:50:54 +00:00
|
|
|
|
2013-09-20 04:37:19 +00:00
|
|
|
#include <limits.h>
|
|
|
|
#include <stdlib.h>
|
2013-09-25 07:20:41 +00:00
|
|
|
#include <assert.h>
|
2014-04-11 07:19:01 +00:00
|
|
|
#include <libgen.h>
|
2013-09-20 04:37:19 +00:00
|
|
|
|
2013-09-18 07:06:28 +00:00
|
|
|
#include "serval.h"
|
2012-07-29 12:50:54 +00:00
|
|
|
#include "conf.h"
|
|
|
|
#include "log.h"
|
2013-09-18 07:06:28 +00:00
|
|
|
#include "strbuf_helpers.h"
|
2013-11-25 01:45:20 +00:00
|
|
|
#include "socket.h"
|
2012-07-29 12:50:54 +00:00
|
|
|
|
2014-03-26 05:05:43 +00:00
|
|
|
/* Form the name of an AF_UNIX (local) socket in the /var/run/serval (or instance) directory as an
|
|
|
|
* absolute path. Under Linux, this will create a socket name in the abstract namespace. This
|
|
|
|
* permits us to use local sockets on Android despite its lack of a shared writeable directory on a
|
|
|
|
* UFS partition.
|
2013-09-20 04:37:19 +00:00
|
|
|
*
|
|
|
|
* The absolute file name is resolved to its real path using realpath(3), to ensure that name
|
|
|
|
* comparisons of addresses returned by recvmsg(2) can reliably be used on systems where the
|
|
|
|
* instance path may have a symbolic link in it.
|
2013-09-18 07:06:28 +00:00
|
|
|
*
|
2013-09-20 04:37:19 +00:00
|
|
|
* Returns -1 if the path name overruns the size of a sockaddr_un structure, or if realpath(3) fails
|
|
|
|
* with an error. The contents of *addr and *addrlen are undefined in this case.
|
2013-09-18 07:06:28 +00:00
|
|
|
*
|
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
* @author Daniel O'Connor <daniel@servalproject.com>
|
2012-07-29 12:50:54 +00:00
|
|
|
*/
|
2013-11-25 01:45:20 +00:00
|
|
|
int _make_local_sockaddr(struct __sourceloc __whence, struct socket_address *addr, const char *fmt, ...)
|
2013-09-18 07:06:28 +00:00
|
|
|
{
|
|
|
|
bzero(addr, sizeof(*addr));
|
2013-11-29 02:26:59 +00:00
|
|
|
addr->local.sun_family = AF_UNIX;
|
2013-09-19 07:56:06 +00:00
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
2014-03-26 05:05:43 +00:00
|
|
|
int r = vformf_serval_run_path(addr->local.sun_path, sizeof addr->local.sun_path, fmt, ap);
|
2013-09-19 07:56:06 +00:00
|
|
|
va_end(ap);
|
2013-09-20 04:37:19 +00:00
|
|
|
if (!r)
|
|
|
|
return WHY("socket name overflow");
|
2013-11-29 02:26:59 +00:00
|
|
|
addr->addrlen=sizeof addr->local.sun_family + strlen(addr->local.sun_path) + 1;
|
2013-11-25 01:45:20 +00:00
|
|
|
// TODO perform real path transformation in making the serval instance path
|
|
|
|
// if (real_sockaddr(addr, addr) == -1)
|
|
|
|
// return -1;
|
|
|
|
|
2012-07-29 12:50:54 +00:00
|
|
|
#ifdef USE_ABSTRACT_NAMESPACE
|
2013-09-19 07:56:06 +00:00
|
|
|
// For the abstract name we use the absolute path name with the initial '/' replaced by the
|
|
|
|
// leading nul. This ensures that different instances of the Serval daemon have different socket
|
|
|
|
// names.
|
2013-11-29 02:26:59 +00:00
|
|
|
addr->local.sun_path[0] = '\0'; // mark as Linux abstract socket
|
2013-11-25 01:45:20 +00:00
|
|
|
--addr->addrlen; // do not count trailing nul in abstract socket name
|
2013-09-19 07:56:06 +00:00
|
|
|
#endif // USE_ABSTRACT_NAMESPACE
|
2013-09-18 07:06:28 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-09-20 04:37:19 +00:00
|
|
|
/* Converts an AF_UNIX local socket file name to contain a real path name using realpath(3), leaves
|
|
|
|
* all other socket types intact, including abstract local socket names. Returns -1 in case of an
|
|
|
|
* error from realpath(3) or a buffer overflow, without modifying *dst_addr or *dst_addrlen.
|
|
|
|
* Returns 1 if the path is changed and puts the modified path in *dst_addr and *dst_addrlen.
|
|
|
|
* Returns 0 if not the path is not changed and copies from *src_addr to *dst_addr, src_addrlen to
|
|
|
|
* *dst_addrlen.
|
|
|
|
*
|
|
|
|
* Can safely be used to perform an in-place conversion by using src_addr == dst_addr and
|
|
|
|
* dst_addrlen == &src_addrlen.
|
|
|
|
*
|
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
2013-11-25 01:45:20 +00:00
|
|
|
int real_sockaddr(const struct socket_address *src_addr, struct socket_address *dst_addr)
|
2013-09-20 04:37:19 +00:00
|
|
|
{
|
2013-12-10 06:22:53 +00:00
|
|
|
assert(src_addr->addrlen > sizeof src_addr->local.sun_family);
|
|
|
|
size_t src_path_len = src_addr->addrlen - sizeof src_addr->local.sun_family;
|
2013-11-29 02:26:59 +00:00
|
|
|
if ( src_addr->addrlen >= sizeof src_addr->local.sun_family + 1
|
|
|
|
&& src_addr->local.sun_family == AF_UNIX
|
|
|
|
&& src_addr->local.sun_path[0] != '\0'
|
|
|
|
&& src_addr->local.sun_path[src_path_len - 1] == '\0'
|
2013-09-20 04:37:19 +00:00
|
|
|
) {
|
|
|
|
char real_path[PATH_MAX];
|
|
|
|
size_t real_path_len;
|
2013-11-29 02:26:59 +00:00
|
|
|
if (realpath(src_addr->local.sun_path, real_path) == NULL)
|
|
|
|
return WHYF_perror("realpath(%s)", alloca_str_toprint(src_addr->local.sun_path));
|
|
|
|
else if ((real_path_len = strlen(real_path) + 1) > sizeof dst_addr->local.sun_path)
|
2013-11-25 01:45:20 +00:00
|
|
|
return WHYF("sockaddr overrun: realpath(%s) returned %s",
|
2013-11-29 02:26:59 +00:00
|
|
|
alloca_str_toprint(src_addr->local.sun_path), alloca_str_toprint(real_path));
|
2013-09-20 04:37:19 +00:00
|
|
|
else if ( real_path_len != src_path_len
|
2013-11-29 02:26:59 +00:00
|
|
|
|| memcmp(real_path, src_addr->local.sun_path, src_path_len) != 0
|
2013-09-20 04:37:19 +00:00
|
|
|
) {
|
2013-11-29 02:26:59 +00:00
|
|
|
memcpy(dst_addr->local.sun_path, real_path, real_path_len);
|
|
|
|
dst_addr->addrlen = real_path_len + sizeof dst_addr->local.sun_family;
|
2013-09-20 04:37:19 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2013-11-25 01:45:20 +00:00
|
|
|
if (dst_addr != src_addr){
|
|
|
|
memcpy(&dst_addr->addr, &src_addr->addr, src_addr->addrlen);
|
|
|
|
dst_addr->addrlen = src_addr->addrlen;
|
|
|
|
}
|
2013-09-20 04:37:19 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compare any two struct sockaddr. Return -1, 0 or 1. Copes with invalid and truncated sockaddr
|
|
|
|
* structures.
|
2013-09-19 07:56:06 +00:00
|
|
|
*
|
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
2013-11-25 01:45:20 +00:00
|
|
|
int cmp_sockaddr(const struct socket_address *addrA, const struct socket_address *addrB)
|
2013-09-19 07:56:06 +00:00
|
|
|
{
|
|
|
|
// Two zero-length sockaddrs are equal.
|
2013-11-25 01:45:20 +00:00
|
|
|
if (addrA->addrlen == 0 && addrB->addrlen == 0)
|
2013-09-19 07:56:06 +00:00
|
|
|
return 0;
|
|
|
|
// If either sockaddr is truncated, then we compare the bytes we have.
|
2013-11-25 01:45:20 +00:00
|
|
|
if (addrA->addrlen < sizeof addrA->addr.sa_family || addrB->addrlen < sizeof addrB->addr.sa_family) {
|
|
|
|
int c = memcmp(addrA, addrB, addrA->addrlen < addrB->addrlen ? addrA->addrlen : addrB->addrlen);
|
2013-09-19 07:56:06 +00:00
|
|
|
if (c == 0)
|
2013-11-25 01:45:20 +00:00
|
|
|
c = addrA->addrlen < addrB->addrlen ? -1 : addrA->addrlen > addrB->addrlen ? 1 : 0;
|
2013-09-19 07:56:06 +00:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
// Order first by address family.
|
2013-11-25 01:45:20 +00:00
|
|
|
if (addrA->addr.sa_family < addrB->addr.sa_family)
|
2013-09-19 07:56:06 +00:00
|
|
|
return -1;
|
2013-11-25 01:45:20 +00:00
|
|
|
if (addrA->addr.sa_family > addrB->addr.sa_family)
|
2013-09-19 07:56:06 +00:00
|
|
|
return 1;
|
|
|
|
// Both addresses are in the same family...
|
2013-11-25 01:45:20 +00:00
|
|
|
switch (addrA->addr.sa_family) {
|
2014-01-20 05:31:24 +00:00
|
|
|
case AF_INET: {
|
|
|
|
if (addrA->inet.sin_addr.s_addr < addrB->inet.sin_addr.s_addr)
|
|
|
|
return -1;
|
|
|
|
if (addrA->inet.sin_addr.s_addr > addrB->inet.sin_addr.s_addr)
|
|
|
|
return 1;
|
|
|
|
if (addrA->inet.sin_port < addrB->inet.sin_port)
|
|
|
|
return -1;
|
|
|
|
if (addrA->inet.sin_port > addrB->inet.sin_port)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}break;
|
2013-09-19 07:56:06 +00:00
|
|
|
case AF_UNIX: {
|
2013-11-29 02:26:59 +00:00
|
|
|
unsigned pathlenA = addrA->addrlen - sizeof (addrA->local.sun_family);
|
|
|
|
unsigned pathlenB = addrB->addrlen - sizeof (addrB->local.sun_family);
|
2013-09-20 04:37:19 +00:00
|
|
|
int c;
|
2013-09-19 07:56:06 +00:00
|
|
|
if ( pathlenA > 1 && pathlenB > 1
|
2013-11-29 02:26:59 +00:00
|
|
|
&& addrA->local.sun_path[0] == '\0'
|
|
|
|
&& addrB->local.sun_path[0] == '\0'
|
2013-09-19 07:56:06 +00:00
|
|
|
) {
|
|
|
|
// Both abstract sockets - just compare names, nul bytes are not terminators.
|
2013-11-29 02:26:59 +00:00
|
|
|
c = memcmp(&addrA->local.sun_path[1],
|
|
|
|
&addrB->local.sun_path[1],
|
2013-09-20 04:37:19 +00:00
|
|
|
(pathlenA < pathlenB ? pathlenA : pathlenB) - 1);
|
|
|
|
} else {
|
|
|
|
// Either or both are named local file sockets. If the file names are identical up to the
|
|
|
|
// first nul, then the addresses are equal. This collates abstract socket names, whose first
|
|
|
|
// character is a nul, ahead of all non-empty file socket names.
|
2013-11-29 02:26:59 +00:00
|
|
|
c = strncmp(addrA->local.sun_path,
|
|
|
|
addrB->local.sun_path,
|
2013-09-20 04:37:19 +00:00
|
|
|
(pathlenA < pathlenB ? pathlenA : pathlenB));
|
2013-09-19 07:56:06 +00:00
|
|
|
}
|
|
|
|
if (c == 0)
|
|
|
|
c = pathlenA < pathlenB ? -1 : pathlenA > pathlenB ? 1 : 0;
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2013-09-20 04:37:19 +00:00
|
|
|
// Fall back to comparing raw data bytes.
|
2013-11-25 01:45:20 +00:00
|
|
|
int c = memcmp(addrA->addr.sa_data, addrB->addr.sa_data,
|
|
|
|
(addrA->addrlen < addrB->addrlen ? addrA->addrlen : addrB->addrlen) - sizeof addrA->addr.sa_family);
|
2013-09-19 07:56:06 +00:00
|
|
|
if (c == 0)
|
2013-11-25 01:45:20 +00:00
|
|
|
c = addrA->addrlen < addrB->addrlen ? -1 : addrA->addrlen > addrB->addrlen ? 1 : 0;
|
2013-12-09 07:15:47 +00:00
|
|
|
|
2013-09-19 07:56:06 +00:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2013-09-18 18:39:16 +00:00
|
|
|
int _esocket(struct __sourceloc __whence, int domain, int type, int protocol)
|
2013-09-18 07:06:28 +00:00
|
|
|
{
|
|
|
|
int fd;
|
2013-09-18 18:41:00 +00:00
|
|
|
if ((fd = socket(domain, type, protocol)) == -1)
|
2013-09-18 07:06:28 +00:00
|
|
|
return WHYF_perror("socket(%s, %s, 0)", alloca_socket_domain(domain), alloca_socket_type(type));
|
|
|
|
if (config.debug.io || config.debug.verbose_io)
|
|
|
|
DEBUGF("socket(%s, %s, 0) -> %d", alloca_socket_domain(domain), alloca_socket_type(type), fd);
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2014-02-20 04:14:38 +00:00
|
|
|
int _socket_connect(struct __sourceloc __whence, int sock, const struct socket_address *addr)
|
2013-09-18 07:06:28 +00:00
|
|
|
{
|
2014-02-20 04:14:38 +00:00
|
|
|
if (connect(sock, &addr->addr, addr->addrlen) == -1)
|
|
|
|
return WHYF_perror("connect(%d,%s,%lu)", sock, alloca_socket_address(addr), (unsigned long)addr->addrlen);
|
2013-09-18 07:06:28 +00:00
|
|
|
if (config.debug.io || config.debug.verbose_io)
|
2014-02-20 04:14:38 +00:00
|
|
|
DEBUGF("connect(%d, %s, %lu)", sock, alloca_socket_address(addr), (unsigned long)addr->addrlen);
|
2013-09-18 07:06:28 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-02-20 04:14:38 +00:00
|
|
|
int _socket_bind(struct __sourceloc __whence, int sock, const struct socket_address *addr)
|
2013-09-18 07:06:28 +00:00
|
|
|
{
|
2014-02-20 04:14:38 +00:00
|
|
|
assert(addr->addrlen > sizeof addr->addr.sa_family);
|
|
|
|
if (addr->addr.sa_family == AF_UNIX && addr->local.sun_path[0] != '\0') {
|
|
|
|
assert(addr->local.sun_path[addr->addrlen - sizeof addr->local.sun_family - 1] == '\0');
|
2014-04-11 07:19:01 +00:00
|
|
|
// make sure the path exists, create it if we can
|
|
|
|
size_t dirsiz = strlen(addr->local.sun_path) + 1;
|
|
|
|
char dir_buf[dirsiz];
|
|
|
|
strcpy(dir_buf, addr->local.sun_path);
|
|
|
|
const char *dir = dirname(dir_buf); // modifies dir_buf[]
|
|
|
|
if (mkdirs_info(dir, 0700) == -1)
|
|
|
|
return WHY_perror("mkdirs()");
|
|
|
|
// remove a previous socket
|
2014-02-20 04:14:38 +00:00
|
|
|
if (unlink(addr->local.sun_path) == -1 && errno != ENOENT)
|
|
|
|
WARNF_perror("unlink(%s)", alloca_str_toprint(addr->local.sun_path));
|
2013-09-18 07:06:28 +00:00
|
|
|
if (config.debug.io || config.debug.verbose_io)
|
2014-02-20 04:14:38 +00:00
|
|
|
DEBUGF("unlink(%s)", alloca_str_toprint(addr->local.sun_path));
|
2013-09-18 07:06:28 +00:00
|
|
|
}
|
2014-02-20 04:14:38 +00:00
|
|
|
if (bind(sock, &addr->addr, addr->addrlen) == -1)
|
|
|
|
return WHYF_perror("bind(%d,%s,%lu)", sock, alloca_socket_address(addr), (unsigned long)addr->addrlen);
|
2013-09-18 07:06:28 +00:00
|
|
|
if (config.debug.io || config.debug.verbose_io)
|
2014-02-20 04:14:38 +00:00
|
|
|
DEBUGF("bind(%d, %s, %lu)", sock, alloca_socket_address(addr), (unsigned long)addr->addrlen);
|
2013-09-18 07:06:28 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-09-18 18:39:16 +00:00
|
|
|
int _socket_listen(struct __sourceloc __whence, int sock, int backlog)
|
2013-09-18 07:06:28 +00:00
|
|
|
{
|
|
|
|
if (listen(sock, backlog) == -1)
|
|
|
|
return WHYF_perror("listen(%d,%d)", sock, backlog);
|
|
|
|
if (config.debug.io || config.debug.verbose_io)
|
|
|
|
DEBUGF("listen(%d, %d)", sock, backlog);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-09-18 18:39:16 +00:00
|
|
|
int _socket_set_reuseaddr(struct __sourceloc __whence, int sock, int reuseP)
|
2013-09-18 07:06:28 +00:00
|
|
|
{
|
|
|
|
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuseP, sizeof reuseP) == -1) {
|
|
|
|
WARNF_perror("setsockopt(%d,SOL_SOCKET,SO_REUSEADDR,&%d,%u)", sock, reuseP, (unsigned)sizeof reuseP);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (config.debug.io || config.debug.verbose_io)
|
|
|
|
DEBUGF("setsockopt(%d, SOL_SOCKET, SO_REUSEADDR, &%d, %u)", sock, reuseP, (unsigned)sizeof reuseP);
|
|
|
|
return 0;
|
|
|
|
}
|
2012-07-29 12:50:54 +00:00
|
|
|
|
2013-09-18 18:39:16 +00:00
|
|
|
int _socket_set_rcvbufsize(struct __sourceloc __whence, int sock, unsigned buffer_size)
|
2013-09-18 07:06:28 +00:00
|
|
|
{
|
|
|
|
if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &buffer_size, sizeof buffer_size) == -1) {
|
|
|
|
WARNF_perror("setsockopt(%d,SOL_SOCKET,SO_RCVBUF,&%u,%u)", sock, buffer_size, (unsigned)sizeof buffer_size);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (config.debug.io || config.debug.verbose_io)
|
|
|
|
DEBUGF("setsockopt(%d, SOL_SOCKET, SO_RCVBUF, &%u, %u)", sock, buffer_size, (unsigned)sizeof buffer_size);
|
|
|
|
return 0;
|
2012-07-29 12:50:54 +00:00
|
|
|
}
|
2013-11-25 01:45:20 +00:00
|
|
|
|
2013-12-09 07:15:47 +00:00
|
|
|
int socket_unlink_close(int sock)
|
|
|
|
{
|
|
|
|
// get the socket name and unlink it from the filesystem if not abstract
|
|
|
|
struct socket_address addr;
|
|
|
|
addr.addrlen = sizeof addr.store;
|
|
|
|
if (getsockname(sock, &addr.addr, &addr.addrlen))
|
|
|
|
WHYF_perror("getsockname(%d)", sock);
|
|
|
|
else if (addr.addr.sa_family==AF_UNIX
|
|
|
|
&& addr.addrlen > sizeof addr.local.sun_family
|
|
|
|
&& addr.addrlen <= sizeof addr.local
|
|
|
|
&& addr.local.sun_path[0] != '\0') {
|
|
|
|
if (unlink(addr.local.sun_path) == -1)
|
|
|
|
WARNF_perror("unlink(%s)", alloca_str_toprint(addr.local.sun_path));
|
|
|
|
}
|
|
|
|
close(sock);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-11-25 01:45:20 +00:00
|
|
|
ssize_t _send_message(struct __sourceloc __whence, int fd, const struct socket_address *address, const struct fragmented_data *data)
|
|
|
|
{
|
|
|
|
struct msghdr hdr={
|
|
|
|
.msg_name=(void *)&address->addr,
|
|
|
|
.msg_namelen=address->addrlen,
|
|
|
|
.msg_iov=(struct iovec*)data->iov,
|
|
|
|
.msg_iovlen=data->fragment_count,
|
|
|
|
};
|
|
|
|
|
|
|
|
ssize_t ret = sendmsg(fd, &hdr, 0);
|
|
|
|
if (ret==-1)
|
|
|
|
WHYF_perror("sendmsg(%d,%s,%lu)", fd, alloca_socket_address(address), (unsigned long)address->addrlen);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t _recv_message(struct __sourceloc __whence, int fd, struct socket_address *address, struct fragmented_data *data)
|
|
|
|
{
|
|
|
|
struct msghdr hdr={
|
|
|
|
.msg_name=(void *)&address->addr,
|
|
|
|
.msg_namelen=address->addrlen,
|
|
|
|
.msg_iov=data->iov,
|
|
|
|
.msg_iovlen=data->fragment_count,
|
|
|
|
};
|
|
|
|
ssize_t ret = recvmsg(fd, &hdr, 0);
|
|
|
|
if (ret==-1)
|
|
|
|
WHYF_perror("recvmsg(%d,%s,%lu)", fd, alloca_socket_address(address), (unsigned long)address->addrlen);
|
2013-12-09 07:15:47 +00:00
|
|
|
address->addrlen = hdr.msg_namelen;
|
2013-11-25 01:45:20 +00:00
|
|
|
return ret;
|
|
|
|
}
|