source: mainline/uspace/lib/c/generic/inet/hostname.c@ fcab7ef

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since fcab7ef was a62ceaf, checked in by Jiri Svoboda <jiri@…>, 9 years ago

Need better interfaces for handling internet host and host:port specifications.

  • Property mode set to 100644
File size: 3.1 KB
Line 
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
29/** @addtogroup libc
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>
39#include <str.h>
40
41/** Parse host name
42 *
43 * Determine whether string begins with a valid host name and where that
44 * host name ends.
45 *
46 * Currntly accepting names per 'host' definition in RFC 1738
47 * (Uniform Resource Locators) - generic URI syntax not accepted.
48 *
49 * @param str String containing host name
50 * @param rname Place to store pointer to new string containing just the
51 * host name or @c NULL if not interested
52 * @param endptr Place to store pointer to next character in string or @c NULL
53 * if no extra characters in the string are allowed
54 *
55 * @return EOK on success. EINVAL if @a str does not start with a valid
56 * host name or if @a endptr is @c NULL and @a str contains
57 * extra characters at the end. ENOMEM if out of memory
58 */
59int inet_hostname_parse(const char *str, char **rname, char **endptr)
60{
61 const char *c;
62 const char *dstart = NULL;
63 char *name;
64
65 c = str;
66 while (isalnum(*c)) {
67 /* Domain label */
68 dstart = c;
69
70 do {
71 ++c;
72 } while (isalnum(*c) || *c == '-');
73
74 /* Domain separator? */
75 if (*c != '.' || !isalnum(c[1]))
76 break;
77
78 ++c;
79 }
80
81 /*
82 * Last domain label must not start with a digit (to distinguish from
83 * IPv4 address)
84 */
85 if (dstart == NULL || !isalpha(*dstart))
86 return EINVAL;
87
88 /* If endptr is null, check that entire string matched */
89 if (endptr == NULL && *c != '\0')
90 return EINVAL;
91
92 if (endptr != NULL)
93 *endptr = (char *)c;
94
95 if (rname != NULL) {
96 name = str_ndup(str, c - str);
97 if (name == NULL)
98 return ENOMEM;
99
100 *rname = name;
101 }
102
103 return EOK;
104}
105
106/** @}
107 */
Note: See TracBrowser for help on using the repository browser.