From 8688f731373416f3b761ae38e45a6840c2fa932f Mon Sep 17 00:00:00 2001 From: Fredrik Thulin <fredrik@thulin.net> Date: Mon, 14 Jan 2013 13:59:04 +0100 Subject: [PATCH] Move _parse_host_port to gck-rpc-util (and rename). New name : gck_rpc_parse_host_port(). --- gck-rpc-dispatch.c | 53 +--------------------------------------------- gck-rpc-private.h | 3 +++ gck-rpc-util.c | 52 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 52 deletions(-) diff --git a/gck-rpc-dispatch.c b/gck-rpc-dispatch.c index f3a6db6..1f17d2c 100644 --- a/gck-rpc-dispatch.c +++ b/gck-rpc-dispatch.c @@ -2299,57 +2299,6 @@ void gck_rpc_layer_inetd(CK_FUNCTION_LIST_PTR module) run_dispatch_thread(&cs); } -/* - * Parses prefix into two strings (host and port). Port may be a NULL pointer - * if none is specified. Since this code does not decode port in any way, a - * service name works too (but requires other code (like - * _get_listening_socket()) able to resolve service names). - * - * This should work for IPv4 and IPv6 inputs : - * - * 0.0.0.0:2345 - * 0.0.0.0 - * [::]:2345 - * [::] - * [::1]:2345 - * localhost:2345 - * localhost - * - * Returns 0 on failure, and 1 on success. - */ -static int _parse_host_port(const char *prefix, char **host, char **port) -{ - char *p = NULL; - int is_ipv6; - - is_ipv6 = (prefix[0] == '[') ? 1 : 0; - - *host = strdup(prefix + is_ipv6); - *port = NULL; - - if (*host == NULL) { - gck_rpc_warn("out of memory"); - return 0; - } - - if (is_ipv6 && prefix[0] == '[') - p = strchr(*host, ']'); - else - p = strchr(*host, ':'); - - if (p) { - is_ipv6 = (*p == ']'); /* remember if separator was ']' */ - - *p = '\0'; /* replace separator will NULL to terminate *host */ - *port = p + 1; - - if (is_ipv6 && (**port == ':')) - *port = p + 2; - } - - return 1; -} - /* * Try to get a listening socket for host and port (host may be either a name or * an IP address, as string) and port (a string with a service name or a port @@ -2481,7 +2430,7 @@ int gck_rpc_layer_initialize(const char *prefix, CK_FUNCTION_LIST_PTR module) */ char *host, *port; - if (! _parse_host_port(prefix + 6, &host, &port)) { + if (! gck_rpc_parse_host_port(prefix + 6, &host, &port)) { free(host); return -1; } diff --git a/gck-rpc-private.h b/gck-rpc-private.h index 02c1e9d..cf7f6ad 100644 --- a/gck-rpc-private.h +++ b/gck-rpc-private.h @@ -328,4 +328,7 @@ int gck_rpc_mechanism_has_no_parameters(CK_MECHANISM_TYPE mech); int gck_rpc_has_bad_sized_ulong_parameter(CK_ATTRIBUTE_PTR attr); int gck_rpc_has_ulong_parameter(CK_ATTRIBUTE_TYPE type); +/* Parses strings (prefix) to host and port components. */ +int gck_rpc_parse_host_port(const char *prefix, char **host, char **port); + #endif /* GCK_RPC_CALLS_H */ diff --git a/gck-rpc-util.c b/gck-rpc-util.c index 85a4587..cf3c745 100644 --- a/gck-rpc-util.c +++ b/gck-rpc-util.c @@ -229,3 +229,55 @@ gck_rpc_has_bad_sized_ulong_parameter(CK_ATTRIBUTE_PTR attr) return 0; return gck_rpc_has_ulong_parameter(attr->type); } + +/* + * Parses prefix into two strings (host and port). Port may be a NULL pointer + * if none is specified. Since this code does not decode port in any way, a + * service name works too (but requires other code (like + * _get_listening_socket()) able to resolve service names). + * + * This should work for IPv4 and IPv6 inputs : + * + * 0.0.0.0:2345 + * 0.0.0.0 + * [::]:2345 + * [::] + * [::1]:2345 + * localhost:2345 + * localhost + * localhost:p11proxy (if p11proxy is a known service name) + * + * Returns 0 on failure, and 1 on success. + */ +int gck_rpc_parse_host_port(const char *prefix, char **host, char **port) +{ + char *p = NULL; + int is_ipv6; + + is_ipv6 = (prefix[0] == '[') ? 1 : 0; + + *host = strdup(prefix + is_ipv6); + *port = NULL; + + if (*host == NULL) { + gck_rpc_warn("out of memory"); + return 0; + } + + if (is_ipv6 && prefix[0] == '[') + p = strchr(*host, ']'); + else + p = strchr(*host, ':'); + + if (p) { + is_ipv6 = (*p == ']'); /* remember if separator was ']' */ + + *p = '\0'; /* replace separator will NULL to terminate *host */ + *port = p + 1; + + if (is_ipv6 && (**port == ':')) + *port = p + 2; + } + + return 1; +} -- GitLab