| 1 | /* |
| 2 | * This file is part of DisOrder. |
| 3 | * Copyright (C) 2004, 2007, 2008 Richard Kettlewell |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | /** @file lib/addr.c |
| 19 | * @brief Socket address support */ |
| 20 | |
| 21 | #include "common.h" |
| 22 | |
| 23 | #include <sys/types.h> |
| 24 | #include <sys/socket.h> |
| 25 | #include <netinet/in.h> |
| 26 | #include <arpa/inet.h> |
| 27 | #include <sys/un.h> |
| 28 | |
| 29 | #include "log.h" |
| 30 | #include "printf.h" |
| 31 | #include "addr.h" |
| 32 | #include "mem.h" |
| 33 | #include "syscalls.h" |
| 34 | #include "configuration.h" |
| 35 | #include "vector.h" |
| 36 | |
| 37 | /** @brief Convert a pair of strings to an address |
| 38 | * @param a Pointer to string list |
| 39 | * @param pref Hints structure for getaddrinfo, or NULL |
| 40 | * @param namep Where to store address description, or NULL |
| 41 | * @return Address info structure or NULL on error |
| 42 | * |
| 43 | * This converts one or two strings into an address specification suitable |
| 44 | * for passing to socket(), bind() etc. |
| 45 | * |
| 46 | * If there is only one string then it is assumed to be the service |
| 47 | * name (port number). If there are two then the first is the host |
| 48 | * name and the second the service name. |
| 49 | * |
| 50 | * @p namep is used to return a description of the address suitable |
| 51 | * for use in log messages. |
| 52 | * |
| 53 | * If an error occurs a message is logged and a null pointer returned. |
| 54 | */ |
| 55 | struct addrinfo *get_address(const struct stringlist *a, |
| 56 | const struct addrinfo *pref, |
| 57 | char **namep) { |
| 58 | struct addrinfo *res; |
| 59 | char *name; |
| 60 | int rc; |
| 61 | |
| 62 | switch(a->n) { |
| 63 | case 1: |
| 64 | byte_xasprintf(&name, "host * service %s", a->s[0]); |
| 65 | if((rc = getaddrinfo(0, a->s[0], pref, &res))) { |
| 66 | disorder_error(0, "getaddrinfo %s: %s", a->s[0], gai_strerror(rc)); |
| 67 | return 0; |
| 68 | } |
| 69 | break; |
| 70 | case 2: |
| 71 | byte_xasprintf(&name, "host %s service %s", a->s[0], a->s[1]); |
| 72 | if((rc = getaddrinfo(a->s[0], a->s[1], pref, &res))) { |
| 73 | disorder_error(0, "getaddrinfo %s %s: %s", |
| 74 | a->s[0], a->s[1], gai_strerror(rc)); |
| 75 | return 0; |
| 76 | } |
| 77 | break; |
| 78 | default: |
| 79 | disorder_error(0, "invalid network address specification (n=%d)", a->n); |
| 80 | return 0; |
| 81 | } |
| 82 | if(!res || (pref && res->ai_socktype != pref->ai_socktype)) { |
| 83 | disorder_error(0, "getaddrinfo didn't give us a suitable socket address"); |
| 84 | if(res) |
| 85 | freeaddrinfo(res); |
| 86 | return 0; |
| 87 | } |
| 88 | if(namep) |
| 89 | *namep = name; |
| 90 | return res; |
| 91 | } |
| 92 | |
| 93 | /** @brief Comparison function for address information |
| 94 | * |
| 95 | * Suitable for qsort(). |
| 96 | */ |
| 97 | int addrinfocmp(const struct addrinfo *a, |
| 98 | const struct addrinfo *b) { |
| 99 | if(a->ai_family != b->ai_family) return a->ai_family - b->ai_family; |
| 100 | if(a->ai_socktype != b->ai_socktype) return a->ai_socktype - b->ai_socktype; |
| 101 | if(a->ai_protocol != b->ai_protocol) return a->ai_protocol - b->ai_protocol; |
| 102 | return sockaddrcmp(a->ai_addr, b->ai_addr); |
| 103 | } |
| 104 | |
| 105 | /** @brief Comparison function for socket addresses |
| 106 | * |
| 107 | * Suitable for qsort(). |
| 108 | */ |
| 109 | int sockaddrcmp(const struct sockaddr *a, |
| 110 | const struct sockaddr *b) { |
| 111 | const struct sockaddr_in *ina, *inb; |
| 112 | const struct sockaddr_in6 *in6a, *in6b; |
| 113 | |
| 114 | if(a->sa_family != b->sa_family) return a->sa_family - b->sa_family; |
| 115 | switch(a->sa_family) { |
| 116 | case PF_INET: |
| 117 | ina = (const struct sockaddr_in *)a; |
| 118 | inb = (const struct sockaddr_in *)b; |
| 119 | if(ina->sin_port != inb->sin_port) return ina->sin_port - inb->sin_port; |
| 120 | return ina->sin_addr.s_addr - inb->sin_addr.s_addr; |
| 121 | break; |
| 122 | case PF_INET6: |
| 123 | in6a = (const struct sockaddr_in6 *)a; |
| 124 | in6b = (const struct sockaddr_in6 *)b; |
| 125 | if(in6a->sin6_port != in6b->sin6_port) |
| 126 | return in6a->sin6_port - in6b->sin6_port; |
| 127 | return memcmp(&in6a->sin6_addr, &in6b->sin6_addr, |
| 128 | sizeof (struct in6_addr)); |
| 129 | default: |
| 130 | disorder_fatal(0, "unsupported protocol family %d", a->sa_family); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | /** @brief Return nonzero if @p sin4 is an IPv4 multicast address */ |
| 135 | static inline int multicast4(const struct sockaddr_in *sin4) { |
| 136 | return IN_MULTICAST(ntohl(sin4->sin_addr.s_addr)); |
| 137 | } |
| 138 | |
| 139 | /** @brief Return nonzero if @p sin6 is an IPv6 multicast address */ |
| 140 | static inline int multicast6(const struct sockaddr_in6 *sin6) { |
| 141 | return IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr); |
| 142 | } |
| 143 | |
| 144 | /** @brief Return true if @p sa represents a multicast address */ |
| 145 | int multicast(const struct sockaddr *sa) { |
| 146 | switch(sa->sa_family) { |
| 147 | case AF_INET: |
| 148 | return multicast4((const struct sockaddr_in *)sa); |
| 149 | case AF_INET6: |
| 150 | return multicast6((const struct sockaddr_in6 *)sa); |
| 151 | default: |
| 152 | return 0; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | /** @brief Format an IPv4 address */ |
| 157 | static inline char *format_sockaddr4(const struct sockaddr_in *sin4) { |
| 158 | char buffer[1024], *r; |
| 159 | |
| 160 | if(sin4->sin_port) |
| 161 | byte_xasprintf(&r, "%s port %u", |
| 162 | inet_ntop(sin4->sin_family, &sin4->sin_addr, |
| 163 | buffer, sizeof buffer), |
| 164 | ntohs(sin4->sin_port)); |
| 165 | else |
| 166 | byte_xasprintf(&r, "%s", |
| 167 | inet_ntop(sin4->sin_family, &sin4->sin_addr, |
| 168 | buffer, sizeof buffer)); |
| 169 | return r; |
| 170 | } |
| 171 | |
| 172 | /** @brief Format an IPv6 address */ |
| 173 | static inline char *format_sockaddr6(const struct sockaddr_in6 *sin6) { |
| 174 | char buffer[1024], *r; |
| 175 | |
| 176 | if(sin6->sin6_port) |
| 177 | byte_xasprintf(&r, "%s port %u", |
| 178 | inet_ntop(sin6->sin6_family, &sin6->sin6_addr, |
| 179 | buffer, sizeof buffer), |
| 180 | ntohs(sin6->sin6_port)); |
| 181 | else |
| 182 | byte_xasprintf(&r, "%s", |
| 183 | inet_ntop(sin6->sin6_family, &sin6->sin6_addr, |
| 184 | buffer, sizeof buffer)); |
| 185 | return r; |
| 186 | } |
| 187 | |
| 188 | /** @brief Format a UNIX socket address */ |
| 189 | static inline char *format_sockaddrun(const struct sockaddr_un *sun) { |
| 190 | return xstrdup(sun->sun_path); |
| 191 | } |
| 192 | |
| 193 | /** @brief Construct a text description a sockaddr |
| 194 | * @param sa Socket address |
| 195 | * @return Human-readable form of address |
| 196 | */ |
| 197 | char *format_sockaddr(const struct sockaddr *sa) { |
| 198 | switch(sa->sa_family) { |
| 199 | case AF_INET: |
| 200 | return format_sockaddr4((const struct sockaddr_in *)sa); |
| 201 | case AF_INET6: |
| 202 | return format_sockaddr6((const struct sockaddr_in6 *)sa); |
| 203 | case AF_UNIX: |
| 204 | return format_sockaddrun((const struct sockaddr_un *)sa); |
| 205 | default: |
| 206 | return 0; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | /** @brief Parse the text form of a network address |
| 211 | * @param na Where to store result |
| 212 | * @param nvec Number of strings |
| 213 | * @param vec List of strings |
| 214 | * @return 0 on success, -1 on syntax error |
| 215 | */ |
| 216 | int netaddress_parse(struct netaddress *na, |
| 217 | int nvec, |
| 218 | char **vec) { |
| 219 | const char *port; |
| 220 | |
| 221 | na->af = AF_UNSPEC; |
| 222 | if(nvec > 0 && vec[0][0] == '-') { |
| 223 | if(!strcmp(vec[0], "-4")) |
| 224 | na->af = AF_INET; |
| 225 | else if(!strcmp(vec[0], "-6")) |
| 226 | na->af = AF_INET6; |
| 227 | else if(!strcmp(vec[0], "-unix")) |
| 228 | na->af = AF_UNIX; |
| 229 | else if(!strcmp(vec[0], "-")) |
| 230 | na->af = AF_UNSPEC; |
| 231 | else |
| 232 | return -1; |
| 233 | --nvec; |
| 234 | ++vec; |
| 235 | } |
| 236 | if(nvec == 0) |
| 237 | return -1; |
| 238 | /* Possibilities are: |
| 239 | * |
| 240 | * /path/to/unix/socket an AF_UNIX socket |
| 241 | * * PORT any address, specific port |
| 242 | * PORT any address, specific port |
| 243 | * ADDRESS PORT specific address, specific port |
| 244 | */ |
| 245 | if(vec[0][0] == '/' && na->af == AF_UNSPEC) |
| 246 | na->af = AF_UNIX; |
| 247 | if(na->af == AF_UNIX) { |
| 248 | if(nvec != 1) |
| 249 | return -1; |
| 250 | na->address = xstrdup(vec[0]); |
| 251 | na->port = -1; /* makes no sense */ |
| 252 | } else { |
| 253 | switch(nvec) { |
| 254 | case 1: |
| 255 | na->address = NULL; |
| 256 | port = vec[0]; |
| 257 | break; |
| 258 | case 2: |
| 259 | if(!strcmp(vec[0], "*")) |
| 260 | na->address = NULL; |
| 261 | else |
| 262 | na->address = xstrdup(vec[0]); |
| 263 | port = vec[1]; |
| 264 | break; |
| 265 | default: |
| 266 | return -1; |
| 267 | } |
| 268 | if(port[strspn(port, "0123456789")]) |
| 269 | return -1; |
| 270 | long p; |
| 271 | int e = xstrtol(&p, port, NULL, 10); |
| 272 | |
| 273 | if(e) |
| 274 | return -1; |
| 275 | if(p > 65535) |
| 276 | return -1; |
| 277 | na->port = (int)p; |
| 278 | } |
| 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | /** @brief Format a @ref netaddress structure |
| 283 | * @param na Network address to format |
| 284 | * @param nvecp Where to put string count, or NULL |
| 285 | * @param vecp Where to put strings |
| 286 | * |
| 287 | * The formatted form is suitable for passing to netaddress_parse(). |
| 288 | */ |
| 289 | void netaddress_format(const struct netaddress *na, |
| 290 | int *nvecp, |
| 291 | char ***vecp) { |
| 292 | struct vector v[1]; |
| 293 | |
| 294 | vector_init(v); |
| 295 | switch(na->af) { |
| 296 | case AF_UNSPEC: vector_append(v, xstrdup("-")); break; |
| 297 | case AF_INET: vector_append(v, xstrdup("-4")); break; |
| 298 | case AF_INET6: vector_append(v, xstrdup("-6")); break; |
| 299 | case AF_UNIX: vector_append(v, xstrdup("-unix")); break; |
| 300 | } |
| 301 | if(na->address) |
| 302 | vector_append(v, xstrdup(na->address)); |
| 303 | else |
| 304 | vector_append(v, xstrdup("*")); |
| 305 | if(na->port != -1) { |
| 306 | char buffer[64]; |
| 307 | |
| 308 | snprintf(buffer, sizeof buffer, "%d", na->port); |
| 309 | vector_append(v, xstrdup(buffer)); |
| 310 | } |
| 311 | vector_terminate(v); |
| 312 | if(nvecp) |
| 313 | *nvecp = v->nvec; |
| 314 | if(vecp) |
| 315 | *vecp = v->vec; |
| 316 | } |
| 317 | |
| 318 | /** @brief Resolve a network address |
| 319 | * @param na Address structure |
| 320 | * @param passive True if passive (bindable) address is desired |
| 321 | * @param protocol Protocol number desired (e.g. @c IPPROTO_TCP) |
| 322 | * @return List of suitable addresses or NULL |
| 323 | */ |
| 324 | struct addrinfo *netaddress_resolve(const struct netaddress *na, |
| 325 | int passive, |
| 326 | int protocol) { |
| 327 | struct addrinfo *res, hints[1]; |
| 328 | char service[64]; |
| 329 | int rc; |
| 330 | |
| 331 | memset(hints, 0, sizeof hints); |
| 332 | hints->ai_family = na->af; |
| 333 | hints->ai_protocol = protocol; |
| 334 | hints->ai_flags = passive ? AI_PASSIVE : 0; |
| 335 | snprintf(service, sizeof service, "%d", na->port); |
| 336 | rc = getaddrinfo(na->address, service, hints, &res); |
| 337 | if(rc) { |
| 338 | disorder_error(0, "getaddrinfo %s %d: %s", |
| 339 | na->address ? na->address : "*", |
| 340 | na->port, gai_strerror(rc)); |
| 341 | return NULL; |
| 342 | } |
| 343 | return res; |
| 344 | } |
| 345 | |
| 346 | /* |
| 347 | Local Variables: |
| 348 | c-basic-offset:2 |
| 349 | comment-column:40 |
| 350 | End: |
| 351 | */ |