summaryrefslogtreecommitdiff
path: root/src/lib/libressl/ressl_util.c
diff options
context:
space:
mode:
authorjsing <>2014-07-12 01:20:25 +0000
committerjsing <>2014-07-12 01:20:25 +0000
commit2b0153d4f076d501b21de2e54937a5bb1b139635 (patch)
treecb217fd8fb935cea61fc26de366b7e407229df65 /src/lib/libressl/ressl_util.c
parentc95157e4b6c5e281cb496ef41f9969df25abef91 (diff)
downloadopenbsd-2b0153d4f076d501b21de2e54937a5bb1b139635.tar.gz
openbsd-2b0153d4f076d501b21de2e54937a5bb1b139635.tar.bz2
openbsd-2b0153d4f076d501b21de2e54937a5bb1b139635.zip
Initial version of libressl - a library that provides a clean, simple,
consistent and secure-by-default API for SSL clients (and soon servers). This is a long way from complete and the interface will likely change substantially - committing now so that further work can happen in the tree. Initiated by tedu@ and inspired by discussions with tedu@, beck@ and other developers.
Diffstat (limited to 'src/lib/libressl/ressl_util.c')
-rw-r--r--src/lib/libressl/ressl_util.c80
1 files changed, 80 insertions, 0 deletions
diff --git a/src/lib/libressl/ressl_util.c b/src/lib/libressl/ressl_util.c
new file mode 100644
index 0000000000..ee7b1edbd3
--- /dev/null
+++ b/src/lib/libressl/ressl_util.c
@@ -0,0 +1,80 @@
1/*
2 * Copyright (c) 2014 Joel Sing <jsing@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17#include <stdlib.h>
18
19#include "ressl_internal.h"
20
21/*
22 * Extract the host and port from a colon separated value. For a literal IPv6
23 * address the address must be contained with square braces. If a host and
24 * port are successfully extracted, the function will return 0 and the
25 * caller is responsible for freeing the host and port. If no port is found
26 * then the function will return 1, with both host and port being NULL.
27 * On memory allocation failure -1 will be returned.
28 */
29int
30ressl_host_port(const char *hostport, char **host, char **port)
31{
32 char *h, *p, *s;
33 int rv = 1;
34
35 *host = NULL;
36 *port = NULL;
37
38 if ((s = strdup(hostport)) == NULL)
39 goto fail;
40
41 h = p = s;
42
43 /* See if this is an IPv6 literal with square braces. */
44 if (p[0] == '[') {
45 h++;
46 if ((p = strchr(s, ']')) == NULL)
47 goto done;
48 *p++ = '\0';
49 }
50
51 /* Find the port seperator. */
52 if ((p = strchr(p, ':')) == NULL)
53 goto done;
54
55 /* If there is another separator then we have issues. */
56 if (strchr(p + 1, ':') != NULL)
57 goto done;
58
59 *p++ = '\0';
60
61 if (asprintf(host, "%s", h) == -1)
62 goto fail;
63 if (asprintf(port, "%s", p) == -1)
64 goto fail;
65
66 rv = 0;
67 goto done;
68
69fail:
70 free(*host);
71 *host = NULL;
72 free(*port);
73 *port = NULL;
74 rv = -1;
75
76done:
77 free(s);
78
79 return (rv);
80}