summaryrefslogtreecommitdiffstats
path: root/lib/libtls/tls_util.c
diff options
context:
space:
mode:
authorjsing <jsing@openbsd.org>2014-10-31 13:46:17 +0000
committerjsing <jsing@openbsd.org>2014-10-31 13:46:17 +0000
commitb600beed7ba1c10835be75e1838655c890ab04e6 (patch)
tree0f89b091789322b4c938c23a6f23a7966ea3f8b9 /lib/libtls/tls_util.c
parentredirect error output to stderr instead of stdout (diff)
downloadwireguard-openbsd-b600beed7ba1c10835be75e1838655c890ab04e6.tar.xz
wireguard-openbsd-b600beed7ba1c10835be75e1838655c890ab04e6.zip
Rename libressl to libtls to avoid confusion and to make it easier to
distinguish between LibreSSL (the project) and libressl (the library). Discussed with many.
Diffstat (limited to 'lib/libtls/tls_util.c')
-rw-r--r--lib/libtls/tls_util.c81
1 files changed, 81 insertions, 0 deletions
diff --git a/lib/libtls/tls_util.c b/lib/libtls/tls_util.c
new file mode 100644
index 00000000000..2adfb674b8f
--- /dev/null
+++ b/lib/libtls/tls_util.c
@@ -0,0 +1,81 @@
+/* $OpenBSD: tls_util.c,v 1.1 2014/10/31 13:46:17 jsing Exp $ */
+/*
+ * Copyright (c) 2014 Joel Sing <jsing@openbsd.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <stdlib.h>
+
+#include "tls_internal.h"
+
+/*
+ * Extract the host and port from a colon separated value. For a literal IPv6
+ * address the address must be contained with square braces. If a host and
+ * port are successfully extracted, the function will return 0 and the
+ * caller is responsible for freeing the host and port. If no port is found
+ * then the function will return 1, with both host and port being NULL.
+ * On memory allocation failure -1 will be returned.
+ */
+int
+tls_host_port(const char *hostport, char **host, char **port)
+{
+ char *h, *p, *s;
+ int rv = 1;
+
+ *host = NULL;
+ *port = NULL;
+
+ if ((s = strdup(hostport)) == NULL)
+ goto fail;
+
+ h = p = s;
+
+ /* See if this is an IPv6 literal with square braces. */
+ if (p[0] == '[') {
+ h++;
+ if ((p = strchr(s, ']')) == NULL)
+ goto done;
+ *p++ = '\0';
+ }
+
+ /* Find the port seperator. */
+ if ((p = strchr(p, ':')) == NULL)
+ goto done;
+
+ /* If there is another separator then we have issues. */
+ if (strchr(p + 1, ':') != NULL)
+ goto done;
+
+ *p++ = '\0';
+
+ if (asprintf(host, "%s", h) == -1)
+ goto fail;
+ if (asprintf(port, "%s", p) == -1)
+ goto fail;
+
+ rv = 0;
+ goto done;
+
+fail:
+ free(*host);
+ *host = NULL;
+ free(*port);
+ *port = NULL;
+ rv = -1;
+
+done:
+ free(s);
+
+ return (rv);
+}