1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
systemd is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
/* stuff imported from systemd without any changes */
#ifndef _GNU_SOURCE
# define _GNU_SOURCE
#endif
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <net/if.h>
#include "util.h"
int safe_atou(const char *s, unsigned *ret_u) {
char *x = NULL;
unsigned long l;
assert(s);
assert(ret_u);
/* strtoul() is happy to parse negative values, and silently
* converts them to unsigned values without generating an
* error. We want a clean error, hence let's look for the "-"
* prefix on our own, and generate an error. But let's do so
* only after strtoul() validated that the string is clean
* otherwise, so that we return EINVAL preferably over
* ERANGE. */
errno = 0;
l = strtoul(s, &x, 0);
if (errno > 0)
return -errno;
if (!x || x == s || *x)
return -EINVAL;
if (s[0] == '-')
return -ERANGE;
if ((unsigned long) (unsigned) l != l)
return -ERANGE;
*ret_u = (unsigned) l;
return 0;
}
static bool socket_ipv6_is_supported(void) {
if (access("/proc/net/if_inet6", F_OK) != 0)
return false;
return true;
}
static int assign_address(const char *s,
uint16_t port,
union sockaddr_union *addr, unsigned *addr_len) {
int r;
/* IPv4 in w.x.y.z:p notation? */
r = inet_pton(AF_INET, s, &addr->in.sin_addr);
if (r < 0)
return -errno;
if (r > 0) {
/* Gotcha, it's a traditional IPv4 address */
addr->in.sin_family = AF_INET;
addr->in.sin_port = htobe16(port);
*addr_len = sizeof(struct sockaddr_in);
} else {
unsigned idx;
if (strlen(s) > IF_NAMESIZE-1)
return -EINVAL;
/* Uh, our last resort, an interface name */
idx = if_nametoindex(s);
if (idx == 0)
return -EINVAL;
addr->in6.sin6_family = AF_INET6;
addr->in6.sin6_port = htobe16(port);
addr->in6.sin6_scope_id = idx;
addr->in6.sin6_addr = in6addr_any;
*addr_len = sizeof(struct sockaddr_in6);
}
return 0;
}
int parse_sockaddr(const char *s,
union sockaddr_union *addr, unsigned *addr_len) {
char *e, *n;
unsigned u;
int r;
if (*s == '[') {
/* IPv6 in [x:.....:z]:p notation */
e = strchr(s+1, ']');
if (!e)
return -EINVAL;
n = strndupa(s+1, e-s-1);
errno = 0;
if (inet_pton(AF_INET6, n, &addr->in6.sin6_addr) <= 0)
return errno > 0 ? -errno : -EINVAL;
e++;
if (*e) {
if (*e != ':')
return -EINVAL;
e++;
r = safe_atou(e, &u);
if (r < 0)
return r;
if (u <= 0 || u > 0xFFFF)
return -EINVAL;
addr->in6.sin6_port = htobe16((uint16_t)u);
}
addr->in6.sin6_family = AF_INET6;
*addr_len = sizeof(struct sockaddr_in6);
} else {
e = strchr(s, ':');
if (e) {
r = safe_atou(e+1, &u);
if (r < 0)
return r;
if (u <= 0 || u > 0xFFFF)
return -EINVAL;
n = strndupa(s, e-s);
return assign_address(n, u, addr, addr_len);
} else {
r = safe_atou(s, &u);
if (r < 0)
return assign_address(s, 0, addr, addr_len);
/* Just a port */
if (u <= 0 || u > 0xFFFF)
return -EINVAL;
if (socket_ipv6_is_supported()) {
addr->in6.sin6_family = AF_INET6;
addr->in6.sin6_port = htobe16((uint16_t)u);
addr->in6.sin6_addr = in6addr_any;
*addr_len = sizeof(struct sockaddr_in6);
} else {
addr->in.sin_family = AF_INET;
addr->in.sin_port = htobe16((uint16_t)u);
addr->in.sin_addr.s_addr = INADDR_ANY;
*addr_len = sizeof(struct sockaddr_in);
}
}
}
return 0;
}
|