1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* lib/krb5/krb/parse_host_string.c - Parse host strings into host and port */
4 * Copyright (C) 2016 by the Massachusetts Institute of Technology.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30 * OF THE POSSIBILITY OF SUCH DAMAGE.
36 /* Return true if s is composed solely of digits. */
38 k5_is_string_numeric(const char *s)
43 for (; *s != '\0'; s++) {
52 * Parse a string containing a host specifier. The expected format for the
57 * host and port are optional, though one must be present. host may have
58 * brackets around it for IPv6 addresses.
61 * address - The address string that should be parsed.
62 * default_port - The default port to use if no port is found.
63 * host_out - An output pointer for the parsed host, or NULL if no host was
64 * specified or an error occured. Must be freed.
65 * port_out - An output pointer for the parsed port. Will be 0 on error.
67 * Returns 0 on success, otherwise an error.
70 k5_parse_host_string(const char *address, int default_port, char **host_out,
75 const char *p, *host = NULL, *port = NULL;
76 char *endptr, *hostname = NULL;
83 if (address == NULL || *address == '\0' || *address == ':')
85 if (default_port < 0 || default_port > 65535)
88 /* Find the bounds of the host string and the start of the port string. */
89 if (k5_is_string_numeric(address)) {
91 } else if (*address == '[' && (p = strchr(address, ']')) != NULL) {
98 hostlen = strcspn(host, " \t:");
99 if (host[hostlen] == ':')
100 port = host + hostlen + 1;
103 /* Parse the port number, or use the default port. */
106 l = strtoul(port, &endptr, 10);
107 if (errno || endptr == port || *endptr != '\0' || l > 65535)
111 port_num = default_port;
114 /* Copy the host if it was specified. */
116 hostname = k5memdup0(host, hostlen, &ret);
117 if (hostname == NULL)
121 *host_out = hostname;
122 *port_out = port_num;