socket-util.c revision eff05270986a13e7de93ae16311f654d3f7c166f
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering This file is part of systemd.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Copyright 2010 Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is free software; you can redistribute it and/or modify it
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering under the terms of the GNU Lesser General Public License as published by
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering (at your option) any later version.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is distributed in the hope that it will be useful, but
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Lesser General Public License for more details.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering You should have received a copy of the GNU Lesser General Public License
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringint socket_address_parse(SocketAddress *a, const char *s) {
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering if (*s == '[') {
6073b6f26ab9fc6bf335faa7073ec443eef093fdTom Gundersen /* IPv6 in [x:.....:z]:p notation */
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering log_warning("Binding to IPv6 address not available since kernel does not support IPv6.");
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering if (inet_pton(AF_INET6, n, &a->sockaddr.in6.sin6_addr) <= 0)
3e684349c2cead2e6fd2f816c34eb17daba23a49Lennart Poettering if (*e != ':')
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (u <= 0 || u > 0xFFFF)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering a->sockaddr.in6.sin6_port = htons((uint16_t) u);
cab5b05903096e1c9cf5575ccc73f89d15c8db69Lennart Poettering } else if (*s == '/') {
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen /* AF_UNIX socket */
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen a->size = offsetof(struct sockaddr_un, sun_path) + l + 1;
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen } else if (*s == '@') {
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen /* Abstract AF_UNIX socket */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (l >= sizeof(a->sockaddr.un.sun_path) - 1)
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen a->size = offsetof(struct sockaddr_un, sun_path) + 1 + l;
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen if (u <= 0 || u > 0xFFFF)
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen /* IPv4 in w.x.y.z:p notation? */
84129d46cd6e95e142973da93aede4c7433c9600Lennart Poettering r = inet_pton(AF_INET, n, &a->sockaddr.in.sin_addr);
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen /* Gotcha, it's a traditional IPv4 address */
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen a->sockaddr.in.sin_port = htons((uint16_t) u);
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen /* Uh, our last resort, an interface name */
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen log_warning("Binding to interface is not available since kernel does not support IPv6.");
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering a->sockaddr.in6.sin6_port = htons((uint16_t) u);
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering /* Just a port */
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt if (u <= 0 || u > 0xFFFF)
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering a->sockaddr.in6.sin6_port = htons((uint16_t) u);
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering a->sockaddr.in.sin_port = htons((uint16_t) u);
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering a->sockaddr.in.sin_addr.s_addr = INADDR_ANY;
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poetteringint socket_address_parse_netlink(SocketAddress *a, const char *s) {
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering if (sscanf(s, "%ms %u", &sfamily, &group) < 1)
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering family = netlink_family_from_string(sfamily);
assert(a);
switch (socket_address_family(a)) {
case AF_INET:
return -EINVAL;
return -EINVAL;
return -EINVAL;
case AF_INET6:
return -EINVAL;
return -EINVAL;
return -EINVAL;
case AF_UNIX:
return -EINVAL;
return -EINVAL;
return -EINVAL;
return -EINVAL;
case AF_NETLINK:
return -EINVAL;
return -EINVAL;
return -EAFNOSUPPORT;
assert(a);
r = socket_address_verify(a);
return -ENOMEM;
assert(a);
assert(a);
assert(b);
if (socket_address_verify(a) < 0 ||
socket_address_verify(b) < 0)
switch (socket_address_family(a)) {
case AF_INET:
case AF_INET6:
if (memcmp(&a->sockaddr.in6.sin6_addr, &b->sockaddr.in6.sin6_addr, sizeof(a->sockaddr.in6.sin6_addr)) != 0)
case AF_UNIX:
case AF_NETLINK:
struct SocketAddress b;
assert(a);
assert(s);
if (socket_address_parse(&b, s) < 0)
return socket_address_equal(a, &b);
struct SocketAddress b;
assert(a);
assert(s);
if (socket_address_parse_netlink(&b, s) < 0)
return socket_address_equal(a, &b);
assert(a);
return NULL;
return NULL;
bool socket_ipv6_is_supported(void) {
bool enabled;
free(l);
return enabled;
assert(a);
if (a->protocol != 0) {
case AF_INET:
case AF_INET6:
case AF_UNIX:
memcmp(sa.un.sun_path, a->sockaddr.un.sun_path, salen - offsetof(struct sockaddr_un, sun_path)) == 0;
case AF_INET: {
uint32_t a;
if (asprintf(&p,
return -ENOMEM;
case AF_INET6: {
static const unsigned char ipv4_prefix[] = {
if (asprintf(&p,
return -ENOMEM;
char a[INET6_ADDRSTRLEN];
if (asprintf(&p,
return -ENOMEM;
case AF_UNIX:
return -ENOMEM;
return -ENOMEM;
if (!ret)
return -ENOMEM;
return -ENOTSUP;
*ret = p;
return -errno;
return -ENOMEM;
return -errno;
static const char* const netlink_family_table[] = {
static const char* const socket_address_bind_ipv6_only_table[_SOCKET_ADDRESS_BIND_IPV6_ONLY_MAX] = {