[a62ceaf] | 1 | /*
|
---|
| 2 | * Copyright (c) 2016 Jiri Svoboda
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
[edeee9f] | 29 | /** @addtogroup libinet
|
---|
[a62ceaf] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file Internet host name
|
---|
| 33 | */
|
---|
| 34 |
|
---|
| 35 | #include <ctype.h>
|
---|
| 36 | #include <errno.h>
|
---|
| 37 | #include <inet/hostname.h>
|
---|
| 38 | #include <stdlib.h>
|
---|
[582a0b8] | 39 | #include <stddef.h>
|
---|
[a62ceaf] | 40 | #include <str.h>
|
---|
| 41 |
|
---|
| 42 | /** Parse host name
|
---|
| 43 | *
|
---|
| 44 | * Determine whether string begins with a valid host name and where that
|
---|
| 45 | * host name ends.
|
---|
| 46 | *
|
---|
| 47 | * Currntly accepting names per 'host' definition in RFC 1738
|
---|
| 48 | * (Uniform Resource Locators) - generic URI syntax not accepted.
|
---|
| 49 | *
|
---|
| 50 | * @param str String containing host name
|
---|
| 51 | * @param rname Place to store pointer to new string containing just the
|
---|
| 52 | * host name or @c NULL if not interested
|
---|
| 53 | * @param endptr Place to store pointer to next character in string or @c NULL
|
---|
| 54 | * if no extra characters in the string are allowed
|
---|
| 55 | *
|
---|
| 56 | * @return EOK on success. EINVAL if @a str does not start with a valid
|
---|
| 57 | * host name or if @a endptr is @c NULL and @a str contains
|
---|
| 58 | * extra characters at the end. ENOMEM if out of memory
|
---|
| 59 | */
|
---|
[b7fd2a0] | 60 | errno_t inet_hostname_parse(const char *str, char **rname, char **endptr)
|
---|
[a62ceaf] | 61 | {
|
---|
| 62 | const char *c;
|
---|
| 63 | const char *dstart = NULL;
|
---|
| 64 | char *name;
|
---|
| 65 |
|
---|
| 66 | c = str;
|
---|
| 67 | while (isalnum(*c)) {
|
---|
| 68 | /* Domain label */
|
---|
| 69 | dstart = c;
|
---|
| 70 |
|
---|
| 71 | do {
|
---|
| 72 | ++c;
|
---|
| 73 | } while (isalnum(*c) || *c == '-');
|
---|
| 74 |
|
---|
| 75 | /* Domain separator? */
|
---|
| 76 | if (*c != '.' || !isalnum(c[1]))
|
---|
| 77 | break;
|
---|
| 78 |
|
---|
| 79 | ++c;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | /*
|
---|
| 83 | * Last domain label must not start with a digit (to distinguish from
|
---|
| 84 | * IPv4 address)
|
---|
| 85 | */
|
---|
| 86 | if (dstart == NULL || !isalpha(*dstart))
|
---|
| 87 | return EINVAL;
|
---|
| 88 |
|
---|
| 89 | /* If endptr is null, check that entire string matched */
|
---|
| 90 | if (endptr == NULL && *c != '\0')
|
---|
| 91 | return EINVAL;
|
---|
| 92 |
|
---|
| 93 | if (endptr != NULL)
|
---|
| 94 | *endptr = (char *)c;
|
---|
| 95 |
|
---|
| 96 | if (rname != NULL) {
|
---|
| 97 | name = str_ndup(str, c - str);
|
---|
| 98 | if (name == NULL)
|
---|
| 99 | return ENOMEM;
|
---|
| 100 |
|
---|
| 101 | *rname = name;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | return EOK;
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | /** @}
|
---|
| 108 | */
|
---|